From d7c1ecf09aa3dbd154369dae0da72764f4cec83f Mon Sep 17 00:00:00 2001 From: Etienne Roudeix Date: Thu, 19 Dec 2013 14:17:22 +0100 Subject: [PATCH] coupon management in order and cart process --- core/lib/Thelia/Action/Coupon.php | 53 ++++++++++++++++--- core/lib/Thelia/Action/Order.php | 21 ++++++-- core/lib/Thelia/Core/Event/TheliaEvents.php | 1 + core/lib/Thelia/Coupon/CouponManager.php | 22 +++----- .../Front/Controller/CartController.php | 29 ++++++++++ .../Front/Controller/CouponController.php | 22 +++++++- .../Front/Controller/OrderController.php | 1 + .../frontOffice/default/order-invoice.html | 3 ++ 8 files changed, 124 insertions(+), 28 deletions(-) diff --git a/core/lib/Thelia/Action/Coupon.php b/core/lib/Thelia/Action/Coupon.php index efda81229..2042d3e66 100755 --- a/core/lib/Thelia/Action/Coupon.php +++ b/core/lib/Thelia/Action/Coupon.php @@ -28,6 +28,7 @@ use Thelia\Condition\ConditionFactory; use Thelia\Condition\Implementation\ConditionInterface; use Thelia\Core\Event\Coupon\CouponConsumeEvent; use Thelia\Core\Event\Coupon\CouponCreateOrUpdateEvent; +use Thelia\Core\Event\Order\OrderEvent; use Thelia\Core\Event\TheliaEvents; use Thelia\Core\HttpFoundation\Request; use Thelia\Coupon\CouponFactory; @@ -122,13 +123,6 @@ class Coupon extends BaseAction implements EventSubscriberInterface $request->getSession()->setConsumedCoupons($consumedCoupons); $totalDiscount = $couponManager->getDiscount(); - // @todo insert false product in cart with the name of the coupon and the discount as negative price - - // Decrement coupon quantity - // @todo move this part in after order event - $couponQuery = CouponQuery::create(); - $couponModel = $couponQuery->findOneByCode($coupon->getCode()); - $couponManager->decrementQuantity($couponModel); $request ->getSession() @@ -206,6 +200,47 @@ class Coupon extends BaseAction implements EventSubscriberInterface $event->setCouponModel($coupon); } + /** + * @param \Thelia\Core\Event\Order\OrderEvent $event + */ + public function testFreePostage(OrderEvent $event) + { + /** @var CouponManager $couponManager */ + $couponManager = $this->container->get('thelia.coupon.manager'); + + if($couponManager->isCouponRemovingPostage()) { + $order = $event->getOrder(); + + $order->setPostage(0); + + $event->setOrder($order); + + $event->stopPropagation(); + } + } + + /** + * @param \Thelia\Core\Event\Order\OrderEvent $event + */ + public function decreaseCouponQuantity(OrderEvent $event) + { + $request = $this->container->get('request'); + + /** @var CouponManager $couponManager */ + $couponManager = $this->container->get('thelia.coupon.manager'); + + $consumedCoupons = $request->getSession()->getConsumedCoupons(); + + if (is_array($consumedCoupons)) { + foreach($consumedCoupons as $couponCode) { + // Decrement coupon quantity + $couponQuery = CouponQuery::create(); + $couponModel = $couponQuery->findOneByCode($couponCode); + $couponManager->decrementQuantity($couponModel); + } + } + } + /** * Returns an array of event names this subscriber listens to. * @@ -232,7 +267,9 @@ class Coupon extends BaseAction implements EventSubscriberInterface TheliaEvents::COUPON_CREATE => array("create", 128), TheliaEvents::COUPON_UPDATE => array("update", 128), TheliaEvents::COUPON_CONSUME => array("consume", 128), - TheliaEvents::COUPON_CONDITION_UPDATE => array("updateCondition", 128) + TheliaEvents::COUPON_CONDITION_UPDATE => array("updateCondition", 128), + TheliaEvents::ORDER_SET_POSTAGE => array("testFreePostage", 256), + TheliaEvents::ORDER_BEFORE_PAYMENT => array("decreaseCouponQuantity", 128), ); } } diff --git a/core/lib/Thelia/Action/Order.php b/core/lib/Thelia/Action/Order.php index 824514a22..b0c09ae1a 100755 --- a/core/lib/Thelia/Action/Order.php +++ b/core/lib/Thelia/Action/Order.php @@ -72,7 +72,18 @@ class Order extends BaseAction implements EventSubscriberInterface { $order = $event->getOrder(); - $order->setDeliveryModuleId($event->getDeliveryModule()); + $order->setPostage($event->getPostage()); + + $event->setOrder($order); + } + + /** + * @param \Thelia\Core\Event\Order\OrderEvent $event + */ + public function setPostage(OrderEvent $event) + { + $order = $event->getOrder(); + $order->setPostage($event->getPostage()); $event->setOrder($order); @@ -178,6 +189,8 @@ class Order extends BaseAction implements EventSubscriberInterface OrderStatusQuery::create()->findOneByCode(OrderStatus::CODE_NOT_PAID)->getId() ); + /* refresh discount @todo */ + /* memorize discount */ $placedOrder->setDiscount( $cart->getDiscount() @@ -267,10 +280,7 @@ class Order extends BaseAction implements EventSubscriberInterface } } - /* discount @todo */ - /* refresh discount */ - - /* memorize coupons */ + /* memorize coupons @todo */ $con->commit(); @@ -420,6 +430,7 @@ class Order extends BaseAction implements EventSubscriberInterface return array( TheliaEvents::ORDER_SET_DELIVERY_ADDRESS => array("setDeliveryAddress", 128), TheliaEvents::ORDER_SET_DELIVERY_MODULE => array("setDeliveryModule", 128), + TheliaEvents::ORDER_SET_POSTAGE => array("setPostage", 128), TheliaEvents::ORDER_SET_INVOICE_ADDRESS => array("setInvoiceAddress", 128), TheliaEvents::ORDER_SET_PAYMENT_MODULE => array("setPaymentModule", 128), TheliaEvents::ORDER_PAY => array("create", 128), diff --git a/core/lib/Thelia/Core/Event/TheliaEvents.php b/core/lib/Thelia/Core/Event/TheliaEvents.php index 77c781321..3d5a5504a 100755 --- a/core/lib/Thelia/Core/Event/TheliaEvents.php +++ b/core/lib/Thelia/Core/Event/TheliaEvents.php @@ -370,6 +370,7 @@ final class TheliaEvents */ const ORDER_SET_DELIVERY_ADDRESS = "action.order.setDeliveryAddress"; const ORDER_SET_DELIVERY_MODULE = "action.order.setDeliveryModule"; + const ORDER_SET_POSTAGE = "action.order.setPostage"; const ORDER_SET_INVOICE_ADDRESS = "action.order.setInvoiceAddress"; const ORDER_SET_PAYMENT_MODULE = "action.order.setPaymentModule"; const ORDER_PAY = "action.order.pay"; diff --git a/core/lib/Thelia/Coupon/CouponManager.php b/core/lib/Thelia/Coupon/CouponManager.php index c4eff7888..17d85b8cb 100644 --- a/core/lib/Thelia/Coupon/CouponManager.php +++ b/core/lib/Thelia/Coupon/CouponManager.php @@ -82,15 +82,8 @@ class CouponManager if (count($this->coupons) > 0) { $couponsKept = $this->sortCoupons($this->coupons); - $isRemovingPostage = $this->isCouponRemovingPostage($couponsKept); - $discount = $this->getEffect($couponsKept); - if ($isRemovingPostage) { - $postage = $this->facade->getCheckoutPostagePrice(); - $discount += $postage; - } - // Just In Case test $checkoutTotalPrice = $this->facade->getCartTotalPrice(); if ($discount >= $checkoutTotalPrice) { @@ -103,23 +96,24 @@ class CouponManager /** * Check if there is a Coupon removing Postage - * - * @param array $couponsKept Array of CouponInterface sorted - * * @return bool */ - protected function isCouponRemovingPostage(array $couponsKept) + public function isCouponRemovingPostage() { - $isRemovingPostage = false; + if (count($this->coupons) == 0) { + return false; + } + + $couponsKept = $this->sortCoupons($this->coupons); /** @var CouponInterface $coupon */ foreach ($couponsKept as $coupon) { if ($coupon->isRemovingPostage()) { - $isRemovingPostage = true; + return true; } } - return $isRemovingPostage; + return false; } /** diff --git a/local/modules/Front/Controller/CartController.php b/local/modules/Front/Controller/CartController.php index d3d24c3bd..5750cb306 100755 --- a/local/modules/Front/Controller/CartController.php +++ b/local/modules/Front/Controller/CartController.php @@ -24,11 +24,13 @@ 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\Form\CartAdd; +use Thelia\Model\AddressQuery; class CartController extends BaseFrontController { @@ -53,6 +55,8 @@ class CartController extends BaseFrontController $this->getDispatcher()->dispatch(TheliaEvents::CART_ADDITEM, $cartEvent); + $this->afterModifyCart(); + $this->redirectSuccess(); } catch (PropelException $e) { @@ -83,6 +87,8 @@ class CartController extends BaseFrontController try { $this->dispatch(TheliaEvents::CART_UPDATEITEM, $cartEvent); + $this->afterModifyCart(); + $this->redirectSuccess(); } catch (PropelException $e) { $this->getParserContext()->setGeneralError($e->getMessage()); @@ -98,6 +104,8 @@ class CartController extends BaseFrontController try { $this->getDispatcher()->dispatch(TheliaEvents::CART_DELETEITEM, $cartEvent); + $this->afterModifyCart(); + $this->redirectSuccess(); } catch (PropelException $e) { \Thelia\Log\Tlog::getInstance()->error(sprintf("error during deleting cartItem with message : %s", $e->getMessage())); @@ -142,4 +150,25 @@ class CartController extends BaseFrontController return $cartAdd; } + protected function afterModifyCart() + { + /* recalculate postage amount */ + $order = $this->getSession()->getOrder(); + if(null !== $order) { + $deliveryModule = $order->getModuleRelatedByDeliveryModuleId(); + $deliveryAddress = AddressQuery::create()->findPk($order->chosenDeliveryAddress); + + 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); + } + } + } + + } diff --git a/local/modules/Front/Controller/CouponController.php b/local/modules/Front/Controller/CouponController.php index 90adba504..73eef34b7 100755 --- a/local/modules/Front/Controller/CouponController.php +++ b/local/modules/Front/Controller/CouponController.php @@ -25,11 +25,12 @@ namespace Front\Controller; use Propel\Runtime\Exception\PropelException; use Thelia\Controller\Front\BaseFrontController; use Thelia\Core\Event\Coupon\CouponConsumeEvent; +use Thelia\Core\Event\Order\OrderEvent; use Thelia\Form\CouponCode; use Thelia\Form\Exception\FormValidationException; use Thelia\Core\Event\TheliaEvents; use Thelia\Log\Tlog; -use Thelia\Model\Order; +use Thelia\Model\AddressQuery; /** * Class CouponController @@ -65,6 +66,25 @@ class CouponController extends BaseFrontController // Dispatch Event to the Action $this->getDispatcher()->dispatch(TheliaEvents::COUPON_CONSUME, $couponConsumeEvent); + /* recalculate postage amount */ + $order = $this->getSession()->getOrder(); + if(null !== $order) { + $deliveryModule = $order->getModuleRelatedByDeliveryModuleId(); + $deliveryAddress = AddressQuery::create()->findPk($order->chosenDeliveryAddress); + + 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); + } + } + + $this->redirect($couponCodeForm->getSuccessUrl()); + } catch (FormValidationException $e) { $message = sprintf('Please check your coupon code: %s', $e->getMessage()); } catch (PropelException $e) { diff --git a/local/modules/Front/Controller/OrderController.php b/local/modules/Front/Controller/OrderController.php index 5f496209e..0fa1fb4f2 100755 --- a/local/modules/Front/Controller/OrderController.php +++ b/local/modules/Front/Controller/OrderController.php @@ -95,6 +95,7 @@ class OrderController extends BaseFrontController $this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_DELIVERY_ADDRESS, $orderEvent); $this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_DELIVERY_MODULE, $orderEvent); + $this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_POSTAGE, $orderEvent); $this->redirectToRoute("order.invoice"); diff --git a/templates/frontOffice/default/order-invoice.html b/templates/frontOffice/default/order-invoice.html index 78bf56667..149162110 100755 --- a/templates/frontOffice/default/order-invoice.html +++ b/templates/frontOffice/default/order-invoice.html @@ -134,6 +134,9 @@ + {form_field form=$form field='success_url'} + + {/form_field} {form_field form=$form field='coupon-code'}