Example #1
0
 /**
  * Retrieve all tax rates as an options array.
  *
  * @return array
  */
 public function toOptionArray()
 {
     if (!$this->options) {
         $searchCriteria = $this->searchCriteriaBuilder->create();
         $searchResults = $this->taxRateService->searchTaxRates($searchCriteria);
         $this->options = $this->converter->toOptionArray($searchResults->getItems(), TaxRate::KEY_ID, TaxRate::KEY_CODE);
     }
     return $this->options;
 }
 /**
  * {@inheritdoc}
  */
 public function loadData($printQuery = false, $logQuery = false)
 {
     if (!$this->isLoaded()) {
         $searchCriteria = $this->getSearchCriteria();
         $searchResults = $this->rateService->searchTaxRates($searchCriteria);
         $this->_totalRecords = $searchResults->getTotalCount();
         foreach ($searchResults->getItems() as $taxRate) {
             $this->_addItem($this->createTaxRateCollectionItem($taxRate));
         }
         $this->_setIsLoaded();
     }
     return $this;
 }
Example #3
0
 /**
  * Return the tax rate titles associated with a store view.
  *
  * @return array
  */
 public function getTitles()
 {
     if (is_null($this->_titles)) {
         $this->_titles = array();
         $taxRateId = $this->_coreRegistry->registry(RegistryConstants::CURRENT_TAX_RATE_ID);
         $titles = array();
         if ($taxRateId) {
             $rate = $this->_taxRateService->getTaxRate($taxRateId);
             $titles = $rate->getTitles();
         }
         foreach ($titles as $title) {
             $this->_titles[$title->getStoreId()] = $title->getValue();
         }
         foreach ($this->getStores() as $store) {
             if (!isset($this->_titles[$store->getId()])) {
                 $this->_titles[$store->getId()] = '';
             }
         }
     }
     return $this->_titles;
 }
Example #4
0
 /**
  * {@inheritdoc}
  */
 public function getRatesByCustomerAndProductTaxClassId($customerTaxClassId, $productTaxClassId)
 {
     $this->searchCriteriaBuilder->addFilter([$this->filterBuilder->setField(TaxRule::CUSTOMER_TAX_CLASS_IDS)->setValue([$customerTaxClassId])->create()]);
     $this->searchCriteriaBuilder->addFilter([$this->filterBuilder->setField(TaxRule::PRODUCT_TAX_CLASS_IDS)->setValue([$productTaxClassId])->create()]);
     $searchResults = $this->searchTaxRules($this->searchCriteriaBuilder->create());
     $taxRules = $searchResults->getItems();
     $rates = [];
     foreach ($taxRules as $taxRule) {
         $rateIds = $taxRule->getTaxRateIds();
         if (!empty($rateIds)) {
             foreach ($rateIds as $rateId) {
                 $rates[] = $this->taxRateService->getTaxRate($rateId);
             }
         }
     }
     return $rates;
 }
Example #5
0
 /**
  *
  * @param \Magento\Framework\Service\V1\Data\Filter[] $filters
  * @param \Magento\Framework\Service\V1\Data\Filter[] $filterGroup
  * @param $expectedRateCodes
  *
  * @magentoDbIsolation enabled
  * @dataProvider searchTaxRatesDataProvider
  */
 public function testSearchTaxRates($filters, $filterGroup, $expectedRateCodes)
 {
     $taxRates = $this->taxRateFixtureFactory->createTaxRates([['percentage' => 7.5, 'country' => 'US', 'region' => '42'], ['percentage' => 7.5, 'country' => 'US', 'region' => '12'], ['percentage' => 22.0, 'country' => 'US', 'region' => '42'], ['percentage' => 10.0, 'country' => 'US', 'region' => '12']]);
     /** @var \Magento\Framework\Service\V1\Data\SearchCriteriaBuilder $searchBuilder */
     $searchBuilder = Bootstrap::getObjectManager()->create('Magento\\Framework\\Service\\V1\\Data\\SearchCriteriaBuilder');
     foreach ($filters as $filter) {
         $searchBuilder->addFilter([$filter]);
     }
     if (!is_null($filterGroup)) {
         $searchBuilder->addFilter($filterGroup);
     }
     $searchCriteria = $searchBuilder->create();
     $searchResults = $this->taxRateService->searchTaxRates($searchCriteria);
     $items = [];
     foreach ($expectedRateCodes as $rateCode) {
         $rateId = $taxRates[$rateCode];
         $items[] = $this->taxRateService->getTaxRate($rateId);
     }
     $resultsBuilder = Bootstrap::getObjectManager()->create('Magento\\Tax\\Service\\V1\\Data\\TaxRateSearchResultsBuilder');
     $expectedResult = $resultsBuilder->setItems($items)->setTotalCount(count($items))->setSearchCriteria($searchCriteria)->create();
     $this->assertEquals($expectedResult, $searchResults);
 }
 public function testSearchTaxRate()
 {
     $collectionMock = $this->getMockBuilder('Magento\\Tax\\Model\\Resource\\Calculation\\Rate\\Collection')->disableOriginalConstructor()->setMethods(['addFieldToFilter', 'getSize', 'load', 'getIterator', 'joinRegionTable'])->getMock();
     $this->mockReturnValue($collectionMock, ['getSize' => 1, 'getIterator' => new \ArrayIterator([$this->rateModelMock])]);
     $this->rateFactoryMock->expects($this->atLeastOnce())->method('create')->will($this->returnValue($this->rateModelMock));
     $this->mockReturnValue($this->rateModelMock, ['load' => $this->returnSelf(), 'getCollection' => $collectionMock]);
     $taxRate = $this->taxRateBuilder->setId(2)->setCode('Rate-Code')->setCountryId('US')->setPercentageRate(0.1)->setPostcode('55555')->setRegionId(self::REGION_ID)->create();
     $this->converterMock->expects($this->once())->method('createTaxRateDataObjectFromModel')->with($this->rateModelMock)->will($this->returnValue($taxRate));
     $filterBuilder = $this->objectManager->getObject('\\Magento\\Framework\\Service\\V1\\Data\\FilterBuilder');
     $filter = $filterBuilder->setField(TaxRate::KEY_REGION_ID)->setValue(self::REGION_ID)->create();
     $this->searchCriteriaBuilder->addFilter([$filter])->addSortOrder('id', \Magento\Framework\Service\V1\Data\SearchCriteria::SORT_ASC);
     $this->createService();
     $searchCriteria = $this->searchCriteriaBuilder->create();
     $searchResults = $this->taxRateService->searchTaxRates($searchCriteria);
     $items = $searchResults->getItems();
     $this->assertNotNull($searchResults);
     $this->assertSame($searchCriteria, $searchResults->getSearchCriteria());
     $this->assertEquals(1, $searchResults->getTotalCount());
     $this->assertNotNull($items);
     $this->assertFalse(empty($items));
     $this->assertEquals(self::REGION_ID, $items[0]->getRegionId());
 }
Example #7
0
 /**
  *
  * @magentoDbIsolation enabled
  */
 public function testGetRatesByCustomerAndProductTaxClassId()
 {
     $this->setUpDefaultRules();
     $taxRateIds = $this->taxRuleService->getTaxRule(current($this->taxRules))->getTaxRateIds();
     $expectedRates = [];
     foreach ($taxRateIds as $rateId) {
         $expectedRates[] = $this->taxRateService->getTaxRate($rateId);
     }
     $rates = $this->taxRuleService->getRatesByCustomerAndProductTaxClassId($this->taxClasses['DefaultCustomerClass'], $this->taxClasses['DefaultProductClass']);
     $this->assertCount(2, $rates);
     $this->assertEquals($expectedRates, $rates);
 }
Example #8
0
 /**
  * @return $this
  */
 protected function _prepareForm()
 {
     $taxRateId = $this->_coreRegistry->registry(RegistryConstants::CURRENT_TAX_RATE_ID);
     try {
         if ($taxRateId) {
             $taxRateDataObject = $this->_taxRateService->getTaxRate($taxRateId);
         }
     } catch (NoSuchEntityException $e) {
         /* tax rate not found */
     }
     $sessionFormValues = (array) $this->_coreRegistry->registry(RegistryConstants::CURRENT_TAX_RATE_FORM_DATA);
     $formData = isset($taxRateDataObject) ? $this->extractTaxRateData($taxRateDataObject) : [];
     $formData = array_merge($formData, $sessionFormValues);
     if (isset($formData['zip_is_range']) && $formData['zip_is_range'] && !isset($formData['tax_postcode'])) {
         $formData['tax_postcode'] = $formData['zip_from'] . '-' . $formData['zip_to'];
     }
     /** @var \Magento\Framework\Data\Form $form */
     $form = $this->_formFactory->create();
     $countries = $this->_country->toOptionArray(false, 'US');
     unset($countries[0]);
     if (!isset($formData['tax_country_id'])) {
         $formData['tax_country_id'] = $this->_scopeConfig->getValue(\Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_COUNTRY, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
     }
     if (!isset($formData['tax_region_id'])) {
         $formData['tax_region_id'] = $this->_scopeConfig->getValue(\Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_REGION, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
     }
     $regionCollection = $this->_regionFactory->create()->getCollection()->addCountryFilter($formData['tax_country_id']);
     $regions = $regionCollection->toOptionArray();
     if ($regions) {
         $regions[0]['label'] = '*';
     } else {
         $regions = array(array('value' => '', 'label' => '*'));
     }
     $legend = $this->getShowLegend() ? __('Tax Rate Information') : '';
     $fieldset = $form->addFieldset('base_fieldset', array('legend' => $legend));
     if (isset($formData['tax_calculation_rate_id']) && $formData['tax_calculation_rate_id'] > 0) {
         $fieldset->addField('tax_calculation_rate_id', 'hidden', array('name' => 'tax_calculation_rate_id', 'value' => $formData['tax_calculation_rate_id']));
     }
     $fieldset->addField('code', 'text', array('name' => 'code', 'label' => __('Tax Identifier'), 'title' => __('Tax Identifier'), 'class' => 'required-entry', 'required' => true));
     $fieldset->addField('zip_is_range', 'checkbox', array('name' => 'zip_is_range', 'label' => __('Zip/Post is Range'), 'value' => '1'));
     if (!isset($formData['tax_postcode'])) {
         $formData['tax_postcode'] = $this->_scopeConfig->getValue(\Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_POSTCODE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
     }
     $fieldset->addField('tax_postcode', 'text', array('name' => 'tax_postcode', 'label' => __('Zip/Post Code'), 'note' => __("'*' - matches any; 'xyz*' - matches any that begins on 'xyz' and are not longer than %1.", $this->_taxData->getPostCodeSubStringLength())));
     $fieldset->addField('zip_from', 'text', array('name' => 'zip_from', 'label' => __('Range From'), 'required' => true, 'maxlength' => 9, 'class' => 'validate-digits', 'css_class' => 'hidden'));
     $fieldset->addField('zip_to', 'text', array('name' => 'zip_to', 'label' => __('Range To'), 'required' => true, 'maxlength' => 9, 'class' => 'validate-digits', 'css_class' => 'hidden'));
     $fieldset->addField('tax_region_id', 'select', array('name' => 'tax_region_id', 'label' => __('State'), 'values' => $regions));
     $fieldset->addField('tax_country_id', 'select', array('name' => 'tax_country_id', 'label' => __('Country'), 'required' => true, 'values' => $countries));
     $fieldset->addField('rate', 'text', array('name' => 'rate', 'label' => __('Rate Percent'), 'title' => __('Rate Percent'), 'required' => true, 'class' => 'validate-not-negative-number'));
     $form->setAction($this->getUrl('tax/rate/save'));
     $form->setUseContainer(true);
     $form->setId(self::FORM_ELEMENT_ID);
     $form->setMethod('post');
     if (!$this->_storeManager->hasSingleStore()) {
         $form->addElement($this->_fieldsetFactory->create()->setLegend(__('Tax Titles')));
     }
     if (isset($formData['zip_is_range']) && $formData['zip_is_range']) {
         list($formData['zip_from'], $formData['zip_to']) = explode('-', $formData['tax_postcode']);
     }
     $form->setValues($formData);
     $this->setForm($form);
     $this->setChild('form_after', $this->getLayout()->createBlock('Magento\\Framework\\View\\Element\\Template')->setTemplate('Magento_Tax::rate/js.phtml'));
     return parent::_prepareForm();
 }