Exemple #1
0
 public function parseResults(LoopResult $loopResult)
 {
     $country = $this->getCurrentCountry();
     $state = $this->getCurrentState();
     $cart = $this->request->getSession()->getSessionCart($this->dispatcher);
     $virtual = $cart->isVirtual();
     /** @var Module $deliveryModule */
     foreach ($loopResult->getResultDataCollection() as $deliveryModule) {
         $areaDeliveryModule = AreaDeliveryModuleQuery::create()->findByCountryAndModule($country, $deliveryModule, $state);
         if (null === $areaDeliveryModule && false === $virtual) {
             continue;
         }
         /** @var DeliveryModuleInterface $moduleInstance */
         $moduleInstance = $deliveryModule->getDeliveryModuleInstance($this->container);
         if (true === $virtual && false === $moduleInstance->handleVirtualProductDelivery() && false === $this->getBackendContext()) {
             continue;
         }
         $loopResultRow = new LoopResultRow($deliveryModule);
         try {
             // Check if module is valid, by calling isValidDelivery(),
             // or catching a DeliveryException.
             if ($moduleInstance->isValidDelivery($country)) {
                 $postage = OrderPostage::loadFromPostage($moduleInstance->getPostage($country));
                 $loopResultRow->set('ID', $deliveryModule->getId())->set('CODE', $deliveryModule->getCode())->set('TITLE', $deliveryModule->getVirtualColumn('i18n_TITLE'))->set('CHAPO', $deliveryModule->getVirtualColumn('i18n_CHAPO'))->set('DESCRIPTION', $deliveryModule->getVirtualColumn('i18n_DESCRIPTION'))->set('POSTSCRIPTUM', $deliveryModule->getVirtualColumn('i18n_POSTSCRIPTUM'))->set('POSTAGE', $postage->getAmount())->set('POSTAGE_TAX', $postage->getAmountTax())->set('POSTAGE_UNTAXED', $postage->getAmount() - $postage->getAmountTax())->set('POSTAGE_TAX_RULE_TITLE', $postage->getTaxRuleTitle());
                 $this->addOutputFields($loopResultRow, $deliveryModule);
                 $loopResult->addRow($loopResultRow);
             }
         } catch (DeliveryException $ex) {
             // Module is not available
         }
     }
     return $loopResult;
 }
 /**
  * Coupon consuming
  */
 public function consumeAction()
 {
     $this->checkCartNotEmpty();
     $message = false;
     $couponCodeForm = $this->createForm(FrontForm::COUPON_CONSUME);
     try {
         $form = $this->validateForm($couponCodeForm, 'post');
         $couponCode = $form->get('coupon-code')->getData();
         if (null === $couponCode || empty($couponCode)) {
             $message = true;
             throw new \Exception($this->getTranslator()->trans('Coupon code can\'t be empty', [], Front::MESSAGE_DOMAIN));
         }
         $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->getChoosenDeliveryAddress());
             if (null !== $deliveryModule && null !== $deliveryAddress) {
                 $moduleInstance = $deliveryModule->getDeliveryModuleInstance($this->container);
                 $orderEvent = new OrderEvent($order);
                 try {
                     $postage = OrderPostage::loadFromPostage($moduleInstance->getPostage($deliveryAddress->getCountry()));
                     $orderEvent->setPostage($postage->getAmount());
                     $orderEvent->setPostageTax($postage->getAmountTax());
                     $orderEvent->setPostageTaxRuleTitle($postage->getTaxRuleTitle());
                     $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);
                 }
             }
         }
         return $this->generateSuccessRedirect($couponCodeForm);
     } catch (FormValidationException $e) {
         $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 = $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()));
         $couponCodeForm->setErrorMessage($message);
         $this->getParserContext()->addForm($couponCodeForm)->setGeneralError($message);
     }
     return $this->generateErrorRedirect($couponCodeForm);
 }
Exemple #3
0
 protected function afterModifyCart()
 {
     /* recalculate postage amount */
     $order = $this->getSession()->getOrder();
     if (null !== $order) {
         $deliveryModule = $order->getModuleRelatedByDeliveryModuleId();
         $deliveryAddress = AddressQuery::create()->findPk($order->getChoosenDeliveryAddress());
         if (null !== $deliveryModule && null !== $deliveryAddress) {
             $moduleInstance = $deliveryModule->getDeliveryModuleInstance($this->container);
             $orderEvent = new OrderEvent($order);
             try {
                 $postage = OrderPostage::loadFromPostage($moduleInstance->getPostage($deliveryAddress->getCountry()));
                 $orderEvent->setPostage($postage->getAmount());
                 $orderEvent->setPostageTax($postage->getAmountTax());
                 $orderEvent->setPostageTaxRuleTitle($postage->getTaxRuleTitle());
                 $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);
             }
         }
     }
 }
Exemple #4
0
 /**
  * set delivery address
  * set delivery module
  */
 public function deliver()
 {
     $this->checkAuth();
     $this->checkCartNotEmpty();
     $message = false;
     $orderDelivery = $this->createForm(FrontForm::ORDER_DELIVER);
     try {
         $form = $this->validateForm($orderDelivery, "post");
         $deliveryAddressId = $form->get("delivery-address")->getData();
         $deliveryModuleId = $form->get("delivery-module")->getData();
         $deliveryAddress = AddressQuery::create()->findPk($deliveryAddressId);
         $deliveryModule = ModuleQuery::create()->findPk($deliveryModuleId);
         /* check that the delivery address belongs to the current customer */
         if ($deliveryAddress->getCustomerId() !== $this->getSecurityContext()->getCustomerUser()->getId()) {
             throw new \Exception($this->getTranslator()->trans("Delivery address does not belong to the current customer", [], Front::MESSAGE_DOMAIN));
         }
         /* check that the delivery module fetches the delivery address area */
         if (null === AreaDeliveryModuleQuery::create()->findByCountryAndModule($deliveryAddress->getCountry(), $deliveryModule)) {
             throw new \Exception($this->getTranslator()->trans("Delivery module cannot be use with selected delivery address", [], Front::MESSAGE_DOMAIN));
         }
         /* get postage amount */
         $moduleInstance = $deliveryModule->getDeliveryModuleInstance($this->container);
         $postage = OrderPostage::loadFromPostage($moduleInstance->getPostage($deliveryAddress->getCountry()));
         $orderEvent = $this->getOrderEvent();
         $orderEvent->setDeliveryAddress($deliveryAddressId);
         $orderEvent->setDeliveryModule($deliveryModuleId);
         $orderEvent->setPostage($postage->getAmount());
         $orderEvent->setPostageTax($postage->getAmountTax());
         $orderEvent->setPostageTaxRuleTitle($postage->getTaxRuleTitle());
         $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);
         return $this->generateRedirectFromRoute("order.invoice");
     } catch (FormValidationException $e) {
         $message = $this->getTranslator()->trans("Please check your input: %s", ['%s' => $e->getMessage()], Front::MESSAGE_DOMAIN);
     } catch (PropelException $e) {
         $this->getParserContext()->setGeneralError($e->getMessage());
     } catch (\Exception $e) {
         $message = $this->getTranslator()->trans("Sorry, an error occured: %s", ['%s' => $e->getMessage()], Front::MESSAGE_DOMAIN);
     }
     if ($message !== false) {
         Tlog::getInstance()->error(sprintf("Error during order delivery process : %s. Exception was %s", $message, $e->getMessage()));
         $orderDelivery->setErrorMessage($message);
         $this->getParserContext()->addForm($orderDelivery)->setGeneralError($message);
     }
 }
 protected function getShippings($country)
 {
     $search = ModuleQuery::create()->filterByActivate(1)->filterByType(BaseModule::DELIVERY_MODULE_TYPE, Criteria::EQUAL)->find();
     if (null === $country) {
         throw new \Exception($this->getTranslator()->trans('Target country not defined for GoogleShopping', [], GoogleShopping::DOMAIN_NAME));
     }
     $deliveries = array();
     /** @var Module $deliveryModule */
     foreach ($search as $deliveryModule) {
         $areaDeliveryModule = AreaDeliveryModuleQuery::create()->findByCountryAndModule($country, $deliveryModule);
         if (null === $areaDeliveryModule) {
             continue;
         }
         $moduleInstance = $deliveryModule->getDeliveryModuleInstance($this->container);
         if ($moduleInstance->isValidDelivery($country)) {
             $postage = OrderPostage::loadFromPostage($moduleInstance->getPostage($country));
             $deliveries[$deliveryModule->getTitle()] = $postage;
         }
     }
     return $deliveries;
 }
Exemple #6
0
 /**
  * Retrieve the cheapest delivery for country
  *
  * @param  \Thelia\Model\Country   $country
  * @return DeliveryModuleInterface
  */
 protected function getCheapestDelivery(Country $country)
 {
     $deliveryModules = ModuleQuery::create()->filterByActivate(1)->filterByType(BaseModule::DELIVERY_MODULE_TYPE, Criteria::EQUAL)->find();
     /** @var \Thelia\Model\Module $deliveryModule */
     foreach ($deliveryModules as $deliveryModule) {
         $moduleInstance = $deliveryModule->getDeliveryModuleInstance($this->container);
         try {
             // Check if module is valid, by calling isValidDelivery(),
             // or catching a DeliveryException.
             if ($moduleInstance->isValidDelivery($country)) {
                 $postage = OrderPostage::loadFromPostage($moduleInstance->getPostage($country));
                 if (null === $this->postage || $this->postage > $postage->getAmount()) {
                     $this->postage = $postage->getAmount();
                     $this->postageTax = $postage->getAmountTax();
                     $this->postageTaxRuleTitle = $postage->getTaxRuleTitle();
                     $this->deliveryId = $deliveryModule->getId();
                 }
             }
         } catch (DeliveryException $ex) {
             // Module is not available
         }
     }
 }
 /**
  * @param null|double|OrderPostage $postage
  */
 public function setPostage($postage)
 {
     $this->postage = OrderPostage::loadFromPostage($postage);
     return $this;
 }