Esempio n. 1
0
 /**
  * @param array $taxDetailsDataArray
  *
  * @dataProvider taxDetailsPopulateDataProvider
  */
 public function testTaxDetailsPopulate($taxDetailsDataArray)
 {
     $taxDetailsDataObjectFromArray = $this->taxDetailsBuilder->populateWithArray($taxDetailsDataArray)->create();
     $taxDetailsDataObjectFromObject = $this->taxDetailsBuilder->populate($taxDetailsDataObjectFromArray)->create();
     $this->assertEquals($taxDetailsDataArray, $taxDetailsDataObjectFromArray->__toArray());
     $this->assertEquals($taxDetailsDataArray, $taxDetailsDataObjectFromObject->__toArray());
     $this->assertEquals($taxDetailsDataObjectFromArray, $taxDetailsDataObjectFromObject);
 }
 /**
  * Callback function that creates a tax details item with applied taxes from a quote details item for testing.
  *
  * @param QuoteDetailsItem $item
  * @return Data\TaxDetails\Item
  */
 public function createTaxDetailsItemWithAppliedTaxes(QuoteDetailsItem $item)
 {
     $appliedTaxRateBuilder = $this->taxDetailsBuilder->getAppliedTaxBuilder();
     $taxRateBuilder = $appliedTaxRateBuilder->getAppliedTaxRateBuilder();
     $rate = $taxRateBuilder->setPercent(self::TAX)->setCode('TAX')->setTitle('Tax')->create();
     $appliedTaxes = $appliedTaxRateBuilder->setAmount($item->getUnitPrice() * self::TAX)->setTaxRateKey('TAX_RATE')->setPercent(self::TAX)->setRates([$rate])->create();
     return $this->taxDetailsItemBuilder->populate($this->createTaxDetailsItem($item))->setAppliedTaxes([$appliedTaxes])->create();
 }
 /**
  * {@inheritdoc}
  */
 public function calculateTax(QuoteDetails $quoteDetails, $storeId = null)
 {
     if (is_null($storeId)) {
         $storeId = $this->storeManager->getStore()->getStoreId();
     }
     // initial TaxDetails data
     $taxDetailsData = [TaxDetails::KEY_SUBTOTAL => 0.0, TaxDetails::KEY_TAX_AMOUNT => 0.0, TaxDetails::KEY_DISCOUNT_TAX_COMPENSATION_AMOUNT => 0.0, TaxDetails::KEY_APPLIED_TAXES => [], TaxDetails::KEY_ITEMS => []];
     $items = $quoteDetails->getItems();
     if (empty($items)) {
         return $this->taxDetailsBuilder->populateWithArray($taxDetailsData)->create();
     }
     $this->computeRelationships($items);
     $addressRequest = $this->getAddressTaxRequest($quoteDetails, $storeId, $quoteDetails->getCustomerId());
     if ($this->config->priceIncludesTax($storeId)) {
         $storeRequest = $this->getStoreTaxRequest($storeId);
         $classIds = [];
         foreach ($items as $item) {
             if ($item->getTaxClassId()) {
                 $classIds[] = $item->getTaxClassId();
             }
         }
         $classIds = array_unique($classIds);
         $addressRequest->setProductClassId($classIds);
         $storeRequest->setProductClassId($classIds);
         if ((bool) $this->config->crossBorderTradeEnabled($storeId)) {
             $addressRequest->setSameRateAsStore(true);
         } else {
             $addressRequest->setSameRateAsStore($this->calculator->compareRequests($storeRequest, $addressRequest));
         }
     }
     // init rounding deltas for this quote
     $this->roundingDeltas = [];
     // init discount tax compensations array
     $this->discountTaxCompensations = [];
     $processedItems = [];
     /** @var QuoteDetailsItem $item */
     foreach ($this->keyedItems as $item) {
         if (isset($this->parentToChildren[$item->getCode()])) {
             $processedChildren = [];
             foreach ($this->parentToChildren[$item->getCode()] as $child) {
                 $processedItem = $this->processItem($child, $addressRequest, $storeId);
                 $taxDetailsData = $this->aggregateItemData($taxDetailsData, $processedItem);
                 $processedItems[$processedItem->getCode()] = $processedItem;
                 $processedChildren[] = $processedItem;
             }
             $processedItemBuilder = $this->calculateParent($processedChildren, $item->getQuantity());
             $processedItemBuilder->setCode($item->getCode());
             $processedItemBuilder->setType($item->getType());
             $processedItem = $processedItemBuilder->create();
         } else {
             $processedItem = $this->processItem($item, $addressRequest, $storeId);
             $taxDetailsData = $this->aggregateItemData($taxDetailsData, $processedItem);
         }
         $processedItems[$processedItem->getCode()] = $processedItem;
     }
     return $this->taxDetailsBuilder->populateWithArray($taxDetailsData)->setItems($processedItems)->create();
 }
 /**
  * {@inheritdoc}
  */
 public function calculateTax(QuoteDetails $quoteDetails, $storeId = null)
 {
     if (is_null($storeId)) {
         $storeId = $this->storeManager->getStore()->getStoreId();
     }
     // initial TaxDetails data
     $taxDetailsData = [TaxDetails::KEY_SUBTOTAL => 0.0, TaxDetails::KEY_TAX_AMOUNT => 0.0, TaxDetails::KEY_DISCOUNT_TAX_COMPENSATION_AMOUNT => 0.0, TaxDetails::KEY_APPLIED_TAXES => [], TaxDetails::KEY_ITEMS => []];
     $items = $quoteDetails->getItems();
     if (empty($items)) {
         return $this->taxDetailsBuilder->populateWithArray($taxDetailsData)->create();
     }
     $this->computeRelationships($items);
     $calculator = $this->calculatorFactory->create($this->config->getAlgorithm($storeId), $storeId, $quoteDetails->getBillingAddress(), $quoteDetails->getShippingAddress(), $this->taxClassService->getTaxClassId($quoteDetails->getCustomerTaxClassKey(), 'customer'), $quoteDetails->getCustomerId());
     $processedItems = [];
     /** @var QuoteDetailsItem $item */
     foreach ($this->keyedItems as $item) {
         if (isset($this->parentToChildren[$item->getCode()])) {
             $processedChildren = [];
             foreach ($this->parentToChildren[$item->getCode()] as $child) {
                 $processedItem = $this->processItem($child, $calculator);
                 $taxDetailsData = $this->aggregateItemData($taxDetailsData, $processedItem);
                 $processedItems[$processedItem->getCode()] = $processedItem;
                 $processedChildren[] = $processedItem;
             }
             $processedItemBuilder = $this->calculateParent($processedChildren, $item->getQuantity());
             $processedItemBuilder->setCode($item->getCode());
             $processedItemBuilder->setType($item->getType());
             $processedItem = $processedItemBuilder->create();
         } else {
             $processedItem = $this->processItem($item, $calculator);
             $taxDetailsData = $this->aggregateItemData($taxDetailsData, $processedItem);
         }
         $processedItems[$processedItem->getCode()] = $processedItem;
     }
     return $this->taxDetailsBuilder->populateWithArray($taxDetailsData)->setItems($processedItems)->create();
 }