Пример #1
0
 /**
  * Retrieve TaxRate Model from registry given an id
  *
  * @param int $taxRateId
  * @return TaxRateModel
  * @throws NoSuchEntityException
  */
 public function retrieveTaxRate($taxRateId)
 {
     if (isset($this->taxRateRegistryById[$taxRateId])) {
         return $this->taxRateRegistryById[$taxRateId];
     }
     /** @var TaxRateModel $taxRateModel */
     $taxRateModel = $this->taxRateModelFactory->create()->load($taxRateId);
     if (!$taxRateModel->getId()) {
         // tax rate does not exist
         throw NoSuchEntityException::singleField('taxRateId', $taxRateId);
     }
     $this->taxRateRegistryById[$taxRateModel->getId()] = $taxRateModel;
     return $taxRateModel;
 }
Пример #2
0
 /**
  * {@inheritdoc}
  */
 public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
 {
     /** @var \Magento\Tax\Model\Resource\Calculation\Rate\Collection $collection */
     $collection = $this->rateFactory->create()->getCollection();
     $collection->joinRegionTable();
     //Add filters from root filter group to the collection
     foreach ($searchCriteria->getFilterGroups() as $group) {
         $this->addFilterGroupToCollection($group, $collection);
     }
     $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());
     $taxRate = [];
     /** @var \Magento\Tax\Model\Calculation\Rate $taxRateModel */
     foreach ($collection as $taxRateModel) {
         $taxRate[] = $taxRateModel;
     }
     return $this->taxRateSearchResultsFactory->create()->setItems($taxRate)->setTotalCount($collection->getSize())->setSearchCriteria($searchCriteria);
 }
Пример #3
0
 /**
  * Import single rate
  *
  * @param array $rateData
  * @param array $regionsCache cache of regions of already used countries (is used to optimize performance)
  * @param array $storesCache cache of stores related to tax rate titles
  * @return array regions cache populated with regions related to country of imported tax rate
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 protected function _importRate(array $rateData, array $regionsCache, array $storesCache)
 {
     // data with index 1 must represent country code
     $countryCode = $rateData[1];
     $country = $this->_countryFactory->create()->loadByCode($countryCode, 'iso2_code');
     if (!$country->getId()) {
         throw new \Magento\Framework\Exception\LocalizedException(__('One of the countries has invalid code.'));
     }
     $regionsCache = $this->_addCountryRegionsToCache($countryCode, $regionsCache);
     // data with index 2 must represent region code
     $regionCode = $rateData[2];
     if (!empty($regionsCache[$countryCode][$regionCode])) {
         $regionId = $regionsCache[$countryCode][$regionCode] == '*' ? 0 : $regionsCache[$countryCode][$regionCode];
         // data with index 3 must represent postcode
         $postCode = empty($rateData[3]) ? null : $rateData[3];
         $modelData = ['code' => $rateData[0], 'tax_country_id' => $rateData[1], 'tax_region_id' => $regionId, 'tax_postcode' => $postCode, 'rate' => $rateData[4], 'zip_is_range' => $rateData[5], 'zip_from' => $rateData[6], 'zip_to' => $rateData[7]];
         // try to load existing rate
         /** @var $rateModel \Magento\Tax\Model\Calculation\Rate */
         $rateModel = $this->_taxRateFactory->create()->loadByCode($modelData['code']);
         $rateModel->addData($modelData);
         // compose titles list
         $rateTitles = [];
         foreach ($storesCache as $fileFieldIndex => $storeId) {
             $rateTitles[$storeId] = $rateData[$fileFieldIndex];
         }
         $rateModel->setTitle($rateTitles);
         $rateModel->save();
     }
     return $regionsCache;
 }
Пример #4
0
 /**
  * {@inheritdoc}
  */
 public function run()
 {
     $this->logger->log('Installing taxes:');
     $fixtureFile = 'Tax/tax_rate.csv';
     $fixtureFilePath = $this->fixtureHelper->getPath($fixtureFile);
     /** @var \Magento\Tools\SampleData\Helper\Csv\Reader $csvReader */
     $csvReader = $this->csvReaderFactory->create(['fileName' => $fixtureFilePath, 'mode' => 'r']);
     foreach ($csvReader as $data) {
         $taxRate = $this->rateFactory->create();
         $taxRate->setCode($data['code'])->setTaxCountryId($data['tax_country_id'])->setTaxRegionId($data['tax_region_id'])->setTaxPostcode($data['tax_postcode'])->setRate($data['rate']);
         $this->taxRateRepository->save($taxRate);
         $this->logger->logInline('.');
     }
     $fixtureFile = 'Tax/tax_rule.csv';
     $fixtureFilePath = $this->fixtureHelper->getPath($fixtureFile);
     /** @var \Magento\Tools\SampleData\Helper\Csv\Reader $csvReader */
     $csvReader = $this->csvReaderFactory->create(['fileName' => $fixtureFilePath, 'mode' => 'r']);
     foreach ($csvReader as $data) {
         $taxRate = $this->taxRateFactory->create()->loadByCode($data['tax_rate']);
         $taxRule = $this->ruleFactory->create();
         $taxRule->setCode($data['code'])->setTaxRateIds([$taxRate->getId()])->setCustomerTaxClassIds([$data['tax_customer_class']])->setProductTaxClassIds([$data['tax_product_class']])->setPriority($data['priority'])->setCalculateSubtotal($data['calculate_subtotal'])->setPosition($data['position']);
         $this->taxRuleRepository->save($taxRule);
         $this->logger->logInline('.');
     }
 }
 /**
  * {@inheritdoc}
  */
 public function run()
 {
     $this->logger->log('Installing taxes:');
     $fixtureFile = 'Tax/tax_rate.csv';
     $fixtureFilePath = $this->fixtureHelper->getPath($fixtureFile);
     /** @var \Magento\SampleData\Helper\Csv\Reader $csvReader */
     $csvReader = $this->csvReaderFactory->create(['fileName' => $fixtureFilePath, 'mode' => 'r']);
     foreach ($csvReader as $data) {
         if ($this->rateFactory->create()->loadByCode($data['code'])->getId()) {
             continue;
         }
         $taxRate = $this->rateFactory->create();
         $taxRate->setCode($data['code'])->setTaxCountryId($data['tax_country_id'])->setTaxRegionId($data['tax_region_id'])->setTaxPostcode($data['tax_postcode'])->setRate($data['rate']);
         $this->taxRateRepository->save($taxRate);
         $this->logger->logInline('.');
     }
     $fixtureFile = 'Tax/tax_rule.csv';
     $fixtureFilePath = $this->fixtureHelper->getPath($fixtureFile);
     /** @var \Magento\SampleData\Helper\Csv\Reader $csvReader */
     $csvReader = $this->csvReaderFactory->create(['fileName' => $fixtureFilePath, 'mode' => 'r']);
     foreach ($csvReader as $data) {
         $filter = $this->filterBuilder->setField('code')->setConditionType('=')->setValue($data['code'])->create();
         $criteria = $this->criteriaBuilder->addFilters([$filter])->create();
         $existingRates = $this->taxRuleRepository->getList($criteria)->getItems();
         if (!empty($existingRates)) {
             continue;
         }
         $taxRate = $this->taxRateFactory->create()->loadByCode($data['tax_rate']);
         $taxRule = $this->ruleFactory->create();
         $taxRule->setCode($data['code'])->setTaxRateIds([$taxRate->getId()])->setCustomerTaxClassIds([$data['tax_customer_class']])->setProductTaxClassIds([$data['tax_product_class']])->setPriority($data['priority'])->setCalculateSubtotal($data['calculate_subtotal'])->setPosition($data['position']);
         $this->taxRuleRepository->save($taxRule);
         $this->logger->logInline('.');
     }
 }
Пример #6
0
 /**
  * Convert a TaxRate data object to rate model
  *
  * @param TaxRateDataObject $taxRate
  * @return TaxRateModel
  */
 public function createTaxRateModel(TaxRateDataObject $taxRate)
 {
     $rateModel = $this->taxRateModelFactory->create();
     $rateId = $taxRate->getId();
     if ($rateId) {
         $rateModel->setId($rateId);
     }
     $rateModel->setTaxCountryId($taxRate->getCountryId());
     $rateModel->setTaxRegionId($taxRate->getRegionId());
     $rateModel->setRate($taxRate->getPercentageRate());
     $rateModel->setCode($taxRate->getCode());
     $rateModel->setTaxPostcode($taxRate->getPostCode());
     $rateModel->setRegionName($taxRate->getRegionName());
     $zipRange = $taxRate->getZipRange();
     if ($zipRange) {
         $zipFrom = $zipRange->getFrom();
         $zipTo = $zipRange->getTo();
         if (!empty($zipFrom) || !empty($zipTo)) {
             $rateModel->setZipIsRange(1);
         }
         $rateModel->setZipFrom($zipFrom);
         $rateModel->setZipTo($zipTo);
     }
     return $rateModel;
 }
Пример #7
0
 /**
  * @param array $data
  * @dataProvider createTaxRateDataObjectFromModelDataProvider
  */
 public function testCreateTaxRateDataObjectFromModel($data)
 {
     $taxRateModel = $this->taxRateFactory->create(['data' => $data]);
     $taxRateDataOjectBuilder = $this->objectManager->create('Magento\\Tax\\Service\\V1\\Data\\TaxRateBuilder');
     $zipRangeDataObjectBuilder = $this->objectManager->create('Magento\\Tax\\Service\\V1\\Data\\ZipRangeBuilder');
     /** @var  $converter \Magento\Tax\Model\Calculation\Rate\Converter */
     $converter = $this->objectManager->create('Magento\\Tax\\Model\\Calculation\\Rate\\Converter', ['taxRateDataObjectBuilder' => $taxRateDataOjectBuilder, 'zipRangeDataObjectBuilder' => $zipRangeDataObjectBuilder]);
     $taxRateDataObject = $converter->createTaxRateDataObjectFromModel($taxRateModel);
     $this->assertEquals($taxRateModel->getId(), $taxRateDataObject->getId());
     $this->assertEquals($taxRateModel->getTaxCountryId(), $taxRateDataObject->getCountryId());
     $this->assertEquals($taxRateModel->getTaxRegionId(), $taxRateDataObject->getRegionId());
     $this->assertEquals($taxRateModel->getTaxPostcode(), $taxRateDataObject->getPostcode());
     $this->assertEquals($taxRateModel->getCode(), $taxRateDataObject->getcode());
     $this->assertEquals($taxRateModel->getRate(), $taxRateDataObject->getPercentageRate());
     $zipIsRange = $taxRateModel->getZipIsRange();
     if ($zipIsRange) {
         $this->assertEquals($taxRateModel->getZipFrom(), $taxRateDataObject->getZipRange()->getFrom());
         $this->assertEquals($taxRateModel->getZipTo(), $taxRateDataObject->getZipRange()->getTo());
     } else {
         $this->assertNull($taxRateDataObject->getZipRange());
     }
 }
Пример #8
0
 /**
  * {@inheritdoc}
  */
 public function install(array $fixtures)
 {
     foreach ($fixtures as $fileName) {
         $fileName = $this->fixtureManager->getFixture($fileName);
         if (!file_exists($fileName)) {
             continue;
         }
         $rows = $this->csvReader->getData($fileName);
         $header = array_shift($rows);
         foreach ($rows as $row) {
             $data = [];
             foreach ($row as $key => $value) {
                 $data[$header[$key]] = $value;
             }
             if ($this->rateFactory->create()->loadByCode($data['code'])->getId()) {
                 continue;
             }
             $taxRate = $this->rateFactory->create();
             $taxRate->setCode($data['code'])->setTaxCountryId($data['tax_country_id'])->setTaxRegionId($data['tax_region_id'])->setTaxPostcode($data['tax_postcode'])->setRate($data['rate']);
             $this->taxRateRepository->save($taxRate);
         }
         $fixtureFile = 'Magento_TaxSampleData::fixtures/tax_rule.csv';
         $fixtureFileName = $this->fixtureManager->getFixture($fixtureFile);
         if (!file_exists($fileName)) {
             continue;
         }
         $rows = $this->csvReader->getData($fixtureFileName);
         $header = array_shift($rows);
         foreach ($rows as $row) {
             $data = [];
             foreach ($row as $key => $value) {
                 $data[$header[$key]] = $value;
             }
             $filter = $this->filterBuilder->setField('code')->setConditionType('=')->setValue($data['code'])->create();
             $criteria = $this->criteriaBuilder->addFilters([$filter])->create();
             $existingRates = $this->taxRuleRepository->getList($criteria)->getItems();
             if (!empty($existingRates)) {
                 continue;
             }
             $taxRate = $this->taxRateFactory->create()->loadByCode($data['tax_rate']);
             $taxRule = $this->ruleFactory->create();
             $taxRule->setCode($data['code'])->setTaxRateIds([$taxRate->getId()])->setCustomerTaxClassIds([$data['tax_customer_class']])->setProductTaxClassIds([$data['tax_product_class']])->setPriority($data['priority'])->setCalculateSubtotal($data['calculate_subtotal'])->setPosition($data['position']);
             $this->taxRuleRepository->save($taxRule);
         }
     }
 }
Пример #9
0
 /**
  * {@inheritdoc}
  */
 public function searchTaxRates(SearchCriteria $searchCriteria)
 {
     /** @var \Magento\Tax\Model\Resource\Calculation\Rate\Collection $collection */
     $collection = $this->rateFactory->create()->getCollection();
     $collection->joinRegionTable();
     //Add filters from root filter group to the collection
     foreach ($searchCriteria->getFilterGroups() as $group) {
         $this->addFilterGroupToCollection($group, $collection);
     }
     $sortOrders = $searchCriteria->getSortOrders();
     if ($sortOrders) {
         foreach ($sortOrders as $field => $direction) {
             $collection->addOrder($field, $direction == SearchCriteria::SORT_ASC ? 'ASC' : 'DESC');
         }
     }
     $collection->setCurPage($searchCriteria->getCurrentPage());
     $collection->setPageSize($searchCriteria->getPageSize());
     $taxRate = [];
     /** @var \Magento\Tax\Model\Calculation\Rate $taxRateModel */
     foreach ($collection as $taxRateModel) {
         $taxRate[] = $this->converter->createTaxRateDataObjectFromModel($taxRateModel);
     }
     return $this->taxRateSearchResultsBuilder->setItems($taxRate)->setTotalCount($collection->getSize())->setSearchCriteria($searchCriteria)->create();
 }