Merge pull request #310 from roadster31/modulesfix

Modulesfix
This commit is contained in:
Manuel Raynaud
2014-04-18 09:19:06 +02:00
13 changed files with 606 additions and 353 deletions

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

@@ -1 +1,120 @@
{"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\u00e9enne 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\u2019Europe de l\u2019Est (hors Union Europ\u00e9enne), Norv\u00e8ge, 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\u2019Afrique 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}}}
{"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\u00e9enne 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\u2019Europe de l\u2019Est (hors Union Europ\u00e9enne), Norv\u00e8ge, 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\u2019Afrique 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
}
}}

View File

@@ -23,14 +23,16 @@
namespace Front\Controller;
use Propel\Runtime\Exception\PropelException;
use Thelia\Controller\Front\BaseFrontController;
use Thelia\Core\Event\Order\OrderEvent;
use Thelia\Form\Exception\FormValidationException;
use Thelia\Core\Event\Cart\CartEvent;
use Thelia\Core\Event\TheliaEvents;
use Symfony\Component\HttpFoundation\Request;
use Thelia\Controller\Front\BaseFrontController;
use Thelia\Core\Event\Cart\CartEvent;
use Thelia\Core\Event\Order\OrderEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Form\CartAdd;
use Thelia\Form\Exception\FormValidationException;
use Thelia\Log\Tlog;
use Thelia\Model\AddressQuery;
use Thelia\Module\Exception\DeliveryException;
class CartController extends BaseFrontController
{
@@ -60,7 +62,7 @@ class CartController extends BaseFrontController
$this->redirectSuccess();
} catch (PropelException $e) {
\Thelia\Log\Tlog::getInstance()->error(sprintf("Failed to add item to cart with message : %s", $e->getMessage()));
Tlog::getInstance()->error(sprintf("Failed to add item to cart with message : %s", $e->getMessage()));
$message = "Failed to add this article to your cart, please try again";
} catch (FormValidationException $e) {
$message = $e->getMessage();
@@ -108,7 +110,7 @@ class CartController extends BaseFrontController
$this->redirectSuccess();
} catch (PropelException $e) {
\Thelia\Log\Tlog::getInstance()->error(sprintf("error during deleting cartItem with message : %s", $e->getMessage()));
Tlog::getInstance()->error(sprintf("error during deleting cartItem with message : %s", $e->getMessage()));
$this->getParserContext()->setGeneralError($e->getMessage());
}
@@ -160,14 +162,24 @@ 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

@@ -26,11 +26,12 @@ use Propel\Runtime\Exception\PropelException;
use Thelia\Controller\Front\BaseFrontController;
use Thelia\Core\Event\Coupon\CouponConsumeEvent;
use Thelia\Core\Event\Order\OrderEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Form\CouponCode;
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);
}
}
}