/** * {@inheritdoc} */ public function loadData($printQuery = false, $logQuery = false) { if (!$this->isLoaded()) { $searchCriteria = $this->getSearchCriteria(); $searchResults = $this->ruleService->searchTaxRules($searchCriteria); $this->_totalRecords = $searchResults->getTotalCount(); foreach ($searchResults->getItems() as $taxRule) { $this->_addItem($this->createTaxRuleCollectionItem($taxRule)); } $this->_setIsLoaded(); } return $this; }
/** * @return $this */ protected function _prepareForm() { $taxRuleId = $this->_coreRegistry->registry('tax_rule_id'); try { $taxRule = $this->ruleService->getTaxRule($taxRuleId); } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { /** Tax rule not found */ } /** @var \Magento\Framework\Data\Form $form */ $form = $this->_formFactory->create(array('data' => array('id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post'))); $sessionFormValues = (array) $this->_coreRegistry->registry('tax_rule_form_data'); $taxRuleData = isset($taxRule) ? $this->extractTaxRuleData($taxRule) : []; $formValues = array_merge($taxRuleData, $sessionFormValues); $fieldset = $form->addFieldset('base_fieldset', array('legend' => __('Tax Rule Information'))); $fieldset->addField('code', 'text', array('name' => 'code', 'value' => isset($formValues['code']) ? $formValues['code'] : '', 'label' => __('Name'), 'class' => 'required-entry', 'required' => true)); // Editable multiselect for customer tax class $selectConfig = $this->getTaxClassSelectConfig(TaxClassServiceInterface::TYPE_CUSTOMER); $selectedCustomerTax = isset($formValues['tax_customer_class']) ? $formValues['tax_customer_class'] : $this->getDefaultCustomerTaxClass(); $fieldset->addField('tax_customer_class', 'editablemultiselect', array('name' => 'tax_customer_class', 'label' => __('Customer Tax Class'), 'class' => 'required-entry', 'values' => $this->customerTaxClassSource->getAllOptions(false), 'value' => $selectedCustomerTax, 'required' => true, 'select_config' => $selectConfig), false, true); // Editable multiselect for product tax class $selectConfig = $this->getTaxClassSelectConfig(TaxClassServiceInterface::TYPE_PRODUCT); $selectedProductTax = isset($formValues['tax_product_class']) ? $formValues['tax_product_class'] : $this->getDefaultProductTaxClass(); $fieldset->addField('tax_product_class', 'editablemultiselect', array('name' => 'tax_product_class', 'label' => __('Product Tax Class'), 'class' => 'required-entry', 'values' => $this->productTaxClassSource->getAllOptions(false), 'value' => $selectedProductTax, 'required' => true, 'select_config' => $selectConfig), false, true); $fieldset->addField('tax_rate', 'editablemultiselect', array('name' => 'tax_rate', 'label' => __('Tax Rate'), 'class' => 'required-entry', 'values' => $this->rateSource->toOptionArray(), 'value' => isset($formValues['tax_rate']) ? $formValues['tax_rate'] : [], 'required' => true, 'element_js_class' => 'TaxRateEditableMultiselect', 'select_config' => array('is_entity_editable' => true))); $fieldset->addField('priority', 'text', array('name' => 'priority', 'label' => __('Priority'), 'class' => 'validate-not-negative-number', 'value' => isset($formValues['priority']) ? $formValues['priority'] : 0, 'required' => true, 'note' => __('Tax rates at the same priority are added, others are compounded.')), false, true); $fieldset->addField('calculate_subtotal', 'checkbox', array('name' => 'calculate_subtotal', 'label' => __('Calculate Off Subtotal Only'), 'onclick' => 'this.value = this.checked ? 1 : 0;', 'checked' => isset($formValues['calculate_subtotal']) ? $formValues['calculate_subtotal'] : 0), false, true); $fieldset->addField('position', 'text', array('name' => 'position', 'label' => __('Sort Order'), 'class' => 'validate-not-negative-number', 'value' => isset($formValues['position']) ? $formValues['position'] : 0, 'required' => true), false, true); if (isset($taxRule)) { $fieldset->addField('tax_calculation_rule_id', 'hidden', array('name' => 'tax_calculation_rule_id', 'value' => $taxRule->getId(), 'no_span' => true)); } $form->setAction($this->getUrl('tax/rule/save')); $form->setUseContainer($this->getUseContainer()); $this->setForm($form); return parent::_prepareForm(); }
public function testSearchTaxRulesSingleResult() { /** @var \PHPUnit_Framework_MockObject_MockObject | * \Magento\Tax\Model\Resource\Calculation\Rule\Collection $mockCollection */ $mockCollection = $this->getMockBuilder('\\Magento\\Tax\\Model\\Resource\\Calculation\\Rule\\Collection')->disableOriginalConstructor()->setMethods(['__wakeup', 'getItems', 'getSize', 'addFieldToFilter', '_beforeLoad', 'getIterator'])->getMock(); /** @var \Magento\Tax\Service\V1\Data\TaxRuleBuilder $taxRuleBuilder */ $taxRuleBuilder = $this->objectManager->getObject('Magento\\Tax\\Service\\V1\\Data\\TaxRuleBuilder'); /** @var \Magento\Tax\Service\V1\Data\TaxRule $taxRule */ $taxRule = $taxRuleBuilder->create(); $taxRuleModel = $this->getMockBuilder('Magento\\Tax\\Model\\Calculation\\Rule')->disableOriginalConstructor()->getMock(); $mockCollection->expects($this->once())->method('getIterator')->will($this->returnValue(new \ArrayIterator([$taxRuleModel]))); $mockCollection->expects($this->once())->method('getSize')->will($this->returnValue(1)); $filterBuilder = $this->objectManager->getObject('\\Magento\\Framework\\Service\\V1\\Data\\FilterBuilder'); $filter = $filterBuilder->setField('code')->setValue('code')->setConditionType('eq')->create(); $filterGroupBuilder = $this->objectManager->getObject('Magento\\Framework\\Service\\V1\\Data\\Search\\FilterGroupBuilder'); /** @var \Magento\Framework\Service\V1\Data\SearchCriteriaBuilder $searchCriteriaBuilder */ $searchCriteriaBuilder = $this->objectManager->getObject('Magento\\Framework\\Service\\V1\\Data\\SearchCriteriaBuilder', ['filterGroupBuilder' => $filterGroupBuilder]); $searchCriteria = $searchCriteriaBuilder->addFilter([$filter])->addSortOrder('id', \Magento\Framework\Service\V1\Data\SearchCriteria::SORT_ASC)->create(); /** @var \Magento\Tax\Service\V1\Data\TaxRuleSearchResultsBuilder $searchResultsBuilder */ $searchResultsBuilder = $this->objectManager->getObject('\\Magento\\Tax\\Service\\V1\\Data\\TaxRuleSearchResultsBuilder'); $expectedResults = $searchResultsBuilder->setSearchCriteria($searchCriteria)->setItems([$taxRule])->setTotalCount(1)->create(); $this->ruleModelMock->expects($this->once())->method('getCollection')->will($this->returnValue($mockCollection)); $this->converterMock->expects($this->once())->method('createTaxRuleDataObjectFromModel')->with($taxRuleModel)->will($this->returnValue($taxRule)); $actualResults = $this->taxRuleService->searchTaxRules($searchCriteria); $this->assertNotNull($actualResults); $this->assertSame($searchCriteria, $actualResults->getSearchCriteria()); $this->assertSame($expectedResults->getSearchCriteria(), $actualResults->getSearchCriteria()); $this->assertSame($expectedResults->getTotalCount(), $actualResults->getTotalCount()); $this->assertEquals($expectedResults->getItems(), $actualResults->getItems()); $this->assertSame($taxRule, $actualResults->getItems()[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); }
/** * * @param Filter[] $filters * @param Filter[] $filterGroup * @param string[] $expectedResultCodes The codes of the tax rules that are expected to be found * * @magentoDbIsolation enabled * @dataProvider searchTaxRulesDataProvider */ public function testSearchTaxRules($filters, $filterGroup, $expectedRuleCodes) { $this->setUpDefaultRules(); /** @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->taxRuleService->searchTaxRules($searchCriteria); $items = []; foreach ($expectedRuleCodes as $ruleCode) { $ruleId = $this->taxRules[$ruleCode]; $items[] = $this->taxRuleService->getTaxRule($ruleId); } /** @var TaxRuleSearchResultsBuilder $resultsBuilder */ $resultsBuilder = Bootstrap::getObjectManager()->create('Magento\\Tax\\Service\\V1\\Data\\TaxRuleSearchResultsBuilder'); $expectedResult = $resultsBuilder->setItems($items)->setTotalCount(count($items))->setSearchCriteria($searchCriteria)->create(); $this->assertEquals($expectedResult, $searchResults); $this->tearDownDefaultRules(); }