/** * @param TaxRuleEvent $event */ public function updateTaxes(TaxRuleEvent $event) { if (null !== ($taxRule = TaxRuleQuery::create()->findPk($event->getId()))) { if (!is_array($taxList = $event->getTaxList())) { $taxList = json_decode($taxList, true); } /* clean the current tax rule for the countries */ TaxRuleCountryQuery::create()->filterByTaxRule($taxRule)->filterByCountryId($event->getCountryList(), Criteria::IN)->delete(); /* for each country */ foreach ($event->getCountryList() as $country) { $position = 1; /* on applique les nouvelles regles */ foreach ($taxList as $tax) { if (is_array($tax)) { foreach ($tax as $samePositionTax) { $taxModel = new TaxRuleCountry(); $taxModel->setTaxRule($taxRule)->setCountryId($country)->setTaxId($samePositionTax)->setPosition($position); $taxModel->save(); } } else { $taxModel = new TaxRuleCountry(); $taxModel->setTaxRule($taxRule)->setCountryId($country)->setTaxId($tax)->setPosition($position); $taxModel->save(); } $position++; } } $event->setTaxRule($taxRule); } }
/** * @param TaxRuleEvent $event */ public function updateTaxes(TaxRuleEvent $event) { if (null !== ($taxRule = TaxRuleQuery::create()->findPk($event->getId()))) { $taxList = $this->getArrayFromJson($event->getTaxList()); $countryList = $this->getArrayFromJson22Compat($event->getCountryList()); $countryDeletedList = $this->getArrayFromJson22Compat($event->getCountryDeletedList()); /* clean the current tax rule for the countries/states */ $deletes = array_merge($countryList, $countryDeletedList); foreach ($deletes as $item) { TaxRuleCountryQuery::create()->filterByTaxRule($taxRule)->filterByCountryId(intval($item[0]), Criteria::EQUAL)->filterByStateId(intval($item[1]) !== 0 ? $item[1] : null, Criteria::EQUAL)->delete(); } /* for each country */ foreach ($countryList as $item) { $position = 1; $countryId = intval($item[0]); $stateId = intval($item[1]); /* on applique les nouvelles regles */ foreach ($taxList as $tax) { if (is_array($tax)) { foreach ($tax as $samePositionTax) { $taxModel = new TaxRuleCountry(); $taxModel->setTaxRule($taxRule)->setCountryId($countryId)->setStateId($stateId ?: null)->setTaxId($samePositionTax)->setPosition($position); $taxModel->save(); } } else { $taxModel = new TaxRuleCountry(); $taxModel->setTaxRule($taxRule)->setCountryId($countryId)->setStateId($stateId ?: null)->setTaxId($tax)->setPosition($position); $taxModel->save(); } $position++; } } $event->setTaxRule($taxRule); } }
protected function migrateTaxRules(MigrateCountryEvent $event) { $con = Propel::getWriteConnection(TaxRuleCountryTableMap::DATABASE_NAME); $con->beginTransaction(); try { $updatedRows = TaxRuleCountryQuery::create()->filterByCountryId($event->getCountry())->update(['CountryId' => $event->getNewCountry(), 'StateId' => $event->getNewState()]); $con->commit(); return $updatedRows; } catch (PropelException $e) { $con->rollback(); throw $e; } }
public function buildModelCriteria() { $search = TaxQuery::create(); /* manage translations */ $this->configureI18nProcessing($search, array('TITLE', 'DESCRIPTION')); $id = $this->getId(); if (null !== $id) { $search->filterById($id, Criteria::IN); } $exclude = $this->getExclude(); if (null !== $exclude) { $search->filterById($exclude, Criteria::NOT_IN); } $country = $this->getCountry(); $taxRule = $this->getTaxRule(); if (null !== $taxRule && null !== $country) { $search->filterByTaxRuleCountry(TaxRuleCountryQuery::create()->filterByCountryId($country, Criteria::EQUAL)->filterByTaxRuleId($taxRule, Criteria::IN)->find(), Criteria::IN); } $excludeTaxRule = $this->getExcludeTaxRule(); if (null !== $excludeTaxRule && null !== $country) { $excludedTaxes = TaxRuleCountryQuery::create()->filterByCountryId($country, Criteria::EQUAL)->filterByTaxRuleId($excludeTaxRule, Criteria::IN)->find(); /*DOES NOT WORK * $search->filterByTaxRuleCountry( $excludedTaxes, Criteria::NOT_IN );*/ foreach ($excludedTaxes as $excludedTax) { $search->filterByTaxRuleCountry($excludedTax, Criteria::NOT_EQUAL); } } $orders = $this->getOrder(); foreach ($orders as $order) { switch ($order) { case "id": $search->orderById(Criteria::ASC); break; case "id_reverse": $search->orderById(Criteria::DESC); break; case "alpha": $search->addAscendingOrderByColumn('i18n_TITLE'); break; case "alpha_reverse": $search->addDescendingOrderByColumn('i18n_TITLE'); break; } } return $search; }
/** * @inheritdoc */ public function buildModelCriteria() { $ask = $this->getAsk(); if ($ask === 'countries') { return null; } $country = $this->getCountry(); $state = $this->getState(); $taxRule = $this->getTaxRule(); $search = TaxRuleCountryQuery::create(); $search->filterByCountryId($country); $search->filterByStateId($state); $search->filterByTaxRuleId($taxRule); /* manage tax translation */ $this->configureI18nProcessing($search, array('TITLE', 'DESCRIPTION'), TaxTableMap::TABLE_NAME, 'TAX_ID'); $search->orderByPosition(Criteria::ASC); return $search; }
public function buildModelCriteria() { $search = TaxRuleCountryQuery::create(); $ask = $this->getAsk(); $country = $this->getCountry(); $taxRule = $this->getTaxRule(); if ($ask === 'countries') { $this->taxCountForOriginCountry = TaxRuleCountryQuery::create()->filterByCountryId($country)->count(); if ($this->taxCountForOriginCountry > 0) { $search->groupByCountryId(); $originalCountryJoin = new Join(); $originalCountryJoin->addExplicitCondition(TaxRuleCountryTableMap::TABLE_NAME, 'TAX_RULE_ID', null, TaxRuleCountryTableMap::TABLE_NAME, 'TAX_RULE_ID', 'origin'); $originalCountryJoin->addExplicitCondition(TaxRuleCountryTableMap::TABLE_NAME, 'TAX_ID', null, TaxRuleCountryTableMap::TABLE_NAME, 'TAX_ID', 'origin'); $originalCountryJoin->addExplicitCondition(TaxRuleCountryTableMap::TABLE_NAME, 'POSITION', null, TaxRuleCountryTableMap::TABLE_NAME, 'POSITION', 'origin'); $originalCountryJoin->addExplicitCondition(TaxRuleCountryTableMap::TABLE_NAME, 'COUNTRY_ID', null, TaxRuleCountryTableMap::TABLE_NAME, 'COUNTRY_ID', 'origin', Criteria::NOT_EQUAL); $originalCountryJoin->setJoinType(Criteria::LEFT_JOIN); $search->addJoinObject($originalCountryJoin, 's_to_o'); $search->where('`origin`.`COUNTRY_ID`' . Criteria::EQUAL . '?', $country, \PDO::PARAM_INT); //$search->having('COUNT(*)=?', $this->taxCountForOriginCountry, \PDO::PARAM_INT); $search->filterByTaxRuleId($taxRule); /* manage tax translation */ $this->configureI18nProcessing($search, array('TITLE', 'CHAPO', 'DESCRIPTION', 'POSTSCRIPTUM'), CountryTableMap::TABLE_NAME, 'COUNTRY_ID'); $search->addAscendingOrderByColumn('`' . CountryTableMap::TABLE_NAME . '_i18n_TITLE`'); } else { $search = CountryQuery::create()->joinTaxRuleCountry('trc', Criteria::LEFT_JOIN); /* manage tax translation */ $this->configureI18nProcessing($search); $search->where('ISNULL(`trc`.`COUNTRY_ID`)'); $search->addAscendingOrderByColumn('i18n_TITLE'); } } elseif ($ask === 'taxes') { $search->filterByCountryId($country); /* manage tax translation */ $this->configureI18nProcessing($search, array('TITLE', 'DESCRIPTION'), TaxTableMap::TABLE_NAME, 'TAX_ID'); $search->filterByTaxRuleId($taxRule); $search->orderByPosition(Criteria::ASC); } return $search; }
/** * If this collection has already been initialized with * an identical criteria, it returns the collection. * Otherwise if this Tax is new, it will return * an empty collection; or if this Tax has previously * been saved, it will retrieve related TaxRuleCountries from storage. * * This method is protected by default in order to keep the public * api reasonable. You can provide public methods for those you * actually need in Tax. * * @param Criteria $criteria optional Criteria object to narrow the query * @param ConnectionInterface $con optional connection object * @param string $joinBehavior optional join type to use (defaults to Criteria::LEFT_JOIN) * @return Collection|ChildTaxRuleCountry[] List of ChildTaxRuleCountry objects */ public function getTaxRuleCountriesJoinState($criteria = null, $con = null, $joinBehavior = Criteria::LEFT_JOIN) { $query = ChildTaxRuleCountryQuery::create(null, $criteria); $query->joinWith('State', $joinBehavior); return $this->getTaxRuleCountries($query, $con); }
protected function getPersistentData($taxRuleId) { $taxRule = TaxRuleQuery::create()->findPk($taxRuleId); if (null === $taxRule) { throw new HttpException(404, json_encode(["error" => sprintf("The tax rule %d doesn't exist", $taxRuleId)])); } $countries = TaxRuleCountryQuery::create()->filterByTaxRuleId($taxRuleId)->distinct()->select(TaxRuleCountryTableMap::COUNTRY_ID)->find()->toArray(); $taxes = TaxRuleCountryQuery::create()->filterByTaxRuleId($taxRuleId)->distinct()->select(TaxRuleCountryTableMap::TAX_ID)->find()->toArray(); $data = ["default" => (bool) $taxRule->getIsDefault(), "tax" => $taxes, "country" => $countries]; return $data; }
protected function getSpecification($taxRuleId) { $taxRuleCountries = TaxRuleCountryQuery::create()->filterByTaxRuleId($taxRuleId)->orderByCountryId()->orderByStateId()->orderByPosition()->orderByTaxId()->find(); $specKey = []; $specifications = []; $taxRules = []; if (!$taxRuleCountries->isEmpty()) { $taxRuleCountry = $taxRuleCountries->getFirst(); $currentCountryId = $taxRuleCountry->getCountryId(); $currentStateId = intval($taxRuleCountry->getStateId()); while (true) { $hasChanged = $taxRuleCountry === null || $taxRuleCountry->getCountryId() != $currentCountryId || intval($taxRuleCountry->getStateId()) != $currentStateId; if ($hasChanged) { $specification = implode(',', $specKey); $specifications[] = ['country' => $currentCountryId, 'state' => $currentStateId, 'specification' => $specification]; if (!in_array($specification, $taxRules)) { $taxRules[] = $specification; } if (null === $taxRuleCountry) { break; } $currentCountryId = $taxRuleCountry->getCountryId(); $currentStateId = intval($taxRuleCountry->getStateId()); $specKey = []; } $specKey[] = $taxRuleCountry->getTaxId() . '-' . $taxRuleCountry->getPosition(); $taxRuleCountry = $taxRuleCountries->getNext(); } } $data = ['taxRules' => $taxRules, 'specifications' => $specifications]; return $data; }
/** * Performs an INSERT on the database, given a TaxRuleCountry or Criteria object. * * @param mixed $criteria Criteria or TaxRuleCountry object containing data that is used to create the INSERT statement. * @param ConnectionInterface $con the ConnectionInterface connection to use * @return mixed The new primary key. * @throws PropelException Any exceptions caught during processing will be * rethrown wrapped into a PropelException. */ public static function doInsert($criteria, ConnectionInterface $con = null) { if (null === $con) { $con = Propel::getServiceContainer()->getWriteConnection(TaxRuleCountryTableMap::DATABASE_NAME); } if ($criteria instanceof Criteria) { $criteria = clone $criteria; // rename for clarity } else { $criteria = $criteria->buildCriteria(); // build Criteria from TaxRuleCountry object } // Set the correct dbName $query = TaxRuleCountryQuery::create()->mergeWith($criteria); try { // use transaction because $criteria could contain info // for more than one table (I guess, conceivably) $con->beginTransaction(); $pk = $query->doInsert($con); $con->commit(); } catch (PropelException $e) { $con->rollBack(); throw $e; } return $pk; }
/** * Removes this object from datastore and sets delete attribute. * * @param ConnectionInterface $con * @return void * @throws PropelException * @see TaxRuleCountry::setDeleted() * @see TaxRuleCountry::isDeleted() */ public function delete(ConnectionInterface $con = null) { if ($this->isDeleted()) { throw new PropelException("This object has already been deleted."); } if ($con === null) { $con = Propel::getServiceContainer()->getWriteConnection(TaxRuleCountryTableMap::DATABASE_NAME); } $con->beginTransaction(); try { $deleteQuery = ChildTaxRuleCountryQuery::create()->filterByPrimaryKey($this->getPrimaryKey()); $ret = $this->preDelete($con); if ($ret) { $deleteQuery->delete($con); $this->postDelete($con); $con->commit(); $this->setDeleted(true); } else { $con->commit(); } } catch (Exception $e) { $con->rollBack(); throw $e; } }
/** * @return string */ public function doExport() { /** * Define the cache */ $cache = []; $cache["brand"] = []; $cache["category"] = []; $cache["breadcrumb"] = []; $cache["feature"]["title"] = []; $cache["feature"]["value"] = []; $cache["attribute"]["title"] = []; $cache["attribute"]["value"] = []; $fakeCartItem = new CartItem(); $fakeCart = new Cart(); $fakeCart->addCartItem($fakeCartItem); /** @var \Thelia\Model\Country $country */ $country = CountryQuery::create()->findOneById(ConfigQuery::create()->read('store_country', null)); $deliveryModuleModelId = ShoppingFluxConfigQuery::getDeliveryModuleId(); $deliveryModuleModel = ModuleQuery::create()->findPk($deliveryModuleModelId); /** @var \Thelia\Module\AbstractDeliveryModule $deliveryModule */ $deliveryModule = $deliveryModuleModel->getModuleInstance($this->container); /** * Build fake Request to inject in the module */ $fakeRequest = new Request(); $fakeRequest->setSession((new FakeSession())->setCart($fakeCart)); $deliveryModule->setRequest($fakeRequest); /** * Currency */ $currency = Currency::getDefaultCurrency(); /** * Load ecotax */ $ecotax = TaxQuery::create()->findPk(ShoppingFluxConfigQuery::getEcotaxRuleId()); /** * If there's a problem in the configuration, load a fake tax */ if ($ecotax === null) { $ecotax = new Tax(); $ecotax->setType('\\Thelia\\TaxEngine\\TaxType\\FixAmountTaxType')->setRequirements(array('amount' => 0)); } /** * Load the tax instance */ $ecotaxInstance = $ecotax->getTypeInstance(); // Compatibility with Thelia <= 2.0.2 $ecotaxInstance->loadRequirements($ecotax->getRequirements()); // We can pass any product as Argument, it is not used $ecotax = $ecotaxInstance->fixAmountRetriever(new Product()); /** @var \Thelia\Model\Product $product */ foreach ($this->getData() as $product) { $product->setLocale($this->locale); $node = $this->xml->addChild("produit"); /** * Parent id */ $node->addChild("id", $product->getId()); $node->addChild("nom", $product->getTitle()); $node->addChild("url", URL::getInstance()->absoluteUrl("/", ["view" => "product", "product_id" => $product->getId()])); $node->addChild("description-courte", $product->getChapo()); $node->addChild("description", $product->getDescription()); /** * Images URL */ $imagesNode = $node->addChild("images"); /** @var \Thelia\Model\ProductImage $productImage */ foreach ($product->getProductImages() as $productImage) { $imagesNode->addChild("image", URL::getInstance()->absoluteUrl("/shoppingflux/image/" . $productImage->getId())); } /** * Product Brand */ if ($product->getBrand()) { $brand = $product->getBrand(); $brand->setLocale($this->locale); if (!array_key_exists($brandId = $brand->getId(), $cache["brand"])) { $cache["brand"][$brandId] = $brand->getTitle(); } $node->addChild("marque", $cache["brand"][$brandId]); } else { $node->addChild("marque", null); } $node->addChild("url-marque"); /** * Compute breadcrumb */ $category = $product->getCategories()[0]; $category->setLocale($this->locale); if (!array_key_exists($categoryId = $category->getId(), $cache["category"])) { $cache["category"][$categoryId] = $category->getTitle(); $breadcrumb = []; do { $category->setLocale($this->locale); $breadcrumb[] = $category->getTitle(); } while (null !== ($category = CategoryQuery::create()->findPk($category->getParent()))); $reversedBreadcrumb = array_reverse($breadcrumb); $reversedBreadcrumb[] = $product->getTitle(); $cache["breadcrumb"][$categoryId] = implode(" > ", $reversedBreadcrumb); } $node->addChild("rayon", $cache["category"][$categoryId]); $node->addChild("fil-ariane", $cache["breadcrumb"][$categoryId]); /** * Features */ $featuresNode = $node->addChild("caracteristiques"); foreach ($product->getFeatureProducts() as $featureProduct) { if ($featureProduct->getFeatureAv() !== null && $featureProduct->getFeature() !== null) { if (!array_key_exists($featureId = $featureProduct->getFeature()->getId(), $cache["feature"]["title"])) { $featureProduct->getFeatureAv()->setLocale($this->locale); $featureProduct->getFeature()->setLocale($this->locale); $cache["feature"]["title"][$featureId] = trim(preg_replace("#[^a-z0-9_\\-]#i", "_", $featureProduct->getFeature()->getTitle()), "_"); $cache["feature"]["value"][$featureId] = $featureProduct->getFeatureAv()->getTitle(); } $featuresNode->addChild($cache["feature"]["title"][$featureId], $cache["feature"]["value"][$featureId]); } } /** * Compute VAT */ $taxRuleCountry = TaxRuleCountryQuery::create()->filterByTaxRule($product->getTaxRule())->findOne(); $tax = $taxRuleCountry->getTax(); /** @var \Thelia\TaxEngine\TaxType\PricePercentTaxType $taxType*/ $taxType = $tax->getTypeInstance(); if (array_key_exists("percent", $taxRequirements = $taxType->getRequirements())) { $node->addChild("tva", $taxRequirements["percent"]); } /** * Compute product sale elements */ $productSaleElements = $product->getProductSaleElementss(); $psesNode = $node->addChild("declinaisons"); /** @var \Thelia\Model\ProductSaleElements $pse */ foreach ($productSaleElements as $pse) { /** * Fake the cart so that module::getPostage() returns the price * for only one object */ $fakeCartItem->setProductSaleElements($pse); /** * If the object is too heavy, don't export it */ try { $shipping_price = $deliveryModule->getPostage($country); } catch (DeliveryException $e) { continue; } $productPrice = $pse->getPricesByCurrency($currency); $pse->setVirtualColumn("price_PRICE", $productPrice->getPrice()); $pse->setVirtualColumn("price_PROMO_PRICE", $productPrice->getPromoPrice()); $deliveryTimeMin = null; $deliveryTimeMax = null; $pseNode = $psesNode->addChild("declinaison"); /** * Child id */ $pseNode->addChild("id", $product->getId() . "_" . $pse->getId()); $pseNode->addChild("prix-ttc", $pse->getPromo() ? $pse->getPromoPrice() : $pse->getPrice()); $pseNode->addChild("prix-ttc-barre", $pse->getPromo() ? $pse->getPrice() : null); $pseNode->addChild("quantite", $pse->getQuantity()); $pseNode->addChild("ean", $pse->getEanCode()); $pseNode->addChild("poids", $pse->getWeight()); $pseNode->addChild("ecotaxe", $ecotax); $pseNode->addChild("frais-de-port", $shipping_price); $pseNode->addChild("delai-livraison-mini", $deliveryTimeMin); $pseNode->addChild("delai-livraison-maxi", $deliveryTimeMax); $pseAttrNode = $pseNode->addChild("attributs"); /** @var \Thelia\Model\AttributeCombination $attr */ foreach ($pse->getAttributeCombinations() as $attr) { if ($attr->getAttribute() !== null && $attr->getAttributeAv() !== null) { if (!array_key_exists($attributeId = $attr->getAttribute()->getId(), $cache["attribute"]["title"])) { $attr->getAttribute()->setLocale($this->locale); $attr->getAttributeAv()->setLocale($this->locale); $cache["attribute"]["title"][$attributeId] = trim(preg_replace("#[^a-z0-9_\\-]#i", "_", $attr->getAttribute()->getTitle()), "_"); $cache["attribute"]["value"][$attributeId] = $attr->getAttributeAv()->getTitle(); } $pseAttrNode->addChild($cache["attribute"]["title"][$attributeId], $cache["attribute"]["value"][$attributeId]); } } $pseNode->addChild("promo-de"); $pseNode->addChild("promo-a"); } } /** * Then return a well formed string */ $dom = new \DOMDocument("1.0"); $dom->preserveWhiteSpace = false; $dom->formatOutput = true; $dom->loadXML($this->xml->asXML()); return $dom->saveXML(); }