Popup.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\ViewModel\Rma\RmaShipment\Tracking;
  6. use Magento\Framework\App\Config\ScopeConfigInterface;
  7. use Magento\Framework\Stdlib\DateTime\DateTimeFormatterInterface;
  8. use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
  9. use Magento\Framework\UrlInterface;
  10. use Magento\Framework\View\Element\Block\ArgumentInterface;
  11. use Magento\Store\Model\ScopeInterface;
  12. use Temando\Shipping\Model\ResourceModel\Repository\ShipmentRepositoryInterface;
  13. use Temando\Shipping\Model\ResourceModel\Rma\RmaAccess;
  14. use Temando\Shipping\Model\Shipment\FulfillmentInterface;
  15. use Temando\Shipping\Model\ShipmentInterface;
  16. use Temando\Shipping\Model\Shipping\Carrier;
  17. /**
  18. * View model for tracking popup.
  19. *
  20. * @package Temando\Shipping\ViewModel
  21. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  22. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  23. * @link http://www.temando.com/
  24. */
  25. class Popup implements ArgumentInterface
  26. {
  27. /**
  28. * @var RmaAccess
  29. */
  30. private $rmaAccess;
  31. /**
  32. * @var UrlInterface
  33. */
  34. private $urlBuilder;
  35. /**
  36. * @var Carrier
  37. */
  38. private $carrier;
  39. /**
  40. * @var ShipmentRepositoryInterface
  41. */
  42. private $shipmentRepository;
  43. /**
  44. * @var DateTimeFormatterInterface
  45. */
  46. private $dateTimeFormatter;
  47. /**
  48. * @var TimezoneInterface
  49. */
  50. private $localeDate;
  51. /**
  52. * Copyright information
  53. *
  54. * @var string
  55. */
  56. private $copyright;
  57. /**
  58. * @var ScopeConfigInterface
  59. */
  60. private $scopeConfig;
  61. /**
  62. * Popup constructor.
  63. * @param RmaAccess $rmaAccess
  64. * @param UrlInterface $urlBuilder
  65. * @param Carrier $carrier
  66. * @param DateTimeFormatterInterface $dateTimeFormatter
  67. * @param TimezoneInterface $timezone
  68. * @param ShipmentRepositoryInterface $shipmentRepository
  69. * @param ScopeConfigInterface $scopeConfig
  70. */
  71. public function __construct(
  72. RmaAccess $rmaAccess,
  73. UrlInterface $urlBuilder,
  74. Carrier $carrier,
  75. DateTimeFormatterInterface $dateTimeFormatter,
  76. TimezoneInterface $timezone,
  77. ShipmentRepositoryInterface $shipmentRepository,
  78. ScopeConfigInterface $scopeConfig
  79. ) {
  80. $this->rmaAccess = $rmaAccess;
  81. $this->urlBuilder = $urlBuilder;
  82. $this->carrier = $carrier;
  83. $this->shipmentRepository = $shipmentRepository;
  84. $this->dateTimeFormatter = $dateTimeFormatter;
  85. $this->localeDate = $timezone;
  86. $this->scopeConfig = $scopeConfig;
  87. }
  88. /**
  89. * Get return shipment from platform.
  90. *
  91. * @return ShipmentInterface
  92. */
  93. private function getShipment()
  94. {
  95. return $this->rmaAccess->getCurrentRmaShipment();
  96. }
  97. /**
  98. * Get carrier name from configuration.
  99. *
  100. * @return string
  101. */
  102. public function getCarrierName()
  103. {
  104. return $this->carrier->getConfigData('title');
  105. }
  106. /**
  107. * Get tracking number from fulfillment.
  108. *
  109. * @return string
  110. */
  111. public function getTrackingNumber()
  112. {
  113. $shipment = $this->getShipment();
  114. if (!$shipment->getFulfillment() instanceof FulfillmentInterface) {
  115. return '';
  116. }
  117. return $shipment->getFulfillment()->getTrackingReference();
  118. }
  119. /**
  120. * Get carrier name from fulfillment.
  121. *
  122. * @return string
  123. */
  124. public function getCarrierTitle()
  125. {
  126. $shipment = $this->getShipment();
  127. if (!$shipment->getFulfillment() instanceof FulfillmentInterface) {
  128. return $this->getCarrierName();
  129. }
  130. $carrierTitle = sprintf(
  131. '%s - %s',
  132. $shipment->getFulfillment()->getCarrierName(),
  133. $shipment->getFulfillment()->getServiceName()
  134. );
  135. return $carrierTitle;
  136. }
  137. /**
  138. * Get tracking url from package or fulfillment.
  139. *
  140. * @return string
  141. */
  142. public function getTrackingUrl()
  143. {
  144. $shipment = $this->getShipment();
  145. $trackingNumber = $this->getTrackingNumber();
  146. // read tracking url from booking fulfillment
  147. $trackingUrl = $shipment->getFulfillment()->getTrackingUrl();
  148. // check if there is a matching tracking url in the packages
  149. foreach ($shipment->getPackages() as $package) {
  150. if (!$package->getTrackingUrl()) {
  151. continue;
  152. }
  153. if ($package->getTrackingReference() === $trackingNumber) {
  154. $trackingUrl = $package->getTrackingUrl();
  155. }
  156. }
  157. return (string) $trackingUrl;
  158. }
  159. /**
  160. * Format given date and time in current locale without changing timezone
  161. *
  162. * @param string $date
  163. * @param string $time
  164. * @return string
  165. */
  166. public function formatDeliveryDateTime($date, $time)
  167. {
  168. return $this->formatDeliveryDate($date) . ' ' . $this->formatDeliveryTime($time);
  169. }
  170. /**
  171. * Format given date in current locale without changing timezone
  172. *
  173. * @param string $date
  174. * @return string
  175. */
  176. public function formatDeliveryDate($date)
  177. {
  178. $format = $this->localeDate->getDateFormat(\IntlDateFormatter::MEDIUM);
  179. return $this->dateTimeFormatter->formatObject($this->localeDate->date(new \DateTime($date)), $format);
  180. }
  181. /**
  182. * Format given time [+ date] in current locale without changing timezone
  183. *
  184. * @param string $time
  185. * @param string $date
  186. * @return string
  187. */
  188. public function formatDeliveryTime($time, $date = null)
  189. {
  190. if (!empty($date)) {
  191. $time = $date . ' ' . $time;
  192. }
  193. $format = $this->localeDate->getTimeFormat(\IntlDateFormatter::SHORT);
  194. return $this->dateTimeFormatter->formatObject($this->localeDate->date(new \DateTime($time)), $format);
  195. }
  196. /**
  197. * Retrieve copyright information
  198. *
  199. * @return string
  200. */
  201. public function getCopyright()
  202. {
  203. if (!$this->copyright) {
  204. $this->copyright = $this->scopeConfig->getValue('design/footer/copyright', ScopeInterface::SCOPE_STORE);
  205. }
  206. return __($this->copyright);
  207. }
  208. }