GrandTotalDetailsPlugin.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Model\Quote;
  7. use Magento\Quote\Api\Data\TotalSegmentExtensionFactory;
  8. use Magento\Framework\Serialize\Serializer\Json;
  9. use Magento\Framework\App\ObjectManager;
  10. class GrandTotalDetailsPlugin
  11. {
  12. /**
  13. * @var \Magento\Tax\Api\Data\GrandTotalDetailsInterfaceFactory
  14. */
  15. private $detailsFactory;
  16. /**
  17. * @var \Magento\Tax\Api\Data\GrandTotalRatesInterfaceFactory
  18. */
  19. private $ratesFactory;
  20. /**
  21. * @var TotalSegmentExtensionFactory
  22. */
  23. private $totalSegmentExtensionFactory;
  24. /**
  25. * @var \Magento\Tax\Model\Config
  26. */
  27. private $taxConfig;
  28. /**
  29. * @var string
  30. */
  31. private $code;
  32. /**
  33. * @var Json
  34. */
  35. private $serializer;
  36. /**
  37. * Constructor
  38. *
  39. * @param \Magento\Tax\Api\Data\GrandTotalDetailsInterfaceFactory $detailsFactory
  40. * @param \Magento\Tax\Api\Data\GrandTotalRatesInterfaceFactory $ratesFactory
  41. * @param TotalSegmentExtensionFactory $totalSegmentExtensionFactory
  42. * @param \Magento\Tax\Model\Config $taxConfig
  43. * @param Json $serializer
  44. */
  45. public function __construct(
  46. \Magento\Tax\Api\Data\GrandTotalDetailsInterfaceFactory $detailsFactory,
  47. \Magento\Tax\Api\Data\GrandTotalRatesInterfaceFactory $ratesFactory,
  48. TotalSegmentExtensionFactory $totalSegmentExtensionFactory,
  49. \Magento\Tax\Model\Config $taxConfig,
  50. Json $serializer
  51. ) {
  52. $this->detailsFactory = $detailsFactory;
  53. $this->ratesFactory = $ratesFactory;
  54. $this->totalSegmentExtensionFactory = $totalSegmentExtensionFactory;
  55. $this->taxConfig = $taxConfig;
  56. $this->serializer = $serializer;
  57. $this->code = 'tax';
  58. }
  59. /**
  60. * @param array $rates
  61. * @return array
  62. */
  63. protected function getRatesData($rates)
  64. {
  65. $taxRates = [];
  66. foreach ($rates as $rate) {
  67. $taxRate = $this->ratesFactory->create([]);
  68. $taxRate->setPercent($rate['percent']);
  69. $taxRate->setTitle($rate['title']);
  70. $taxRates[] = $taxRate;
  71. }
  72. return $taxRates;
  73. }
  74. /**
  75. * @param \Magento\Quote\Model\Cart\TotalsConverter $subject
  76. * @param \Magento\Quote\Api\Data\TotalSegmentInterface[] $totalSegments
  77. * @param \Magento\Quote\Model\Quote\Address\Total[] $addressTotals
  78. * @return \Magento\Quote\Api\Data\TotalSegmentInterface[]
  79. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  80. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  81. */
  82. public function afterProcess(
  83. \Magento\Quote\Model\Cart\TotalsConverter $subject,
  84. array $totalSegments,
  85. array $addressTotals = []
  86. ) {
  87. if (!array_key_exists($this->code, $addressTotals)) {
  88. return $totalSegments;
  89. }
  90. $taxes = $addressTotals['tax']->getData();
  91. if (!array_key_exists('full_info', $taxes)) {
  92. return $totalSegments;
  93. }
  94. $detailsId = 1;
  95. $finalData = [];
  96. $fullInfo = $taxes['full_info'];
  97. if (is_string($fullInfo)) {
  98. $fullInfo = $this->serializer->unserialize($fullInfo);
  99. }
  100. foreach ($fullInfo as $info) {
  101. if ((array_key_exists('hidden', $info) && $info['hidden'])
  102. || ($info['amount'] == 0 && $this->taxConfig->displayCartZeroTax())
  103. ) {
  104. continue;
  105. }
  106. $taxDetails = $this->detailsFactory->create([]);
  107. $taxDetails->setAmount($info['amount']);
  108. $taxRates = $this->getRatesData($info['rates']);
  109. $taxDetails->setRates($taxRates);
  110. $taxDetails->setGroupId($detailsId);
  111. $finalData[] = $taxDetails;
  112. $detailsId++;
  113. }
  114. $attributes = $totalSegments[$this->code]->getExtensionAttributes();
  115. if ($attributes === null) {
  116. $attributes = $this->totalSegmentExtensionFactory->create();
  117. }
  118. $attributes->setTaxGrandtotalDetails($finalData);
  119. $totalSegments[$this->code]->setExtensionAttributes($attributes);
  120. return $totalSegments;
  121. }
  122. }