Track.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\Order\Shipment;
  7. use Magento\Framework\Api\AttributeValueFactory;
  8. use Magento\Framework\Exception\LocalizedException;
  9. use Magento\Sales\Api\Data\ShipmentTrackInterface;
  10. use Magento\Sales\Model\AbstractModel;
  11. /**
  12. * @api
  13. * @author Magento Core Team <core@magentocommerce.com>
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. * @since 100.0.2
  16. */
  17. class Track extends AbstractModel implements ShipmentTrackInterface
  18. {
  19. /**
  20. * Code of custom carrier
  21. */
  22. const CUSTOM_CARRIER_CODE = 'custom';
  23. /**
  24. * @var \Magento\Sales\Model\Order\Shipment|null
  25. */
  26. protected $_shipment = null;
  27. /**
  28. * @var string
  29. */
  30. protected $_eventPrefix = 'sales_order_shipment_track';
  31. /**
  32. * @var string
  33. */
  34. protected $_eventObject = 'track';
  35. /**
  36. * @var \Magento\Store\Model\StoreManagerInterface
  37. */
  38. protected $_storeManager;
  39. /**
  40. * @var \Magento\Sales\Api\ShipmentRepositoryInterface
  41. */
  42. protected $shipmentRepository;
  43. /**
  44. * @param \Magento\Framework\Model\Context $context
  45. * @param \Magento\Framework\Registry $registry
  46. * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
  47. * @param AttributeValueFactory $customAttributeFactory
  48. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  49. * @param \Magento\Sales\Api\ShipmentRepositoryInterface $shipmentRepository
  50. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  51. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  52. * @param array $data
  53. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  54. */
  55. public function __construct(
  56. \Magento\Framework\Model\Context $context,
  57. \Magento\Framework\Registry $registry,
  58. \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
  59. AttributeValueFactory $customAttributeFactory,
  60. \Magento\Store\Model\StoreManagerInterface $storeManager,
  61. \Magento\Sales\Api\ShipmentRepositoryInterface $shipmentRepository,
  62. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  63. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  64. array $data = []
  65. ) {
  66. parent::__construct(
  67. $context,
  68. $registry,
  69. $extensionFactory,
  70. $customAttributeFactory,
  71. $resource,
  72. $resourceCollection,
  73. $data
  74. );
  75. $this->_storeManager = $storeManager;
  76. $this->shipmentRepository = $shipmentRepository;
  77. }
  78. /**
  79. * Initialize resource model
  80. *
  81. * @return void
  82. */
  83. protected function _construct()
  84. {
  85. $this->_init(\Magento\Sales\Model\ResourceModel\Order\Shipment\Track::class);
  86. }
  87. /**
  88. * Tracking number getter
  89. *
  90. * @codeCoverageIgnore
  91. *
  92. * @return string
  93. */
  94. public function getNumber()
  95. {
  96. return $this->getData('track_number');
  97. }
  98. /**
  99. * Tracking number setter
  100. *
  101. * @codeCoverageIgnore
  102. *
  103. * @param string $number
  104. * @return \Magento\Framework\DataObject
  105. */
  106. public function setNumber($number)
  107. {
  108. return $this->setData('track_number', $number);
  109. }
  110. /**
  111. * Declare Shipment instance
  112. *
  113. * @codeCoverageIgnore
  114. *
  115. * @param \Magento\Sales\Model\Order\Shipment $shipment
  116. * @return $this
  117. */
  118. public function setShipment(\Magento\Sales\Model\Order\Shipment $shipment)
  119. {
  120. $this->_shipment = $shipment;
  121. return $this;
  122. }
  123. /**
  124. * Retrieve Shipment instance
  125. *
  126. * @return \Magento\Sales\Model\Order\Shipment
  127. * @throws \Magento\Framework\Exception\LocalizedException
  128. */
  129. public function getShipment()
  130. {
  131. if (!$this->_shipment instanceof \Magento\Sales\Model\Order\Shipment) {
  132. if ($this->getParentId()) {
  133. $this->_shipment = $this->shipmentRepository->get($this->getParentId());
  134. } else {
  135. throw new LocalizedException(__("Parent shipment cannot be loaded for track object."));
  136. }
  137. }
  138. return $this->_shipment;
  139. }
  140. /**
  141. * Check whether custom carrier was used for this track
  142. *
  143. * @return bool
  144. */
  145. public function isCustom()
  146. {
  147. return $this->getCarrierCode() == self::CUSTOM_CARRIER_CODE;
  148. }
  149. /**
  150. * Retrieve hash code of current order
  151. *
  152. * @return string
  153. */
  154. public function getProtectCode()
  155. {
  156. return (string)$this->getShipment()->getProtectCode();
  157. }
  158. /**
  159. * Get store object
  160. *
  161. * @return \Magento\Store\Model\Store
  162. */
  163. public function getStore()
  164. {
  165. if ($this->getShipment()) {
  166. return $this->getShipment()->getStore();
  167. }
  168. return $this->_storeManager->getStore();
  169. }
  170. /**
  171. * Get store id
  172. *
  173. * @return int
  174. */
  175. public function getStoreId()
  176. {
  177. return $this->getStore()->getId();
  178. }
  179. /**
  180. * Add data to the object.
  181. *
  182. * Retains previous data in the object.
  183. *
  184. * @param array $data
  185. * @return $this
  186. */
  187. public function addData(array $data)
  188. {
  189. if (array_key_exists('number', $data)) {
  190. $this->setNumber($data['number']);
  191. unset($data['number']);
  192. }
  193. return parent::addData($data);
  194. }
  195. //@codeCoverageIgnoreStart
  196. /**
  197. * Returns track_number
  198. *
  199. * @return string
  200. */
  201. public function getTrackNumber()
  202. {
  203. return $this->getData(ShipmentTrackInterface::TRACK_NUMBER);
  204. }
  205. /**
  206. * Returns carrier_code
  207. *
  208. * @return string
  209. */
  210. public function getCarrierCode()
  211. {
  212. return $this->getData(ShipmentTrackInterface::CARRIER_CODE);
  213. }
  214. /**
  215. * Returns created_at
  216. *
  217. * @return string
  218. */
  219. public function getCreatedAt()
  220. {
  221. return $this->getData(ShipmentTrackInterface::CREATED_AT);
  222. }
  223. /**
  224. * {@inheritdoc}
  225. */
  226. public function setCreatedAt($createdAt)
  227. {
  228. return $this->setData(ShipmentTrackInterface::CREATED_AT, $createdAt);
  229. }
  230. /**
  231. * Returns description
  232. *
  233. * @return string
  234. */
  235. public function getDescription()
  236. {
  237. return $this->getData(ShipmentTrackInterface::DESCRIPTION);
  238. }
  239. /**
  240. * Returns order_id
  241. *
  242. * @return int
  243. */
  244. public function getOrderId()
  245. {
  246. return $this->getData(ShipmentTrackInterface::ORDER_ID);
  247. }
  248. /**
  249. * Returns parent_id
  250. *
  251. * @return int
  252. */
  253. public function getParentId()
  254. {
  255. return $this->getData(ShipmentTrackInterface::PARENT_ID);
  256. }
  257. /**
  258. * Returns qty
  259. *
  260. * @return float
  261. */
  262. public function getQty()
  263. {
  264. return $this->getData(ShipmentTrackInterface::QTY);
  265. }
  266. /**
  267. * Returns title
  268. *
  269. * @return string
  270. */
  271. public function getTitle()
  272. {
  273. return $this->getData(ShipmentTrackInterface::TITLE);
  274. }
  275. /**
  276. * Returns updated_at
  277. *
  278. * @return string
  279. */
  280. public function getUpdatedAt()
  281. {
  282. return $this->getData(ShipmentTrackInterface::UPDATED_AT);
  283. }
  284. /**
  285. * Returns weight
  286. *
  287. * @return float
  288. */
  289. public function getWeight()
  290. {
  291. return $this->getData(ShipmentTrackInterface::WEIGHT);
  292. }
  293. /**
  294. * {@inheritdoc}
  295. */
  296. public function setUpdatedAt($timestamp)
  297. {
  298. return $this->setData(ShipmentTrackInterface::UPDATED_AT, $timestamp);
  299. }
  300. /**
  301. * {@inheritdoc}
  302. */
  303. public function setParentId($id)
  304. {
  305. return $this->setData(ShipmentTrackInterface::PARENT_ID, $id);
  306. }
  307. /**
  308. * {@inheritdoc}
  309. */
  310. public function setWeight($weight)
  311. {
  312. return $this->setData(ShipmentTrackInterface::WEIGHT, $weight);
  313. }
  314. /**
  315. * {@inheritdoc}
  316. */
  317. public function setQty($qty)
  318. {
  319. return $this->setData(ShipmentTrackInterface::QTY, $qty);
  320. }
  321. /**
  322. * {@inheritdoc}
  323. */
  324. public function setOrderId($id)
  325. {
  326. return $this->setData(ShipmentTrackInterface::ORDER_ID, $id);
  327. }
  328. /**
  329. * {@inheritdoc}
  330. */
  331. public function setTrackNumber($trackNumber)
  332. {
  333. return $this->setData(ShipmentTrackInterface::TRACK_NUMBER, $trackNumber);
  334. }
  335. /**
  336. * {@inheritdoc}
  337. */
  338. public function setDescription($description)
  339. {
  340. return $this->setData(ShipmentTrackInterface::DESCRIPTION, $description);
  341. }
  342. /**
  343. * {@inheritdoc}
  344. */
  345. public function setTitle($title)
  346. {
  347. return $this->setData(ShipmentTrackInterface::TITLE, $title);
  348. }
  349. /**
  350. * {@inheritdoc}
  351. */
  352. public function setCarrierCode($code)
  353. {
  354. return $this->setData(ShipmentTrackInterface::CARRIER_CODE, $code);
  355. }
  356. /**
  357. * {@inheritdoc}
  358. *
  359. * @return \Magento\Sales\Api\Data\ShipmentTrackExtensionInterface|null
  360. */
  361. public function getExtensionAttributes()
  362. {
  363. return $this->_getExtensionAttributes();
  364. }
  365. /**
  366. * {@inheritdoc}
  367. *
  368. * @param \Magento\Sales\Api\Data\ShipmentTrackExtensionInterface $extensionAttributes
  369. * @return $this
  370. */
  371. public function setExtensionAttributes(\Magento\Sales\Api\Data\ShipmentTrackExtensionInterface $extensionAttributes)
  372. {
  373. return $this->_setExtensionAttributes($extensionAttributes);
  374. }
  375. //@codeCoverageIgnoreEnd
  376. }