/**
  * @magentoDbIsolation enabled
  */
 public function testGetRatesByCustomerAndProductTaxClassId()
 {
     $this->setUpDefaultRules();
     $taxRateIds = $this->taxRuleRepository->get(current($this->taxRules))->getTaxRateIds();
     $expectedRates = [];
     foreach ($taxRateIds as $rateId) {
         $expectedRates[] = $this->taxRateRepository->get($rateId);
     }
     $rates = $this->taxRateManagement->getRatesByCustomerAndProductTaxClassId($this->taxClasses['DefaultCustomerClass'], $this->taxClasses['DefaultProductClass']);
     $this->assertCount(2, $rates);
     $this->assertEquals($expectedRates, $rates);
 }
Exemple #2
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\Exception\LocalizedException
  */
 public function convertAttribute($product, $entry)
 {
     $entry->cleanTaxes();
     if ($this->_taxData->getConfig()->priceIncludesTax()) {
         return $entry;
     }
     $defaultCustomerTaxClassId = $this->_getDefaultCustomerTaxClassId($product->getStoreId());
     $rates = $this->_taxRateManagement->getRatesByCustomerAndProductTaxClassId($defaultCustomerTaxClassId, $product->getTaxClassId());
     $targetCountry = $this->_config->getTargetCountry($product->getStoreId());
     $ratesTotal = 0;
     foreach ($rates as $rate) {
         $countryId = $rate->getTaxCountryId();
         $postcode = $rate->getTaxPostcode();
         if ($targetCountry == $countryId) {
             $regions = $this->_getRegionsByRegionId($rate->getTaxRegionId(), $postcode);
             $ratesTotal += count($regions);
             if ($ratesTotal > self::RATES_MAX) {
                 throw new \Magento\Framework\Exception\LocalizedException(__('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' => [TaxClassKeyInterface::KEY_TYPE => TaxClassKeyInterface::TYPE_ID, TaxClassKeyInterface::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->getTaxRegionId()], 'postcode' => $postcode];
                 $shippingAddressDataArray = ['country_id' => $countryId, 'region' => ['region_id' => $rate->getTaxRegionId()], 'postcode' => $postcode];
                 $quoteDetailsDataArray = ['billing_address' => $billingAddressDataArray, 'shipping_address' => $shippingAddressDataArray, 'customer_tax_class_key' => [TaxClassKeyInterface::KEY_TYPE => TaxClassKeyInterface::TYPE_ID, TaxClassKeyInterface::KEY_VALUE => $defaultCustomerTaxClassId], 'items' => [$quoteDetailsItemDataArray]];
                 $quoteDetailsObject = $this->_quoteDetailsFactory->create();
                 $this->dataObjectHelper->populateWithArray($quoteDetailsObject, $quoteDetailsDataArray, '\\Magento\\Tax\\Api\\Data\\QuoteDetailsInterface');
                 $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;
 }