/**
  * @param array $quoteDetailsData
  * @param array $taxDetailsData
  * @param string $calculateCallback Name of a function within this test class which will be executed to create
  *      a tax details item.
  * @return void
  * @dataProvider calculateTaxProvider
  */
 public function testCalculateTax($quoteDetailsData, $taxDetailsData, $calculateCallback = 'createTaxDetailsItem')
 {
     $storeMock = $this->getMockBuilder('Magento\\Store\\Model\\Store')->disableOriginalConstructor()->getMock();
     $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($storeMock));
     $calculatorMock = $this->getMockBuilder('\\Magento\\Tax\\Model\\Calculation\\AbstractCalculator')->disableOriginalConstructor()->setMethods(['calculate'])->getMockForAbstractClass();
     $this->calculatorFactory->expects($this->any())->method('create')->will($this->returnValue($calculatorMock));
     $calculatorMock->expects($this->any())->method('calculate')->will($this->returnCallback([$this, $calculateCallback]));
     $quoteDetails = $this->quoteDetailsBuilder->populateWithArray($quoteDetailsData)->create();
     $taxDetails = $this->taxCalculationService->calculateTax($quoteDetails);
     $this->assertEquals($taxDetailsData, $taxDetails->__toArray());
 }
Example #2
0
 /**
  * @magentoDbIsolation enabled
  * @dataProvider multiRulesUnitBasedDataProvider
  * @magentoConfigFixture default_store tax/calculation/algorithm UNIT_BASE_CALCULATION
  */
 public function testMultiRulesUnitBased($quoteDetailsData, $expectedTaxDetails)
 {
     $quoteDetailsData = $this->performTaxClassSubstitution($quoteDetailsData);
     $quoteDetails = $this->quoteDetailsBuilder->populateWithArray($quoteDetailsData)->create();
     $taxDetails = $this->taxCalculationService->calculateTax($quoteDetails);
     $this->assertEquals($expectedTaxDetails, $taxDetails->__toArray());
 }
Example #3
0
 /**
  * Collect tax totals for quote address
  *
  * @param   Address $address
  * @return  $this
  */
 public function collect(Address $address)
 {
     parent::collect($address);
     $items = $this->_getAddressItems($address);
     if (!$items) {
         return $this;
     }
     //Preparation for calling taxCalculationService with base currency
     $quoteDetails = $this->prepareQuoteDetails($address, true);
     $baseTaxDetailsBase = $this->taxCalculationService->calculateTax($quoteDetails, $address->getQuote()->getStore()->getStoreId());
     //Preparation for calling taxCalculationService with display currency
     $quoteDetails = $this->prepareQuoteDetails($address, false);
     $taxDetails = $this->taxCalculationService->calculateTax($quoteDetails, $address->getQuote()->getStore()->getStoreId());
     //Populate address and items with tax calculation results
     $this->updateTaxInfo($address, $taxDetails, $baseTaxDetailsBase);
     if ($this->processExtraSubtotalAmount()) {
         $address->addTotalAmount('tax', $address->getExtraTaxAmount());
         $address->addBaseTotalAmount('tax', $address->getBaseExtraTaxAmount());
         $address->addTotalAmount('subtotal', $address->getExtraSubtotalAmount());
         $address->addBaseTotalAmount('subtotal', $address->getBaseExtraSubtotalAmount());
         $address->setSubtotalInclTax($address->getSubtotalInclTax() + $address->getExtraSubtotalAmount() + $address->getExtraTaxAmount());
         $address->setBaseSubtotalInclTax($address->getBaseSubtotalInclTax() + $address->getBaseExtraSubtotalAmount() + $address->getBaseExtraTaxAmount());
     }
     return $this;
 }
Example #4
0
 /**
  * Set current attribute to entry (for specified product)
  *
  * @param \Magento\Catalog\Model\Product $product
  * @param \Magento\Framework\Gdata\Gshopping\Entry $entry
  * @return \Magento\Framework\Gdata\Gshopping\Entry
  * @throws \Magento\Framework\Model\Exception
  */
 public function convertAttribute($product, $entry)
 {
     $entry->cleanTaxes();
     if ($this->_taxData->getConfig()->priceIncludesTax()) {
         return $entry;
     }
     $defaultCustomerTaxClassId = $this->_getDefaultCustomerTaxClassId($product->getStoreId());
     $rates = $this->_taxRuleService->getRatesByCustomerAndProductTaxClassId($defaultCustomerTaxClassId, $product->getTaxClassId());
     $targetCountry = $this->_config->getTargetCountry($product->getStoreId());
     $ratesTotal = 0;
     foreach ($rates as $rate) {
         $countryId = $rate->getCountryId();
         $postcode = $rate->getPostcode();
         if ($targetCountry == $countryId) {
             $regions = $this->_getRegionsByRegionId($rate->getRegionId(), $postcode);
             $ratesTotal += count($regions);
             if ($ratesTotal > self::RATES_MAX) {
                 throw new \Magento\Framework\Model\Exception(__("Google shopping only supports %1 tax rates per product", self::RATES_MAX));
             }
             foreach ($regions as $region) {
                 $adjustments = $product->getPriceInfo()->getAdjustments();
                 if (array_key_exists('tax', $adjustments)) {
                     $taxIncluded = true;
                 } else {
                     $taxIncluded = false;
                 }
                 $quoteDetailsItemDataArray = ['code' => $product->getSku(), 'type' => 'product', 'tax_class_key' => [TaxClassKey::KEY_TYPE => TaxClassKey::TYPE_ID, TaxClassKey::KEY_VALUE => $product->getTaxClassId()], 'unit_price' => $product->getPrice(), 'quantity' => 1, 'tax_included' => $taxIncluded, 'short_description' => $product->getName()];
                 $billingAddressDataArray = ['country_id' => $countryId, 'region' => ['region_id' => $rate->getRegionId()], 'postcode' => $postcode];
                 $shippingAddressDataArray = ['country_id' => $countryId, 'region' => ['region_id' => $rate->getRegionId()], 'postcode' => $postcode];
                 $quoteDetailsDataArray = ['billing_address' => $billingAddressDataArray, 'shipping_address' => $shippingAddressDataArray, 'customer_tax_class_key' => [TaxClassKey::KEY_TYPE => TaxClassKey::TYPE_ID, TaxClassKey::KEY_VALUE => $defaultCustomerTaxClassId], 'items' => [$quoteDetailsItemDataArray]];
                 $quoteDetailsObject = $this->_quoteDetailsBuilder->populateWithArray($quoteDetailsDataArray)->create();
                 $taxDetails = $this->_taxCalculationService->calculateTax($quoteDetailsObject, $product->getStoreId());
                 $taxRate = $taxDetails->getTaxAmount() / $taxDetails->getSubtotal() * 100;
                 $entry->addTax(['tax_rate' => $taxRate, 'tax_country' => $countryId, 'tax_region' => $region]);
             }
         }
     }
     return $entry;
 }