ShipmentDetails.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\ViewModel\Shipment;
  6. use Magento\Backend\Model\UrlInterface;
  7. use Magento\Framework\DataObject;
  8. use Magento\Framework\View\Element\Block\ArgumentInterface;
  9. use Temando\Shipping\Model\DispatchProviderInterface;
  10. use Temando\Shipping\Model\ResourceModel\Rma\RmaAccess;
  11. use Temando\Shipping\Model\Shipment\PackageInterface;
  12. use Temando\Shipping\Model\Shipment\ShipmentProviderInterface;
  13. use Temando\Shipping\Model\ShipmentInterface;
  14. use Temando\Shipping\Model\ShipmentInterfaceFactory;
  15. /**
  16. * View model for shipment related information.
  17. *
  18. * @package Temando\Shipping\ViewModel
  19. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  20. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  21. * @link https://www.temando.com/
  22. */
  23. class ShipmentDetails implements ArgumentInterface
  24. {
  25. /**
  26. * @var ShipmentInterfaceFactory
  27. */
  28. private $shipmentFactory;
  29. /**
  30. * @var ShipmentProviderInterface
  31. */
  32. private $shipmentProvider;
  33. /**
  34. * @var DispatchProviderInterface
  35. */
  36. private $dispatchProvider;
  37. /**
  38. * @var RmaAccess
  39. */
  40. private $rmaAccess;
  41. /**
  42. * @var UrlInterface
  43. */
  44. private $urlBuilder;
  45. /**
  46. * ShipmentDetails constructor.
  47. * @param ShipmentInterfaceFactory $shipmentFactory
  48. * @param ShipmentProviderInterface $shipmentProvider
  49. * @param DispatchProviderInterface $dispatchProvider
  50. * @param RmaAccess $rmaAccess
  51. * @param UrlInterface $urlBuilder
  52. */
  53. public function __construct(
  54. ShipmentInterfaceFactory $shipmentFactory,
  55. ShipmentProviderInterface $shipmentProvider,
  56. DispatchProviderInterface $dispatchProvider,
  57. RmaAccess $rmaAccess,
  58. UrlInterface $urlBuilder
  59. ) {
  60. $this->shipmentFactory = $shipmentFactory;
  61. $this->shipmentProvider = $shipmentProvider;
  62. $this->dispatchProvider = $dispatchProvider;
  63. $this->rmaAccess = $rmaAccess;
  64. $this->urlBuilder = $urlBuilder;
  65. }
  66. /**
  67. * Get the Shipment.
  68. *
  69. * @return ShipmentInterface
  70. */
  71. private function getShipment(): ShipmentInterface
  72. {
  73. if ($this->shipmentProvider->getShipment()) {
  74. return $this->shipmentProvider->getShipment();
  75. }
  76. if ($this->rmaAccess->getCurrentRmaShipment()) {
  77. return $this->rmaAccess->getCurrentRmaShipment();
  78. }
  79. return $this->shipmentFactory->create();
  80. }
  81. /**
  82. * Get the view action URL.
  83. *
  84. * @param string $extShipmentId
  85. * @return string
  86. */
  87. public function getViewActionUrl($extShipmentId): string
  88. {
  89. return $this->urlBuilder->getUrl('temando/shipment/view', ['shipment_id' => $extShipmentId]);
  90. }
  91. /**
  92. * Get External Shipment ID.
  93. *
  94. * @return string
  95. */
  96. public function getExtShipmentId(): string
  97. {
  98. $shipment = $this->getShipment();
  99. return ($shipment ? (string) $shipment->getShipmentId() : '');
  100. }
  101. /**
  102. * Get Customer Reference.
  103. *
  104. * @return string
  105. */
  106. public function getCustomerReference(): string
  107. {
  108. $shipment = $this->getShipment();
  109. return ($shipment ? (string) $shipment->getCustomerReference() : '');
  110. }
  111. /**
  112. * Get Shipment Status
  113. *
  114. * @return string
  115. */
  116. public function getStatus(): string
  117. {
  118. $shipment = $this->getShipment();
  119. return ($shipment ? ucwords($shipment->getStatus()) : '');
  120. }
  121. /**
  122. * Check if status can be shown.
  123. *
  124. * @return bool
  125. */
  126. public function showStatus(): bool
  127. {
  128. $shipment = $this->getShipment();
  129. return ($shipment && $shipment->getStatus() === 'cancelled');
  130. }
  131. /**
  132. * Get Shipment documentation.
  133. *
  134. * @return DataObject[]
  135. */
  136. public function getDocumentation(): array
  137. {
  138. if ($this->dispatchProvider->getDispatch()) {
  139. $dispatch = $this->dispatchProvider->getDispatch();
  140. $documentation = $dispatch->getDocumentation();
  141. } else {
  142. $shipment = $this->getShipment();
  143. $documentation = $shipment->getDocumentation();
  144. }
  145. /** @var DataObject[] $documentation */
  146. return $documentation ?: [];
  147. }
  148. /**
  149. * Get Shipment packages.
  150. *
  151. * @return DataObject[]
  152. */
  153. public function getPackages(): array
  154. {
  155. /** @var DataObject[] $packages */
  156. $packages = $this->getShipment()->getPackages();
  157. return $packages ?: [];
  158. }
  159. /**
  160. * Get Shipment items.
  161. *
  162. * @return DataObject[]
  163. */
  164. public function getItems(): array
  165. {
  166. $packages = $this->getShipment()->getPackages() ?: [];
  167. $items = array_reduce($packages, function (array $items, PackageInterface $package) {
  168. $items = array_merge($items, $package->getItems());
  169. return $items;
  170. }, []);
  171. return $items;
  172. }
  173. /**
  174. * Get Documentation display name.
  175. *
  176. * @param string $documentationType
  177. * @return \Magento\Framework\Phrase
  178. */
  179. public function getDocumentationDisplayName($documentationType): \Magento\Framework\Phrase
  180. {
  181. $fileTypeNames = [
  182. 'nafta' => 'NAFTA',
  183. 'certificateOfOrigin' => 'Certificate Of Origin',
  184. 'cn22' => 'CN 22',
  185. 'cn23' => 'CN 23',
  186. 'codTurnInPage' => 'Cash On Delivery Turn In Page',
  187. 'commercialInvoice' => 'Commercial Invoice',
  188. 'customerInvoice' => 'Customer Invoice',
  189. 'highValueReport' => 'High Value Report',
  190. 'manifestSummary' => 'Manifest Summary',
  191. 'packageLabel' => 'Package Label',
  192. 'packageReturnLabel' => 'Package Return Label',
  193. 'packagingList' => 'Packaging List',
  194. 'proofOfDelivery' => 'Proof Of Delivery'
  195. ];
  196. $displayName = isset($fileTypeNames[$documentationType])
  197. ? $fileTypeNames[$documentationType]
  198. : $documentationType;
  199. return __($displayName);
  200. }
  201. /**
  202. * Check if Shipment is paperless.
  203. *
  204. * @return bool
  205. */
  206. public function isShipmentPaperless(): bool
  207. {
  208. $shipment = $this->getShipment();
  209. if ($shipment->getShipmentId()) {
  210. $originCountryCode = $shipment->getOriginLocation()->getCountryCode();
  211. $destinationCountryCode = $shipment->getDestinationLocation()->getCountryCode();
  212. if (($originCountryCode != $destinationCountryCode) && $shipment->isPaperless()) {
  213. return true;
  214. }
  215. }
  216. return false;
  217. }
  218. }