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); }
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); } } } }
/** * 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; }
/** * 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; }
/** * Calculate and return delivery price in the shop's default currency * * @param Country $country the country to deliver to. * * @return OrderPostage|float the delivery price * @throws DeliveryException if the postage price cannot be calculated. */ public function getPostage(Country $country) { $postage = new OrderPostage(); $freeShipping = intval(self::getConfigValue(TNTFranceConfigValue::FREE_SHIPPING)); if (0 == $freeShipping) { $data = TNTFrance::getExtraOrderData($this->getRequest()->getSession()->getSessionCart()->getId(), true); if (array_key_exists('tnt_serviceCode', $data)) { $cartEvent = new CartEvent($this->getRequest()->getSession()->getSessionCart($this->getDispatcher())); $this->getDispatcher()->dispatch(OrderAction::TNT_CALCUL_CART_WEIGHT, $cartEvent); $postage->setAmount(self::calculPriceForService($data['tnt_serviceCode'], $cartEvent->getCart()->getVirtualColumn('total_package'), $cartEvent->getCart()->getVirtualColumn('total_weight'))); } } return $postage->getAmount(); }
/** * @param Cart $cart * @param $areaId * @return |null */ protected function getSlicePostage(Cart $cart, Country $country) { $config = self::getConfig(); $currency = $cart->getCurrency(); $areaId = $country->getAreaId(); $query = CustomDeliverySliceQuery::create()->filterByAreaId($areaId); if ($config['method'] != CustomDelivery::METHOD_PRICE) { $query->filterByWeightMax($cart->getWeight(), Criteria::GREATER_THAN); $query->orderByWeightMax(Criteria::ASC); } if ($config['method'] != CustomDelivery::METHOD_WEIGHT) { $total = $cart->getTotalAmount(); // convert amount to the default currency if (0 == $currency->getByDefault()) { $total = $total / $currency->getRate(); } $query->filterByPriceMax($total, Criteria::GREATER_THAN); $query->orderByPriceMax(Criteria::ASC); } $slice = $query->findOne(); $postage = null; if (null !== $slice) { $postage = new OrderPostage(); if (0 == $currency->getByDefault()) { $price = $slice->getPrice() * $currency->getRate(); } else { $price = $slice->getPrice(); } $price = round($price, 2); $postage->setAmount($price); $postage->setAmountTax(0); // taxed amount if (0 !== $config['tax']) { $taxRuleI18N = I18n::forceI18nRetrieving($this->getRequest()->getSession()->getLang()->getLocale(), 'TaxRule', $config['tax']); $taxRule = TaxRuleQuery::create()->findPk($config['tax']); if (null !== $taxRule) { $taxCalculator = new Calculator(); $taxCalculator->loadTaxRuleWithoutProduct($taxRule, $country); $postage->setAmount(round($taxCalculator->getTaxedPrice($price), 2)); $postage->setAmountTax($postage->getAmount() - $price); $postage->setTaxRuleTitle($taxRuleI18N->getTitle()); } } } return $postage; }