Info.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Shipping\Model;
  7. use Magento\Sales\Model\Order\Shipment;
  8. class Info extends \Magento\Framework\DataObject
  9. {
  10. /**
  11. * Tracking info
  12. *
  13. * @var array
  14. */
  15. protected $_trackingInfo = [];
  16. /**
  17. * Shipping data
  18. *
  19. * @var \Magento\Shipping\Helper\Data
  20. */
  21. protected $_shippingData;
  22. /**
  23. * @var \Magento\Sales\Model\OrderFactory
  24. */
  25. protected $_orderFactory;
  26. /**
  27. * @var \Magento\Sales\Api\ShipmentRepositoryInterface
  28. */
  29. protected $shipmentRepository;
  30. /**
  31. * @var \Magento\Shipping\Model\Order\TrackFactory
  32. */
  33. protected $_trackFactory;
  34. /**
  35. * @var \Magento\Sales\Model\ResourceModel\Order\Shipment\Track\CollectionFactory
  36. */
  37. protected $_trackCollectionFactory;
  38. /**
  39. * @param \Magento\Shipping\Helper\Data $shippingData
  40. * @param \Magento\Sales\Model\OrderFactory $orderFactory
  41. * @param \Magento\Sales\Api\ShipmentRepositoryInterface $shipmentRepository
  42. * @param \Magento\Shipping\Model\Order\TrackFactory $trackFactory
  43. * @param \Magento\Shipping\Model\ResourceModel\Order\Track\CollectionFactory $trackCollectionFactory
  44. * @param array $data
  45. */
  46. public function __construct(
  47. \Magento\Shipping\Helper\Data $shippingData,
  48. \Magento\Sales\Model\OrderFactory $orderFactory,
  49. \Magento\Sales\Api\ShipmentRepositoryInterface $shipmentRepository,
  50. \Magento\Shipping\Model\Order\TrackFactory $trackFactory,
  51. \Magento\Shipping\Model\ResourceModel\Order\Track\CollectionFactory $trackCollectionFactory,
  52. array $data = []
  53. ) {
  54. $this->_shippingData = $shippingData;
  55. $this->_orderFactory = $orderFactory;
  56. $this->shipmentRepository = $shipmentRepository;
  57. $this->_trackFactory = $trackFactory;
  58. $this->_trackCollectionFactory = $trackCollectionFactory;
  59. parent::__construct($data);
  60. }
  61. /**
  62. * Generating tracking info
  63. *
  64. * @param array $hash
  65. * @return $this
  66. */
  67. public function loadByHash($hash)
  68. {
  69. /* @var $helper \Magento\Shipping\Helper\Data */
  70. $helper = $this->_shippingData;
  71. $data = $helper->decodeTrackingHash($hash);
  72. if (!empty($data)) {
  73. $this->setData($data['key'], $data['id']);
  74. $this->setProtectCode($data['hash']);
  75. if ($this->getOrderId() > 0) {
  76. $this->getTrackingInfoByOrder();
  77. } elseif ($this->getShipId() > 0) {
  78. $this->getTrackingInfoByShip();
  79. } else {
  80. $this->getTrackingInfoByTrackId();
  81. }
  82. }
  83. return $this;
  84. }
  85. /**
  86. * Retrieve tracking info
  87. *
  88. * @return array
  89. */
  90. public function getTrackingInfo()
  91. {
  92. return $this->_trackingInfo;
  93. }
  94. /**
  95. * Instantiate order model
  96. *
  97. * @return \Magento\Sales\Model\Order|bool
  98. */
  99. protected function _initOrder()
  100. {
  101. /** @var \Magento\Sales\Model\Order $order */
  102. $order = $this->_orderFactory->create()->load($this->getOrderId());
  103. if (!$order->getId() || $this->getProtectCode() !== $order->getProtectCode()) {
  104. return false;
  105. }
  106. return $order;
  107. }
  108. /**
  109. * Instantiate ship model
  110. *
  111. * @return Shipment|bool
  112. */
  113. protected function _initShipment()
  114. {
  115. /* @var $model Shipment */
  116. $ship = $this->shipmentRepository->get($this->getShipId());
  117. if (!$ship->getEntityId() || $this->getProtectCode() !== $ship->getProtectCode()) {
  118. return false;
  119. }
  120. return $ship;
  121. }
  122. /**
  123. * Retrieve all tracking by order id
  124. *
  125. * @return array
  126. */
  127. public function getTrackingInfoByOrder()
  128. {
  129. $shipTrack = [];
  130. $order = $this->_initOrder();
  131. if ($order) {
  132. $shipments = $order->getShipmentsCollection();
  133. foreach ($shipments as $shipment) {
  134. $increment_id = $shipment->getIncrementId();
  135. $tracks = $this->_getTracksCollection($shipment);
  136. $trackingInfos = [];
  137. foreach ($tracks as $track) {
  138. $trackingInfos[] = $track->getNumberDetail();
  139. }
  140. $shipTrack[$increment_id] = $trackingInfos;
  141. }
  142. }
  143. $this->_trackingInfo = $shipTrack;
  144. return $this->_trackingInfo;
  145. }
  146. /**
  147. * Retrieve all tracking by ship id
  148. *
  149. * @return array
  150. */
  151. public function getTrackingInfoByShip()
  152. {
  153. $shipTrack = [];
  154. $shipment = $this->_initShipment();
  155. if ($shipment) {
  156. $increment_id = $shipment->getIncrementId();
  157. $tracks = $this->_getTracksCollection($shipment);
  158. $trackingInfos = [];
  159. foreach ($tracks as $track) {
  160. $trackingInfos[] = $track->getNumberDetail();
  161. }
  162. $shipTrack[$increment_id] = $trackingInfos;
  163. }
  164. $this->_trackingInfo = $shipTrack;
  165. return $this->_trackingInfo;
  166. }
  167. /**
  168. * Retrieve tracking by tracking entity id
  169. *
  170. * @return array
  171. */
  172. public function getTrackingInfoByTrackId()
  173. {
  174. /** @var \Magento\Shipping\Model\Order\Track $track */
  175. $track = $this->_trackFactory->create()->load($this->getTrackId());
  176. if ($track->getId() && $this->getProtectCode() === $track->getProtectCode()) {
  177. $this->_trackingInfo = [[$track->getNumberDetail()]];
  178. }
  179. return $this->_trackingInfo;
  180. }
  181. /**
  182. * @param Shipment $shipment
  183. * @return \Magento\Shipping\Model\ResourceModel\Order\Track\Collection
  184. */
  185. protected function _getTracksCollection(Shipment $shipment)
  186. {
  187. $tracks = $this->_trackCollectionFactory->create()->setShipmentFilter($shipment->getId());
  188. if ($shipment->getId()) {
  189. foreach ($tracks as $track) {
  190. $track->setShipment($shipment);
  191. }
  192. }
  193. return $tracks;
  194. }
  195. }