BatchDetails.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\ViewModel\Batch;
  6. use Magento\Directory\Model\RegionFactory;
  7. use Magento\Framework\DataObjectFactory;
  8. use Magento\Framework\Exception\LocalizedException;
  9. use Magento\Framework\View\Element\Block\ArgumentInterface;
  10. use Magento\Sales\Api\Data\OrderAddressInterfaceFactory;
  11. use Magento\Sales\Api\Data\ShipmentItemInterface;
  12. use Magento\Sales\Model\Order\Shipment;
  13. use Temando\Shipping\Api\Data\Delivery\OrderCollectionPointInterface;
  14. use Temando\Shipping\Model\BatchInterface;
  15. use Temando\Shipping\Model\BatchProviderInterface;
  16. use Temando\Shipping\Model\ResourceModel\Repository\OrderCollectionPointRepositoryInterface;
  17. use Temando\Shipping\ViewModel\DataProvider\BatchUrl;
  18. use Temando\Shipping\ViewModel\DataProvider\OrderAddress as AddressRenderer;
  19. use Temando\Shipping\ViewModel\DataProvider\OrderDate;
  20. use Temando\Shipping\ViewModel\DataProvider\OrderUrl;
  21. /**
  22. * View model for batch details page.
  23. *
  24. * @package Temando\Shipping\ViewModel
  25. * @author Rhodri Davies <rhodri.davies@temando.com>
  26. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  27. * @link https://www.temando.com/
  28. */
  29. class BatchDetails implements ArgumentInterface
  30. {
  31. /**
  32. * @var BatchProviderInterface
  33. */
  34. private $batchProvider;
  35. /**
  36. * @var OrderDate
  37. */
  38. private $orderDate;
  39. /**
  40. * @var BatchUrl
  41. */
  42. private $batchUrl;
  43. /**
  44. * @var OrderUrl
  45. */
  46. private $orderUrl;
  47. /**
  48. * @var DataObjectFactory
  49. */
  50. private $dataObjectFactory;
  51. /**
  52. * @var AddressRenderer
  53. */
  54. private $addressRenderer;
  55. /**
  56. * @var OrderAddressInterfaceFactory
  57. */
  58. private $addressFactory;
  59. /**
  60. * @var OrderCollectionPointRepositoryInterface
  61. */
  62. private $collectionPointRepository;
  63. /**
  64. * @var RegionFactory
  65. */
  66. private $regionFactory;
  67. /**
  68. * BatchDetails constructor.
  69. * @param BatchProviderInterface $batchProvider
  70. * @param OrderDate $orderDate
  71. * @param BatchUrl $batchUrl
  72. * @param OrderUrl $orderUrl
  73. * @param DataObjectFactory $dataObjectFactory
  74. * @param AddressRenderer $addressRenderer
  75. * @param OrderAddressInterfaceFactory $orderAddressInterfaceFactory
  76. * @param OrderCollectionPointRepositoryInterface $collectionPointRepository
  77. * @param RegionFactory $regionFactory
  78. */
  79. public function __construct(
  80. BatchProviderInterface $batchProvider,
  81. OrderDate $orderDate,
  82. BatchUrl $batchUrl,
  83. OrderUrl $orderUrl,
  84. DataObjectFactory $dataObjectFactory,
  85. AddressRenderer $addressRenderer,
  86. OrderAddressInterfaceFactory $orderAddressInterfaceFactory,
  87. OrderCollectionPointRepositoryInterface $collectionPointRepository,
  88. RegionFactory $regionFactory
  89. ) {
  90. $this->batchProvider = $batchProvider;
  91. $this->orderDate = $orderDate;
  92. $this->batchUrl = $batchUrl;
  93. $this->orderUrl = $orderUrl;
  94. $this->dataObjectFactory = $dataObjectFactory;
  95. $this->addressRenderer = $addressRenderer;
  96. $this->addressFactory = $orderAddressInterfaceFactory;
  97. $this->collectionPointRepository = $collectionPointRepository;
  98. $this->regionFactory = $regionFactory;
  99. }
  100. /**
  101. * @return \Magento\Framework\DataObject|null
  102. */
  103. public function getBatch()
  104. {
  105. /** @var \Temando\Shipping\Model\Batch $batch */
  106. $batch = $this->batchProvider->getBatch();
  107. return $batch;
  108. }
  109. /**
  110. * @param string $date
  111. * @return \DateTime
  112. */
  113. public function getDate(string $date): \DateTime
  114. {
  115. return $this->orderDate->getDate($date);
  116. }
  117. /**
  118. * Obtain batch create url
  119. *
  120. * @return string
  121. */
  122. public function getNewActionUrl(): string
  123. {
  124. return $this->batchUrl->getNewActionUrl();
  125. }
  126. /**
  127. * Obtain batch listing url
  128. *
  129. * @return string
  130. */
  131. public function getListActionUrl(): string
  132. {
  133. return $this->batchUrl->getListActionUrl();
  134. }
  135. /**
  136. * Obtain url for troubleshooting failed batches
  137. *
  138. * @return string
  139. */
  140. public function getSolveUrl(): string
  141. {
  142. return $this->batchUrl->getSolveActionUrl([
  143. BatchInterface::BATCH_ID => $this->batchProvider->getBatch()->getBatchId(),
  144. ]);
  145. }
  146. /**
  147. * @param string $orderId
  148. * @return string
  149. */
  150. public function getOrderViewUrl(string $orderId): string
  151. {
  152. return $this->orderUrl->getViewActionUrl(['order_id' => $orderId]);
  153. }
  154. /**
  155. * @param $extShipmentId
  156. * @return \Magento\Sales\Api\Data\OrderInterface | null
  157. */
  158. private function getSalesShipmentOrder($extShipmentId)
  159. {
  160. $orders = $this->batchProvider->getOrders();
  161. if (isset($orders[$extShipmentId])) {
  162. return $orders[$extShipmentId];
  163. }
  164. return null;
  165. }
  166. /**
  167. * Returns ShipmentInfo based on extShipmentId.
  168. *
  169. * fixme(nr): replace by some proper piece of software
  170. *
  171. * @param string $extShipmentId
  172. * @return \Magento\Framework\DataObject
  173. */
  174. public function getShipmentInfoForGrid($extShipmentId)
  175. {
  176. $info = [];
  177. /** @var \Magento\Sales\Model\Order $order */
  178. $order = $this->getSalesShipmentOrder($extShipmentId);
  179. if ($order) {
  180. /** @var Shipment $shipment */
  181. $shipment = $order->getShipmentsCollection()->getFirstItem();
  182. $shippingAddress = $shipment->getShippingAddress();
  183. $shippingAddressId = $shippingAddress->getId();
  184. try {
  185. /** @var OrderCollectionPointInterface $collectionPoint */
  186. $collectionPoint = $this->collectionPointRepository->get($shippingAddressId);
  187. /** @var \Magento\Directory\Model\Region $region */
  188. $region = $this->regionFactory->create();
  189. $region = $region->loadByCode($collectionPoint->getRegion(), $collectionPoint->getCountry());
  190. $regionName = $region->getName();
  191. $addressData = [
  192. 'company' => $collectionPoint->getName(),
  193. 'street' => $collectionPoint->getStreet(),
  194. 'region' => $regionName,
  195. 'city' => $collectionPoint->getCity(),
  196. 'postcode' => $collectionPoint->getPostcode(),
  197. 'country_id' => $collectionPoint->getCountry(),
  198. ];
  199. $shippingAddress = $this->addressFactory->create($addressData);
  200. } catch (LocalizedException $e) {
  201. $shippingAddress = $shipment->getShippingAddress();
  202. }
  203. $formattedShippingAddress = $this->addressRenderer->getFormattedAddress($shippingAddress);
  204. $itemsInfo = $this->getItemsOrderedInfo($shipment->getItems());
  205. $info = [
  206. 'shipment_id' => $shipment->getId(),
  207. 'ship_to_name' => $shippingAddress->getFirstname() . ' ' . $shippingAddress->getLastname(),
  208. 'destination_address' => $formattedShippingAddress,
  209. 'items_ordered' => $itemsInfo,
  210. ];
  211. }
  212. $shipmentInfo = $this->dataObjectFactory->create(['data' => $info]);
  213. return $shipmentInfo;
  214. }
  215. /**
  216. * @param ShipmentItemInterface[] $items
  217. * @return string
  218. */
  219. private function getItemsOrderedInfo($items)
  220. {
  221. $info = '';
  222. foreach ($items as $item) {
  223. $info .= $item->getName() . ' x ' . (int) $item->getQty() . '<br>';
  224. }
  225. return $info;
  226. }
  227. }