Restored previous version

This commit is contained in:
Franck Allimant
2014-04-18 00:06:49 +02:00
parent 4b18c4ffba
commit 784ab68ca5

View File

@@ -1,118 +1,106 @@
<?php <?php
/*************************************************************************************/ /*************************************************************************************/
/* */ /* */
/* Thelia */ /* Thelia */
/* */ /* */
/* Copyright (c) OpenStudio */ /* Copyright (c) OpenStudio */
/* email : info@thelia.net */ /* email : info@thelia.net */
/* web : http://www.thelia.net */ /* web : http://www.thelia.net */
/* */ /* */
/* This program is free software; you can redistribute it and/or modify */ /* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */ /* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */ /* the Free Software Foundation; either version 3 of the License */
/* */ /* */
/* This program is distributed in the hope that it will be useful, */ /* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */ /* GNU General Public License for more details. */
/* */ /* */
/* You should have received a copy of the GNU General Public License */ /* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */ /* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */ /* */
/*************************************************************************************/ /*************************************************************************************/
namespace Front\Controller; namespace Front\Controller;
use Propel\Runtime\Exception\PropelException; use Propel\Runtime\Exception\PropelException;
use Thelia\Controller\Front\BaseFrontController; use Thelia\Controller\Front\BaseFrontController;
use Thelia\Core\Event\Coupon\CouponConsumeEvent; use Thelia\Core\Event\Coupon\CouponConsumeEvent;
use Thelia\Core\Event\Order\OrderEvent; use Thelia\Core\Event\Order\OrderEvent;
use Thelia\Form\CouponCode; use Thelia\Form\CouponCode;
use Thelia\Form\Exception\FormValidationException; use Thelia\Form\Exception\FormValidationException;
use Thelia\Core\Event\TheliaEvents; use Thelia\Core\Event\TheliaEvents;
use Thelia\Log\Tlog; use Thelia\Log\Tlog;
use Thelia\Model\AddressQuery; use Thelia\Model\AddressQuery;
use Thelia\Module\Exception\DeliveryException;
/**
/** * Class CouponController
* Class CouponController * @package Thelia\Controller\Front
* @package Thelia\Controller\Front * @author Guillaume MOREL <gmorel@openstudio.fr>
* @author Guillaume MOREL <gmorel@openstudio.fr> */
*/ class CouponController extends BaseFrontController
class CouponController extends BaseFrontController {
{
/**
/** * Test Coupon consuming
* Test Coupon consuming */
*/ public function consumeAction()
public function consumeAction() {
{ $this->checkAuth();
$this->checkAuth(); $this->checkCartNotEmpty();
$this->checkCartNotEmpty();
$message = false;
$message = false; $couponCodeForm = new CouponCode($this->getRequest());
$couponCodeForm = new CouponCode($this->getRequest());
try {
try { $form = $this->validateForm($couponCodeForm, 'post');
$form = $this->validateForm($couponCodeForm, 'post');
$couponCode = $form->get('coupon-code')->getData();
$couponCode = $form->get('coupon-code')->getData();
if (null === $couponCode || empty($couponCode)) {
if (null === $couponCode || empty($couponCode)) { $message = true;
$message = true; throw new \Exception('Coupon code can\'t be empty');
throw new \Exception('Coupon code can\'t be empty'); }
}
$couponConsumeEvent = new CouponConsumeEvent($couponCode);
$couponConsumeEvent = new CouponConsumeEvent($couponCode);
// Dispatch Event to the Action
// Dispatch Event to the Action $this->getDispatcher()->dispatch(TheliaEvents::COUPON_CONSUME, $couponConsumeEvent);
$this->getDispatcher()->dispatch(TheliaEvents::COUPON_CONSUME, $couponConsumeEvent);
/* recalculate postage amount */
/* recalculate postage amount */ $order = $this->getSession()->getOrder();
$order = $this->getSession()->getOrder(); if (null !== $order) {
if (null !== $order) { $deliveryModule = $order->getModuleRelatedByDeliveryModuleId();
$deliveryModule = $order->getModuleRelatedByDeliveryModuleId(); $deliveryAddress = AddressQuery::create()->findPk($order->chosenDeliveryAddress);
$deliveryAddress = AddressQuery::create()->findPk($order->chosenDeliveryAddress);
if (null !== $deliveryModule && null !== $deliveryAddress) {
if (null !== $deliveryModule && null !== $deliveryAddress) { $moduleInstance = $this->container->get(sprintf('module.%s', $deliveryModule->getCode()));
$moduleInstance = $this->container->get(sprintf('module.%s', $deliveryModule->getCode())); $postage = $moduleInstance->getPostage($deliveryAddress->getCountry());
$orderEvent = new OrderEvent($order); $orderEvent = new OrderEvent($order);
$orderEvent->setPostage($postage);
try {
$postage = $moduleInstance->getPostage($deliveryAddress->getCountry()); $this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_POSTAGE, $orderEvent);
}
$orderEvent->setPostage($postage); }
$this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_POSTAGE, $orderEvent); $this->redirect($couponCodeForm->getSuccessUrl());
}
catch (DeliveryException $ex) { } catch (FormValidationException $e) {
// The postage has been chosen, but changes dues to coupon causes an exception. $message = sprintf('Please check your coupon code: %s', $e->getMessage());
// Reset the postage data in the order } catch (PropelException $e) {
$orderEvent->setDeliveryModule(0); $this->getParserContext()->setGeneralError($e->getMessage());
} catch (\Exception $e) {
$this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_DELIVERY_MODULE, $orderEvent); $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()));
$this->redirect($couponCodeForm->getSuccessUrl());
$couponCodeForm->setErrorMessage($message);
} catch (FormValidationException $e) {
$message = sprintf('Please check your coupon code: %s', $e->getMessage()); $this->getParserContext()
} catch (PropelException $e) { ->addForm($couponCodeForm)
$this->getParserContext()->setGeneralError($e->getMessage()); ->setGeneralError($message);
} 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);
}
}
}