TaxQuoteResponse.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * @copyright Vertex. All rights reserved. https://www.vertexinc.com/
  4. * @author Mediotype https://www.mediotype.com/
  5. */
  6. namespace Vertex\Tax\Model\TaxQuote;
  7. use Magento\Framework\DataObject;
  8. use Magento\Framework\DataObjectFactory;
  9. use Vertex\Data\LineItemInterface;
  10. use Vertex\Data\LineItemInterfaceFactory;
  11. use Vertex\Services\Quote\ResponseInterface;
  12. /**
  13. * Quotation Response object
  14. */
  15. class TaxQuoteResponse
  16. {
  17. /** @var DataObjectFactory */
  18. private $dataObjectFactory;
  19. /** @var DataObject[] */
  20. private $quoteTaxedItems;
  21. /**
  22. * @param DataObjectFactory $dataObjectFactory
  23. */
  24. public function __construct(DataObjectFactory $dataObjectFactory)
  25. {
  26. $this->dataObjectFactory = $dataObjectFactory;
  27. }
  28. /**
  29. * Get all items with tax data
  30. *
  31. * @return DataObject[]
  32. */
  33. public function getQuoteTaxedItems()
  34. {
  35. return $this->quoteTaxedItems;
  36. }
  37. /**
  38. * Parse a Quotation Request response and store it's data here
  39. *
  40. * @param ResponseInterface $response
  41. * @return $this
  42. */
  43. public function parseResponse(ResponseInterface $response)
  44. {
  45. $this->prepareQuoteTaxedItems($response->getLineItems());
  46. return $this;
  47. }
  48. /**
  49. * Prepare the taxable item data
  50. *
  51. * @param LineItemInterface[] $itemsTax
  52. */
  53. public function prepareQuoteTaxedItems(array $itemsTax)
  54. {
  55. $quoteTaxedItems = [];
  56. foreach ($itemsTax as $item) {
  57. $itemTotalTax = $item->getTotalTax() ?: 0;
  58. $taxRate = $this->getTaxRate($item);
  59. $taxPercent = $taxRate * 100;
  60. $itemId = $item->getLineItemId();
  61. if ($itemId === null) {
  62. continue;
  63. }
  64. $taxItemInfo = $this->dataObjectFactory->create();
  65. $taxItemInfo->setProductClass($this->getProductClass($item));
  66. $taxItemInfo->setProductSku($this->getProductSku($item));
  67. if ($item->getQuantity() !== null) {
  68. $taxItemInfo->setProductQty($item->getQuantity());
  69. }
  70. if ($item->getUnitPrice() !== null) {
  71. $taxItemInfo->setUnitPrice($item->getUnitPrice());
  72. }
  73. $taxItemInfo->setTaxRate($taxRate);
  74. $taxItemInfo->setTaxPercent($taxPercent);
  75. $taxItemInfo->setTaxAmount($itemTotalTax);
  76. $quoteTaxedItems[$itemId] = $taxItemInfo;
  77. }
  78. $this->quoteTaxedItems = $quoteTaxedItems;
  79. }
  80. /**
  81. * Get an item's product class
  82. *
  83. * @param LineItemInterface $item
  84. * @return string
  85. */
  86. private function getProductClass(LineItemInterface $item)
  87. {
  88. return $item->getProductClass() ?: '';
  89. }
  90. /**
  91. * Get an item's product sku
  92. *
  93. * @param LineItemInterface $item
  94. * @return string
  95. */
  96. private function getProductSku(LineItemInterface $item)
  97. {
  98. return $item->getProductCode() ?: '';
  99. }
  100. /**
  101. * Get the Tax Rate from the response
  102. *
  103. * @param LineItemInterface $item
  104. * @return float|int
  105. */
  106. private function getTaxRate(LineItemInterface $item)
  107. {
  108. $taxRate = 0;
  109. foreach ($item->getTaxes() as $tax) {
  110. $taxRate += $tax->getEffectiveRate();
  111. }
  112. return $taxRate;
  113. }
  114. }