Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Data.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Tax\Helper;
7 
19 
29 {
33  const CONFIG_DEFAULT_CUSTOMER_TAX_CLASS = 'tax/classes/default_customer_tax_class';
34 
38  const CONFIG_DEFAULT_PRODUCT_TAX_CLASS = 'tax/classes/default_product_tax_class';
39 
45  protected $_config;
46 
52  protected $_postCodeSubStringLength = 10;
53 
59  protected $jsonHelper;
60 
64  protected $_storeManager;
65 
69  protected $_localeFormat;
70 
75 
79  protected $_localeResolver;
80 
84  protected $catalogHelper;
85 
90 
94  protected $priceCurrency;
95 
99  private $serializer;
100 
117  public function __construct(
118  \Magento\Framework\App\Helper\Context $context,
119  \Magento\Framework\Json\Helper\Data $jsonHelper,
120  Config $taxConfig,
121  \Magento\Store\Model\StoreManagerInterface $storeManager,
122  \Magento\Framework\Locale\FormatInterface $localeFormat,
123  \Magento\Tax\Model\ResourceModel\Sales\Order\Tax\CollectionFactory $orderTaxCollectionFactory,
124  \Magento\Framework\Locale\ResolverInterface $localeResolver,
125  \Magento\Catalog\Helper\Data $catalogHelper,
128  Json $serializer = null
129  ) {
130  parent::__construct($context);
131  $this->priceCurrency = $priceCurrency;
132  $this->_config = $taxConfig;
133  $this->jsonHelper = $jsonHelper;
134  $this->_storeManager = $storeManager;
135  $this->_localeFormat = $localeFormat;
136  $this->_orderTaxCollectionFactory = $orderTaxCollectionFactory;
137  $this->_localeResolver = $localeResolver;
138  $this->catalogHelper = $catalogHelper;
139  $this->orderTaxManagement = $orderTaxManagement;
140  $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
141  }
142 
148  public function getPostCodeSubStringLength()
149  {
150  $len = (int) $this->_postCodeSubStringLength;
151  if ($len <= 0) {
152  $len = 10;
153  }
154 
155  return $len;
156  }
157 
163  public function getConfig()
164  {
165  return $this->_config;
166  }
167 
174  public function priceIncludesTax($store = null)
175  {
176  return $this->_config->priceIncludesTax($store) || $this->_config->getNeedUseShippingExcludeTax();
177  }
178 
185  public function applyTaxAfterDiscount($store = null)
186  {
187  return $this->_config->applyTaxAfterDiscount($store);
188  }
189 
199  public function getPriceDisplayType($store = null)
200  {
201  return $this->_config->getPriceDisplayType($store);
202  }
203 
211  public function needPriceConversion($store = null)
212  {
213  return $this->_config->needPriceConversion($store);
214  }
215 
222  public function displayFullSummary($store = null)
223  {
224  return $this->_config->displayCartFullSummary($store);
225  }
226 
233  public function displayZeroTax($store = null)
234  {
235  return $this->_config->displayCartZeroTax($store);
236  }
237 
244  public function displayCartPriceInclTax($store = null)
245  {
246  return $this->_config->displayCartPricesInclTax($store);
247  }
248 
255  public function displayCartPriceExclTax($store = null)
256  {
257  return $this->_config->displayCartPricesExclTax($store);
258  }
259 
266  public function displayCartBothPrices($store = null)
267  {
268  return $this->_config->displayCartPricesBoth($store);
269  }
270 
277  public function displaySalesPriceInclTax($store = null)
278  {
279  return $this->_config->displaySalesPricesInclTax($store);
280  }
281 
288  public function displaySalesPriceExclTax($store = null)
289  {
290  return $this->_config->displaySalesPricesExclTax($store);
291  }
292 
299  public function displaySalesBothPrices($store = null)
300  {
301  return $this->_config->displaySalesPricesBoth($store);
302  }
303 
310  public function displaySalesSubtotalBoth($store = null)
311  {
312  return $this->_config->displaySalesSubtotalBoth($store);
313  }
314 
321  public function displaySalesSubtotalInclTax($store = null)
322  {
323  return $this->_config->displaySalesSubtotalInclTax($store);
324  }
325 
332  public function displaySalesSubtotalExclTax($store = null)
333  {
334  return $this->_config->displaySalesSubtotalExclTax($store);
335  }
336 
343  public function getPriceFormat($store = null)
344  {
345  $this->_localeResolver->emulate($store);
346  $priceFormat = $this->_localeFormat->getPriceFormat();
347  $this->_localeResolver->revert();
348  if ($store) {
349  $priceFormat['pattern'] = $this->_storeManager->getStore($store)->getCurrentCurrency()->getOutputFormat();
350  }
351 
352  return $this->jsonHelper->jsonEncode($priceFormat);
353  }
354 
360  public function displayPriceIncludingTax()
361  {
363  }
364 
370  public function displayPriceExcludingTax()
371  {
373  }
374 
381  public function displayBothPrices($store = null)
382  {
384  }
385 
392  public function shippingPriceIncludesTax($store = null)
393  {
394  return $this->_config->shippingPriceIncludesTax($store);
395  }
396 
403  public function getShippingPriceDisplayType($store = null)
404  {
405  return $this->_config->getShippingPriceDisplayType($store);
406  }
407 
414  {
416  }
417 
424  {
426  }
427 
433  public function displayShippingBothPrices()
434  {
436  }
437 
444  public function getShippingTaxClass($store)
445  {
446  return $this->_config->getShippingTaxClass($store);
447  }
448 
459  public function getShippingPrice($price, $includingTax = null, $shippingAddress = null, $ctc = null, $store = null)
460  {
461  $pseudoProduct = new \Magento\Framework\DataObject();
462  $pseudoProduct->setTaxClassId($this->getShippingTaxClass($store));
463 
464  $billingAddress = false;
465  if ($shippingAddress && $shippingAddress->getQuote() && $shippingAddress->getQuote()->getBillingAddress()) {
466  $billingAddress = $shippingAddress->getQuote()->getBillingAddress();
467  }
468 
469  $price = $this->catalogHelper->getTaxPrice(
470  $pseudoProduct,
471  $price,
472  $includingTax,
475  $ctc,
476  $store,
478  );
479 
480  return $price;
481  }
482 
489  public function discountTax($store = null)
490  {
491  return $this->_config->discountTax($store);
492  }
493 
500  public function getTaxBasedOn($store = null)
501  {
502  return $this->scopeConfig->getValue(
505  $store
506  );
507  }
508 
515  public function applyTaxOnCustomPrice($store = null)
516  {
517  return (int) $this->scopeConfig->getValue(
520  $store
521  ) == 0;
522  }
523 
530  public function applyTaxOnOriginalPrice($store = null)
531  {
532  return (int) $this->scopeConfig->getValue(
535  $store
536  ) == 1;
537  }
538 
548  public function getCalculationSequence($store = null)
549  {
550  return $this->_config->getCalculationSequence($store);
551  }
552 
559  public function getCalculationAlgorithm($store = null)
560  {
561  return $this->_config->getAlgorithm($store);
562  }
563 
581  public function getCalculatedTaxes($source)
582  {
583  $taxClassAmount = [];
584  if (empty($source)) {
585  return $taxClassAmount;
586  }
587  $current = $source;
588  if ($source instanceof Invoice || $source instanceof Creditmemo) {
589  $source = $current->getOrder();
590  }
591  if ($current == $source) {
592  $taxClassAmount = $this->calculateTaxForOrder($current);
593  } else {
594  $taxClassAmount = $this->calculateTaxForItems($source, $current);
595  }
596 
597  foreach ($taxClassAmount as $key => $tax) {
598  $taxClassAmount[$key]['tax_amount'] = $this->priceCurrency->round($tax['tax_amount']);
599  $taxClassAmount[$key]['base_tax_amount'] = $this->priceCurrency->round($tax['base_tax_amount']);
600  }
601 
602  return array_values($taxClassAmount);
603  }
604 
623  private function _aggregateTaxes($taxClassAmount, OrderTaxDetailsItemInterface $itemTaxDetail, $ratio)
624  {
625  $itemAppliedTaxes = $itemTaxDetail->getAppliedTaxes();
626  foreach ($itemAppliedTaxes as $itemAppliedTax) {
627  $taxAmount = $itemAppliedTax->getAmount() * $ratio;
628  $baseTaxAmount = $itemAppliedTax->getBaseAmount() * $ratio;
629 
630  if (0 == $taxAmount && 0 == $baseTaxAmount) {
631  continue;
632  }
633  $taxCode = $itemAppliedTax->getCode();
634  if (!isset($taxClassAmount[$taxCode])) {
635  $taxClassAmount[$taxCode]['title'] = $itemAppliedTax->getTitle();
636  $taxClassAmount[$taxCode]['percent'] = $itemAppliedTax->getPercent();
637  $taxClassAmount[$taxCode]['tax_amount'] = $taxAmount;
638  $taxClassAmount[$taxCode]['base_tax_amount'] = $baseTaxAmount;
639  } else {
640  $taxClassAmount[$taxCode]['tax_amount'] += $taxAmount;
641  $taxClassAmount[$taxCode]['base_tax_amount'] += $baseTaxAmount;
642  }
643  }
644 
645  return $taxClassAmount;
646  }
647 
654  protected function _getTaxRateSubtotals($order)
655  {
656  return $this->_orderTaxCollectionFactory->create()->loadByOrder($order)->toArray();
657  }
658 
664  public function getDefaultCustomerTaxClass()
665  {
666  return $this->scopeConfig->getValue(
667  self::CONFIG_DEFAULT_CUSTOMER_TAX_CLASS,
669  );
670  }
671 
677  public function getDefaultProductTaxClass()
678  {
679  return $this->scopeConfig->getValue(
680  self::CONFIG_DEFAULT_PRODUCT_TAX_CLASS,
682  );
683  }
684 
691  public function isCrossBorderTradeEnabled($store = null)
692  {
693  return (bool) $this->_config->crossBorderTradeEnabled($store);
694  }
695 
700  protected function calculateTaxForOrder(EntityInterface $current)
701  {
702  $taxClassAmount = [];
703 
704  $orderTaxDetails = $this->orderTaxManagement->getOrderTaxDetails($current->getId());
705  $appliedTaxes = $orderTaxDetails->getAppliedTaxes();
706  foreach ($appliedTaxes as $appliedTax) {
707  $taxCode = $appliedTax->getCode();
708  $taxClassAmount[$taxCode]['tax_amount'] = $appliedTax->getAmount();
709  $taxClassAmount[$taxCode]['base_tax_amount'] = $appliedTax->getBaseAmount();
710  $taxClassAmount[$taxCode]['title'] = $appliedTax->getTitle();
711  $taxClassAmount[$taxCode]['percent'] = $appliedTax->getPercent();
712  }
713 
714  return $taxClassAmount;
715  }
716 
723  protected function calculateTaxForItems(EntityInterface $order, EntityInterface $salesItem)
724  {
725  $taxClassAmount = [];
726 
727  $orderTaxDetails = $this->orderTaxManagement->getOrderTaxDetails($order->getId());
728 
729  // Apply any taxes for the items
731  foreach ($salesItem->getItems() as $item) {
732  $orderItem = $item->getOrderItem();
733  $orderItemId = $orderItem->getId();
734  $orderItemTax = $orderItem->getTaxAmount();
735  $itemTax = $item->getTaxAmount();
736  if (!$itemTax || !(float)$orderItemTax) {
737  continue;
738  }
739  //An invoiced item or credit memo item can have a different qty than its order item qty
740  $itemRatio = $itemTax / $orderItemTax;
741  $itemTaxDetails = $orderTaxDetails->getItems();
742  foreach ($itemTaxDetails as $itemTaxDetail) {
743  //Aggregate taxable items associated with an item
744  if ($itemTaxDetail->getItemId() == $orderItemId) {
745  $taxClassAmount = $this->_aggregateTaxes($taxClassAmount, $itemTaxDetail, $itemRatio);
746  } elseif ($itemTaxDetail->getAssociatedItemId() == $orderItemId) {
747  $taxableItemType = $itemTaxDetail->getType();
748  $ratio = $itemRatio;
749  if ($item->getTaxRatio()) {
750  $taxRatio = $this->serializer->unserialize($item->getTaxRatio());
751  if (isset($taxRatio[$taxableItemType])) {
752  $ratio = $taxRatio[$taxableItemType];
753  }
754  }
755  $taxClassAmount = $this->_aggregateTaxes($taxClassAmount, $itemTaxDetail, $ratio);
756  }
757  }
758  }
759 
760  // Apply any taxes for shipping
761  $shippingTaxAmount = $salesItem->getShippingTaxAmount();
762  $originalShippingTaxAmount = $order->getShippingTaxAmount();
763  if ($shippingTaxAmount && $originalShippingTaxAmount &&
764  $shippingTaxAmount != 0 && (float)$originalShippingTaxAmount
765  ) {
766  //An invoice or credit memo can have a different qty than its order
767  $shippingRatio = $shippingTaxAmount / $originalShippingTaxAmount;
768  $itemTaxDetails = $orderTaxDetails->getItems();
769  foreach ($itemTaxDetails as $itemTaxDetail) {
770  //Aggregate taxable items associated with shipping
771  if ($itemTaxDetail->getType() == \Magento\Quote\Model\Quote\Address::TYPE_SHIPPING) {
772  $taxClassAmount = $this->_aggregateTaxes($taxClassAmount, $itemTaxDetail, $shippingRatio);
773  }
774  }
775  }
776 
777  return $taxClassAmount;
778  }
779 
787  {
788  if ($this->displayBothPrices($store)) {
789  return true;
790  }
791 
792  $priceInclTax = $this->priceIncludesTax($store);
793  if ($priceInclTax) {
794  return ($this->isCrossBorderTradeEnabled($store) xor $this->displayPriceIncludingTax());
795  } else {
796  return $this->displayPriceIncludingTax();
797  }
798  }
799 }
const CONFIG_DEFAULT_CUSTOMER_TAX_CLASS
Definition: Data.php:33
displayCartPriceExclTax($store=null)
Definition: Data.php:255
isCatalogPriceDisplayAffectedByTax($store=null)
Definition: Data.php:786
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$billingAddress
Definition: order.php:25
const CONFIG_XML_PATH_APPLY_ON
Definition: Config.php:40
displaySalesPriceExclTax($store=null)
Definition: Data.php:288
$orderItem
Definition: order.php:30
displaySalesSubtotalExclTax($store=null)
Definition: Data.php:332
getPriceFormat($store=null)
Definition: Data.php:343
$source
Definition: source.php:23
$shippingAddress
Definition: order.php:40
$order
Definition: order.php:55
$storeManager
displayBothPrices($store=null)
Definition: Data.php:381
_getTaxRateSubtotals($order)
Definition: Data.php:654
$price
const CONFIG_DEFAULT_PRODUCT_TAX_CLASS
Definition: Data.php:38
getCalculationAlgorithm($store=null)
Definition: Data.php:559
priceIncludesTax($store=null)
Definition: Data.php:174
getCalculationSequence($store=null)
Definition: Data.php:548
const DISPLAY_TYPE_EXCLUDING_TAX
Definition: Config.php:102
const DISPLAY_TYPE_INCLUDING_TAX
Definition: Config.php:104
discountTax($store=null)
Definition: Data.php:489
displayShippingPriceIncludingTax()
Definition: Data.php:413
applyTaxOnCustomPrice($store=null)
Definition: Data.php:515
getShippingTaxClass($store)
Definition: Data.php:444
getShippingPriceDisplayType($store=null)
Definition: Data.php:403
getShippingPrice($price, $includingTax=null, $shippingAddress=null, $ctc=null, $store=null)
Definition: Data.php:459
getPriceDisplayType($store=null)
Definition: Data.php:199
getCalculatedTaxes($source)
Definition: Data.php:581
displaySalesSubtotalInclTax($store=null)
Definition: Data.php:321
displayCartBothPrices($store=null)
Definition: Data.php:266
displaySalesSubtotalBoth($store=null)
Definition: Data.php:310
getTaxBasedOn($store=null)
Definition: Data.php:500
__construct(\Magento\Framework\App\Helper\Context $context, \Magento\Framework\Json\Helper\Data $jsonHelper, Config $taxConfig, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Locale\FormatInterface $localeFormat, \Magento\Tax\Model\ResourceModel\Sales\Order\Tax\CollectionFactory $orderTaxCollectionFactory, \Magento\Framework\Locale\ResolverInterface $localeResolver, \Magento\Catalog\Helper\Data $catalogHelper, OrderTaxManagementInterface $orderTaxManagement, PriceCurrencyInterface $priceCurrency, Json $serializer=null)
Definition: Data.php:117
displayCartPriceInclTax($store=null)
Definition: Data.php:244
const CONFIG_XML_PATH_BASED_ON
Definition: Config.php:38
displayFullSummary($store=null)
Definition: Data.php:222
displayShippingPriceExcludingTax()
Definition: Data.php:423
displayZeroTax($store=null)
Definition: Data.php:233
shippingPriceIncludesTax($store=null)
Definition: Data.php:392
isCrossBorderTradeEnabled($store=null)
Definition: Data.php:691
needPriceConversion($store=null)
Definition: Data.php:211
applyTaxOnOriginalPrice($store=null)
Definition: Data.php:530
calculateTaxForOrder(EntityInterface $current)
Definition: Data.php:700
displaySalesBothPrices($store=null)
Definition: Data.php:299
displaySalesPriceInclTax($store=null)
Definition: Data.php:277
applyTaxAfterDiscount($store=null)
Definition: Data.php:185