Ca avance pas mal sur DHL...

This commit is contained in:
2020-12-04 20:59:52 +01:00
parent 2453c207aa
commit 9572b71f14
12 changed files with 2518 additions and 558 deletions

View File

@@ -4,6 +4,8 @@ namespace DHL;
use DHL\Model\Config\DHLConfigValue;
use DHL\Model\DHLDeliveryPrice;
use DHL\Model\DHLDeliveryPriceQuery;
use Propel\Runtime\ActiveQuery\Criteria;
use Propel\Runtime\Connection\ConnectionInterface;
use Thelia\Core\Translation\Translator;
use Thelia\Exception\TheliaProcessException;
@@ -14,7 +16,6 @@ use Thelia\Model\AreaQuery;
use Thelia\Model\Country;
use Thelia\Model\CountryArea;
use Thelia\Model\CountryQuery;
use Thelia\Model\Currency;
use Thelia\Module\AbstractDeliveryModule;
use Thelia\Module\Exception\DeliveryException;
@@ -29,10 +30,11 @@ class DHL extends AbstractDeliveryModule
const JSON_PRICE_RESOURCE = "/Config/prices.json";
const WEBSERVICE_URL = 'tracking_url';
public static function getPrices()
public static function getPrices($areaId)
{
if (null === self::$prices) {
self::$prices = json_decode(DHL::getConfigValue(DHLConfigValue::PRICES, null), true);
self::$prices = DHLDeliveryPriceQuery::create()
->findByAreaId($areaId);
}
return self::$prices;
@@ -40,14 +42,13 @@ class DHL extends AbstractDeliveryModule
public function postActivation(ConnectionInterface $con = null)
{
self::setConfigValue(DHLConfigValue::ENABLED, 1);
self::setConfigValue(self::WEBSERVICE_URL, "https://www.dhl.com/fr-fr/home/suivi.html?tracking-id=");
$database = new Database($con);
$database->insertSql(null, array(__DIR__ . '/Config/thelia.sql'));
// Create DHL shipping zones for relay and home delivery
// Create DHL shipping zones for home delivery
$moduleId = self::getModuleId();
$rateFromEuro = Currency::getDefaultCurrency()->getRate();
$moduleConfiguration = json_decode(file_get_contents(__DIR__. '/Config/prices.json'));
if (false === $moduleConfiguration) {
throw new TheliaProcessException("Invalid JSON configuration for DHL module");
@@ -82,34 +83,26 @@ class DHL extends AbstractDeliveryModule
(new DHLDeliveryPrice())
->setAreaId($area->getId())
->setMaxWeight($price->up_to)
->setPriceWithTax($price->price_euro * $rateFromEuro)
->setPriceWithTax($price->price_euro)
->save();
}
}
}
public function isValidDelivery(Country $country)
{
if (0 == self::getConfigValue(DHLConfigValue::ENABLED, 1)) {
return false;
}
if (null !== $area = $this->getAreaForCountry($country)) {
$areaId = $area->getId();
$prices = self::getPrices();
$prices = self::getPrices($areaId);
/* Check if DHL delivers the area */
if (isset($prices[$areaId]) && isset($prices[$areaId]["slices"])) {
if (isset($prices)) {
// 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);
$maxWeight = DHLDeliveryPriceQuery::create()
->orderByMaxWeight(Criteria::DESC)
->findOneByAreaId($areaId)
->getMaxWeight();
$cartWeight = $this->getRequest()->getSession()->getSessionCart($this->getDispatcher())->getWeight();
@@ -132,10 +125,10 @@ class DHL extends AbstractDeliveryModule
public static function getPostageAmount($areaId, $weight)
{
$postage = 0;
$prices = self::getPrices();
$prices = self::getPrices($areaId);
/* check if DHL delivers the asked area */
if (!isset($prices[$areaId]) || !isset($prices[$areaId]["slices"])) {
if (!isset($prices)) {
throw new DeliveryException(
Translator::getInstance()->trans(
"DHL delivery unavailable for the delivery country",
@@ -145,12 +138,12 @@ class DHL extends AbstractDeliveryModule
);
}
$areaPrices = $prices[$areaId]["slices"];
ksort($areaPrices);
$maxWeight = DHLDeliveryPriceQuery::create()
->orderByMaxWeight(Criteria::DESC)
->findOneByAreaId($areaId)
->getMaxWeight();
/* Check cart weight is below the maximum weight */
end($areaPrices);
$maxWeight = key($areaPrices);
if ($weight > $maxWeight) {
throw new DeliveryException(
Translator::getInstance()->trans(
@@ -161,7 +154,10 @@ class DHL extends AbstractDeliveryModule
);
}
$postage = current($areaPrices);
$postageArray = DHLDeliveryPriceQuery::create()
->findByAreaId($areaId);
$postage = current($postageArray);
while (prev($areaPrices)) {
if ($weight > key($areaPrices)) {
@@ -194,11 +190,12 @@ class DHL extends AbstractDeliveryModule
return $postage;
}
public function update($currentVersion, $newVersion, ConnectionInterface $con = null)
{
$uploadDir = __DIR__ . '/Config/prices.json';
if (is_readable($uploadDir) && DHL::getConfigValue(DHLConfigValue::PRICES, null) == null) {
DHL::setConfigValue(DHLConfigValue::PRICES, file_get_contents($uploadDir));
}
}
// public function update($currentVersion, $newVersion, ConnectionInterface $con = null)
// {
// $uploadDir = __DIR__ . '/Config/prices.json';
// if (is_readable($uploadDir) && DHL::getConfigValue(DHLConfigValue::PRICES, null) == null) {
// DHL::setConfigValue(DHLConfigValue::PRICES, file_get_contents($uploadDir));
// }
// }
}