/** * Creates a collection item that represents a tax rule for the tax rules grid. * * @param TaxRule $taxRule Input data for creating the item. * @return \Magento\Framework\Object Collection item that represents a tax rule */ protected function createTaxRuleCollectionItem(TaxRule $taxRule) { $collectionItem = new \Magento\Framework\Object(); $collectionItem->setTaxCalculationRuleId($taxRule->getId()); $collectionItem->setCode($taxRule->getCode()); /* should cast to string so that some optional fields won't be null on the collection grid pages */ $collectionItem->setPriority((string) $taxRule->getPriority()); $collectionItem->setPosition((string) $taxRule->getSortOrder()); $collectionItem->setCalculateSubtotal($taxRule->getCalculateSubtotal() ? '1' : '0'); $collectionItem->setCustomerTaxClasses($taxRule->getCustomerTaxClassIds()); $collectionItem->setProductTaxClasses($taxRule->getProductTaxClassIds()); $collectionItem->setTaxRates($taxRule->getTaxRateIds()); return $collectionItem; }
/** * Validate tax rule * * @param TaxRule $rule * @return void * @throws InputException * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ private function validate(TaxRule $rule) { $exception = new InputException(); if (!\Zend_Validate::is(trim($rule->getSortOrder()), 'GreaterThan', [-1])) { $exception->addError(InputException::INVALID_FIELD_MIN_VALUE, ['fieldName' => TaxRule::SORT_ORDER, 'value' => $rule->getSortOrder(), 'minValue' => 0]); } if (!\Zend_Validate::is(trim($rule->getPriority()), 'GreaterThan', [-1])) { $exception->addError(InputException::INVALID_FIELD_MIN_VALUE, ['fieldName' => TaxRule::PRIORITY, 'value' => $rule->getPriority(), 'minValue' => 0]); } // Code is required if (!\Zend_Validate::is(trim($rule->getCode()), 'NotEmpty')) { $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => TaxRule::CODE]); } // customer tax class ids is required if ($rule->getCustomerTaxClassIds() === null || !$rule->getCustomerTaxClassIds()) { $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => TaxRule::CUSTOMER_TAX_CLASS_IDS]); } // product tax class ids is required if ($rule->getProductTaxClassIds() === null || !$rule->getProductTaxClassIds()) { $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => TaxRule::PRODUCT_TAX_CLASS_IDS]); } // tax rate ids is required if ($rule->getTaxRateIds() === null || !$rule->getTaxRateIds()) { $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => TaxRule::TAX_RATE_IDS]); } // throw exception if errors were found if ($exception->wasErrorAdded()) { throw $exception; } }
/** * Extract tax rule data in a format which is * * @param \Magento\Tax\Service\V1\Data\TaxRule $taxRule * @return array */ protected function extractTaxRuleData($taxRule) { $taxRuleData = ['code' => $taxRule->getCode(), 'tax_customer_class' => $taxRule->getCustomerTaxClassIds(), 'tax_product_class' => $taxRule->getProductTaxClassIds(), 'tax_rate' => $taxRule->getTaxRateIds(), 'priority' => $taxRule->getPriority(), 'position' => $taxRule->getSortOrder(), 'calculate_subtotal' => $taxRule->getCalculateSubtotal()]; return $taxRuleData; }
/** * Convert a tax rule data object to tax rule model * * @param TaxRuleDataObject $taxRule * @return TaxRuleModel */ public function createTaxRuleModel(TaxRuleDataObject $taxRuleDataObject) { $taxRuleModel = $this->taxRuleModelFactory->create(); $ruleId = $taxRuleDataObject->getId(); if ($ruleId) { $taxRuleModel->setId($ruleId); } $taxRuleModel->setTaxCustomerClass($taxRuleDataObject->getCustomerTaxClassIds()); $taxRuleModel->setTaxProductClass($taxRuleDataObject->getProductTaxClassIds()); $taxRuleModel->setTaxRate($taxRuleDataObject->getTaxRateIds()); $taxRuleModel->setCode($taxRuleDataObject->getCode()); $taxRuleModel->setPriority($taxRuleDataObject->getPriority()); $taxRuleModel->setPosition($taxRuleDataObject->getSortOrder()); $taxRuleModel->setCalculateSubtotal($taxRuleDataObject->getCalculateSubtotal()); return $taxRuleModel; }
/** * Validate tax rule * * @param TaxRule $rule * @return void * @throws InputException * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ private function validate(TaxRule $rule) { $exception = new InputException(); // SortOrder is required and must be 0 or greater if (!\Zend_Validate::is(trim($rule->getSortOrder()), 'NotEmpty')) { $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => TaxRule::SORT_ORDER]); } if (!\Zend_Validate::is(trim($rule->getSortOrder()), 'GreaterThan', [-1])) { $exception->addError(InputException::INVALID_FIELD_MIN_VALUE, ['fieldName' => TaxRule::SORT_ORDER, 'value' => $rule->getSortOrder(), 'minValue' => 0]); } // Priority is required and must be 0 or greater if (!\Zend_Validate::is(trim($rule->getPriority()), 'NotEmpty')) { $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => TaxRule::PRIORITY]); } if (!\Zend_Validate::is(trim($rule->getPriority()), 'GreaterThan', [-1])) { $exception->addError(InputException::INVALID_FIELD_MIN_VALUE, ['fieldName' => TaxRule::PRIORITY, 'value' => $rule->getPriority(), 'minValue' => 0]); } // Code is required if (!\Zend_Validate::is(trim($rule->getCode()), 'NotEmpty')) { $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => TaxRule::CODE]); } // customer tax class ids is required if ($rule->getCustomerTaxClassIds() === null || !$rule->getCustomerTaxClassIds()) { $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => TaxRule::CUSTOMER_TAX_CLASS_IDS]); } else { // see if the customer tax class ids exist $customerTaxClassIds = $rule->getCustomerTaxClassIds(); foreach ($customerTaxClassIds as $customerTaxClassId) { try { $taxClass = $this->taxClassService->getTaxClass($customerTaxClassId); if (is_null($taxClass) || !($taxClass->getClassType() == TaxClassModel::TAX_CLASS_TYPE_CUSTOMER)) { $exception->addError(NoSuchEntityException::MESSAGE_SINGLE_FIELD, ['fieldName' => TaxRule::CUSTOMER_TAX_CLASS_IDS, 'value' => $customerTaxClassId]); } } catch (NoSuchEntityException $e) { $exception->addError($e->getRawMessage(), $e->getParameters()); } } } // product tax class ids is required if ($rule->getProductTaxClassIds() === null || !$rule->getProductTaxClassIds()) { $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => TaxRule::PRODUCT_TAX_CLASS_IDS]); } else { // see if the product tax class ids exist $productTaxClassIds = $rule->getProductTaxClassIds(); foreach ($productTaxClassIds as $productTaxClassId) { try { $taxClass = $this->taxClassService->getTaxClass($productTaxClassId); if (is_null($taxClass) || !($taxClass->getClassType() == TaxClassModel::TAX_CLASS_TYPE_PRODUCT)) { $exception->addError(NoSuchEntityException::MESSAGE_SINGLE_FIELD, ['fieldName' => TaxRule::PRODUCT_TAX_CLASS_IDS, 'value' => $productTaxClassId]); } } catch (NoSuchEntityException $e) { $exception->addError($e->getRawMessage(), $e->getParameters()); } } } // tax rate ids is required if ($rule->getTaxRateIds() === null || !$rule->getTaxRateIds()) { $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => TaxRule::TAX_RATE_IDS]); } // throw exception if errors were found if ($exception->wasErrorAdded()) { throw $exception; } }