/**
  * Creates a collection item that represents a tax rate for the tax rates grid.
  *
  * @param TaxRate $taxRate Input data for creating the item.
  * @return \Magento\Framework\Object Collection item that represents a tax rate
  */
 protected function createTaxRateCollectionItem(TaxRate $taxRate)
 {
     $collectionItem = new \Magento\Framework\Object();
     $collectionItem->setTaxCalculationRateId($taxRate->getId());
     $collectionItem->setCode($taxRate->getCode());
     $collectionItem->setTaxCountryId($taxRate->getTaxCountryId());
     $collectionItem->setTaxRegionId($taxRate->getTaxRegionId());
     $collectionItem->setRegionName($taxRate->getRegionName());
     $collectionItem->setTaxPostcode($taxRate->getTaxPostcode());
     $collectionItem->setRate($taxRate->getRate());
     $collectionItem->setTitles($this->rateConverter->createTitleArrayFromServiceObject($taxRate));
     if ($taxRate->getZipTo() != null && $taxRate->getZipFrom() != null) {
         /* must be a "1" for existing code (e.g. JavaScript) to work */
         $collectionItem->setZipIsRange("1");
         $collectionItem->setZipFrom($taxRate->getZipFrom());
         $collectionItem->setZipTo($taxRate->getZipTo());
     } else {
         $collectionItem->setZipIsRange(null);
         $collectionItem->setZipFrom(null);
         $collectionItem->setZipTo(null);
     }
     return $collectionItem;
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function save(\Magento\Tax\Api\Data\TaxRateInterface $taxRate)
 {
     if ($taxRate->getId()) {
         $this->rateRegistry->retrieveTaxRate($taxRate->getId());
     }
     $this->validate($taxRate);
     $taxRateTitles = $this->converter->createTitleArrayFromServiceObject($taxRate);
     try {
         $this->resourceModel->save($taxRate);
         $taxRate->saveTitles($taxRateTitles);
     } catch (LocalizedException $e) {
         throw $e;
     }
     $this->rateRegistry->registerTaxRate($taxRate);
     return $taxRate;
 }
Example #3
0
 /**
  * Save Tax Rate
  *
  * @param TaxRateDataObject $taxRate
  * @throws InputException
  * @throws ModelException
  * @return RateModel
  */
 protected function saveTaxRate(TaxRateDataObject $taxRate)
 {
     $this->validate($taxRate);
     $taxRateModel = $this->converter->createTaxRateModel($taxRate);
     $taxRateTitles = $this->converter->createTitleArrayFromServiceObject($taxRate);
     try {
         $taxRateModel->save();
         $taxRateModel->saveTitles($taxRateTitles);
     } catch (ModelException $e) {
         if ($e->getCode() == ModelException::ERROR_CODE_ENTITY_ALREADY_EXISTS) {
             throw new InputException($e->getMessage());
         } else {
             throw $e;
         }
     }
     $this->rateRegistry->registerTaxRate($taxRateModel);
     return $taxRateModel;
 }
Example #4
0
 public function testCreateTitlesFromServiceObjectWhenTitlesAreNotProvided()
 {
     $taxRateMock = $this->getMock('Magento\\Tax\\Api\\Data\\TaxRateInterface');
     $taxRateMock->expects($this->once())->method('getTitles')->willReturn([]);
     $this->assertEquals([], $this->converter->createTitleArrayFromServiceObject($taxRateMock));
 }