findOne(); } catch (\Exception $ex) { // the table doest not exist $injectSql = true; } if (true === $injectSql) { $database = new Database($con); $database->insertSql(null, [__DIR__ . '/Config/thelia.sql']); } return true; } public function postActivation(ConnectionInterface $con = null) { // register config variables if (null === ConfigQuery::read(self::CONFIG_TRACKING_URL, null)) { ConfigQuery::write(self::CONFIG_TRACKING_URL, self::DEFAULT_TRACKING_URL); } if (null === ConfigQuery::read(self::CONFIG_PICKING_METHOD, null)) { ConfigQuery::write(self::CONFIG_PICKING_METHOD, self::DEFAULT_PICKING_METHOD); } if (null === ConfigQuery::read(self::CONFIG_TAX_RULE_ID, null)) { ConfigQuery::write(self::CONFIG_TAX_RULE_ID, self::DEFAULT_TAX_RULE_ID); } // create new message if (null === MessageQuery::create()->findOneByName('mail_custom_delivery')) { $message = new Message(); $message ->setName('mail_custom_delivery') ->setHtmlTemplateFileName('custom-delivery-shipping.html') ->setHtmlLayoutFileName('') ->setTextTemplateFileName('custom-delivery-shipping.txt') ->setTextLayoutFileName('') ->setSecured(0); $languages = LangQuery::create()->find(); foreach ($languages as $language) { $locale = $language->getLocale(); $message->setLocale($locale); $message->setTitle( $this->trans('Custom delivery shipping message', [], $locale) ); $message->setSubject( $this->trans('Your order {$order_ref} has been shipped', [], $locale) ); } $message->save(); } } protected function trans($id, array $parameters = [], $locale = null) { if (null === $this->translator) { $this->translator = Translator::getInstance(); } return $this->translator->trans($id, $parameters, CustomDelivery::MESSAGE_DOMAIN, $locale); } /** * This method is called by the Delivery loop, to check if the current module has to be displayed to the customer. * Override it to implements your delivery rules/ * * If you return true, the delivery method will de displayed to the customer * If you return false, the delivery method will not be displayed * * @param Country $country the country to deliver to. * * @return boolean */ public function isValidDelivery(Country $country) { // Retrieve the cart $cart = $this->getRequest()->getSession()->getSessionCart($this->getDispatcher()); /** @var CustomDeliverySlice $slice */ $slice = $this->getSlicePostage($cart, $country); return null !== $slice; } /** * @param Cart $cart * @param $areaId * @return |null */ protected function getSlicePostage(Cart $cart, Country $country) { $config = self::getConfig(); $currency = $cart->getCurrency(); $areas = CountryAreaQuery::create() ->select([ CountryAreaTableMap::AREA_ID ]) ->findByCountryId($country->getId()) ; $query = CustomDeliverySliceQuery::create()->filterByAreaId($areas, Criteria::IN); 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; } public static function getConfig() { $config = [ 'url' => ( ConfigQuery::read(self::CONFIG_TRACKING_URL, self::DEFAULT_TRACKING_URL) ), 'method' => ( intval(ConfigQuery::read(self::CONFIG_PICKING_METHOD, self::DEFAULT_PICKING_METHOD)) ), 'tax' => ( intval(ConfigQuery::read(self::CONFIG_TAX_RULE_ID, self::DEFAULT_TAX_RULE_ID)) ) ]; return $config; } /** * Calculate and return delivery price in the shop's default currency * * @param Country $country the country to deliver to. * * @return OrderPostage the delivery price * @throws DeliveryException if the postage price cannot be calculated. */ public function getPostage(Country $country) { $areaId = $country->getAreaId(); $cart = $this->getRequest()->getSession()->getSessionCart($this->getDispatcher()); /** @var CustomDeliverySlice $slice */ $postage = $this->getSlicePostage($cart, $country); if (null === $postage) { throw new DeliveryException(); } return $postage; } /** * * This method return true if your delivery manages virtual product delivery. * * @return bool */ public function handleVirtualProductDelivery() { return false; } }