/** * @magentoDbIsolation enabled */ public function testGetRatesByCustomerAndProductTaxClassId() { $this->setUpDefaultRules(); $taxRateIds = $this->taxRuleRepository->get(current($this->taxRules))->getTaxRateIds(); $expectedRates = []; foreach ($taxRateIds as $rateId) { $expectedRates[] = $this->taxRateRepository->get($rateId); } $rates = $this->taxRateManagement->getRatesByCustomerAndProductTaxClassId($this->taxClasses['DefaultCustomerClass'], $this->taxClasses['DefaultProductClass']); $this->assertCount(2, $rates); $this->assertEquals($expectedRates, $rates); }
/** * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ protected function _prepareForm() { $taxRuleId = $this->_coreRegistry->registry('tax_rule_id'); try { $taxRule = $this->ruleService->get($taxRuleId); } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { /** Tax rule not found */ } /** @var \Magento\Framework\Data\Form $form */ $form = $this->_formFactory->create(['data' => ['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', ['legend' => __('Tax Rule Information')]); $fieldset->addField('code', 'text', ['name' => 'code', 'value' => isset($formValues['code']) ? $formValues['code'] : '', 'label' => __('Name'), 'class' => 'required-entry', 'required' => true]); // Editable multiselect for customer tax class $selectConfig = $this->getTaxClassSelectConfig(TaxClassManagementInterface::TYPE_CUSTOMER); $options = $this->customerTaxClassSource->getAllOptions(false); if (!empty($options)) { $selected = $options[0]; } else { $selected = null; } // Use the rule data or pick the first class in the list $selectedCustomerTax = isset($formValues['tax_customer_class']) ? $formValues['tax_customer_class'] : $selected; $fieldset->addField('tax_customer_class', 'editablemultiselect', ['name' => 'tax_customer_class', 'label' => __('Customer Tax Class'), 'class' => 'required-entry', 'values' => $options, 'value' => $selectedCustomerTax, 'required' => true, 'select_config' => $selectConfig], false, true); // Editable multiselect for product tax class $selectConfig = $this->getTaxClassSelectConfig(TaxClassManagementInterface::TYPE_PRODUCT); $options = $this->productTaxClassSource->getAllOptions(false); if (!empty($options)) { $selected = $options[0]; } else { $selected = null; } // Use the rule data or pick the first class in the list $selectedProductTax = isset($formValues['tax_product_class']) ? $formValues['tax_product_class'] : $selected; $fieldset->addField('tax_product_class', 'editablemultiselect', ['name' => 'tax_product_class', 'label' => __('Product Tax Class'), 'class' => 'required-entry', 'values' => $options, 'value' => $selectedProductTax, 'required' => true, 'select_config' => $selectConfig], false, true); $fieldset->addField('tax_rate', 'editablemultiselect', ['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' => ['is_entity_editable' => true]]); $fieldset->addField('priority', 'text', ['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', ['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', ['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', ['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(); }
/** * * @param Filter[] $filters * @param Filter[] $filterGroup * @param string[] $expectedRuleCodes The codes of the tax rules that are expected to be found * * @magentoDbIsolation enabled * @dataProvider searchTaxRulesDataProvider */ public function testGetList($filters, $filterGroup, $expectedRuleCodes) { $this->setUpDefaultRules(); /** @var \Magento\Framework\Api\SearchCriteriaBuilder $searchBuilder */ $searchBuilder = Bootstrap::getObjectManager()->create('Magento\\Framework\\Api\\SearchCriteriaBuilder'); foreach ($filters as $filter) { $searchBuilder->addFilters([$filter]); } if ($filterGroup !== null) { $searchBuilder->addFilters($filterGroup); } $searchCriteria = $searchBuilder->create(); $searchResults = $this->taxRuleRepository->getList($searchCriteria); $items = []; foreach ($expectedRuleCodes as $ruleCode) { $ruleId = $this->taxRules[$ruleCode]; $items[] = $this->taxRuleRepository->get($ruleId); } $this->assertEquals($searchCriteria, $searchResults->getSearchCriteria()); $this->assertEquals(count($expectedRuleCodes), $searchResults->getTotalCount()); foreach ($searchResults->getItems() as $rule) { $this->assertContains($rule->getCode(), $expectedRuleCodes); } $this->tearDownDefaultRules(); }