This commit is contained in:
Franck Allimant
2014-04-17 19:46:21 +02:00
parent 61fc6720fd
commit 12c21b1239
7 changed files with 871 additions and 833 deletions

View File

@@ -106,7 +106,14 @@ class Order extends BaseAction implements EventSubscriberInterface
{
$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);
}

View File

@@ -30,6 +30,7 @@ use Thelia\Model\CountryQuery;
use Thelia\Model\Module;
use Thelia\Module\BaseModule;
use Thelia\Module\DeliveryModuleInterface;
use Thelia\Module\Exception\DeliveryException;
/**
* Class Delivery
@@ -76,7 +77,7 @@ class Delivery extends BaseSpecificModule
try {
// Check if module is valid, by calling isValidDelivery(),
// or catching a DELIVERY_MODULE_UNAVAILABLE OrderException.
// or catching a DeliveryException.
if ($moduleInstance->isValidDelivery($country)) {
@@ -93,11 +94,8 @@ class Delivery extends BaseSpecificModule
$loopResult->addRow($loopResultRow);
}
} catch (OrderException $ex) {
// Re-throw an unknown exception
if ($ex->getCode() !== OrderException::DELIVERY_MODULE_UNAVAILABLE) {
throw $ex;
}
} catch (DeliveryException $ex) {
// Module is not available
}
}

View File

@@ -24,6 +24,7 @@
namespace Thelia\Module;
use Thelia\Model\Country;
use Thelia\Module\Exception\DeliveryException;
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.
*
* @return float the delivery price
* @throws DeliveryException if the postage price cannot be calculated.
*/
public abstract function getPostage(Country $country);
}

View File

@@ -24,6 +24,7 @@
namespace Thelia\Module;
use Thelia\Model\Country;
use Thelia\Module\Exception\DeliveryException;
interface DeliveryModuleInterface extends BaseModuleInterface
{
@@ -46,6 +47,7 @@ interface DeliveryModuleInterface extends BaseModuleInterface
* @param Country $country the country to deliver to.
*
* @return float the delivery price
* @throws DeliveryException if the postage price cannot be calculated.
*/
public function getPostage(Country $country);
}

View File

@@ -25,15 +25,14 @@ namespace Colissimo;
use Colissimo\Model\ColissimoFreeshippingQuery;
use Propel\Runtime\Connection\ConnectionInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Thelia\Core\Translation\Translator;
use Thelia\Exception\OrderException;
use Thelia\Install\Database;
use Thelia\Model\Country;
use Thelia\Module\BaseModule;
use Thelia\Module\DeliveryModuleInterface;
use Thelia\Module\AbstractDeliveryModule;
use Thelia\Module\Exception\DeliveryException;
class Colissimo extends BaseModule implements DeliveryModuleInterface
class Colissimo extends AbstractDeliveryModule
{
protected $request;
protected $dispatcher;
@@ -44,13 +43,38 @@ class Colissimo extends BaseModule implements DeliveryModuleInterface
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);
}
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 $weight
@@ -61,29 +85,36 @@ class Colissimo extends BaseModule implements DeliveryModuleInterface
public static function getPostageAmount($areaId, $weight)
{
$freeshipping = ColissimoFreeshippingQuery::create()->getLast();
$postage=0;
if(!$freeshipping) {
$postage = 0;
if (!$freeshipping) {
$prices = self::getPrices();
/* check if Colissimo delivers the asked area */
if(!isset($prices[$areaId]) || !isset($prices[$areaId]["slices"])) {
throw new OrderException("Colissimo delivery unavailable for the chosen delivery country", OrderException::DELIVERY_MODULE_UNAVAILABLE);
if (!isset($prices[$areaId]) || !isset($prices[$areaId]["slices"])) {
throw new DeliveryException(
Translator::getInstance()->trans("Colissimo delivery unavailable for the delivery country")
);
}
$areaPrices = $prices[$areaId]["slices"];
ksort($areaPrices);
/* check this weight is not too much */
/* Check cart weight is below the maximum weight */
end($areaPrices);
$maxWeight = key($areaPrices);
if($weight > $maxWeight) {
throw new OrderException(sprintf("Colissimo delivery unavailable for this cart weight (%s kg)", $weight), OrderException::DELIVERY_MODULE_UNAVAILABLE);
if ($weight > $maxWeight) {
throw new DeliveryException(
Translator::getInstance()->trans(
"Colissimo delivery unavailable for this cart weight (%weight kg)",
array("%weight" => $weight)
)
);
}
$postage = current($areaPrices);
while(prev($areaPrices)) {
if($weight > key($areaPrices)) {
while (prev($areaPrices)) {
if ($weight > key($areaPrices)) {
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)
{
$database = new Database($con->getWrappedConnection());
$database = new Database($con);
$database->insertSql(null, array(__DIR__ . '/Config/thelia.sql'));
}
@@ -131,7 +142,7 @@ class Colissimo extends BaseModule implements DeliveryModuleInterface
*/
public function getPostage(Country $country)
{
$cartWeight = $this->getContainer()->get('request')->getSession()->getCart()->getWeight();
$cartWeight = $this->getRequest()->getSession()->getCart()->getWeight();
$postage = self::getPostageAmount(
$country->getAreaId(),
@@ -140,10 +151,4 @@ class Colissimo extends BaseModule implements DeliveryModuleInterface
return $postage;
}
public function getCode()
{
return 'Colissimo';
}
}

View File

@@ -31,6 +31,7 @@ use Thelia\Core\Event\TheliaEvents;
use Symfony\Component\HttpFoundation\Request;
use Thelia\Form\CartAdd;
use Thelia\Model\AddressQuery;
use Thelia\Module\Exception\DeliveryException;
class CartController extends BaseFrontController
{
@@ -160,12 +161,23 @@ class CartController extends BaseFrontController
if (null !== $deliveryModule && null !== $deliveryAddress) {
$moduleInstance = $this->container->get(sprintf('module.%s', $deliveryModule->getCode()));
$postage = $moduleInstance->getPostage($deliveryAddress->getCountry());
$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);
}
}
}
}

View File

@@ -31,6 +31,7 @@ use Thelia\Form\Exception\FormValidationException;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Log\Tlog;
use Thelia\Model\AddressQuery;
use Thelia\Module\Exception\DeliveryException;
/**
* Class CouponController
@@ -74,12 +75,23 @@ class CouponController extends BaseFrontController
if (null !== $deliveryModule && null !== $deliveryAddress) {
$moduleInstance = $this->container->get(sprintf('module.%s', $deliveryModule->getCode()));
$postage = $moduleInstance->getPostage($deliveryAddress->getCountry());
$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);
}
}
}