/** * @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); } }
/** * @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); } }