ShippingBuilder.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\Order;
  7. use Magento\Sales\Api\Data\ShippingInterface;
  8. use Magento\Sales\Api\Data\ShippingInterfaceFactory;
  9. use Magento\Sales\Api\Data\TotalInterface;
  10. use Magento\Sales\Api\Data\TotalInterfaceFactory;
  11. use Magento\Sales\Model\Order;
  12. use Magento\Sales\Model\OrderFactory;
  13. /**
  14. * Class ShippingBuilder
  15. * @package Magento\Sales\Model\Order
  16. */
  17. class ShippingBuilder
  18. {
  19. /**
  20. * @var int|null
  21. */
  22. private $orderId = null;
  23. /**
  24. * @var Order
  25. */
  26. private $order;
  27. /**
  28. * @var OrderFactory
  29. */
  30. private $orderFactory;
  31. /**
  32. * @var ShippingInterfaceFactory
  33. */
  34. private $shippingFactory;
  35. /**
  36. * @var TotalInterfaceFactory
  37. */
  38. private $totalFactory;
  39. /**
  40. * ShippingBuilder constructor.
  41. *
  42. * @param OrderFactory $orderFactory
  43. * @param ShippingInterfaceFactory $shippingFactory
  44. * @param TotalInterfaceFactory $totalFactory
  45. */
  46. public function __construct(
  47. OrderFactory $orderFactory,
  48. ShippingInterfaceFactory $shippingFactory,
  49. TotalInterfaceFactory $totalFactory
  50. ) {
  51. $this->orderFactory = $orderFactory;
  52. $this->shippingFactory = $shippingFactory;
  53. $this->totalFactory = $totalFactory;
  54. }
  55. /**
  56. * @param int $orderId
  57. * @return void
  58. */
  59. public function setOrderId($orderId)
  60. {
  61. $this->orderId = $orderId;
  62. }
  63. /**
  64. * @return ShippingInterface|null
  65. */
  66. public function create()
  67. {
  68. $shipping = null;
  69. if ($this->getOrderId()) {
  70. $this->order = $this->orderFactory->create()->load($this->getOrderId());
  71. if ($this->order->getEntityId()) {
  72. /** @var ShippingInterface $shipping */
  73. $shipping = $this->shippingFactory->create();
  74. $shippingAddress = $this->order->getShippingAddress();
  75. if ($shippingAddress) {
  76. $shipping->setAddress($shippingAddress);
  77. }
  78. $shipping->setMethod($this->order->getShippingMethod());
  79. $shipping->setTotal($this->getTotal());
  80. }
  81. }
  82. return $shipping;
  83. }
  84. /**
  85. * @return int|null
  86. */
  87. private function getOrderId()
  88. {
  89. return $this->orderId;
  90. }
  91. /**
  92. * @return TotalInterface
  93. */
  94. private function getTotal()
  95. {
  96. /** @var TotalInterface $total */
  97. $total = $this->totalFactory->create();
  98. $total->setBaseShippingAmount($this->order->getBaseShippingAmount());
  99. $total->setBaseShippingCanceled($this->order->getBaseShippingCanceled());
  100. $total->setBaseShippingDiscountAmount($this->order->getBaseShippingDiscountAmount());
  101. $total->setBaseShippingDiscountTaxCompensationAmnt($this->order->getBaseShippingDiscountTaxCompensationAmnt());
  102. $total->setBaseShippingInclTax($this->order->getBaseShippingInclTax());
  103. $total->setBaseShippingInvoiced($this->order->getBaseShippingInvoiced());
  104. $total->setBaseShippingRefunded($this->order->getBaseShippingRefunded());
  105. $total->setBaseShippingTaxAmount($this->order->getBaseShippingTaxAmount());
  106. $total->setBaseShippingTaxRefunded($this->order->getBaseShippingTaxRefunded());
  107. $total->setShippingAmount($this->order->getShippingAmount());
  108. $total->setShippingCanceled($this->order->getShippingCanceled());
  109. $total->setShippingDiscountAmount($this->order->getShippingDiscountAmount());
  110. $total->setShippingDiscountTaxCompensationAmount($this->order->getShippingDiscountTaxCompensationAmount());
  111. $total->setShippingInclTax($this->order->getShippingInclTax());
  112. $total->setShippingInvoiced($this->order->getShippingInvoiced());
  113. $total->setShippingRefunded($this->order->getShippingRefunded());
  114. $total->setShippingTaxAmount($this->order->getShippingTaxAmount());
  115. $total->setShippingTaxRefunded($this->order->getShippingTaxRefunded());
  116. return $total;
  117. }
  118. }