ShippingAssignmentExtensionLoader.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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\Observer;
  7. use Magento\Sales\Api\Data\OrderExtensionInterface;
  8. use Magento\Sales\Api\Data\ShippingInterface;
  9. use Magento\Sales\Api\Data\TotalInterface;
  10. use Magento\Sales\Model\Order;
  11. use Magento\Sales\Api\Data\OrderExtensionFactory;
  12. use Magento\Sales\Api\Data\ShippingInterfaceFactory;
  13. use Magento\Sales\Api\Data\TotalInterfaceFactory;
  14. use Magento\Sales\Api\Data\ShippingAssignmentInterfaceFactory;
  15. use Magento\Sales\Api\Data\ShippingAssignmentInterface;
  16. /**
  17. * Create Shipping Assignment Extension Attributes for use with Vertex Invoicing
  18. *
  19. * This class is intended to be a compatibility layer, as we shouldn't need to
  20. * create these on our own. However, when Magento is placing a new order that
  21. * order does not contain the Shipping Assignment extension attributes, and
  22. * loading said order again to generate them can cause errors on some versions
  23. * of Magento.
  24. */
  25. class ShippingAssignmentExtensionLoader
  26. {
  27. /** @var ShippingAssignmentInterfaceFactory */
  28. private $assignmentFactory;
  29. /** @var OrderExtensionFactory */
  30. private $extensionFactory;
  31. /** @var ShippingInterfaceFactory */
  32. private $shippingFactory;
  33. /** @var TotalInterfaceFactory */
  34. private $totalFactory;
  35. /**
  36. * @param OrderExtensionFactory $extensionFactory
  37. * @param ShippingInterfaceFactory $shippingFactory
  38. * @param TotalInterfaceFactory $totalFactory
  39. * @param ShippingAssignmentInterfaceFactory $assignmentFactory
  40. */
  41. public function __construct(
  42. OrderExtensionFactory $extensionFactory,
  43. ShippingInterfaceFactory $shippingFactory,
  44. TotalInterfaceFactory $totalFactory,
  45. ShippingAssignmentInterfaceFactory $assignmentFactory
  46. ) {
  47. $this->extensionFactory = $extensionFactory;
  48. $this->shippingFactory = $shippingFactory;
  49. $this->totalFactory = $totalFactory;
  50. $this->assignmentFactory = $assignmentFactory;
  51. }
  52. /**
  53. * Create Shipping and ShippingAssignment objects for an Order
  54. *
  55. * @param Order $order
  56. * @return Order
  57. */
  58. public function loadOnOrder(Order $order)
  59. {
  60. if ($order->getExtensionAttributes() && $order->getExtensionAttributes()->getShippingAssignments()) {
  61. return $order;
  62. }
  63. /** @var OrderExtensionInterface $extensionAttributes */
  64. $extensionAttributes = $order->getExtensionAttributes() ?: $this->extensionFactory->create();
  65. /** @var ShippingInterface $shipping */
  66. $shipping = $this->shippingFactory->create();
  67. $shippingAddress = $order->getShippingAddress();
  68. if ($shippingAddress) {
  69. $shipping->setAddress($shippingAddress);
  70. }
  71. $shipping->setMethod($order->getShippingMethod());
  72. $shipping->setTotal($this->getShippingTotal($order));
  73. /** @var ShippingAssignmentInterface $assignment */
  74. $assignment = $this->assignmentFactory->create();
  75. $assignment->setShipping($shipping);
  76. $assignment->setItems($order->getItems());
  77. $assignment->setStockId($order->getStockId());
  78. $extensionAttributes->setShippingAssignments([$assignment]);
  79. $order->setExtensionAttributes($extensionAttributes);
  80. return $order;
  81. }
  82. /**
  83. * Retrieve the shipping total information
  84. *
  85. * Code duplicated from {@see Magento\Sales\Model\Order\ShippingBuilder::getTotal()}
  86. * Method code © Magento, Inc. All rights reserved.
  87. * Method code has been modified to take a parameter instead of relying on object state
  88. *
  89. * @param Order $order
  90. * @return TotalInterface
  91. */
  92. private function getShippingTotal(Order $order)
  93. {
  94. /** @var TotalInterface $total */
  95. $total = $this->totalFactory->create();
  96. $total->setBaseShippingAmount($order->getBaseShippingAmount());
  97. $total->setBaseShippingCanceled($order->getBaseShippingCanceled());
  98. $total->setBaseShippingDiscountAmount($order->getBaseShippingDiscountAmount());
  99. $total->setBaseShippingDiscountTaxCompensationAmnt($order->getBaseShippingDiscountTaxCompensationAmnt());
  100. $total->setBaseShippingInclTax($order->getBaseShippingInclTax());
  101. $total->setBaseShippingInvoiced($order->getBaseShippingInvoiced());
  102. $total->setBaseShippingRefunded($order->getBaseShippingRefunded());
  103. $total->setBaseShippingTaxAmount($order->getBaseShippingTaxAmount());
  104. $total->setBaseShippingTaxRefunded($order->getBaseShippingTaxRefunded());
  105. $total->setShippingAmount($order->getShippingAmount());
  106. $total->setShippingCanceled($order->getShippingCanceled());
  107. $total->setShippingDiscountAmount($order->getShippingDiscountAmount());
  108. $total->setShippingDiscountTaxCompensationAmount($order->getShippingDiscountTaxCompensationAmount());
  109. $total->setShippingInclTax($order->getShippingInclTax());
  110. $total->setShippingInvoiced($order->getShippingInvoiced());
  111. $total->setShippingRefunded($order->getShippingRefunded());
  112. $total->setShippingTaxAmount($order->getShippingTaxAmount());
  113. $total->setShippingTaxRefunded($order->getShippingTaxRefunded());
  114. return $total;
  115. }
  116. }