Status.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\Order;
  7. use Magento\Framework\Exception\LocalizedException;
  8. /**
  9. * Class Status
  10. *
  11. * @method string getStatus()
  12. * @method $this setStatus(string $status)
  13. * @method string getLabel()
  14. */
  15. class Status extends \Magento\Sales\Model\AbstractModel
  16. {
  17. /**
  18. * @var \Magento\Store\Model\StoreManagerInterface
  19. */
  20. protected $_storeManager;
  21. /**
  22. * @param \Magento\Framework\Model\Context $context
  23. * @param \Magento\Framework\Registry $registry
  24. * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
  25. * @param \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory
  26. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  27. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  28. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  29. * @param array $data
  30. */
  31. public function __construct(
  32. \Magento\Framework\Model\Context $context,
  33. \Magento\Framework\Registry $registry,
  34. \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
  35. \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
  36. \Magento\Store\Model\StoreManagerInterface $storeManager,
  37. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  38. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  39. array $data = []
  40. ) {
  41. parent::__construct(
  42. $context,
  43. $registry,
  44. $extensionFactory,
  45. $customAttributeFactory,
  46. $resource,
  47. $resourceCollection,
  48. $data
  49. );
  50. $this->_storeManager = $storeManager;
  51. }
  52. /**
  53. * @return void
  54. */
  55. protected function _construct()
  56. {
  57. $this->_init(\Magento\Sales\Model\ResourceModel\Order\Status::class);
  58. }
  59. /**
  60. * Assign order status to particular state
  61. *
  62. * @param string $state
  63. * @param bool $isDefault make the status as default one for state
  64. * @param bool $visibleOnFront
  65. * @return $this
  66. * @throws \Exception
  67. */
  68. public function assignState($state, $isDefault = false, $visibleOnFront = false)
  69. {
  70. /** @var \Magento\Sales\Model\ResourceModel\Order\Status $resource */
  71. $resource = $this->_getResource();
  72. $resource->beginTransaction();
  73. try {
  74. $resource->assignState($this->getStatus(), $state, $isDefault, $visibleOnFront);
  75. $resource->commit();
  76. } catch (\Exception $e) {
  77. $resource->rollBack();
  78. throw $e;
  79. }
  80. return $this;
  81. }
  82. /**
  83. * @param string $state
  84. * @return void
  85. * @throws \Magento\Framework\Exception\LocalizedException
  86. */
  87. protected function validateBeforeUnassign($state)
  88. {
  89. if ($this->getResource()->checkIsStateLast($state)) {
  90. throw new LocalizedException(
  91. __("The last status can't be changed and needs to stay assigned to its current state.")
  92. );
  93. }
  94. if ($this->getResource()->checkIsStatusUsed($this->getStatus())) {
  95. throw new LocalizedException(
  96. __("The status can't be unassigned because the status is currently used by an order.")
  97. );
  98. }
  99. }
  100. /**
  101. * Unassigns order status from particular state
  102. *
  103. * @param string $state
  104. * @return $this
  105. * @throws \Exception
  106. */
  107. public function unassignState($state)
  108. {
  109. $this->validateBeforeUnassign($state);
  110. $this->getResource()->unassignState($this->getStatus(), $state);
  111. $this->_eventManager->dispatch(
  112. 'sales_order_status_unassign',
  113. [
  114. 'status' => $this->getStatus(),
  115. 'state' => $state
  116. ]
  117. );
  118. return $this;
  119. }
  120. /**
  121. * Getter for status labels per store
  122. *
  123. * @return array
  124. */
  125. public function getStoreLabels()
  126. {
  127. if ($this->hasData('store_labels')) {
  128. return $this->_getData('store_labels');
  129. }
  130. $labels = $this->_getResource()->getStoreLabels($this);
  131. $this->setData('store_labels', $labels);
  132. return $labels;
  133. }
  134. /**
  135. * Get status label by store
  136. *
  137. * @param null|string|bool|int|\Magento\Store\Model\Store $store
  138. * @return \Magento\Framework\Phrase|string
  139. */
  140. public function getStoreLabel($store = null)
  141. {
  142. $store = $this->_storeManager->getStore($store);
  143. $labels = $this->getStoreLabels();
  144. if (isset($labels[$store->getId()])) {
  145. return $labels[$store->getId()];
  146. } else {
  147. return __($this->getLabel());
  148. }
  149. }
  150. /**
  151. * Load default status per state
  152. *
  153. * @param string $state
  154. * @return $this
  155. */
  156. public function loadDefaultByState($state)
  157. {
  158. $this->load($state, 'default_state');
  159. return $this;
  160. }
  161. }