Init du module DHL (sur la base du module Colissimo)

This commit is contained in:
2020-12-03 17:12:39 +01:00
parent 979767253d
commit 7f00efb081
48 changed files with 4198 additions and 541 deletions

204
local/modules/DHL/DHL.php Normal file
View File

@@ -0,0 +1,204 @@
<?php
namespace DHL;
use DHL\Model\Config\DHLConfigValue;
use DHL\Model\DHLDeliveryPrice;
use Propel\Runtime\Connection\ConnectionInterface;
use Thelia\Core\Translation\Translator;
use Thelia\Exception\TheliaProcessException;
use Thelia\Install\Database;
use Thelia\Model\Area;
use Thelia\Model\AreaDeliveryModule;
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;
class DHL extends AbstractDeliveryModule
{
protected $request;
protected $dispatcher;
private static $prices = null;
const DOMAIN_NAME = 'dhl';
const JSON_PRICE_RESOURCE = "/Config/prices.json";
const WEBSERVICE_URL = 'tracking_url';
public static function getPrices()
{
if (null === self::$prices) {
self::$prices = json_decode(DHL::getConfigValue(DHLConfigValue::PRICES, null), true);
}
return self::$prices;
}
public function postActivation(ConnectionInterface $con = null)
{
self::setConfigValue(DHLConfigValue::ENABLED, 1);
$database = new Database($con);
$database->insertSql(null, array(__DIR__ . '/Config/thelia.sql'));
// Create DHL shipping zones for relay and 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");
}
// Create all shipping zones, and associate DHL module with them.
foreach ($moduleConfiguration->shippingZones as $shippingZone) {
AreaQuery::create()->filterByName($shippingZone->name)->delete();
$area = new Area();
$area
->setName($shippingZone->name)
->save();
foreach ($shippingZone->countries as $countryIsoCode) {
if (null !== $country = CountryQuery::create()->findOneByIsoalpha3($countryIsoCode)) {
(new CountryArea())
->setAreaId($area->getId())
->setCountryId($country->getId())
->save();
}
}
// Attach this zone to our module
(new AreaDeliveryModule())
->setArea($area)
->setDeliveryModuleId($moduleId)
->save();
// Create base prices
foreach ($shippingZone->prices as $price) {
(new DHLDeliveryPrice())
->setAreaId($area->getId())
->setMaxWeight($price->up_to)
->setPriceWithTax($price->price_euro * $rateFromEuro)
->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();
/* Check if DHL 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)
{
$postage = 0;
$prices = self::getPrices();
/* check if DHL delivers the asked area */
if (!isset($prices[$areaId]) || !isset($prices[$areaId]["slices"])) {
throw new DeliveryException(
Translator::getInstance()->trans(
"DHL 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(
"DHL 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
* @return mixed
* @throws \Thelia\Exception\OrderException
*/
public function getPostage(Country $country)
{
$cartWeight = $this->getRequest()->getSession()->getSessionCart($this->getDispatcher())->getWeight();
$postage = self::getPostageAmount(
$this->getAreaForCountry($country)->getId(),
$cartWeight
);
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));
}
}
}