History.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\Order\Status;
  7. use Magento\Framework\Api\AttributeValueFactory;
  8. use Magento\Sales\Api\Data\OrderStatusHistoryInterface;
  9. use Magento\Sales\Model\AbstractModel;
  10. /**
  11. * Order status history comments
  12. *
  13. * @api
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. * @since 100.0.2
  16. */
  17. class History extends AbstractModel implements OrderStatusHistoryInterface
  18. {
  19. const CUSTOMER_NOTIFICATION_NOT_APPLICABLE = 2;
  20. /**
  21. * Order instance
  22. *
  23. * @var \Magento\Sales\Model\Order
  24. */
  25. protected $_order;
  26. /**
  27. * @var string
  28. */
  29. protected $_eventPrefix = 'sales_order_status_history';
  30. /**
  31. * @var string
  32. */
  33. protected $_eventObject = 'status_history';
  34. /**
  35. * @var \Magento\Store\Model\StoreManagerInterface
  36. */
  37. protected $_storeManager;
  38. /**
  39. * @param \Magento\Framework\Model\Context $context
  40. * @param \Magento\Framework\Registry $registry
  41. * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
  42. * @param AttributeValueFactory $customAttributeFactory
  43. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  44. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  45. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  46. * @param array $data
  47. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  48. */
  49. public function __construct(
  50. \Magento\Framework\Model\Context $context,
  51. \Magento\Framework\Registry $registry,
  52. \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
  53. AttributeValueFactory $customAttributeFactory,
  54. \Magento\Store\Model\StoreManagerInterface $storeManager,
  55. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  56. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  57. array $data = []
  58. ) {
  59. parent::__construct(
  60. $context,
  61. $registry,
  62. $extensionFactory,
  63. $customAttributeFactory,
  64. $resource,
  65. $resourceCollection,
  66. $data
  67. );
  68. $this->_storeManager = $storeManager;
  69. }
  70. /**
  71. * Initialize resource model
  72. *
  73. * @return void
  74. */
  75. protected function _construct()
  76. {
  77. $this->_init(\Magento\Sales\Model\ResourceModel\Order\Status\History::class);
  78. }
  79. /**
  80. * Set order object and grab some metadata from it
  81. *
  82. * @param \Magento\Sales\Model\Order $order
  83. * @return $this
  84. */
  85. public function setOrder(\Magento\Sales\Model\Order $order)
  86. {
  87. $this->_order = $order;
  88. $this->setStoreId($order->getStoreId());
  89. return $this;
  90. }
  91. /**
  92. * Notification flag
  93. *
  94. * @param mixed $flag OPTIONAL (notification is not applicable by default)
  95. * @return $this
  96. */
  97. public function setIsCustomerNotified($flag = null)
  98. {
  99. if ($flag === null) {
  100. $flag = self::CUSTOMER_NOTIFICATION_NOT_APPLICABLE;
  101. }
  102. return $this->setData('is_customer_notified', $flag);
  103. }
  104. /**
  105. * Customer Notification Applicable check method
  106. *
  107. * @return boolean
  108. */
  109. public function isCustomerNotificationNotApplicable()
  110. {
  111. return $this->getIsCustomerNotified() == self::CUSTOMER_NOTIFICATION_NOT_APPLICABLE;
  112. }
  113. /**
  114. * Retrieve order instance
  115. *
  116. * @codeCoverageIgnore
  117. *
  118. * @return \Magento\Sales\Model\Order
  119. */
  120. public function getOrder()
  121. {
  122. return $this->_order;
  123. }
  124. /**
  125. * Retrieve status label
  126. *
  127. * @return string|null
  128. */
  129. public function getStatusLabel()
  130. {
  131. if ($this->getOrder()) {
  132. return $this->getOrder()->getConfig()->getStatusLabel($this->getStatus());
  133. }
  134. return null;
  135. }
  136. /**
  137. * Get store object
  138. *
  139. * @return \Magento\Store\Model\Store
  140. */
  141. public function getStore()
  142. {
  143. if ($this->getOrder()) {
  144. return $this->getOrder()->getStore();
  145. }
  146. return $this->_storeManager->getStore();
  147. }
  148. /**
  149. * Set order again if required
  150. *
  151. * @return $this
  152. */
  153. public function beforeSave()
  154. {
  155. parent::beforeSave();
  156. if (!$this->getParentId() && $this->getOrder()) {
  157. $this->setParentId($this->getOrder()->getId());
  158. }
  159. return $this;
  160. }
  161. //@codeCoverageIgnoreStart
  162. /**
  163. * Returns comment
  164. *
  165. * @return string
  166. */
  167. public function getComment()
  168. {
  169. return $this->getData(OrderStatusHistoryInterface::COMMENT);
  170. }
  171. /**
  172. * Returns created_at
  173. *
  174. * @return string
  175. */
  176. public function getCreatedAt()
  177. {
  178. return $this->getData(OrderStatusHistoryInterface::CREATED_AT);
  179. }
  180. /**
  181. * {@inheritdoc}
  182. */
  183. public function setCreatedAt($createdAt)
  184. {
  185. return $this->setData(OrderStatusHistoryInterface::CREATED_AT, $createdAt);
  186. }
  187. /**
  188. * Returns entity_id
  189. *
  190. * @return int
  191. */
  192. public function getEntityId()
  193. {
  194. return $this->getData(OrderStatusHistoryInterface::ENTITY_ID);
  195. }
  196. /**
  197. * Returns entity_name
  198. *
  199. * @return string
  200. */
  201. public function getEntityName()
  202. {
  203. return $this->getData(OrderStatusHistoryInterface::ENTITY_NAME);
  204. }
  205. /**
  206. * Returns is_customer_notified
  207. *
  208. * @return int
  209. */
  210. public function getIsCustomerNotified()
  211. {
  212. return $this->getData(OrderStatusHistoryInterface::IS_CUSTOMER_NOTIFIED);
  213. }
  214. /**
  215. * Returns is_visible_on_front
  216. *
  217. * @return int
  218. */
  219. public function getIsVisibleOnFront()
  220. {
  221. return $this->getData(OrderStatusHistoryInterface::IS_VISIBLE_ON_FRONT);
  222. }
  223. /**
  224. * Returns parent_id
  225. *
  226. * @return int
  227. */
  228. public function getParentId()
  229. {
  230. return $this->getData(OrderStatusHistoryInterface::PARENT_ID);
  231. }
  232. /**
  233. * Returns status
  234. *
  235. * @return string
  236. */
  237. public function getStatus()
  238. {
  239. return $this->getData(OrderStatusHistoryInterface::STATUS);
  240. }
  241. /**
  242. * {@inheritdoc}
  243. */
  244. public function setParentId($id)
  245. {
  246. return $this->setData(OrderStatusHistoryInterface::PARENT_ID, $id);
  247. }
  248. /**
  249. * {@inheritdoc}
  250. */
  251. public function setIsVisibleOnFront($isVisibleOnFront)
  252. {
  253. return $this->setData(OrderStatusHistoryInterface::IS_VISIBLE_ON_FRONT, $isVisibleOnFront);
  254. }
  255. /**
  256. * {@inheritdoc}
  257. */
  258. public function setComment($comment)
  259. {
  260. return $this->setData(OrderStatusHistoryInterface::COMMENT, $comment);
  261. }
  262. /**
  263. * {@inheritdoc}
  264. */
  265. public function setStatus($status)
  266. {
  267. return $this->setData(OrderStatusHistoryInterface::STATUS, $status);
  268. }
  269. /**
  270. * {@inheritdoc}
  271. */
  272. public function setEntityName($entityName)
  273. {
  274. return $this->setData(OrderStatusHistoryInterface::ENTITY_NAME, $entityName);
  275. }
  276. /**
  277. * {@inheritdoc}
  278. *
  279. * @return \Magento\Sales\Api\Data\OrderStatusHistoryExtensionInterface|null
  280. */
  281. public function getExtensionAttributes()
  282. {
  283. return $this->_getExtensionAttributes();
  284. }
  285. /**
  286. * {@inheritdoc}
  287. *
  288. * @param \Magento\Sales\Api\Data\OrderStatusHistoryExtensionInterface $extensionAttributes
  289. * @return $this
  290. */
  291. public function setExtensionAttributes(
  292. \Magento\Sales\Api\Data\OrderStatusHistoryExtensionInterface $extensionAttributes
  293. ) {
  294. return $this->_setExtensionAttributes($extensionAttributes);
  295. }
  296. //@codeCoverageIgnoreEnd
  297. }