Files
domokits/local/modules/FedEx/FedEx.php

144 lines
4.0 KiB
PHP
Executable File

<?php
namespace FedEx;
use FedEx\Model\Config\FedExConfigValue;
use Propel\Runtime\Connection\ConnectionInterface;
use Thelia\Core\Translation\Translator;
use Thelia\Install\Database;
use Thelia\Model\Country;
use Thelia\Model\State;
use Thelia\Module\AbstractDeliveryModule;
use Thelia\Module\Exception\DeliveryException;
class FedEx extends AbstractDeliveryModule
{
const DOMAIN_NAME = 'fedex';
protected $request;
protected $dispatcher;
const TRACKING_URL_PARAMETER = 'tracking_url';
const DEFAULT_TRACKING_URL = "https://www.fedex.com/apps/fedextrack/?action=track&trackingnumber=";
private static $prices = null;
const JSON_PRICE_RESOURCE = "/Config/prices.json";
public static function getPrices()
{
if (null === self::$prices) {
self::$prices = json_decode(FedEx::getConfigValue(FedExConfigValue::PRICES, null), true);
}
return self::$prices;
}
public function postActivation(ConnectionInterface $con = null)
{
self::setConfigValue(FedEx::TRACKING_URL_PARAMETER, FedEx::DEFAULT_TRACKING_URL);
$database = new Database($con);
$database->insertSql(null, array(__DIR__ . '/Config/thelia.sql'));
}
public function isValidDelivery(Country $country, State $state = null)
{
if (null !== $area = $this->getAreaForCountry($country, $state)) {
$areaId = $area->getId();
$prices = self::getPrices();
/* Check if FedEx delivers the area */
if (isset($prices[$areaId]) && isset($prices[$areaId]["slices"])) {
// Yes ! Check if the cart weight is below slice limit
$areaPrices = $prices[$areaId]["slices"];
ksort($areaPrices);
/* Check cart weight is below the maximum weight */
end($areaPrices);
$maxWeight = key($areaPrices);
$cartWeight = $this->getRequest()->getSession()->getSessionCart($this->getDispatcher())->getWeight();
if ($cartWeight <= $maxWeight) {
return true;
}
}
}
return false;
}
/**
* @param $areaId
* @param $weight
*
* @return mixed
* @throws \Thelia\Exception\OrderException
*/
public static function getPostageAmount($areaId, $weight)
{
$prices = self::getPrices();
/* check if FedEx delivers the asked area */
if (!isset($prices[$areaId]) || !isset($prices[$areaId]["slices"])) {
throw new DeliveryException(
Translator::getInstance()->trans(
"FedEx delivery unavailable for the delivery country",
[],
self::DOMAIN_NAME
)
);
}
$areaPrices = $prices[$areaId]["slices"];
ksort($areaPrices);
/* Check cart weight is below the maximum weight */
end($areaPrices);
$maxWeight = key($areaPrices);
if ($weight > $maxWeight) {
throw new DeliveryException(
Translator::getInstance()->trans(
"FedEx delivery unavailable for this cart weight (%weight kg)",
array("%weight" => $weight),
self::DOMAIN_NAME
)
);
}
$postage = current($areaPrices);
while (prev($areaPrices)) {
if ($weight > key($areaPrices)) {
break;
}
$postage = current($areaPrices);
}
return $postage;
}
/**
*
* calculate and return delivery price
*
* @param Country $country
* @param State $state
* @return mixed
*/
public function getPostage(Country $country, State $state = null)
{
$cartWeight = $this->getRequest()->getSession()->getSessionCart($this->getDispatcher())->getWeight();
$postage = self::getPostageAmount(
$this->getAreaForCountry($country, $state)->getId(),
$cartWeight
);
return $postage;
}
}