Files
domokits/local/modules/DHL/DHL.php

221 lines
6.7 KiB
PHP

<?php
namespace DHL;
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;
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\MessageQuery;
use Thelia\Model\ModuleConfigQuery;
use Thelia\Module\AbstractDeliveryModule;
use Thelia\Module\Exception\DeliveryException;
/**
* Class DHL
* @package DHL
* @author Laurent LE CORRE <laurent@thecoredev.fr>
*/
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';
const TRACKING_MESSAGE_NAME = 'mail_dhl';
public static function getPrices($areaId)
{
if (null === self::$prices) {
self::$prices = DHLDeliveryPriceQuery::create()
->findByAreaId($areaId);
}
return self::$prices;
}
public function postActivation(ConnectionInterface $con = null)
{
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 home delivery
$moduleId = self::getModuleId();
$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)
->save();
}
}
}
public function isValidDelivery(Country $country)
{
if (null !== $area = $this->getAreaForCountry($country)) {
$areaId = $area->getId();
$prices = self::getPrices($areaId);
/* Check if DHL delivers the area */
if (isset($prices)) {
// Yes ! Check if the cart weight is below slice limit
$maxWeight = DHLDeliveryPriceQuery::create()
->orderByMaxWeight(Criteria::DESC)
->findOneByAreaId($areaId)
->getMaxWeight();
$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($areaId);
/* check if DHL delivers the asked area */
if (!isset($prices)) {
throw new DeliveryException(
Translator::getInstance()->trans(
"DHL delivery unavailable for the delivery country",
[],
self::DOMAIN_NAME
)
);
}
$maxWeight = DHLDeliveryPriceQuery::create()
->orderByMaxWeight(Criteria::DESC)
->findOneByAreaId($areaId)
->getMaxWeight();
/* Check cart weight is below the maximum weight */
if ($weight > $maxWeight) {
throw new DeliveryException(
Translator::getInstance()->trans(
"DHL delivery unavailable for this cart weight (%weight kg)",
array("%weight" => $weight),
self::DOMAIN_NAME
)
);
}
$deliveryPrice = DHLDeliveryPriceQuery::create()
->filterByAreaId($areaId)
->filterByMaxWeight($weight, Criteria::GREATER_EQUAL)
->orderByMaxWeight(Criteria::ASC)
->findOne();
return $deliveryPrice->getPriceWithTax();
}
/**
*
* 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;
}
/**
* @param ConnectionInterface|null $con
* @param bool $deleteModuleData
* @throws \Propel\Runtime\Exception\PropelException
*/
public function destroy(ConnectionInterface $con = null, $deleteModuleData = false)
{
if ($deleteModuleData) {
// Delete message
MessageQuery::create()->filterByName(self::TRACKING_MESSAGE_NAME)->delete($con);
// Delete module config data
ModuleConfigQuery::create()->filterByModuleId(self::getModuleId())->delete($con);
// Delete module tables.
if (null !== $con) {
$database = new Database($con);
$database->insertSql(null, [__DIR__ . '/Config/drop.sql']);
}
}
parent::destroy($con, $deleteModuleData);
}
}