Example #1
0
 /**
  * Run test getAllOptions method
  *
  * @param bool $isEmpty
  * @param array $expected
  * @dataProvider dataProviderGetAllOptions
  */
 public function testGetAllOptions($isEmpty, array $expected)
 {
     $filterMock = $this->getMock('Magento\\Framework\\Api\\Filter', [], [], '', false);
     $searchCriteriaMock = $this->getMock('Magento\\Framework\\Api\\SearchCriteria', [], [], '', false);
     $searchResultsMock = $this->getMockForAbstractClass('Magento\\Tax\\Api\\Data\\TaxClassSearchResultsInterface', [], '', false, true, true, ['getItems']);
     $taxClassMock = $this->getMockForAbstractClass('Magento\\Tax\\Api\\Data\\TaxClassInterface', ['getClassId', 'getClassName'], '', false, true, true);
     $this->filterBuilderMock->expects($this->once())->method('setField')->with(\Magento\Tax\Model\ClassModel::KEY_TYPE)->willReturnSelf();
     $this->filterBuilderMock->expects($this->once())->method('setValue')->with(\Magento\Tax\Api\TaxClassManagementInterface::TYPE_CUSTOMER)->willReturnSelf();
     $this->filterBuilderMock->expects($this->once())->method('create')->willReturn($filterMock);
     $this->searchCriteriaBuilderMock->expects($this->once())->method('addFilter')->with([$filterMock])->willReturnSelf();
     $this->searchCriteriaBuilderMock->expects($this->once())->method('create')->willReturn($searchCriteriaMock);
     $this->taxClassRepositoryMock->expects($this->once())->method('getList')->with($searchCriteriaMock)->willReturn($searchResultsMock);
     if (!$isEmpty) {
         $taxClassMock->expects($this->once())->method('getClassId')->willReturn(10);
         $taxClassMock->expects($this->once())->method('getClassName')->willReturn('class-name');
         $items = [$taxClassMock];
         $searchResultsMock->expects($this->once())->method('getItems')->willReturn($items);
         // checking of a lack of re-initialization
         for ($i = 10; --$i;) {
             $result = $this->customer->getAllOptions();
             $this->assertEquals($expected, $result);
         }
     } else {
         $items = [];
         $searchResultsMock->expects($this->once())->method('getItems')->willReturn($items);
         // checking exception
         $this->assertEmpty($this->customer->getAllOptions());
     }
 }
Example #2
0
 /**
  * Prepare form for render
  *
  * @return void
  */
 protected function _prepareLayout()
 {
     parent::_prepareLayout();
     /** @var \Magento\Framework\Data\Form $form */
     $form = $this->_formFactory->create();
     $groupId = $this->_coreRegistry->registry(RegistryConstants::CURRENT_GROUP_ID);
     /** @var \Magento\Customer\Api\Data\GroupInterface $customerGroup */
     if ($groupId === null) {
         $customerGroup = $this->groupDataFactory->create();
         $defaultCustomerTaxClass = $this->_taxHelper->getDefaultCustomerTaxClass();
     } else {
         $customerGroup = $this->_groupRepository->getById($groupId);
         $defaultCustomerTaxClass = $customerGroup->getTaxClassId();
     }
     $fieldset = $form->addFieldset('base_fieldset', ['legend' => __('Group Information')]);
     $validateClass = sprintf('required-entry validate-length maximum-length-%d', \Magento\Customer\Model\GroupManagement::GROUP_CODE_MAX_LENGTH);
     $name = $fieldset->addField('customer_group_code', 'text', ['name' => 'code', 'label' => __('Group Name'), 'title' => __('Group Name'), 'note' => __('Maximum length must be less then %1 symbols', \Magento\Customer\Model\GroupManagement::GROUP_CODE_MAX_LENGTH), 'class' => $validateClass, 'required' => true]);
     if ($customerGroup->getId() == 0 && $customerGroup->getCode()) {
         $name->setDisabled(true);
     }
     $fieldset->addField('tax_class_id', 'select', ['name' => 'tax_class', 'label' => __('Tax Class'), 'title' => __('Tax Class'), 'class' => 'required-entry', 'required' => true, 'values' => $this->_taxCustomer->toOptionArray()]);
     if ($customerGroup->getId() !== null) {
         // If edit add id
         $form->addField('id', 'hidden', ['name' => 'id', 'value' => $customerGroup->getId()]);
     }
     if ($this->_backendSession->getCustomerGroupData()) {
         $form->addValues($this->_backendSession->getCustomerGroupData());
         $this->_backendSession->setCustomerGroupData(null);
     } else {
         // TODO: need to figure out how the DATA can work with forms
         $form->addValues(['id' => $customerGroup->getId(), 'customer_group_code' => $customerGroup->getCode(), 'tax_class_id' => $defaultCustomerTaxClass]);
     }
     $form->setUseContainer(true);
     $form->setId('edit_form');
     $form->setAction($this->getUrl('customer/*/save'));
     $form->setMethod('post');
     $this->setForm($form);
 }
Example #3
0
 /**
  * Identify default customer tax class ID.
  *
  * @return int|null
  */
 public function getDefaultCustomerTaxClass()
 {
     $configValue = $this->taxHelper->getDefaultCustomerTaxClass();
     if (!empty($configValue)) {
         return $configValue;
     }
     $taxClasses = $this->customerTaxClassSource->getAllOptions(false);
     if (!empty($taxClasses)) {
         $firstClass = array_shift($taxClasses);
         return isset($firstClass['value']) ? $firstClass['value'] : null;
     } else {
         return null;
     }
 }
Example #4
0
 /**
  * @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();
 }
Example #5
0
 /**
  * Run test getAllOptions method for names integrity
  *
  * @param array $value
  * @dataProvider dataProviderGetAllOptionsNameIntegrity
  */
 public function testGetAllOptionsNameIntegrity(array $value)
 {
     $filterMock = $this->getMock('Magento\\Framework\\Api\\Filter', [], [], '', false);
     $searchCriteriaMock = $this->getMock('Magento\\Framework\\Api\\SearchCriteria', [], [], '', false);
     $searchResultsMock = $this->getMockForAbstractClass('Magento\\Tax\\Api\\Data\\TaxClassSearchResultsInterface', [], '', false, true, true, ['getItems']);
     $taxClassMock = $this->getMockForAbstractClass('Magento\\Tax\\Api\\Data\\TaxClassInterface', ['getClassId', 'getClassName'], '', false, true, true);
     $this->filterBuilderMock->expects($this->once())->method('setField')->with(\Magento\Tax\Model\ClassModel::KEY_TYPE)->willReturnSelf();
     $this->filterBuilderMock->expects($this->once())->method('setValue')->with(\Magento\Tax\Api\TaxClassManagementInterface::TYPE_CUSTOMER)->willReturnSelf();
     $this->filterBuilderMock->expects($this->once())->method('create')->willReturn($filterMock);
     $this->searchCriteriaBuilderMock->expects($this->once())->method('addFilter')->with([$filterMock])->willReturnSelf();
     $this->searchCriteriaBuilderMock->expects($this->once())->method('create')->willReturn($searchCriteriaMock);
     $this->taxClassRepositoryMock->expects($this->once())->method('getList')->with($searchCriteriaMock)->willReturn($searchResultsMock);
     $taxClassMock->expects($this->once())->method('getClassId')->willReturn($value['value']);
     $taxClassMock->expects($this->once())->method('getClassName')->willReturn($value['label']);
     $items = [$taxClassMock];
     $searchResultsMock->expects($this->once())->method('getItems')->willReturn($items);
     $result = $this->customer->getAllOptions();
     $expected = $value;
     $this->assertEquals([$expected], $result);
 }