QuotingDataInitializer.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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\RateRequest;
  6. use Magento\Framework\Exception\LocalizedException;
  7. use Magento\Quote\Api\Data\AddressInterface;
  8. use Magento\Quote\Api\Data\AddressExtensionInterface;
  9. use Magento\Quote\Api\Data\AddressExtensionInterfaceFactory;
  10. use Magento\Quote\Model\Quote\Address\RateRequest;
  11. use Temando\Shipping\Model\OrderInterfaceBuilder;
  12. use Temando\Shipping\Model\ResourceModel\Repository\AddressRepositoryInterface;
  13. use Temando\Shipping\Model\ResourceModel\Repository\CollectionPointSearchRepositoryInterface;
  14. use Temando\Shipping\Model\ResourceModel\Repository\PickupLocationSearchRepositoryInterface;
  15. use Temando\Shipping\Model\ResourceModel\Repository\QuoteCollectionPointRepositoryInterface;
  16. use Temando\Shipping\Model\ResourceModel\Repository\QuotePickupLocationRepositoryInterface;
  17. /**
  18. * Temando Quoting Data Initializer.
  19. *
  20. * @package Temando\Shipping\Model
  21. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  22. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  23. * @link https://www.temando.com/
  24. */
  25. class QuotingDataInitializer
  26. {
  27. /**
  28. * @var Extractor
  29. */
  30. private $rateRequestExtractor;
  31. /**
  32. * @var AddressRepositoryInterface
  33. */
  34. private $checkoutAddressRepository;
  35. /**
  36. * @var AddressExtensionInterfaceFactory
  37. */
  38. private $addressExtensionFactory;
  39. /**
  40. * @var CollectionPointSearchRepositoryInterface
  41. */
  42. private $collectionPointSearchRepository;
  43. /**
  44. * @var QuoteCollectionPointRepositoryInterface
  45. */
  46. private $collectionPointRepository;
  47. /**
  48. * @var PickupLocationSearchRepositoryInterface
  49. */
  50. private $pickupLocationSearchRepository;
  51. /**
  52. * @var QuotePickupLocationRepositoryInterface
  53. */
  54. private $pickupLocationRepository;
  55. /**
  56. * @var OrderInterfaceBuilder
  57. */
  58. private $orderBuilder;
  59. /**
  60. * QuotingDataInitializer constructor.
  61. * @param Extractor $rateRequestExtractor
  62. * @param AddressRepositoryInterface $checkoutAddressRepository
  63. * @param AddressExtensionInterfaceFactory $addressExtensionFactory
  64. * @param CollectionPointSearchRepositoryInterface $collectionPointSearchRepository
  65. * @param QuoteCollectionPointRepositoryInterface $collectionPointRepository
  66. * @param PickupLocationSearchRepositoryInterface $pickupLocationSearchRepository
  67. * @param QuotePickupLocationRepositoryInterface $pickupLocationRepository
  68. * @param OrderInterfaceBuilder $orderBuilder
  69. */
  70. public function __construct(
  71. Extractor $rateRequestExtractor,
  72. AddressRepositoryInterface $checkoutAddressRepository,
  73. AddressExtensionInterfaceFactory $addressExtensionFactory,
  74. CollectionPointSearchRepositoryInterface $collectionPointSearchRepository,
  75. QuoteCollectionPointRepositoryInterface $collectionPointRepository,
  76. PickupLocationSearchRepositoryInterface $pickupLocationSearchRepository,
  77. QuotePickupLocationRepositoryInterface $pickupLocationRepository,
  78. OrderInterfaceBuilder $orderBuilder
  79. ) {
  80. $this->rateRequestExtractor = $rateRequestExtractor;
  81. $this->checkoutAddressRepository = $checkoutAddressRepository;
  82. $this->addressExtensionFactory = $addressExtensionFactory;
  83. $this->collectionPointSearchRepository = $collectionPointSearchRepository;
  84. $this->collectionPointRepository = $collectionPointRepository;
  85. $this->pickupLocationSearchRepository = $pickupLocationSearchRepository;
  86. $this->pickupLocationRepository = $pickupLocationRepository;
  87. $this->orderBuilder = $orderBuilder;
  88. }
  89. /**
  90. * If consumer selected any value-added services during checkout, these
  91. * are added to the shipping address here. Shipping address is part of the
  92. * rate request, which gets added to the order builder separately.
  93. *
  94. * @param AddressInterface $address
  95. * @return bool
  96. */
  97. private function setCheckoutFields(AddressInterface $address)
  98. {
  99. try {
  100. $checkoutAddress = $this->checkoutAddressRepository->getByQuoteAddressId($address->getId());
  101. $checkoutFields = $checkoutAddress->getServiceSelection();
  102. } catch (LocalizedException $e) {
  103. $checkoutFields = [];
  104. }
  105. $addressExtension = $address->getExtensionAttributes();
  106. if ($addressExtension instanceof AddressExtensionInterface) {
  107. $addressExtension->setCheckoutFields($checkoutFields);
  108. } else {
  109. $addressExtension = $this->addressExtensionFactory->create(['data' => [
  110. 'checkout_fields' => $checkoutFields,
  111. ]]);
  112. }
  113. $address->setExtensionAttributes($addressExtension);
  114. return !empty($checkoutFields);
  115. }
  116. /**
  117. * If consumer selected a collection point or triggered a search for
  118. * collection points during checkout, these entities will be added to the
  119. * order builder here.
  120. *
  121. * @param AddressInterface $address
  122. * @return bool
  123. */
  124. private function setCollectionPoint(AddressInterface $address)
  125. {
  126. try {
  127. $collectionPointSearchRequest = $this->collectionPointSearchRepository->get($address->getId());
  128. $this->orderBuilder->setCollectionPointSearchRequest($collectionPointSearchRequest);
  129. } catch (LocalizedException $exception) {
  130. $collectionPointSearchRequest = null;
  131. }
  132. try {
  133. $collectionPoint = $this->collectionPointRepository->getSelected($address->getId());
  134. $this->orderBuilder->setCollectionPoint($collectionPoint);
  135. } catch (LocalizedException $exception) {
  136. $collectionPoint = null;
  137. }
  138. return ($collectionPointSearchRequest || $collectionPoint);
  139. }
  140. /**
  141. * If consumer selected a pickup location or triggered a search for
  142. * pickup locations during checkout, these entities will be added to the
  143. * order builder here.
  144. *
  145. * @param AddressInterface $address
  146. * @return bool
  147. */
  148. private function setPickupLocation(AddressInterface $address)
  149. {
  150. try {
  151. $pickupLocationSearchRequest = $this->pickupLocationSearchRepository->get($address->getId());
  152. $this->orderBuilder->setPickupLocationSearchRequest($pickupLocationSearchRequest);
  153. } catch (LocalizedException $exception) {
  154. $pickupLocationSearchRequest = null;
  155. }
  156. try {
  157. $pickupLocation = $this->pickupLocationRepository->getSelected($address->getId());
  158. $this->orderBuilder->setPickupLocation($pickupLocation);
  159. } catch (LocalizedException $exception) {
  160. $pickupLocation = null;
  161. }
  162. return ($pickupLocationSearchRequest || $pickupLocation);
  163. }
  164. /**
  165. * Create a Temando order for quoting purposes.
  166. *
  167. * The order is being built from the quote and rate request.
  168. * The order may include
  169. * - dynamic checkout fields,
  170. * - delivery location selected during checkout.
  171. *
  172. * @param RateRequest $rateRequest
  173. * @return \Temando\Shipping\Model\OrderInterface
  174. * @throws LocalizedException
  175. */
  176. public function getOrder(RateRequest $rateRequest)
  177. {
  178. $shippingAddress = $this->rateRequestExtractor->getShippingAddress($rateRequest);
  179. $this->setCheckoutFields($shippingAddress);
  180. $this->setCollectionPoint($shippingAddress) || $this->setPickupLocation($shippingAddress);
  181. $this->orderBuilder->setRateRequest($rateRequest);
  182. /** @var \Temando\Shipping\Model\OrderInterface $order */
  183. $order = $this->orderBuilder->create();
  184. return $order;
  185. }
  186. }