diff --git a/core/lib/Thelia/Action/Order.php b/core/lib/Thelia/Action/Order.php index 3d5d2be77..ba794cb4c 100755 --- a/core/lib/Thelia/Action/Order.php +++ b/core/lib/Thelia/Action/Order.php @@ -279,12 +279,13 @@ class Order extends BaseAction implements EventSubscriberInterface $this->getDispatcher()->dispatch(TheliaEvents::CART_CLEAR, new CartEvent($this->getCart($this->getRequest()))); /* call pay method */ - $paymentModuleReflection = new \ReflectionClass($paymentModule->getFullNamespace()); + /*$paymentModuleReflection = new \ReflectionClass($paymentModule->getFullNamespace()); $paymentModuleInstance = $paymentModuleReflection->newInstance(); $paymentModuleInstance->setRequest($this->getRequest()); - $paymentModuleInstance->setDispatcher($this->getDispatcher()); + $paymentModuleInstance->setDispatcher($this->getDispatcher());*/ + $paymentModuleInstance = $this->container->get(sprintf('module.%s', $paymentModule->getCode())); $paymentModuleInstance->pay($placedOrder); } diff --git a/core/lib/Thelia/Controller/Front/OrderController.php b/core/lib/Thelia/Controller/Front/OrderController.php index dc3f5b20f..11e6768d3 100755 --- a/core/lib/Thelia/Controller/Front/OrderController.php +++ b/core/lib/Thelia/Controller/Front/OrderController.php @@ -84,8 +84,10 @@ class OrderController extends BaseFrontController } /* get postage amount */ - $moduleReflection = new \ReflectionClass($deliveryModule->getFullNamespace()); - $moduleInstance = $moduleReflection->newInstance(); + /*$moduleReflection = new \ReflectionClass($deliveryModule->getFullNamespace()); + $moduleInstance = $moduleReflection->newInstance();*/ + + $moduleInstance = $this->container->get(sprintf('module.%s', $deliveryModule->getCode())); $postage = $moduleInstance->getPostage($deliveryAddress->getCountry()); $orderEvent = $this->getOrderEvent(); diff --git a/core/lib/Thelia/Core/Template/Loop/Delivery.php b/core/lib/Thelia/Core/Template/Loop/Delivery.php index e5c56797d..5211d3a79 100644 --- a/core/lib/Thelia/Core/Template/Loop/Delivery.php +++ b/core/lib/Thelia/Core/Template/Loop/Delivery.php @@ -25,8 +25,10 @@ namespace Thelia\Core\Template\Loop; use Thelia\Core\Template\Element\LoopResult; use Thelia\Core\Template\Element\LoopResultRow; use Thelia\Core\Template\Loop\Argument\Argument; +use Thelia\Exception\OrderException; use Thelia\Model\CountryQuery; use Thelia\Module\BaseModule; +use Thelia\Module\DeliveryModuleInterface; /** * Class Delivery @@ -63,14 +65,24 @@ class Delivery extends BaseSpecificModule foreach ($loopResult->getResultDataCollection() as $deliveryModule) { $loopResultRow = new LoopResultRow($deliveryModule); - $moduleReflection = new \ReflectionClass($deliveryModule->getFullNamespace()); - if ($moduleReflection->isSubclassOf("Thelia\Module\DeliveryModuleInterface") === false) { + $moduleInstance = $this->container->get(sprintf('module.%s', $deliveryModule->getCode())); + + if (false === $moduleInstance instanceof DeliveryModuleInterface) { throw new \RuntimeException(sprintf("delivery module %s is not a Thelia\Module\DeliveryModuleInterface", $deliveryModule->getCode())); } - $moduleInstance = $moduleReflection->newInstance(); - $moduleInstance->setRequest($this->request); - $moduleInstance->setDispatcher($this->dispatcher); + try { + $postage = $moduleInstance->getPostage($country); + } catch(OrderException $e) { + switch($e->getCode()) { + case OrderException::DELIVERY_MODULE_UNAVAILABLE: + /* do not show this delivery module */ + continue(2); + break; + default: + throw $e; + } + } $loopResultRow ->set('ID', $deliveryModule->getId()) @@ -78,7 +90,7 @@ class Delivery extends BaseSpecificModule ->set('CHAPO', $deliveryModule->getVirtualColumn('i18n_CHAPO')) ->set('DESCRIPTION', $deliveryModule->getVirtualColumn('i18n_DESCRIPTION')) ->set('POSTSCRIPTUM', $deliveryModule->getVirtualColumn('i18n_POSTSCRIPTUM')) - ->set('POSTAGE', $moduleInstance->getPostage($country)) + ->set('POSTAGE', $postage) ; $loopResult->addRow($loopResultRow); diff --git a/core/lib/Thelia/Core/Template/Loop/Payment.php b/core/lib/Thelia/Core/Template/Loop/Payment.php index 0fde890a9..dee4f176a 100644 --- a/core/lib/Thelia/Core/Template/Loop/Payment.php +++ b/core/lib/Thelia/Core/Template/Loop/Payment.php @@ -26,6 +26,7 @@ use Thelia\Core\Template\Element\LoopResult; use Thelia\Core\Template\Element\LoopResultRow; use Thelia\Core\Template\Element\PropelSearchLoopInterface; use Thelia\Module\BaseModule; +use Thelia\Module\PaymentModuleInterface; /** * Class Payment @@ -47,14 +48,11 @@ class Payment extends BaseSpecificModule implements PropelSearchLoopInterface foreach ($loopResult->getResultDataCollection() as $paymentModule) { $loopResultRow = new LoopResultRow($paymentModule); - $moduleReflection = new \ReflectionClass($paymentModule->getFullNamespace()); - if ($moduleReflection->isSubclassOf("Thelia\Module\PaymentModuleInterface") === false) { + $moduleInstance = $this->container->get(sprintf('module.%s', $paymentModule->getCode())); + + if (false === $moduleInstance instanceof PaymentModuleInterface) { throw new \RuntimeException(sprintf("payment module %s is not a Thelia\Module\PaymentModuleInterface", $paymentModule->getCode())); } - $moduleInstance = $moduleReflection->newInstance(); - - $moduleInstance->setRequest($this->request); - $moduleInstance->setDispatcher($this->dispatcher); $loopResultRow ->set('ID', $paymentModule->getId()) diff --git a/core/lib/Thelia/Core/Thelia.php b/core/lib/Thelia/Core/Thelia.php index b48d2736f..2212686fd 100755 --- a/core/lib/Thelia/Core/Thelia.php +++ b/core/lib/Thelia/Core/Thelia.php @@ -35,6 +35,7 @@ namespace Thelia\Core; use Propel\Runtime\Connection\ConnectionWrapper; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; +use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\Finder\Finder; use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\Config\Loader\LoaderInterface; @@ -134,7 +135,7 @@ class Thelia extends Kernel $defintion = new Definition(); $defintion->setClass($module->getFullNamespace()); - $defintion->addMethodCall("setContainer", array('service_container')); + $defintion->addMethodCall("setContainer", array(new Reference('service_container'))); $container->setDefinition( "module.".$module->getCode(), diff --git a/core/lib/Thelia/Exception/OrderException.php b/core/lib/Thelia/Exception/OrderException.php index 676447858..8654277c5 100755 --- a/core/lib/Thelia/Exception/OrderException.php +++ b/core/lib/Thelia/Exception/OrderException.php @@ -38,6 +38,7 @@ class OrderException extends \RuntimeException const CART_EMPTY = 100; const UNDEFINED_DELIVERY = 200; + const DELIVERY_MODULE_UNAVAILABLE = 201; public function __construct($message, $code = null, $arguments = array(), $previous = null) { diff --git a/core/lib/Thelia/Model/Cart.php b/core/lib/Thelia/Model/Cart.php index 89eeff249..36b604258 100755 --- a/core/lib/Thelia/Model/Cart.php +++ b/core/lib/Thelia/Model/Cart.php @@ -112,4 +112,18 @@ class Cart extends BaseCart return $total; } + + public function getWeight() + { + $weight = 0; + + foreach($this->getCartItems() as $cartItem) { + $itemWeight = $cartItem->getProductSaleElements()->getWeight(); + $itemWeight *= $cartItem->getQuantity(); + + $weight += $itemWeight; + } + + return $weight; + } } diff --git a/install/faker.php b/install/faker.php index d834e294c..23b6b048a 100755 --- a/install/faker.php +++ b/install/faker.php @@ -424,7 +424,7 @@ try { $stock->setQuantity($faker->randomNumber(1,50)); $stock->setPromo($faker->randomNumber(0,1)); $stock->setNewness($faker->randomNumber(0,1)); - $stock->setWeight($faker->randomFloat(2, 100,10000)); + $stock->setWeight($faker->randomFloat(2, 1, 5)); $stock->setIsDefault($i == 0); $stock->setEanCode(substr(str_shuffle("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 13)); $stock->save(); diff --git a/local/modules/Colissimo/Colissimo.php b/local/modules/Colissimo/Colissimo.php index e699a25a1..51c8fc98f 100755 --- a/local/modules/Colissimo/Colissimo.php +++ b/local/modules/Colissimo/Colissimo.php @@ -25,6 +25,7 @@ namespace Colissimo; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\Request; +use Thelia\Exception\OrderException; use Thelia\Model\Country; use Thelia\Module\BaseModule; use Thelia\Module\DeliveryModuleInterface; @@ -34,144 +35,57 @@ class Colissimo extends BaseModule implements DeliveryModuleInterface protected $request; protected $dispatcher; - protected $prices = array( - // area 1 : France - "1" => array( - "slices" => array( // max_weight => price - '0.25' => 5.23, - '0.5' => 5.8, - '0.75' => 6.56, - '1' => 7.13, - '2' => 8.08, - '3' => 9.22, - '5' => 11.31, - '7' => 13.40, - '10' => 16.53, - '15' => 19.14, - '30' => 26.93, - ), - ), - /* - * area 2 : A Zone - * Union Européenne et Suisse - */ - "2" => array( - "slices" => array( // max_weight => price - '1' => 15.34, - '2' => 16.96, - '3' => 20.47, - '4' => 23.99, - '5' => 27.50, - '6' => 31.02, - '7' => 34.53, - '8' => 38.05, - '9' => 41.56, - '10' => 45.08, - '15' => 51.92, - '20' => 58.76, - '25' => 65.60, - '30' => 72.44, - ), - ), - /* - * area 3 : B Zone - * Pays de l’Europe de l’Est (hors Union Européenne), Norvège, Maghreb - */ - "3" => array( - "slices" => array( // max_weight => price - '1' => 18.81, - '2' => 20.62, - '3' => 24.94, - '4' => 29.26, - '5' => 33.58, - '6' => 37.91, - '7' => 42.23, - '8' => 46.55, - '9' => 50.87, - '10' => 55.20, - '15' => 65.08, - '20' => 74.96, - ), - ), - /* - * area 4 : C Zone - * Pays d’Afrique hors Maghreb, Canada, Etats-Unis, Proche et Moyen Orient - */ - "4" => array( - "slices" => array( // max_weight => price - '1' => 22.04, - '2' => 29.55, - '3' => 38.86, - '4' => 48.17, - '5' => 57.48, - '6' => 66.79, - '7' => 76.10, - '8' => 85.41, - '9' => 94.72, - '10' => 104.03, - '15' => 126.92, - '20' => 149.82, - ), - ), - /* - * area 5 : D Zone - * Autres destinations - */ - "5" => array( - "slices" => array( // max_weight => price - '1' => 25.08, - '2' => 37.72, - '3' => 50.26, - '4' => 62.80, - '5' => 75.34, - '6' => 87.88, - '7' => 100.42, - '8' => 112.96, - '9' => 125.50, - '10' => 138.04, - '15' => 162.74, - '20' => 187.44, - ), - ), - "6" => array( // area 6 : France OM1 - "slices" => array( // max_weight => price - '0.5' => 8.27, - '1' => 12.49, - '2' => 17.05, - '3' => 21.61, - '4' => 26.17, - '5' => 30.73, - '6' => 35.29, - '7' => 39.85, - '8' => 44.41, - '9' => 48.97, - '10' => 53.53, - '15' => 76.33, - '20' => 99.13, - '25' => 121.93, - '30' => 144.73, - ), - ), - "7" => array( // area 7 : France OM2 - "slices" => array( // max_weight => price - '0.5' => 9.88, - '1' => 14.92, - '2' => 26.32, - '3' => 37.72, - '4' => 49.12, - '5' => 60.52, - '6' => 71.92, - '7' => 83.32, - '8' => 94.72, - '9' => 106.12, - '10' => 117.52, - '15' => 174.52, - '20' => 231.52, - '25' => 288.52, - '30' => 345.52, - ), - ), - ); + private static $prices = null; + + const JSON_PRICE_RESOURCE = "prices.json"; + + public function getPrices() + { + if(null === self::$prices) { + self::$prices = json_decode(file_get_contents(sprintf('%s/Config/%s', __DIR__, self::JSON_PRICE_RESOURCE)), true); + } + + return self::$prices; + } + + /** + * @param $areaId + * @param $weight + * + * @return mixed + * @throws \Thelia\Exception\OrderException + */ + public function getPostageAmount($areaId, $weight) + { + $prices = $this->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); + } + + $areaPrices = $prices[$areaId]["slices"]; + ksort($areaPrices); + + /* check this weight is not too much */ + 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); + } + + $postage = current($areaPrices); + + while(prev($areaPrices)) { + if($weight > key($areaPrices)) { + break; + } + + $postage = current($areaPrices); + } + + return $postage; + } public function setRequest(Request $request) { @@ -199,11 +113,18 @@ class Colissimo extends BaseModule implements DeliveryModuleInterface * * @param Country $country * @return mixed + * @throws \Thelia\Exception\OrderException */ public function getPostage(Country $country) { - // TODO: Implement getPostage() method. - return 2; + $cartWeight = $this->getContainer()->get('request')->getSession()->getCart()->getWeight(); + + $postage = $this->getPostageAmount( + $country->getAreaId(), + $cartWeight + ); + + return $postage; } public function getCode() diff --git a/local/modules/Colissimo/Config/prices.json b/local/modules/Colissimo/Config/prices.json new file mode 100644 index 000000000..dd1888dc6 --- /dev/null +++ b/local/modules/Colissimo/Config/prices.json @@ -0,0 +1,128 @@ +{ + "1": { + "_info": "area 1 : France", + "slices": { + "0.25": 5.23, + "0.5": 5.8, + "0.75": 6.56, + "1": 7.13, + "2": 8.08, + "3": 9.22, + "5": 11.31, + "7": 13.4, + "10": 16.53, + "15": 19.14, + "30": 26.93 + } + }, + "2": { + "_info": "area 2 : A Zone - Union Européenne et Suisse", + "slices": { + "1": 15.34, + "2": 16.96, + "3": 20.47, + "4": 23.99, + "5": 27.5, + "6": 31.02, + "7": 34.53, + "8": 38.05, + "9": 41.56, + "10": 45.08, + "15": 51.92, + "20": 58.76, + "25": 65.6, + "30": 72.44 + } + }, + "3": { + "_info": "area 3 : B Zone - Pays de l’Europe de l’Est (hors Union Européenne), Norvège, Maghreb", + "slices": { + "1": 18.81, + "2": 20.62, + "3": 24.94, + "4": 29.26, + "5": 33.58, + "6": 37.91, + "7": 42.23, + "8": 46.55, + "9": 50.87, + "10": 55.2, + "15": 65.08, + "20": 74.96 + } + }, + "4": { + "_info": "area 4 : C Zone - Pays d’Afrique hors Maghreb, Canada, Etats-Unis, Proche et Moyen Orient", + "slices": { + "1": 22.04, + "2": 29.55, + "3": 38.86, + "4": 48.17, + "5": 57.48, + "6": 66.79, + "7": 76.1, + "8": 85.41, + "9": 94.72, + "10": 104.03, + "15": 126.92, + "20": 149.82 + } + }, + "5": { + "_info": "area 5 : D Zone - Autres destinations", + "slices": { + "1": 25.08, + "2": 37.72, + "3": 50.26, + "4": 62.8, + "5": 75.34, + "6": 87.88, + "7": 100.42, + "8": 112.96, + "9": 125.5, + "10": 138.04, + "15": 162.74, + "20": 187.44 + } + }, + "6": { + "_info": "area 6 : France OM1", + "slices": { + "0.5": 8.27, + "1": 12.49, + "2": 17.05, + "3": 21.61, + "4": 26.17, + "5": 30.73, + "6": 35.29, + "7": 39.85, + "8": 44.41, + "9": 48.97, + "10": 53.53, + "15": 76.33, + "20": 99.13, + "25": 121.93, + "30": 144.73 + } + }, + "7": { + "_info": "area 7 : France OM2", + "slices": { + "0.5": 9.88, + "1": 14.92, + "2": 26.32, + "3": 37.72, + "4": 49.12, + "5": 60.52, + "6": 71.92, + "7": 83.32, + "8": 94.72, + "9": 106.12, + "10": 117.52, + "15": 174.52, + "20": 231.52, + "25": 288.52, + "30": 345.52 + } + } +} \ No newline at end of file