Location.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\ViewModel\Order;
  6. use Magento\Framework\Exception\LocalizedException;
  7. use Magento\Framework\View\Element\Block\ArgumentInterface;
  8. use Magento\Sales\Api\Data\OrderInterface;
  9. use Magento\Sales\Model\Order;
  10. use Temando\Shipping\Api\Data\Delivery\OrderCollectionPointInterface;
  11. use Temando\Shipping\Api\Data\Delivery\OrderPickupLocationInterface;
  12. use Temando\Shipping\Model\Location\OrderAddressFactory;
  13. use Temando\Shipping\Model\ResourceModel\Repository\OrderCollectionPointRepositoryInterface;
  14. use Temando\Shipping\Model\ResourceModel\Repository\OrderPickupLocationRepositoryInterface;
  15. use Temando\Shipping\ViewModel\DataProvider\OrderAddress as AddressRenderer;
  16. /**
  17. * View model for order locations.
  18. *
  19. * @package Temando\Shipping\ViewModel
  20. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  21. * @author Sebastian Ertner <sebastian.ertner@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 Location implements ArgumentInterface
  26. {
  27. /**
  28. * @var AddressRenderer
  29. */
  30. private $addressRenderer;
  31. /**
  32. * @var OrderCollectionPointRepositoryInterface
  33. */
  34. private $collectionPointRepository;
  35. /**
  36. * @var OrderPickupLocationRepositoryInterface
  37. */
  38. private $pickupLocationRepository;
  39. /**
  40. * @var OrderAddressFactory
  41. */
  42. private $orderAddressFactory;
  43. /**
  44. * @var OrderCollectionPointInterface
  45. */
  46. private $collectionPoint;
  47. /**
  48. * @var OrderPickupLocationInterface
  49. */
  50. private $pickupLocation;
  51. /**
  52. * Location constructor.
  53. *
  54. * @param AddressRenderer $addressRenderer
  55. * @param OrderCollectionPointRepositoryInterface $collectionPointRepository
  56. * @param OrderPickupLocationRepositoryInterface $pickupLocationRepository
  57. * @param OrderAddressFactory $orderAddressFactory
  58. */
  59. public function __construct(
  60. AddressRenderer $addressRenderer,
  61. OrderCollectionPointRepositoryInterface $collectionPointRepository,
  62. OrderPickupLocationRepositoryInterface $pickupLocationRepository,
  63. OrderAddressFactory $orderAddressFactory
  64. ) {
  65. $this->addressRenderer = $addressRenderer;
  66. $this->collectionPointRepository = $collectionPointRepository;
  67. $this->pickupLocationRepository = $pickupLocationRepository;
  68. $this->orderAddressFactory = $orderAddressFactory;
  69. }
  70. /**
  71. * @param OrderInterface|Order $order
  72. *
  73. * @return OrderCollectionPointInterface|null
  74. */
  75. private function getCollectionPoint(OrderInterface $order)
  76. {
  77. if (!$this->collectionPoint) {
  78. try {
  79. $shippingAddressId = $order->getData('shipping_address_id');
  80. $this->collectionPoint = $this->collectionPointRepository->get($shippingAddressId);
  81. } catch (LocalizedException $e) {
  82. $this->collectionPoint = null;
  83. }
  84. }
  85. return $this->collectionPoint;
  86. }
  87. /**
  88. * @param OrderInterface|Order $order
  89. *
  90. * @return OrderPickupLocationInterface|null
  91. */
  92. private function getPickupLocation(OrderInterface $order)
  93. {
  94. if (!$this->pickupLocation) {
  95. try {
  96. $shippingAddressId = $order->getData('shipping_address_id');
  97. $this->pickupLocation = $this->pickupLocationRepository->get($shippingAddressId);
  98. } catch (LocalizedException $e) {
  99. $this->pickupLocation = null;
  100. }
  101. }
  102. return $this->pickupLocation;
  103. }
  104. /**
  105. * @param OrderInterface $order
  106. *
  107. * @return bool
  108. */
  109. public function hasDeliveryLocation(OrderInterface $order)
  110. {
  111. $collectionPoint = $this->getCollectionPoint($order);
  112. $pickUpLocation = $this->getPickupLocation($order);
  113. return ($collectionPoint || $pickUpLocation);
  114. }
  115. /**
  116. * @param OrderInterface $order
  117. *
  118. * @return string
  119. */
  120. public function getDeliveryLocationTitle(OrderInterface $order)
  121. {
  122. if ($this->getCollectionPoint($order)) {
  123. return __('Collection Point');
  124. } elseif ($this->getPickupLocation($order)) {
  125. return __('Pickup Location');
  126. } else {
  127. return '';
  128. }
  129. }
  130. /**
  131. * @param OrderInterface|Order $order
  132. * @return string
  133. */
  134. public function getBillingAddressHtml(OrderInterface $order)
  135. {
  136. return $this->addressRenderer->getFormattedAddress($order->getBillingAddress());
  137. }
  138. /**
  139. * @param OrderInterface|Order $order
  140. * @return string
  141. */
  142. public function getShippingAddressHtml(OrderInterface $order)
  143. {
  144. return $this->addressRenderer->getFormattedAddress($order->getShippingAddress());
  145. }
  146. /**
  147. * @param OrderInterface|Order $order
  148. *
  149. * @return string
  150. */
  151. public function getDeliveryLocationAddressHtml(OrderInterface $order)
  152. {
  153. $collectionPoint = $this->getCollectionPoint($order);
  154. if ($collectionPoint !== null) {
  155. $address = $this->orderAddressFactory->createFromCollectionPoint($collectionPoint);
  156. return $this->addressRenderer->getFormattedAddress($address);
  157. }
  158. $pickupLocation = $this->getPickupLocation($order);
  159. if ($pickupLocation !== null) {
  160. $address = $this->orderAddressFactory->createFromPickupLocation($pickupLocation);
  161. return $this->addressRenderer->getFormattedAddress($address);
  162. }
  163. return '';
  164. }
  165. }