OrderService.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\Service;
  7. use Magento\Sales\Api\OrderManagementInterface;
  8. use Magento\Payment\Gateway\Command\CommandException;
  9. /**
  10. * Class OrderService
  11. *
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class OrderService implements OrderManagementInterface
  15. {
  16. /**
  17. * @var \Magento\Sales\Api\OrderRepositoryInterface
  18. */
  19. protected $orderRepository;
  20. /**
  21. * @var \Magento\Sales\Api\OrderStatusHistoryRepositoryInterface
  22. */
  23. protected $historyRepository;
  24. /**
  25. * @var \Magento\Framework\Api\SearchCriteriaBuilder
  26. */
  27. protected $criteriaBuilder;
  28. /**
  29. * @var \Magento\Framework\Api\FilterBuilder
  30. */
  31. protected $filterBuilder;
  32. /**
  33. * @var \Magento\Sales\Model\OrderNotifier
  34. */
  35. protected $notifier;
  36. /**
  37. * @var \Magento\Framework\Event\ManagerInterface
  38. */
  39. protected $eventManager;
  40. /**
  41. * @var \Magento\Sales\Model\Order\Email\Sender\OrderCommentSender
  42. */
  43. protected $orderCommentSender;
  44. /**
  45. * @var \Magento\Sales\Api\PaymentFailuresInterface
  46. */
  47. private $paymentFailures;
  48. /**
  49. * Constructor
  50. *
  51. * @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
  52. * @param \Magento\Sales\Api\OrderStatusHistoryRepositoryInterface $historyRepository
  53. * @param \Magento\Framework\Api\SearchCriteriaBuilder $criteriaBuilder
  54. * @param \Magento\Framework\Api\FilterBuilder $filterBuilder
  55. * @param \Magento\Sales\Model\OrderNotifier $notifier
  56. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  57. * @param \Magento\Sales\Model\Order\Email\Sender\OrderCommentSender $orderCommentSender
  58. * @param \Magento\Sales\Api\PaymentFailuresInterface|null $paymentFailures
  59. */
  60. public function __construct(
  61. \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
  62. \Magento\Sales\Api\OrderStatusHistoryRepositoryInterface $historyRepository,
  63. \Magento\Framework\Api\SearchCriteriaBuilder $criteriaBuilder,
  64. \Magento\Framework\Api\FilterBuilder $filterBuilder,
  65. \Magento\Sales\Model\OrderNotifier $notifier,
  66. \Magento\Framework\Event\ManagerInterface $eventManager,
  67. \Magento\Sales\Model\Order\Email\Sender\OrderCommentSender $orderCommentSender,
  68. \Magento\Sales\Api\PaymentFailuresInterface $paymentFailures = null
  69. ) {
  70. $this->orderRepository = $orderRepository;
  71. $this->historyRepository = $historyRepository;
  72. $this->criteriaBuilder = $criteriaBuilder;
  73. $this->filterBuilder = $filterBuilder;
  74. $this->notifier = $notifier;
  75. $this->eventManager = $eventManager;
  76. $this->orderCommentSender = $orderCommentSender;
  77. $this->paymentFailures = $paymentFailures ? : \Magento\Framework\App\ObjectManager::getInstance()
  78. ->get(\Magento\Sales\Api\PaymentFailuresInterface::class);
  79. }
  80. /**
  81. * Order cancel
  82. *
  83. * @param int $id
  84. * @return bool
  85. */
  86. public function cancel($id)
  87. {
  88. $order = $this->orderRepository->get($id);
  89. if ($order->canCancel()) {
  90. $order->cancel();
  91. $this->orderRepository->save($order);
  92. return true;
  93. }
  94. return false;
  95. }
  96. /**
  97. * Returns list of comments attached to order
  98. *
  99. * @param int $id
  100. * @return \Magento\Sales\Api\Data\OrderStatusHistorySearchResultInterface
  101. */
  102. public function getCommentsList($id)
  103. {
  104. $this->criteriaBuilder->addFilters(
  105. [$this->filterBuilder->setField('parent_id')->setValue($id)->setConditionType('eq')->create()]
  106. );
  107. $searchCriteria = $this->criteriaBuilder->create();
  108. return $this->historyRepository->getList($searchCriteria);
  109. }
  110. /**
  111. * Add comment to order
  112. *
  113. * @param int $id
  114. * @param \Magento\Sales\Api\Data\OrderStatusHistoryInterface $statusHistory
  115. * @return bool
  116. */
  117. public function addComment($id, \Magento\Sales\Api\Data\OrderStatusHistoryInterface $statusHistory)
  118. {
  119. $order = $this->orderRepository->get($id);
  120. $order->addStatusHistory($statusHistory);
  121. $this->orderRepository->save($order);
  122. $notify = isset($statusHistory['is_customer_notified']) ? $statusHistory['is_customer_notified'] : false;
  123. $comment = trim(strip_tags($statusHistory->getComment()));
  124. $this->orderCommentSender->send($order, $notify, $comment);
  125. return true;
  126. }
  127. /**
  128. * Notify user
  129. *
  130. * @param int $id
  131. * @return bool
  132. */
  133. public function notify($id)
  134. {
  135. $order = $this->orderRepository->get($id);
  136. return $this->notifier->notify($order);
  137. }
  138. /**
  139. * Returns order status
  140. *
  141. * @param int $id
  142. * @return string
  143. */
  144. public function getStatus($id)
  145. {
  146. return $this->orderRepository->get($id)->getStatus();
  147. }
  148. /**
  149. * Order hold
  150. *
  151. * @param int $id
  152. * @return bool
  153. */
  154. public function hold($id)
  155. {
  156. $order = $this->orderRepository->get($id);
  157. $order->hold();
  158. return (bool)$this->orderRepository->save($order);
  159. }
  160. /**
  161. * Order un hold
  162. *
  163. * @param int $id
  164. * @return bool
  165. */
  166. public function unHold($id)
  167. {
  168. $object = $this->orderRepository->get($id);
  169. $object->unhold();
  170. return (bool)$this->orderRepository->save($object);
  171. }
  172. /**
  173. * @param \Magento\Sales\Api\Data\OrderInterface $order
  174. * @return \Magento\Sales\Api\Data\OrderInterface
  175. * @throws \Exception
  176. */
  177. public function place(\Magento\Sales\Api\Data\OrderInterface $order)
  178. {
  179. // transaction will be here
  180. //begin transaction
  181. try {
  182. $order->place();
  183. return $this->orderRepository->save($order);
  184. //commit
  185. } catch (\Exception $e) {
  186. if ($e instanceof CommandException) {
  187. $this->paymentFailures->handle((int)$order->getQuoteId(), __($e->getMessage()));
  188. }
  189. throw $e;
  190. //rollback;
  191. }
  192. }
  193. /**
  194. * Order state setter.
  195. *
  196. * If status is specified, will add order status history with specified comment
  197. * the setData() cannot be overridden because of compatibility issues with resource model
  198. * By default allows to set any state. Can also update status to default or specified value
  199. * Complete and closed states are encapsulated intentionally
  200. *
  201. * @param \Magento\Sales\Api\Data\OrderInterface $order
  202. * @param string $state
  203. * @param string|bool $status
  204. * @param string $comment
  205. * @param bool $isCustomerNotified
  206. * @param bool $shouldProtectState
  207. * @return \Magento\Sales\Model\Service\OrderService
  208. * @throws \Magento\Framework\Exception\LocalizedException
  209. */
  210. public function setState(
  211. \Magento\Sales\Api\Data\OrderInterface $order,
  212. $state,
  213. $status = false,
  214. $comment = '',
  215. $isCustomerNotified = null,
  216. $shouldProtectState = true
  217. ) {
  218. // attempt to set the specified state
  219. if ($shouldProtectState) {
  220. if ($order->isStateProtected($state)) {
  221. throw new \Magento\Framework\Exception\LocalizedException(
  222. __('The Order State "%1" must not be set manually.', $state)
  223. );
  224. }
  225. }
  226. $transport = new \Magento\Framework\DataObject(
  227. [
  228. 'state' => $state,
  229. 'status' => $status,
  230. 'comment' => $comment,
  231. 'is_customer_notified' => $isCustomerNotified
  232. ]
  233. );
  234. $this->eventManager->dispatch(
  235. 'sales_order_state_change_before',
  236. ['order' => $this, 'transport' => $transport]
  237. );
  238. $status = $transport->getStatus();
  239. $order->setData('state', $transport->getState());
  240. // add status history
  241. if ($status) {
  242. if ($status === true) {
  243. $status = $order->getConfig()->getStateDefaultStatus($transport->getState());
  244. }
  245. $order->setStatus($status);
  246. $history = $order->addStatusHistoryComment($transport->getComment(), false);
  247. // no sense to set $status again
  248. $history->setIsCustomerNotified($transport->getIsCustomerNotified());
  249. }
  250. return $this;
  251. }
  252. }