/** * Retrieve TaxRule Model from registry given an id * * @param int $taxRuleId * @return TaxRuleModel * @throws NoSuchEntityException */ public function retrieveTaxRule($taxRuleId) { if (isset($this->registry[$taxRuleId])) { return $this->registry[$taxRuleId]; } $taxRuleModel = $this->taxRuleModelFactory->create()->load($taxRuleId); if (!$taxRuleModel->getId()) { // tax rule does not exist throw NoSuchEntityException::singleField('taxRuleId', $taxRuleId); } $this->registry[$taxRuleModel->getId()] = $taxRuleModel; return $taxRuleModel; }
/** * {@inheritdoc} */ public function searchTaxRules(SearchCriteria $searchCriteria) { $this->taxRuleSearchResultsBuilder->setSearchCriteria($searchCriteria); $collection = $this->taxRuleModelFactory->create()->getCollection(); //Add filters from root filter group to the collection foreach ($searchCriteria->getFilterGroups() as $group) { $this->addFilterGroupToCollection($group, $collection); } $this->taxRuleSearchResultsBuilder->setTotalCount($collection->getSize()); $sortOrders = $searchCriteria->getSortOrders(); if ($sortOrders) { foreach ($sortOrders as $field => $direction) { $field = $this->translateField($field); $collection->addOrder($field, $direction == SearchCriteria::SORT_ASC ? 'ASC' : 'DESC'); } } $collection->setCurPage($searchCriteria->getCurrentPage()); $collection->setPageSize($searchCriteria->getPageSize()); $taxRules = []; /** @var TaxRuleModel $taxRuleModel */ foreach ($collection as $taxRuleModel) { $taxRule = $this->converter->createTaxRuleDataObjectFromModel($taxRuleModel); $taxRules[] = $taxRule; } $this->taxRuleSearchResultsBuilder->setItems($taxRules); return $this->taxRuleSearchResultsBuilder->create(); }
/** * Convert a tax rule data object to tax rule model * * @param TaxRuleDataObject $taxRule * @return TaxRuleModel */ public function createTaxRuleModel(TaxRuleDataObject $taxRuleDataObject) { $taxRuleModel = $this->taxRuleModelFactory->create(); $ruleId = $taxRuleDataObject->getId(); if ($ruleId) { $taxRuleModel->setId($ruleId); } $taxRuleModel->setTaxCustomerClass($taxRuleDataObject->getCustomerTaxClassIds()); $taxRuleModel->setTaxProductClass($taxRuleDataObject->getProductTaxClassIds()); $taxRuleModel->setTaxRate($taxRuleDataObject->getTaxRateIds()); $taxRuleModel->setCode($taxRuleDataObject->getCode()); $taxRuleModel->setPriority($taxRuleDataObject->getPriority()); $taxRuleModel->setPosition($taxRuleDataObject->getSortOrder()); $taxRuleModel->setCalculateSubtotal($taxRuleDataObject->getCalculateSubtotal()); return $taxRuleModel; }
/** * {@inheritdoc} */ public function searchTaxRules(SearchCriteria $searchCriteria) { $this->taxRuleSearchResultsBuilder->setSearchCriteria($searchCriteria); $collection = $this->taxRuleModelFactory->create()->getCollection(); $fields = []; //Add filters from root filter group to the collection foreach ($searchCriteria->getFilterGroups() as $group) { $this->addFilterGroupToCollection($group, $collection); foreach ($group->getFilters() as $filter) { $fields[] = $this->translateField($filter->getField()); } } if ($fields) { if (in_array('cd.customer_tax_class_id', $fields) || in_array('cd.product_tax_class_id', $fields)) { $collection->joinCalculationData('cd'); } } $this->taxRuleSearchResultsBuilder->setTotalCount($collection->getSize()); $sortOrders = $searchCriteria->getSortOrders(); /** @var SortOrder $sortOrder */ if ($sortOrders) { foreach ($sortOrders as $sortOrder) { $collection->addOrder($this->translateField($sortOrder->getField()), $sortOrder->getDirection() == SearchCriteria::SORT_ASC ? 'ASC' : 'DESC'); } } $collection->setCurPage($searchCriteria->getCurrentPage()); $collection->setPageSize($searchCriteria->getPageSize()); $taxRules = []; /** @var TaxRuleModel $taxRuleModel */ foreach ($collection as $taxRuleModel) { $taxRule = $this->converter->createTaxRuleDataObjectFromModel($taxRuleModel); $taxRules[] = $taxRule; } $this->taxRuleSearchResultsBuilder->setItems($taxRules); return $this->taxRuleSearchResultsBuilder->create(); }