OrderDataInitializer.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\Checkout\Submit;
  6. use Magento\Framework\Exception\LocalizedException;
  7. use Magento\Framework\Exception\NoSuchEntityException;
  8. use Magento\Quote\Api\Data\AddressInterface;
  9. use Magento\Quote\Model\ShippingAddressManagementInterface;
  10. use Magento\Sales\Api\Data\OrderInterface;
  11. use Temando\Shipping\Model\OrderInterfaceBuilder;
  12. use Temando\Shipping\Model\ResourceModel\Repository\QuoteCollectionPointRepositoryInterface;
  13. use Temando\Shipping\Model\ResourceModel\Repository\QuotePickupLocationRepositoryInterface;
  14. /**
  15. * Temando Order Data Initializer.
  16. *
  17. * @package Temando\Shipping\Model
  18. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  19. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  20. * @link https://www.temando.com/
  21. */
  22. class OrderDataInitializer
  23. {
  24. /**
  25. * @var QuoteCollectionPointRepositoryInterface
  26. */
  27. private $collectionPointRepository;
  28. /**
  29. * @var QuotePickupLocationRepositoryInterface
  30. */
  31. private $pickupLocationRepository;
  32. /**
  33. * @var ShippingAddressManagementInterface
  34. */
  35. private $addressManagement;
  36. /**
  37. * @var OrderInterfaceBuilder
  38. */
  39. private $orderBuilder;
  40. /**
  41. * OrderDataInitializer constructor.
  42. * @param QuoteCollectionPointRepositoryInterface $collectionPointRepository
  43. * @param QuotePickupLocationRepositoryInterface $pickupLocationRepository
  44. * @param ShippingAddressManagementInterface $addressManagement
  45. * @param OrderInterfaceBuilder $orderBuilder
  46. */
  47. public function __construct(
  48. QuoteCollectionPointRepositoryInterface $collectionPointRepository,
  49. QuotePickupLocationRepositoryInterface $pickupLocationRepository,
  50. ShippingAddressManagementInterface $addressManagement,
  51. OrderInterfaceBuilder $orderBuilder
  52. ) {
  53. $this->collectionPointRepository = $collectionPointRepository;
  54. $this->pickupLocationRepository = $pickupLocationRepository;
  55. $this->addressManagement = $addressManagement;
  56. $this->orderBuilder = $orderBuilder;
  57. }
  58. /**
  59. * If consumer selected a delivery location during checkout, it will be
  60. * added to the order builder here.
  61. *
  62. * @param AddressInterface $address
  63. * @return bool
  64. */
  65. private function setDeliveryLocation(AddressInterface $address = null)
  66. {
  67. if ($address === null) {
  68. return false;
  69. }
  70. try {
  71. $collectionPoint = $this->collectionPointRepository->getSelected($address->getId());
  72. $this->orderBuilder->setCollectionPoint($collectionPoint);
  73. } catch (LocalizedException $exception) {
  74. $collectionPoint = null;
  75. }
  76. if ($collectionPoint) {
  77. return true;
  78. }
  79. try {
  80. $pickupLocation = $this->pickupLocationRepository->getSelected($address->getId());
  81. $this->orderBuilder->setPickupLocation($pickupLocation);
  82. } catch (LocalizedException $exception) {
  83. $pickupLocation = null;
  84. }
  85. return (bool)$pickupLocation;
  86. }
  87. /**
  88. * Create a Temando order for manifestation purposes when checkout is completed.
  89. *
  90. * The order is being built from the placed order.
  91. * The order may include
  92. * - dynamic checkout fields,
  93. * - delivery location selected during checkout.
  94. *
  95. * NOTE: the delivery locations will only be associated to the order address
  96. * after the order was successfully created at the platform. During the process,
  97. * read the selected location from the quote address.
  98. *
  99. * @param OrderInterface $order
  100. * @return \Temando\Shipping\Model\OrderInterface
  101. */
  102. public function getOrder(OrderInterface $order)
  103. {
  104. try {
  105. $shippingAddress = $this->addressManagement->get($order->getQuoteId());
  106. } catch (NoSuchEntityException $exception) {
  107. $shippingAddress = null;
  108. }
  109. $this->setDeliveryLocation($shippingAddress);
  110. $this->orderBuilder->setOrder($order);
  111. /** @var \Temando\Shipping\Model\OrderInterface $order */
  112. $order = $this->orderBuilder->create();
  113. return $order;
  114. }
  115. }