예제 #1
0
파일: Form.php 프로젝트: aiesh/magento2
 /**
  * Extract tax rate data in a format which is
  *
  * @param \Magento\Tax\Service\V1\Data\TaxRate $taxRate
  * @return array
  */
 protected function extractTaxRateData($taxRate)
 {
     $zipRange = $taxRate->getZipRange();
     $formData = ['tax_calculation_rate_id' => $taxRate->getId(), 'tax_country_id' => $taxRate->getCountryId(), 'tax_region_id' => $taxRate->getRegionId(), 'tax_postcode' => $taxRate->getPostcode(), 'code' => $taxRate->getCode(), 'rate' => $taxRate->getPercentageRate(), 'zip_is_range' => false];
     if ($zipRange) {
         $formData['zip_is_range'] = true;
         $formData['zip_from'] = $zipRange->getFrom();
         $formData['zip_to'] = $zipRange->getTo();
     }
     if ($taxRate->getTitles()) {
         $titleData = [];
         foreach ($taxRate->getTitles() as $title) {
             $titleData[] = [$title->getStoreId() => $title->getValue()];
         }
         $formData['title'] = $titleData;
     }
     return $formData;
 }
예제 #2
0
 /**
  * Validate tax rate
  *
  * @param TaxRateDataObject $taxRate
  * @throws InputException
  * @return void
  *
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 private function validate(TaxRateDataObject $taxRate)
 {
     $exception = new InputException();
     $countryCode = $taxRate->getCountryId();
     if (!\Zend_Validate::is($countryCode, 'NotEmpty')) {
         $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => 'country_id']);
     } else {
         if (!\Zend_Validate::is($this->countryFactory->create()->loadByCode($countryCode)->getId(), 'NotEmpty')) {
             $exception->addError(InputException::INVALID_FIELD_VALUE, ['fieldName' => 'country_id', 'value' => $countryCode]);
         }
     }
     $regionCode = $taxRate->getRegionId();
     if (\Zend_Validate::is($regionCode, 'NotEmpty') && !\Zend_Validate::is($this->regionFactory->create()->load($regionCode)->getId(), 'NotEmpty')) {
         $exception->addError(InputException::INVALID_FIELD_VALUE, ['fieldName' => 'region_id', 'value' => $regionCode]);
     }
     if (!\Zend_Validate::is($taxRate->getPercentageRate(), 'NotEmpty')) {
         $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => 'percentage_rate']);
     }
     if (!\Zend_Validate::is(trim($taxRate->getCode()), 'NotEmpty')) {
         $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => 'code']);
     }
     if ($taxRate->getZipRange()) {
         $zipRangeFromTo = ['zip_from' => $taxRate->getZipRange()->getFrom(), 'zip_to' => $taxRate->getZipRange()->getTo()];
         foreach ($zipRangeFromTo as $key => $value) {
             if (!is_numeric($value) || $value < 0) {
                 $exception->addError(InputException::INVALID_FIELD_VALUE, ['fieldName' => $key, 'value' => $value]);
             }
         }
         if ($zipRangeFromTo['zip_from'] > $zipRangeFromTo['zip_to']) {
             $exception->addError('Range To should be equal or greater than Range From.');
         }
     } else {
         if (!\Zend_Validate::is(trim($taxRate->getPostcode()), 'NotEmpty')) {
             $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => 'postcode']);
         }
     }
     if ($exception->wasErrorAdded()) {
         throw $exception;
     }
 }
예제 #3
0
 /**
  * Convert a TaxRate data object to rate model
  *
  * @param TaxRateDataObject $taxRate
  * @return TaxRateModel
  */
 public function createTaxRateModel(TaxRateDataObject $taxRate)
 {
     $rateModel = $this->taxRateModelFactory->create();
     $rateId = $taxRate->getId();
     if ($rateId) {
         $rateModel->setId($rateId);
     }
     $rateModel->setTaxCountryId($taxRate->getCountryId());
     $rateModel->setTaxRegionId($taxRate->getRegionId());
     $rateModel->setRate($taxRate->getPercentageRate());
     $rateModel->setCode($taxRate->getCode());
     $rateModel->setTaxPostcode($taxRate->getPostCode());
     $rateModel->setRegionName($taxRate->getRegionName());
     $zipRange = $taxRate->getZipRange();
     if ($zipRange) {
         $zipFrom = $zipRange->getFrom();
         $zipTo = $zipRange->getTo();
         if (!empty($zipFrom) || !empty($zipTo)) {
             $rateModel->setZipIsRange(1);
         }
         $rateModel->setZipFrom($zipFrom);
         $rateModel->setZipTo($zipTo);
     }
     return $rateModel;
 }