Location.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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\Framework\View\Element\Block\ArgumentInterface;
  7. use Temando\Shipping\Model\Location\OrderAddressFactory;
  8. use Temando\Shipping\Model\ResourceModel\Rma\RmaAccess;
  9. use Temando\Shipping\Model\Shipment\CapabilityInterface;
  10. use Temando\Shipping\Model\Shipment\ShipmentProviderInterface;
  11. use Temando\Shipping\ViewModel\DataProvider\OrderAddress as AddressRenderer;
  12. /**
  13. * View model for shipment locations.
  14. *
  15. * @package Temando\Shipping\ViewModel
  16. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  17. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  18. * @link http://www.temando.com/
  19. */
  20. class Location implements ArgumentInterface
  21. {
  22. /**
  23. * @var ShipmentProviderInterface
  24. */
  25. private $shipmentProvider;
  26. /**
  27. * @var RmaAccess
  28. */
  29. private $rmaAccess;
  30. /**
  31. * @var OrderAddressFactory
  32. */
  33. private $addressFactory;
  34. /**
  35. * @var AddressRenderer
  36. */
  37. private $addressRenderer;
  38. /**
  39. * Location constructor.
  40. * @param ShipmentProviderInterface $shipmentProvider
  41. * @param RmaAccess $rmaAccess
  42. * @param OrderAddressFactory $addressFactory
  43. * @param AddressRenderer $addressRenderer
  44. */
  45. public function __construct(
  46. ShipmentProviderInterface $shipmentProvider,
  47. RmaAccess $rmaAccess,
  48. OrderAddressFactory $addressFactory,
  49. AddressRenderer $addressRenderer
  50. ) {
  51. $this->shipmentProvider = $shipmentProvider;
  52. $this->rmaAccess = $rmaAccess;
  53. $this->addressFactory = $addressFactory;
  54. $this->addressRenderer = $addressRenderer;
  55. }
  56. /**
  57. * Detect whether delivery address is a regular address or some other
  58. * destination type like collection point, click & collect store, etc.
  59. *
  60. * @return bool
  61. */
  62. private function isRegularAddress()
  63. {
  64. $shipment = $this->shipmentProvider->getShipment();
  65. // iterate through all capabilities, conditionally switch `isRegular` flag to `false`
  66. $isRegular = array_reduce($shipment->getCapabilities(), function ($isRegular, CapabilityInterface $capability) {
  67. $capabilityId = $capability->getCapabilityId();
  68. $properties = $capability->getProperties();
  69. $isCapabilityActive = (isset($properties['required']) && $properties['required'] === true);
  70. if ($capabilityId === 'collectionPoints' && $isCapabilityActive) {
  71. return false;
  72. }
  73. return $isRegular;
  74. }, true);
  75. return $isRegular;
  76. }
  77. /**
  78. * @return string
  79. */
  80. public function getShipFromAddressHtml()
  81. {
  82. /** @var \Temando\Shipping\Model\ShipmentInterface $shipment */
  83. $shipment = $this->shipmentProvider->getShipment();
  84. if (!$shipment) {
  85. return '';
  86. }
  87. $originLocation = $shipment->getOriginLocation();
  88. $address = $this->addressFactory->createFromShipmentLocation($originLocation);
  89. return $this->addressRenderer->getFormattedAddress($address);
  90. }
  91. /**
  92. * @return string
  93. */
  94. public function getShipToAddressHtml()
  95. {
  96. /** @var \Temando\Shipping\Model\ShipmentInterface $shipment */
  97. $shipment = $this->shipmentProvider->getShipment();
  98. if (!$shipment) {
  99. return '';
  100. }
  101. $destinationLocation = $shipment->getDestinationLocation();
  102. $address = $this->addressFactory->createFromShipmentLocation($destinationLocation);
  103. return $this->addressRenderer->getFormattedAddress($address);
  104. }
  105. /**
  106. * @return string
  107. */
  108. public function getFinalRecipientAddressHtml()
  109. {
  110. /** @var \Temando\Shipping\Model\ShipmentInterface $shipment */
  111. $shipment = $this->shipmentProvider->getShipment();
  112. if (!$shipment) {
  113. // no platform shipment available
  114. return '';
  115. }
  116. $finalRecipientLocation = $shipment->getFinalRecipientLocation();
  117. if (!$finalRecipientLocation) {
  118. // final recipient property is not available
  119. return '';
  120. }
  121. if ($this->isRegularAddress()) {
  122. // destination equals final recipient, no need to display anything
  123. return '';
  124. }
  125. $address = $this->addressFactory->createFromShipmentLocation($finalRecipientLocation);
  126. return $this->addressRenderer->getFormattedAddress($address);
  127. }
  128. /**
  129. * @return string
  130. */
  131. public function getReturnFromAddressHtml()
  132. {
  133. $shipment = $this->rmaAccess->getCurrentRmaShipment();
  134. if (!$shipment) {
  135. return '';
  136. }
  137. $originLocation = $this->rmaAccess->getCurrentRmaShipment()->getOriginLocation();
  138. $address = $this->addressFactory->createFromShipmentLocation($originLocation);
  139. return $this->addressRenderer->getFormattedAddress($address);
  140. }
  141. /**
  142. * @return string
  143. */
  144. public function getReturnToAddressHtml()
  145. {
  146. $shipment = $this->rmaAccess->getCurrentRmaShipment();
  147. if (!$shipment) {
  148. return '';
  149. }
  150. $destinationLocation = $this->rmaAccess->getCurrentRmaShipment()->getDestinationLocation();
  151. $address = $this->addressFactory->createFromShipmentLocation($destinationLocation);
  152. return $this->addressRenderer->getFormattedAddress($address);
  153. }
  154. /**
  155. * Check if shipment has a origin location.
  156. *
  157. * @return bool
  158. */
  159. public function hasOriginLocation()
  160. {
  161. /** @var \Temando\Shipping\Model\ShipmentInterface $shipment */
  162. $shipment = $this->shipmentProvider->getShipment();
  163. if (!$shipment) {
  164. return false;
  165. }
  166. $originLocation = $shipment->getOriginLocation();
  167. return $originLocation ? true : false;
  168. }
  169. }