OrderShippingProcessor.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace Vertex\Tax\Model\Api\Data\InvoiceRequestBuilder;
  3. use Magento\Sales\Api\Data\OrderExtensionInterface;
  4. use Magento\Sales\Api\Data\OrderInterface;
  5. use Magento\Sales\Api\Data\ShippingAssignmentInterface;
  6. use Magento\Sales\Api\Data\ShippingInterface;
  7. use Magento\Sales\Api\Data\TotalInterface;
  8. use Vertex\Data\LineItemInterfaceFactory;
  9. use Vertex\Services\Invoice\RequestInterface;
  10. use Vertex\Tax\Model\Config;
  11. use Vertex\Tax\Model\Repository\TaxClassNameRepository;
  12. /**
  13. * Processes Shipping on an Order and adds it to an Invoice Request's LineItems
  14. */
  15. class OrderShippingProcessor implements OrderProcessorInterface
  16. {
  17. /** @var TaxClassNameRepository */
  18. private $classNameRepository;
  19. /** @var Config */
  20. private $config;
  21. /** @var LineItemInterfaceFactory */
  22. private $lineItemFactory;
  23. /**
  24. * @param Config $config
  25. * @param TaxClassNameRepository $classNameRepository
  26. * @param LineItemInterfaceFactory $lineItemFactory
  27. */
  28. public function __construct(
  29. Config $config,
  30. TaxClassNameRepository $classNameRepository,
  31. LineItemInterfaceFactory $lineItemFactory
  32. ) {
  33. $this->config = $config;
  34. $this->classNameRepository = $classNameRepository;
  35. $this->lineItemFactory = $lineItemFactory;
  36. }
  37. /**
  38. * @inheritdoc
  39. */
  40. public function process(RequestInterface $request, OrderInterface $order)
  41. {
  42. $lineItems = $request->getLineItems();
  43. $extensionAttributes = $order->getExtensionAttributes();
  44. if ($extensionAttributes === null || !$extensionAttributes instanceof OrderExtensionInterface) {
  45. return $request;
  46. }
  47. /** @var ShippingAssignmentInterface[]|null $shippingAssignments */
  48. $shippingAssignments = $extensionAttributes->getShippingAssignments();
  49. if ($shippingAssignments === null) {
  50. return $request;
  51. }
  52. // Pre-fetch the shipping tax class since all shipment types have the same one
  53. $taxClassId = $this->config->getShippingTaxClassId($order->getStoreId());
  54. $productClass = $this->classNameRepository->getById($taxClassId);
  55. foreach ($shippingAssignments as $shippingAssignment) {
  56. // This just gathers those variables
  57. $shipping = $shippingAssignment->getShipping();
  58. if ($shipping === null || !$shipping instanceof ShippingInterface) {
  59. continue;
  60. }
  61. $total = $shipping->getTotal();
  62. if ($total === null || !$total instanceof TotalInterface) {
  63. continue;
  64. }
  65. $cost = $total->getBaseShippingAmount() - $total->getBaseShippingDiscountAmount();
  66. $lineItem = $this->lineItemFactory->create();
  67. $lineItem->setProductCode($shipping->getMethod());
  68. $lineItem->setProductClass($productClass);
  69. $lineItem->setUnitPrice($cost);
  70. $lineItem->setQuantity(1);
  71. $lineItem->setExtendedPrice($cost);
  72. $lineItems[] = $lineItem;
  73. }
  74. $request->setLineItems($lineItems);
  75. return $request;
  76. }
  77. }