Inital commit
This commit is contained in:
@@ -22,15 +22,18 @@
|
||||
/*************************************************************************************/
|
||||
namespace Front\Controller;
|
||||
|
||||
use Front\Front;
|
||||
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\Exception\UnmatchableConditionException;
|
||||
use Thelia\Form\Definition\FrontForm;
|
||||
use Thelia\Form\Exception\FormValidationException;
|
||||
use Thelia\Log\Tlog;
|
||||
use Thelia\Model\AddressQuery;
|
||||
use Thelia\Model\OrderPostage;
|
||||
use Thelia\Module\Exception\DeliveryException;
|
||||
|
||||
/**
|
||||
@@ -43,7 +46,8 @@ class CouponController extends BaseFrontController
|
||||
/**
|
||||
* Clear all coupons.
|
||||
*/
|
||||
public function clearAllCouponsAction() {
|
||||
public function clearAllCouponsAction()
|
||||
{
|
||||
// Dispatch Event to the Action
|
||||
$this->getDispatcher()->dispatch(TheliaEvents::COUPON_CLEAR_ALL);
|
||||
}
|
||||
@@ -53,11 +57,10 @@ class CouponController extends BaseFrontController
|
||||
*/
|
||||
public function consumeAction()
|
||||
{
|
||||
$this->checkAuth();
|
||||
$this->checkCartNotEmpty();
|
||||
|
||||
$message = false;
|
||||
$couponCodeForm = new CouponCode($this->getRequest());
|
||||
$couponCodeForm = $this->createForm(FrontForm::COUPON_CONSUME);
|
||||
|
||||
try {
|
||||
$form = $this->validateForm($couponCodeForm, 'post');
|
||||
@@ -66,7 +69,13 @@ class CouponController extends BaseFrontController
|
||||
|
||||
if (null === $couponCode || empty($couponCode)) {
|
||||
$message = true;
|
||||
throw new \Exception('Coupon code can\'t be empty');
|
||||
throw new \Exception(
|
||||
$this->getTranslator()->trans(
|
||||
'Coupon code can\'t be empty',
|
||||
[],
|
||||
Front::MESSAGE_DOMAIN
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$couponConsumeEvent = new CouponConsumeEvent($couponCode);
|
||||
@@ -82,19 +91,21 @@ class CouponController extends BaseFrontController
|
||||
$deliveryAddress = AddressQuery::create()->findPk($order->getChoosenDeliveryAddress());
|
||||
|
||||
if (null !== $deliveryModule && null !== $deliveryAddress) {
|
||||
|
||||
$moduleInstance = $deliveryModule->getModuleInstance($this->container);
|
||||
$moduleInstance = $deliveryModule->getDeliveryModuleInstance($this->container);
|
||||
|
||||
$orderEvent = new OrderEvent($order);
|
||||
|
||||
try {
|
||||
$postage = $moduleInstance->getPostage($deliveryAddress->getCountry());
|
||||
$postage = OrderPostage::loadFromPostage(
|
||||
$moduleInstance->getPostage($deliveryAddress->getCountry())
|
||||
);
|
||||
|
||||
$orderEvent->setPostage($postage);
|
||||
$orderEvent->setPostage($postage->getAmount());
|
||||
$orderEvent->setPostageTax($postage->getAmountTax());
|
||||
$orderEvent->setPostageTaxRuleTitle($postage->getTaxRuleTitle());
|
||||
|
||||
$this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_POSTAGE, $orderEvent);
|
||||
}
|
||||
catch (DeliveryException $ex) {
|
||||
} 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);
|
||||
@@ -104,18 +115,37 @@ class CouponController extends BaseFrontController
|
||||
}
|
||||
}
|
||||
|
||||
$this->redirect($couponCodeForm->getSuccessUrl());
|
||||
return $this->generateSuccessRedirect($couponCodeForm);
|
||||
|
||||
} catch (FormValidationException $e) {
|
||||
$message = sprintf('Please check your coupon code: %s', $e->getMessage());
|
||||
$message = $this->getTranslator()->trans(
|
||||
'Please check your coupon code: %message',
|
||||
["%message" => $e->getMessage()],
|
||||
Front::MESSAGE_DOMAIN
|
||||
);
|
||||
} catch (UnmatchableConditionException $e) {
|
||||
$message = $this->getTranslator()->trans(
|
||||
'You should <a href="%sign">sign in</a> or <a href="%register">register</a> to use this coupon',
|
||||
[
|
||||
'%sign' => $this->retrieveUrlFromRouteId('customer.login.view'),
|
||||
'%register' => $this->retrieveUrlFromRouteId('customer.create.view'),
|
||||
],
|
||||
Front::MESSAGE_DOMAIN
|
||||
);
|
||||
} catch (PropelException $e) {
|
||||
$this->getParserContext()->setGeneralError($e->getMessage());
|
||||
} catch (\Exception $e) {
|
||||
$message = sprintf('Sorry, an error occurred: %s', $e->getMessage());
|
||||
$message = $this->getTranslator()->trans(
|
||||
'Sorry, an error occurred: %message',
|
||||
["%message" => $e->getMessage()],
|
||||
Front::MESSAGE_DOMAIN
|
||||
);
|
||||
}
|
||||
|
||||
if ($message !== false) {
|
||||
Tlog::getInstance()->error(sprintf("Error during order delivery process : %s. Exception was %s", $message, $e->getMessage()));
|
||||
Tlog::getInstance()->error(
|
||||
sprintf("Error during order delivery process : %s. Exception was %s", $message, $e->getMessage())
|
||||
);
|
||||
|
||||
$couponCodeForm->setErrorMessage($message);
|
||||
|
||||
@@ -123,5 +153,7 @@ class CouponController extends BaseFrontController
|
||||
->addForm($couponCodeForm)
|
||||
->setGeneralError($message);
|
||||
}
|
||||
|
||||
return $this->generateErrorRedirect($couponCodeForm);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user