Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Tax Class Reference
Inheritance diagram for Tax:
CommonTaxCollector AbstractTotal CollectorInterface ReaderInterface

Public Member Functions

 __construct (\Magento\Tax\Model\Config $taxConfig, \Magento\Tax\Api\TaxCalculationInterface $taxCalculationService, \Magento\Tax\Api\Data\QuoteDetailsInterfaceFactory $quoteDetailsDataObjectFactory, \Magento\Tax\Api\Data\QuoteDetailsItemInterfaceFactory $quoteDetailsItemDataObjectFactory, \Magento\Tax\Api\Data\TaxClassKeyInterfaceFactory $taxClassKeyDataObjectFactory, CustomerAddressFactory $customerAddressFactory, CustomerAddressRegionFactory $customerAddressRegionFactory, \Magento\Tax\Helper\Data $taxData, Json $serializer=null)
 
 collect (\Magento\Quote\Model\Quote $quote, \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment, \Magento\Quote\Model\Quote\Address\Total $total)
 
 mapQuoteExtraTaxables (\Magento\Tax\Api\Data\QuoteDetailsItemInterfaceFactory $itemDataObjectFactory, Address $address, $useBaseCurrency)
 
 fetch (\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
 
 processConfigArray ($config, $store)
 
 getLabel ()
 
- Public Member Functions inherited from CommonTaxCollector
 __construct (\Magento\Tax\Model\Config $taxConfig, \Magento\Tax\Api\TaxCalculationInterface $taxCalculationService, \Magento\Tax\Api\Data\QuoteDetailsInterfaceFactory $quoteDetailsDataObjectFactory, \Magento\Tax\Api\Data\QuoteDetailsItemInterfaceFactory $quoteDetailsItemDataObjectFactory, \Magento\Tax\Api\Data\TaxClassKeyInterfaceFactory $taxClassKeyDataObjectFactory, CustomerAddressFactory $customerAddressFactory, CustomerAddressRegionFactory $customerAddressRegionFactory)
 
 mapAddress (QuoteAddress $address)
 
 mapItems (ShippingAssignmentInterface $shippingAssignment, $priceIncludesTax, $useBaseCurrency)
 
 populateAddressData (QuoteDetailsInterface $quoteDetails, QuoteAddress $address)
 
 updateItemTaxInfo ($quoteItem, $itemTaxDetails, $baseItemTaxDetails, $store)
 
 convertAppliedTaxes ($appliedTaxes, $baseAppliedTaxes, $extraInfo=[])
 
- Public Member Functions inherited from AbstractTotal
 setCode ($code)
 
 getCode ()
 
 getLabel ()
 
 collect (\Magento\Quote\Model\Quote $quote, \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment, \Magento\Quote\Model\Quote\Address\Total $total)
 
 fetch (\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
 
 _setTotal (\Magento\Quote\Model\Quote\Address\Total $total)
 
 getItemRowTotal (\Magento\Quote\Model\Quote\Item\AbstractItem $item)
 
 getItemBaseRowTotal (\Magento\Quote\Model\Quote\Item\AbstractItem $item)
 
 getIsItemRowTotalCompoundable (\Magento\Quote\Model\Quote\Item\AbstractItem $item)
 
 processConfigArray ($config, $store)
 

Protected Member Functions

 clearValues (Address\Total $total)
 
 getQuoteTaxDetails ($shippingAssignment, $total, $useBaseCurrency)
 
- Protected Member Functions inherited from CommonTaxCollector
 prepareQuoteDetails (ShippingAssignmentInterface $shippingAssignment, $itemDataObjects)
 
 processShippingTaxInfo (ShippingAssignmentInterface $shippingAssignment, QuoteAddress\Total $total, $shippingTaxDetails, $baseShippingTaxDetails)
 
 _saveAppliedTaxes (QuoteAddress\Total $total, $applied, $amount, $baseAmount, $rate)
 
 includeShipping ()
 
 includeItems ()
 
 includeExtraTax ()
 
 saveAppliedTaxes ()
 
 getNextIncrement ()
 
- Protected Member Functions inherited from AbstractTotal
 _setAddress (\Magento\Quote\Model\Quote\Address $address)
 
 _getAddress ()
 
 _getTotal ()
 
 _setAmount ($amount)
 
 _setBaseAmount ($baseAmount)
 
 _addAmount ($amount)
 
 _addBaseAmount ($baseAmount)
 
 _getAddressItems (\Magento\Quote\Model\Quote\Address $address)
 

Protected Attributes

 $counter = 0
 
 $_taxData
 
 $_config
 
 $_discountTaxCompensationes = []
 
- Protected Attributes inherited from CommonTaxCollector
 $_config
 
 $counter = 0
 
 $taxCalculationService
 
 $quoteDetailsDataObjectFactory
 
 $customerAddressFactory
 
 $customerAddressRegionFactory
 
 $taxClassKeyDataObjectFactory
 
 $quoteDetailsItemDataObjectFactory
 
- Protected Attributes inherited from AbstractTotal
 $_code
 
 $_address = null
 
 $_canAddAmountToAddress = true
 
 $_canSetAddressAmount = true
 
 $_itemRowTotalKey = null
 
 $total
 

Additional Inherited Members

- Data Fields inherited from CommonTaxCollector
const ITEM_TYPE_SHIPPING = 'shipping'
 
const ITEM_TYPE_PRODUCT = 'product'
 
const ITEM_CODE_SHIPPING = 'shipping'
 
const KEY_ITEM = 'item'
 
const KEY_BASE_ITEM = 'base_item'
 
const KEY_ASSOCIATED_TAXABLE_TYPE = 'type'
 
const KEY_ASSOCIATED_TAXABLE_CODE = 'code'
 
const KEY_ASSOCIATED_TAXABLE_UNIT_PRICE = 'unit_price'
 
const KEY_ASSOCIATED_TAXABLE_BASE_UNIT_PRICE = 'base_unit_price'
 
const KEY_ASSOCIATED_TAXABLE_QUANTITY = 'quantity'
 
const KEY_ASSOCIATED_TAXABLE_TAX_CLASS_ID = 'tax_class_id'
 
const KEY_ASSOCIATED_TAXABLE_PRICE_INCLUDES_TAX = 'price_includes_tax'
 
const KEY_ASSOCIATED_TAXABLE_ASSOCIATION_ITEM_CODE = 'associated_item_code'
 
const ASSOCIATION_ITEM_CODE_FOR_QUOTE = 'quote'
 
const KEY_TAX_DETAILS_TYPE = 'type'
 
const KEY_TAX_DETAILS_CODE = 'code'
 
const KEY_TAX_DETAILS_PRICE_EXCL_TAX = 'price_excl_tax'
 
const KEY_TAX_DETAILS_BASE_PRICE_EXCL_TAX = 'base_price_excl_tax'
 
const KEY_TAX_DETAILS_PRICE_INCL_TAX = 'price_incl_tax'
 
const KEY_TAX_DETAILS_BASE_PRICE_INCL_TAX = 'base_price_incl_tax'
 
const KEY_TAX_DETAILS_ROW_TOTAL = 'row_total_excl_tax'
 
const KEY_TAX_DETAILS_BASE_ROW_TOTAL = 'base_row_total_excl_tax'
 
const KEY_TAX_DETAILS_ROW_TOTAL_INCL_TAX = 'row_total_incl_tax'
 
const KEY_TAX_DETAILS_BASE_ROW_TOTAL_INCL_TAX = 'base_row_total_incl_tax'
 
const KEY_TAX_DETAILS_TAX_PERCENT = 'tax_percent'
 
const KEY_TAX_DETAILS_ROW_TAX = 'row_tax'
 
const KEY_TAX_DETAILS_BASE_ROW_TAX = 'base_row_tax'
 
const KEY_TAX_DETAILS_APPLIED_TAXES = 'applied_taxes'
 

Detailed Description

Tax totals calculation model @SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 21 of file Tax.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Tax\Model\Config  $taxConfig,
\Magento\Tax\Api\TaxCalculationInterface  $taxCalculationService,
\Magento\Tax\Api\Data\QuoteDetailsInterfaceFactory  $quoteDetailsDataObjectFactory,
\Magento\Tax\Api\Data\QuoteDetailsItemInterfaceFactory  $quoteDetailsItemDataObjectFactory,
\Magento\Tax\Api\Data\TaxClassKeyInterfaceFactory  $taxClassKeyDataObjectFactory,
CustomerAddressFactory  $customerAddressFactory,
CustomerAddressRegionFactory  $customerAddressRegionFactory,
\Magento\Tax\Helper\Data  $taxData,
Json  $serializer = null 
)

Class constructor

Parameters
\Magento\Tax\Model\Config$taxConfig
\Magento\Tax\Api\TaxCalculationInterface$taxCalculationService
\Magento\Tax\Api\Data\QuoteDetailsInterfaceFactory$quoteDetailsDataObjectFactory
\Magento\Tax\Api\Data\QuoteDetailsItemInterfaceFactory$quoteDetailsItemDataObjectFactory
\Magento\Tax\Api\Data\TaxClassKeyInterfaceFactory$taxClassKeyDataObjectFactory
CustomerAddressFactory$customerAddressFactory
CustomerAddressRegionFactory$customerAddressRegionFactory
\Magento\Tax\Helper\Data$taxData
Json$serializer

Definition at line 69 of file Tax.php.

79  {
80  $this->setCode('tax');
81  $this->_taxData = $taxData;
82  $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
83  parent::__construct(
84  $taxConfig,
91  );
92  }

Member Function Documentation

◆ clearValues()

clearValues ( Address\Total  $total)
protected

Clear tax related total values in address

Parameters
Address\Total$total
Returns
void

Definition at line 149 of file Tax.php.

150  {
151  $total->setTotalAmount('subtotal', 0);
152  $total->setBaseTotalAmount('subtotal', 0);
153  $total->setTotalAmount('tax', 0);
154  $total->setBaseTotalAmount('tax', 0);
155  $total->setTotalAmount('shipping', 0);
156  $total->setBaseTotalAmount('shipping', 0);
157  $total->setTotalAmount('discount_tax_compensation', 0);
158  $total->setBaseTotalAmount('discount_tax_compensation', 0);
159  $total->setTotalAmount('shipping_discount_tax_compensation', 0);
160  $total->setBaseTotalAmount('shipping_discount_tax_compensation', 0);
161  $total->setSubtotalInclTax(0);
162  $total->setBaseSubtotalInclTax(0);
163  $total->setShippingInclTax(0);
164  $total->setBaseShippingInclTax(0);
165  $total->setShippingTaxAmount(0);
166  $total->setBaseShippingTaxAmount(0);
167  $total->setShippingAmountForDiscount(0);
168  $total->setBaseShippingAmountForDiscount(0);
169  $total->setBaseShippingAmountForDiscount(0);
170  $total->setTotalAmount('extra_tax', 0);
171  $total->setBaseTotalAmount('extra_tax', 0);
172  }

◆ collect()

Collect tax totals for quote address

Parameters
\Magento\Quote\Model\Quote$quote
ShippingAssignmentInterface$shippingAssignment
Address\Total$total
Returns
$this @SuppressWarnings(PHPMD.UnusedFormalParameter)

Definition at line 103 of file Tax.php.

107  {
108  $this->clearValues($total);
109  if (!$shippingAssignment->getItems()) {
110  return $this;
111  }
112 
113  $baseTaxDetails = $this->getQuoteTaxDetails($shippingAssignment, $total, true);
114  $taxDetails = $this->getQuoteTaxDetails($shippingAssignment, $total, false);
115 
116  //Populate address and items with tax calculation results
117  $itemsByType = $this->organizeItemTaxDetailsByType($taxDetails, $baseTaxDetails);
118  if (isset($itemsByType[self::ITEM_TYPE_PRODUCT])) {
119  $this->processProductItems($shippingAssignment, $itemsByType[self::ITEM_TYPE_PRODUCT], $total);
120  }
121 
122  if (isset($itemsByType[self::ITEM_TYPE_SHIPPING])) {
123  $shippingTaxDetails = $itemsByType[self::ITEM_TYPE_SHIPPING][self::ITEM_CODE_SHIPPING][self::KEY_ITEM];
124  $baseShippingTaxDetails =
126  $this->processShippingTaxInfo($shippingAssignment, $total, $shippingTaxDetails, $baseShippingTaxDetails);
127  }
128 
129  //Process taxable items that are not product or shipping
130  $this->processExtraTaxables($total, $itemsByType);
131 
132  //Save applied taxes for each item and the quote in aggregation
133  $this->processAppliedTaxes($total, $shippingAssignment, $itemsByType);
134 
135  if ($this->includeExtraTax()) {
136  $total->addTotalAmount('extra_tax', $total->getExtraTaxAmount());
137  $total->addBaseTotalAmount('extra_tax', $total->getBaseExtraTaxAmount());
138  }
139 
140  return $this;
141  }
clearValues(Address\Total $total)
Definition: Tax.php:149
getQuoteTaxDetails($shippingAssignment, $total, $useBaseCurrency)
Definition: Tax.php:182
processShippingTaxInfo(ShippingAssignmentInterface $shippingAssignment, QuoteAddress\Total $total, $shippingTaxDetails, $baseShippingTaxDetails)

◆ fetch()

Add tax totals information to address object

Parameters
\Magento\Quote\Model\Quote$quote
Address\Total$total
Returns
array|null @SuppressWarnings(PHPMD.CyclomaticComplexity) @SuppressWarnings(PHPMD.NPathComplexity)

Modify subtotal

Definition at line 318 of file Tax.php.

319  {
320  $totals = [];
321  $store = $quote->getStore();
322  $applied = $total->getAppliedTaxes();
323  if (is_string($applied)) {
324  $applied = $this->serializer->unserialize($applied);
325  }
326  $amount = $total->getTaxAmount();
327  if ($amount === null) {
328  $this->enhanceTotalData($quote, $total);
329  $amount = $total->getTaxAmount();
330  }
331  $taxAmount = $amount + $total->getTotalAmount('discount_tax_compensation');
332 
333  $area = null;
334  if ($this->_config->displayCartTaxWithGrandTotal($store) && $total->getGrandTotal()) {
335  $area = 'taxes';
336  }
337 
338  $totals[] = [
339  'code' => $this->getCode(),
340  'title' => __('Tax'),
341  'full_info' => $applied ? $applied : [],
342  'value' => $amount,
343  'area' => $area,
344  ];
345 
349  if ($this->_config->displayCartSubtotalBoth($store) || $this->_config->displayCartSubtotalInclTax($store)) {
350  if ($total->getSubtotalInclTax() > 0) {
351  $subtotalInclTax = $total->getSubtotalInclTax();
352  } else {
353  $subtotalInclTax = $total->getSubtotal() + $taxAmount - $total->getShippingTaxAmount();
354  }
355 
356  $totals[] = [
357  'code' => 'subtotal',
358  'title' => __('Subtotal'),
359  'value' => $subtotalInclTax,
360  'value_incl_tax' => $subtotalInclTax,
361  'value_excl_tax' => $total->getSubtotal(),
362  ];
363  }
364 
365  if (empty($totals)) {
366  return null;
367  }
368  return $totals;
369  }
$quote
__()
Definition: __.php:13
$amount
Definition: order.php:14
$totals
Definition: totalbar.phtml:10

◆ getLabel()

getLabel ( )

Get Tax label

Returns
\Magento\Framework\Phrase

Definition at line 436 of file Tax.php.

437  {
438  return __('Tax');
439  }
__()
Definition: __.php:13

◆ getQuoteTaxDetails()

getQuoteTaxDetails (   $shippingAssignment,
  $total,
  $useBaseCurrency 
)
protected

Call tax calculation service to get tax details on the quote and items

Parameters
ShippingAssignmentInterface$shippingAssignment
Address\Total$total
bool$useBaseCurrency
Returns
\Magento\Tax\Api\Data\TaxDetailsInterface

Definition at line 182 of file Tax.php.

183  {
184  $address = $shippingAssignment->getShipping()->getAddress();
185  //Setup taxable items
186  $priceIncludesTax = $this->_config->priceIncludesTax($address->getQuote()->getStore());
187  $itemDataObjects = $this->mapItems($shippingAssignment, $priceIncludesTax, $useBaseCurrency);
188 
189  //Add shipping
190  $shippingDataObject = $this->getShippingDataObject($shippingAssignment, $total, $useBaseCurrency);
191  if ($shippingDataObject != null) {
192  $itemDataObjects[] = $shippingDataObject;
193  }
194 
195  //process extra taxable items associated only with quote
196  $quoteExtraTaxables = $this->mapQuoteExtraTaxables(
197  $this->quoteDetailsItemDataObjectFactory,
198  $address,
199  $useBaseCurrency
200  );
201  if (!empty($quoteExtraTaxables)) {
202  $itemDataObjects = array_merge($itemDataObjects, $quoteExtraTaxables);
203  }
204 
205  //Preparation for calling taxCalculationService
206  $quoteDetails = $this->prepareQuoteDetails($shippingAssignment, $itemDataObjects);
207 
208  $taxDetails = $this->taxCalculationService
209  ->calculateTax($quoteDetails, $address->getQuote()->getStore()->getStoreId());
210 
211  return $taxDetails;
212  }
$address
Definition: customer.php:38
mapItems(ShippingAssignmentInterface $shippingAssignment, $priceIncludesTax, $useBaseCurrency)
mapQuoteExtraTaxables(\Magento\Tax\Api\Data\QuoteDetailsItemInterfaceFactory $itemDataObjectFactory, Address $address, $useBaseCurrency)
Definition: Tax.php:222
prepareQuoteDetails(ShippingAssignmentInterface $shippingAssignment, $itemDataObjects)

◆ mapQuoteExtraTaxables()

mapQuoteExtraTaxables ( \Magento\Tax\Api\Data\QuoteDetailsItemInterfaceFactory  $itemDataObjectFactory,
Address  $address,
  $useBaseCurrency 
)

Map extra taxables associated with quote

Parameters
\Magento\Tax\Api\Data\QuoteDetailsItemInterfaceFactory$itemDataObjectFactory
Address$address
bool$useBaseCurrency
Returns
\Magento\Tax\Api\Data\QuoteDetailsItemInterface[]

Definition at line 222 of file Tax.php.

226  {
227  $itemDataObjects = [];
228  $extraTaxables = $address->getAssociatedTaxables();
229  if (!$extraTaxables) {
230  return [];
231  }
232 
233  foreach ($extraTaxables as $extraTaxable) {
234  if ($useBaseCurrency) {
235  $unitPrice = $extraTaxable[self::KEY_ASSOCIATED_TAXABLE_BASE_UNIT_PRICE];
236  } else {
237  $unitPrice = $extraTaxable[self::KEY_ASSOCIATED_TAXABLE_UNIT_PRICE];
238  }
239  $itemDataObjects[] = $itemDataObjectFactory->create()
240  ->setCode($extraTaxable[self::KEY_ASSOCIATED_TAXABLE_CODE])
241  ->setType($extraTaxable[self::KEY_ASSOCIATED_TAXABLE_TYPE])
242  ->setQuantity($extraTaxable[self::KEY_ASSOCIATED_TAXABLE_QUANTITY])
243  ->setTaxClassKey(
244  $this->taxClassKeyDataObjectFactory->create()
246  ->setValue($extraTaxable[self::KEY_ASSOCIATED_TAXABLE_TAX_CLASS_ID])
247  )
248  ->setUnitPrice($unitPrice)
249  ->setIsTaxIncluded($extraTaxable[self::KEY_ASSOCIATED_TAXABLE_PRICE_INCLUDES_TAX])
250  ->setAssociatedItemCode($extraTaxable[self::KEY_ASSOCIATED_TAXABLE_ASSOCIATION_ITEM_CODE]);
251  }
252 
253  return $itemDataObjects;
254  }
$address
Definition: customer.php:38

◆ processConfigArray()

processConfigArray (   $config,
  $store 
)

Process model configuration array. This method can be used for changing totals collect sort order

Parameters
array$config
store$store
Returns
array

Definition at line 417 of file Tax.php.

418  {
419  $calculationSequence = $this->_taxData->getCalculationSequence($store);
420  switch ($calculationSequence) {
422  $config['before'][] = 'discount';
423  break;
424  default:
425  $config['after'][] = 'discount';
426  break;
427  }
428  return $config;
429  }
$config
Definition: fraud_order.php:17

Field Documentation

◆ $_config

$_config
protected

Definition at line 42 of file Tax.php.

◆ $_discountTaxCompensationes

$_discountTaxCompensationes = []
protected

Definition at line 49 of file Tax.php.

◆ $_taxData

$_taxData
protected

Definition at line 35 of file Tax.php.

◆ $counter

$counter = 0
protected

Definition at line 28 of file Tax.php.


The documentation for this class was generated from the following file: