Shipping.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Tax\Model\Sales\Total\Quote;
  8. use Magento\Quote\Model\Quote\Address;
  9. use Magento\Quote\Api\Data\ShippingAssignmentInterface;
  10. class Shipping extends CommonTaxCollector
  11. {
  12. /**
  13. * Collect tax totals for shipping. The result can be used to calculate discount on shipping
  14. *
  15. * @param \Magento\Quote\Model\Quote $quote
  16. * @param ShippingAssignmentInterface $shippingAssignment
  17. * @param Address\Total $total
  18. * @return $this
  19. */
  20. public function collect(
  21. \Magento\Quote\Model\Quote $quote,
  22. \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
  23. \Magento\Quote\Model\Quote\Address\Total $total
  24. ) {
  25. $storeId = $quote->getStoreId();
  26. $items = $shippingAssignment->getItems();
  27. if (!$items) {
  28. return $this;
  29. }
  30. //Add shipping
  31. $shippingDataObject = $this->getShippingDataObject($shippingAssignment, $total, false);
  32. $baseShippingDataObject = $this->getShippingDataObject($shippingAssignment, $total, true);
  33. if ($shippingDataObject == null || $baseShippingDataObject == null) {
  34. return $this;
  35. }
  36. $quoteDetails = $this->prepareQuoteDetails($shippingAssignment, [$shippingDataObject]);
  37. $taxDetails = $this->taxCalculationService
  38. ->calculateTax($quoteDetails, $storeId);
  39. $taxDetailsItems = $taxDetails->getItems()[self::ITEM_CODE_SHIPPING];
  40. $baseQuoteDetails = $this->prepareQuoteDetails($shippingAssignment, [$baseShippingDataObject]);
  41. $baseTaxDetails = $this->taxCalculationService
  42. ->calculateTax($baseQuoteDetails, $storeId);
  43. $baseTaxDetailsItems = $baseTaxDetails->getItems()[self::ITEM_CODE_SHIPPING];
  44. $quote->getShippingAddress()
  45. ->setShippingAmount($taxDetailsItems->getRowTotal());
  46. $quote->getShippingAddress()
  47. ->setBaseShippingAmount($baseTaxDetailsItems->getRowTotal());
  48. $this->processShippingTaxInfo(
  49. $shippingAssignment,
  50. $total,
  51. $taxDetailsItems,
  52. $baseTaxDetailsItems
  53. );
  54. return $this;
  55. }
  56. /**
  57. * @param \Magento\Quote\Model\Quote $quote
  58. * @param Address\Total $total
  59. * @return array|null
  60. *
  61. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  62. */
  63. public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
  64. {
  65. if ($total->getShippingInclTax()) {
  66. return [
  67. 'code' => 'shipping',
  68. 'shipping_incl_tax' => $total->getShippingInclTax()
  69. ];
  70. }
  71. return null;
  72. }
  73. }