From 784ab68ca53eb3a76d169e3bccc695d10b743bcd Mon Sep 17 00:00:00 2001 From: Franck Allimant Date: Fri, 18 Apr 2014 00:06:49 +0200 Subject: [PATCH] Restored previous version --- .../Front/Controller/CouponController.php | 224 +++++++++--------- 1 file changed, 106 insertions(+), 118 deletions(-) diff --git a/local/modules/Front/Controller/CouponController.php b/local/modules/Front/Controller/CouponController.php index 185041ce1..ba7433612 100644 --- a/local/modules/Front/Controller/CouponController.php +++ b/local/modules/Front/Controller/CouponController.php @@ -1,118 +1,106 @@ -. */ -/* */ -/*************************************************************************************/ -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\AddressQuery; -use Thelia\Module\Exception\DeliveryException; - -/** - * Class CouponController - * @package Thelia\Controller\Front - * @author Guillaume MOREL - */ -class CouponController extends BaseFrontController -{ - - /** - * Test Coupon consuming - */ - public function consumeAction() - { - $this->checkAuth(); - $this->checkCartNotEmpty(); - - $message = false; - $couponCodeForm = new CouponCode($this->getRequest()); - - try { - $form = $this->validateForm($couponCodeForm, 'post'); - - $couponCode = $form->get('coupon-code')->getData(); - - if (null === $couponCode || empty($couponCode)) { - $message = true; - throw new \Exception('Coupon code can\'t be empty'); - } - - $couponConsumeEvent = new CouponConsumeEvent($couponCode); - - // 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())); - - $orderEvent = new OrderEvent($order); - - 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); - } - } - } - - $this->redirect($couponCodeForm->getSuccessUrl()); - - } catch (FormValidationException $e) { - $message = sprintf('Please check your coupon code: %s', $e->getMessage()); - } catch (PropelException $e) { - $this->getParserContext()->setGeneralError($e->getMessage()); - } catch (\Exception $e) { - $message = sprintf('Sorry, an error occurred: %s', $e->getMessage()); - } - - if ($message !== false) { - Tlog::getInstance()->error(sprintf("Error during order delivery process : %s. Exception was %s", $message, $e->getMessage())); - - $couponCodeForm->setErrorMessage($message); - - $this->getParserContext() - ->addForm($couponCodeForm) - ->setGeneralError($message); - } - } -} +. */ +/* */ +/*************************************************************************************/ +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\AddressQuery; + +/** + * Class CouponController + * @package Thelia\Controller\Front + * @author Guillaume MOREL + */ +class CouponController extends BaseFrontController +{ + + /** + * Test Coupon consuming + */ + public function consumeAction() + { + $this->checkAuth(); + $this->checkCartNotEmpty(); + + $message = false; + $couponCodeForm = new CouponCode($this->getRequest()); + + try { + $form = $this->validateForm($couponCodeForm, 'post'); + + $couponCode = $form->get('coupon-code')->getData(); + + if (null === $couponCode || empty($couponCode)) { + $message = true; + throw new \Exception('Coupon code can\'t be empty'); + } + + $couponConsumeEvent = new CouponConsumeEvent($couponCode); + + // 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) { + $this->getParserContext()->setGeneralError($e->getMessage()); + } catch (\Exception $e) { + $message = sprintf('Sorry, an error occurred: %s', $e->getMessage()); + } + + if ($message !== false) { + Tlog::getInstance()->error(sprintf("Error during order delivery process : %s. Exception was %s", $message, $e->getMessage())); + + $couponCodeForm->setErrorMessage($message); + + $this->getParserContext() + ->addForm($couponCodeForm) + ->setGeneralError($message); + } + } +}