CreditmemoItemProcessor.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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\Api\Data\InvoiceRequestBuilder;
  7. use Magento\Sales\Api\Data\CreditmemoInterface;
  8. use Magento\Sales\Api\Data\CreditmemoItemInterface;
  9. use Vertex\Data\LineItemInterface;
  10. use Vertex\Data\LineItemInterfaceFactory;
  11. use Vertex\Services\Invoice\RequestInterface;
  12. use Vertex\Tax\Model\Repository\TaxClassNameRepository;
  13. /**
  14. * Processes Items on a Creditmemo and converts them to an array of LineItemInterface
  15. */
  16. class CreditmemoItemProcessor implements CreditmemoProcessorInterface
  17. {
  18. /** @var ItemProcessor */
  19. private $itemProcessor;
  20. /** @var LineItemInterfaceFactory */
  21. private $lineItemFactory;
  22. /** @var TaxClassNameRepository */
  23. private $taxClassNameRepository;
  24. /**
  25. * @param ItemProcessor $itemProcessor
  26. * @param LineItemInterfaceFactory $lineItemFactory
  27. * @param TaxClassNameRepository $taxClassNameRepository
  28. */
  29. public function __construct(
  30. ItemProcessor $itemProcessor,
  31. LineItemInterfaceFactory $lineItemFactory,
  32. TaxClassNameRepository $taxClassNameRepository
  33. ) {
  34. $this->itemProcessor = $itemProcessor;
  35. $this->lineItemFactory = $lineItemFactory;
  36. $this->taxClassNameRepository = $taxClassNameRepository;
  37. }
  38. /**
  39. * @inheritdoc
  40. */
  41. public function process(RequestInterface $request, CreditmemoInterface $creditmemo)
  42. {
  43. /** @var CreditmemoItemInterface[] $memoItems All Creditmemo items indexed by id */
  44. $memoItems = [];
  45. /** @var int[] $productIds All Product IDs in the invoice */
  46. $productSku = [];
  47. /** @var LineItemInterface[] $lineItems Vertex SDK LineItems to be returned */
  48. $lineItems = [];
  49. foreach ($creditmemo->getItems() as $item) {
  50. if ($item->getBaseRowTotal() === null) {
  51. continue;
  52. }
  53. $memoItems[$item->getOrderItemId()] = $item;
  54. $productSku[] = $item->getSku();
  55. }
  56. $products = $this->itemProcessor->getProductsIndexedBySku($productSku);
  57. /** @var int[] $taxClasses Key is Creditmemo Item's Order Item ID, Value is Tax Class ID */
  58. $taxClasses = [];
  59. foreach ($memoItems as $item) {
  60. $product = $products[$item->getSku()];
  61. $taxClassAttribute = $product->getCustomAttribute('tax_class_id');
  62. $taxClassId = $taxClassAttribute ? $taxClassAttribute->getValue() : 0;
  63. if ($item->getBaseRowTotal() === null) {
  64. // For bundle products, the parent has a row total of NULL
  65. continue;
  66. }
  67. /** @var LineItemInterface $lineItem */
  68. $lineItem = $this->lineItemFactory->create();
  69. $lineItem->setProductCode($item->getSku());
  70. $lineItem->setQuantity($item->getQty());
  71. $lineItem->setUnitPrice(-1 * $item->getBasePrice());
  72. $lineItem->setExtendedPrice(-1 * ($item->getBaseRowTotal() - $item->getBaseDiscountAmount()));
  73. $lineItem->setLineItemId($item->getOrderItemId());
  74. $taxClasses[$item->getOrderItemId()] = $taxClassId;
  75. if ($lineItem->getExtendedPrice() == 0) {
  76. continue;
  77. }
  78. $lineItems[] = $lineItem;
  79. }
  80. /** @var string[int] $taxClassNames Tax Classes indexed by ID */
  81. $taxClassNames = $this->taxClassNameRepository->getListByIds(array_values($taxClasses));
  82. foreach ($lineItems as $lineItem) {
  83. $lineItemId = $lineItem->getLineItemId();
  84. $taxClass = $taxClasses[$lineItemId];
  85. $taxClassName = $taxClassNames[$taxClass];
  86. $lineItem->setProductClass($taxClassName);
  87. }
  88. $request->setLineItems(array_merge($request->getLineItems(), $lineItems));
  89. return $request;
  90. }
  91. }