Fixes #308
This commit is contained in:
@@ -106,7 +106,14 @@ class Order extends BaseAction implements EventSubscriberInterface
|
|||||||
{
|
{
|
||||||
$order = $event->getOrder();
|
$order = $event->getOrder();
|
||||||
|
|
||||||
$order->setDeliveryModuleId($event->getDeliveryModule());
|
$deliveryModuleId = $event->getDeliveryModule();
|
||||||
|
|
||||||
|
$order->setDeliveryModuleId($deliveryModuleId);
|
||||||
|
|
||||||
|
// Reset postage cost if the delivery module had been removed
|
||||||
|
if ($deliveryModuleId <= 0) {
|
||||||
|
$this->setPostage(0);
|
||||||
|
}
|
||||||
|
|
||||||
$event->setOrder($order);
|
$event->setOrder($order);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ use Thelia\Model\CountryQuery;
|
|||||||
use Thelia\Model\Module;
|
use Thelia\Model\Module;
|
||||||
use Thelia\Module\BaseModule;
|
use Thelia\Module\BaseModule;
|
||||||
use Thelia\Module\DeliveryModuleInterface;
|
use Thelia\Module\DeliveryModuleInterface;
|
||||||
|
use Thelia\Module\Exception\DeliveryException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Delivery
|
* Class Delivery
|
||||||
@@ -76,7 +77,7 @@ class Delivery extends BaseSpecificModule
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// Check if module is valid, by calling isValidDelivery(),
|
// Check if module is valid, by calling isValidDelivery(),
|
||||||
// or catching a DELIVERY_MODULE_UNAVAILABLE OrderException.
|
// or catching a DeliveryException.
|
||||||
|
|
||||||
if ($moduleInstance->isValidDelivery($country)) {
|
if ($moduleInstance->isValidDelivery($country)) {
|
||||||
|
|
||||||
@@ -93,11 +94,8 @@ class Delivery extends BaseSpecificModule
|
|||||||
|
|
||||||
$loopResult->addRow($loopResultRow);
|
$loopResult->addRow($loopResultRow);
|
||||||
}
|
}
|
||||||
} catch (OrderException $ex) {
|
} catch (DeliveryException $ex) {
|
||||||
// Re-throw an unknown exception
|
// Module is not available
|
||||||
if ($ex->getCode() !== OrderException::DELIVERY_MODULE_UNAVAILABLE) {
|
|
||||||
throw $ex;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,7 @@
|
|||||||
namespace Thelia\Module;
|
namespace Thelia\Module;
|
||||||
|
|
||||||
use Thelia\Model\Country;
|
use Thelia\Model\Country;
|
||||||
|
use Thelia\Module\Exception\DeliveryException;
|
||||||
|
|
||||||
abstract class AbstractDeliveryModule extends BaseModule implements DeliveryModuleInterface
|
abstract class AbstractDeliveryModule extends BaseModule implements DeliveryModuleInterface
|
||||||
{
|
{
|
||||||
@@ -46,6 +47,7 @@ abstract class AbstractDeliveryModule extends BaseModule implements DeliveryModu
|
|||||||
* @param Country $country the country to deliver to.
|
* @param Country $country the country to deliver to.
|
||||||
*
|
*
|
||||||
* @return float the delivery price
|
* @return float the delivery price
|
||||||
|
* @throws DeliveryException if the postage price cannot be calculated.
|
||||||
*/
|
*/
|
||||||
public abstract function getPostage(Country $country);
|
public abstract function getPostage(Country $country);
|
||||||
}
|
}
|
||||||
@@ -24,6 +24,7 @@
|
|||||||
namespace Thelia\Module;
|
namespace Thelia\Module;
|
||||||
|
|
||||||
use Thelia\Model\Country;
|
use Thelia\Model\Country;
|
||||||
|
use Thelia\Module\Exception\DeliveryException;
|
||||||
|
|
||||||
interface DeliveryModuleInterface extends BaseModuleInterface
|
interface DeliveryModuleInterface extends BaseModuleInterface
|
||||||
{
|
{
|
||||||
@@ -46,6 +47,7 @@ interface DeliveryModuleInterface extends BaseModuleInterface
|
|||||||
* @param Country $country the country to deliver to.
|
* @param Country $country the country to deliver to.
|
||||||
*
|
*
|
||||||
* @return float the delivery price
|
* @return float the delivery price
|
||||||
|
* @throws DeliveryException if the postage price cannot be calculated.
|
||||||
*/
|
*/
|
||||||
public function getPostage(Country $country);
|
public function getPostage(Country $country);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,15 +25,14 @@ namespace Colissimo;
|
|||||||
|
|
||||||
use Colissimo\Model\ColissimoFreeshippingQuery;
|
use Colissimo\Model\ColissimoFreeshippingQuery;
|
||||||
use Propel\Runtime\Connection\ConnectionInterface;
|
use Propel\Runtime\Connection\ConnectionInterface;
|
||||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
use Thelia\Core\Translation\Translator;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
|
||||||
use Thelia\Exception\OrderException;
|
use Thelia\Exception\OrderException;
|
||||||
use Thelia\Install\Database;
|
use Thelia\Install\Database;
|
||||||
use Thelia\Model\Country;
|
use Thelia\Model\Country;
|
||||||
use Thelia\Module\BaseModule;
|
use Thelia\Module\AbstractDeliveryModule;
|
||||||
use Thelia\Module\DeliveryModuleInterface;
|
use Thelia\Module\Exception\DeliveryException;
|
||||||
|
|
||||||
class Colissimo extends BaseModule implements DeliveryModuleInterface
|
class Colissimo extends AbstractDeliveryModule
|
||||||
{
|
{
|
||||||
protected $request;
|
protected $request;
|
||||||
protected $dispatcher;
|
protected $dispatcher;
|
||||||
@@ -44,13 +43,38 @@ class Colissimo extends BaseModule implements DeliveryModuleInterface
|
|||||||
|
|
||||||
public static function getPrices()
|
public static function getPrices()
|
||||||
{
|
{
|
||||||
if(null === self::$prices) {
|
if (null === self::$prices) {
|
||||||
self::$prices = json_decode(file_get_contents(sprintf('%s%s', __DIR__, self::JSON_PRICE_RESOURCE)), true);
|
self::$prices = json_decode(file_get_contents(sprintf('%s%s', __DIR__, self::JSON_PRICE_RESOURCE)), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
return self::$prices;
|
return self::$prices;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function isValidDelivery(Country $country) {
|
||||||
|
|
||||||
|
$areaId = $country->getAreaId();
|
||||||
|
|
||||||
|
$prices = self::getPrices();
|
||||||
|
|
||||||
|
/* Check if Colissimo 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()->getCart()->getWeight();
|
||||||
|
|
||||||
|
if ($cartWeight <= $maxWeight) return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $areaId
|
* @param $areaId
|
||||||
* @param $weight
|
* @param $weight
|
||||||
@@ -61,29 +85,36 @@ class Colissimo extends BaseModule implements DeliveryModuleInterface
|
|||||||
public static function getPostageAmount($areaId, $weight)
|
public static function getPostageAmount($areaId, $weight)
|
||||||
{
|
{
|
||||||
$freeshipping = ColissimoFreeshippingQuery::create()->getLast();
|
$freeshipping = ColissimoFreeshippingQuery::create()->getLast();
|
||||||
$postage=0;
|
$postage = 0;
|
||||||
if(!$freeshipping) {
|
if (!$freeshipping) {
|
||||||
$prices = self::getPrices();
|
$prices = self::getPrices();
|
||||||
|
|
||||||
/* check if Colissimo delivers the asked area */
|
/* check if Colissimo delivers the asked area */
|
||||||
if(!isset($prices[$areaId]) || !isset($prices[$areaId]["slices"])) {
|
if (!isset($prices[$areaId]) || !isset($prices[$areaId]["slices"])) {
|
||||||
throw new OrderException("Colissimo delivery unavailable for the chosen delivery country", OrderException::DELIVERY_MODULE_UNAVAILABLE);
|
throw new DeliveryException(
|
||||||
|
Translator::getInstance()->trans("Colissimo delivery unavailable for the delivery country")
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$areaPrices = $prices[$areaId]["slices"];
|
$areaPrices = $prices[$areaId]["slices"];
|
||||||
ksort($areaPrices);
|
ksort($areaPrices);
|
||||||
|
|
||||||
/* check this weight is not too much */
|
/* Check cart weight is below the maximum weight */
|
||||||
end($areaPrices);
|
end($areaPrices);
|
||||||
$maxWeight = key($areaPrices);
|
$maxWeight = key($areaPrices);
|
||||||
if($weight > $maxWeight) {
|
if ($weight > $maxWeight) {
|
||||||
throw new OrderException(sprintf("Colissimo delivery unavailable for this cart weight (%s kg)", $weight), OrderException::DELIVERY_MODULE_UNAVAILABLE);
|
throw new DeliveryException(
|
||||||
|
Translator::getInstance()->trans(
|
||||||
|
"Colissimo delivery unavailable for this cart weight (%weight kg)",
|
||||||
|
array("%weight" => $weight)
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$postage = current($areaPrices);
|
$postage = current($areaPrices);
|
||||||
|
|
||||||
while(prev($areaPrices)) {
|
while (prev($areaPrices)) {
|
||||||
if($weight > key($areaPrices)) {
|
if ($weight > key($areaPrices)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,29 +125,9 @@ class Colissimo extends BaseModule implements DeliveryModuleInterface
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setRequest(Request $request)
|
|
||||||
{
|
|
||||||
$this->request = $request;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getRequest()
|
|
||||||
{
|
|
||||||
return $this->request;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setDispatcher(EventDispatcherInterface $dispatcher)
|
|
||||||
{
|
|
||||||
$this->dispatcher = $dispatcher;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getDispatcher()
|
|
||||||
{
|
|
||||||
return $this->dispatcher;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function postActivation(ConnectionInterface $con = null)
|
public function postActivation(ConnectionInterface $con = null)
|
||||||
{
|
{
|
||||||
$database = new Database($con->getWrappedConnection());
|
$database = new Database($con);
|
||||||
|
|
||||||
$database->insertSql(null, array(__DIR__ . '/Config/thelia.sql'));
|
$database->insertSql(null, array(__DIR__ . '/Config/thelia.sql'));
|
||||||
}
|
}
|
||||||
@@ -131,7 +142,7 @@ class Colissimo extends BaseModule implements DeliveryModuleInterface
|
|||||||
*/
|
*/
|
||||||
public function getPostage(Country $country)
|
public function getPostage(Country $country)
|
||||||
{
|
{
|
||||||
$cartWeight = $this->getContainer()->get('request')->getSession()->getCart()->getWeight();
|
$cartWeight = $this->getRequest()->getSession()->getCart()->getWeight();
|
||||||
|
|
||||||
$postage = self::getPostageAmount(
|
$postage = self::getPostageAmount(
|
||||||
$country->getAreaId(),
|
$country->getAreaId(),
|
||||||
@@ -140,10 +151,4 @@ class Colissimo extends BaseModule implements DeliveryModuleInterface
|
|||||||
|
|
||||||
return $postage;
|
return $postage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCode()
|
|
||||||
{
|
|
||||||
return 'Colissimo';
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -31,6 +31,7 @@ use Thelia\Core\Event\TheliaEvents;
|
|||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Thelia\Form\CartAdd;
|
use Thelia\Form\CartAdd;
|
||||||
use Thelia\Model\AddressQuery;
|
use Thelia\Model\AddressQuery;
|
||||||
|
use Thelia\Module\Exception\DeliveryException;
|
||||||
|
|
||||||
class CartController extends BaseFrontController
|
class CartController extends BaseFrontController
|
||||||
{
|
{
|
||||||
@@ -160,12 +161,23 @@ class CartController extends BaseFrontController
|
|||||||
|
|
||||||
if (null !== $deliveryModule && null !== $deliveryAddress) {
|
if (null !== $deliveryModule && null !== $deliveryAddress) {
|
||||||
$moduleInstance = $this->container->get(sprintf('module.%s', $deliveryModule->getCode()));
|
$moduleInstance = $this->container->get(sprintf('module.%s', $deliveryModule->getCode()));
|
||||||
$postage = $moduleInstance->getPostage($deliveryAddress->getCountry());
|
|
||||||
|
|
||||||
$orderEvent = new OrderEvent($order);
|
$orderEvent = new OrderEvent($order);
|
||||||
$orderEvent->setPostage($postage);
|
|
||||||
|
|
||||||
$this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_POSTAGE, $orderEvent);
|
try {
|
||||||
|
$postage = $moduleInstance->getPostage($deliveryAddress->getCountry());
|
||||||
|
|
||||||
|
$orderEvent->setPostage($postage);
|
||||||
|
|
||||||
|
$this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_POSTAGE, $orderEvent);
|
||||||
|
}
|
||||||
|
catch (DeliveryException $ex) {
|
||||||
|
// The postage has been chosen, but changes in the cart causes an exception.
|
||||||
|
// Reset the postage data in the order
|
||||||
|
$orderEvent->setDeliveryModule(0);
|
||||||
|
|
||||||
|
$this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_DELIVERY_MODULE, $orderEvent);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ use Thelia\Form\Exception\FormValidationException;
|
|||||||
use Thelia\Core\Event\TheliaEvents;
|
use Thelia\Core\Event\TheliaEvents;
|
||||||
use Thelia\Log\Tlog;
|
use Thelia\Log\Tlog;
|
||||||
use Thelia\Model\AddressQuery;
|
use Thelia\Model\AddressQuery;
|
||||||
|
use Thelia\Module\Exception\DeliveryException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class CouponController
|
* Class CouponController
|
||||||
@@ -74,12 +75,23 @@ class CouponController extends BaseFrontController
|
|||||||
|
|
||||||
if (null !== $deliveryModule && null !== $deliveryAddress) {
|
if (null !== $deliveryModule && null !== $deliveryAddress) {
|
||||||
$moduleInstance = $this->container->get(sprintf('module.%s', $deliveryModule->getCode()));
|
$moduleInstance = $this->container->get(sprintf('module.%s', $deliveryModule->getCode()));
|
||||||
$postage = $moduleInstance->getPostage($deliveryAddress->getCountry());
|
|
||||||
|
|
||||||
$orderEvent = new OrderEvent($order);
|
$orderEvent = new OrderEvent($order);
|
||||||
$orderEvent->setPostage($postage);
|
|
||||||
|
|
||||||
$this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_POSTAGE, $orderEvent);
|
try {
|
||||||
|
$postage = $moduleInstance->getPostage($deliveryAddress->getCountry());
|
||||||
|
|
||||||
|
$orderEvent->setPostage($postage);
|
||||||
|
|
||||||
|
$this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_POSTAGE, $orderEvent);
|
||||||
|
}
|
||||||
|
catch (DeliveryException $ex) {
|
||||||
|
// The postage has been chosen, but changes dues to coupon causes an exception.
|
||||||
|
// Reset the postage data in the order
|
||||||
|
$orderEvent->setDeliveryModule(0);
|
||||||
|
|
||||||
|
$this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_DELIVERY_MODULE, $orderEvent);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user