123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Sales\Model\Service;
- use Magento\Sales\Api\OrderManagementInterface;
- use Magento\Payment\Gateway\Command\CommandException;
- /**
- * Class OrderService
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class OrderService implements OrderManagementInterface
- {
- /**
- * @var \Magento\Sales\Api\OrderRepositoryInterface
- */
- protected $orderRepository;
- /**
- * @var \Magento\Sales\Api\OrderStatusHistoryRepositoryInterface
- */
- protected $historyRepository;
- /**
- * @var \Magento\Framework\Api\SearchCriteriaBuilder
- */
- protected $criteriaBuilder;
- /**
- * @var \Magento\Framework\Api\FilterBuilder
- */
- protected $filterBuilder;
- /**
- * @var \Magento\Sales\Model\OrderNotifier
- */
- protected $notifier;
- /**
- * @var \Magento\Framework\Event\ManagerInterface
- */
- protected $eventManager;
- /**
- * @var \Magento\Sales\Model\Order\Email\Sender\OrderCommentSender
- */
- protected $orderCommentSender;
- /**
- * @var \Magento\Sales\Api\PaymentFailuresInterface
- */
- private $paymentFailures;
- /**
- * Constructor
- *
- * @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
- * @param \Magento\Sales\Api\OrderStatusHistoryRepositoryInterface $historyRepository
- * @param \Magento\Framework\Api\SearchCriteriaBuilder $criteriaBuilder
- * @param \Magento\Framework\Api\FilterBuilder $filterBuilder
- * @param \Magento\Sales\Model\OrderNotifier $notifier
- * @param \Magento\Framework\Event\ManagerInterface $eventManager
- * @param \Magento\Sales\Model\Order\Email\Sender\OrderCommentSender $orderCommentSender
- * @param \Magento\Sales\Api\PaymentFailuresInterface|null $paymentFailures
- */
- public function __construct(
- \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
- \Magento\Sales\Api\OrderStatusHistoryRepositoryInterface $historyRepository,
- \Magento\Framework\Api\SearchCriteriaBuilder $criteriaBuilder,
- \Magento\Framework\Api\FilterBuilder $filterBuilder,
- \Magento\Sales\Model\OrderNotifier $notifier,
- \Magento\Framework\Event\ManagerInterface $eventManager,
- \Magento\Sales\Model\Order\Email\Sender\OrderCommentSender $orderCommentSender,
- \Magento\Sales\Api\PaymentFailuresInterface $paymentFailures = null
- ) {
- $this->orderRepository = $orderRepository;
- $this->historyRepository = $historyRepository;
- $this->criteriaBuilder = $criteriaBuilder;
- $this->filterBuilder = $filterBuilder;
- $this->notifier = $notifier;
- $this->eventManager = $eventManager;
- $this->orderCommentSender = $orderCommentSender;
- $this->paymentFailures = $paymentFailures ? : \Magento\Framework\App\ObjectManager::getInstance()
- ->get(\Magento\Sales\Api\PaymentFailuresInterface::class);
- }
- /**
- * Order cancel
- *
- * @param int $id
- * @return bool
- */
- public function cancel($id)
- {
- $order = $this->orderRepository->get($id);
- if ($order->canCancel()) {
- $order->cancel();
- $this->orderRepository->save($order);
- return true;
- }
- return false;
- }
- /**
- * Returns list of comments attached to order
- *
- * @param int $id
- * @return \Magento\Sales\Api\Data\OrderStatusHistorySearchResultInterface
- */
- public function getCommentsList($id)
- {
- $this->criteriaBuilder->addFilters(
- [$this->filterBuilder->setField('parent_id')->setValue($id)->setConditionType('eq')->create()]
- );
- $searchCriteria = $this->criteriaBuilder->create();
- return $this->historyRepository->getList($searchCriteria);
- }
- /**
- * Add comment to order
- *
- * @param int $id
- * @param \Magento\Sales\Api\Data\OrderStatusHistoryInterface $statusHistory
- * @return bool
- */
- public function addComment($id, \Magento\Sales\Api\Data\OrderStatusHistoryInterface $statusHistory)
- {
- $order = $this->orderRepository->get($id);
- $order->addStatusHistory($statusHistory);
- $this->orderRepository->save($order);
- $notify = isset($statusHistory['is_customer_notified']) ? $statusHistory['is_customer_notified'] : false;
- $comment = trim(strip_tags($statusHistory->getComment()));
- $this->orderCommentSender->send($order, $notify, $comment);
- return true;
- }
- /**
- * Notify user
- *
- * @param int $id
- * @return bool
- */
- public function notify($id)
- {
- $order = $this->orderRepository->get($id);
- return $this->notifier->notify($order);
- }
- /**
- * Returns order status
- *
- * @param int $id
- * @return string
- */
- public function getStatus($id)
- {
- return $this->orderRepository->get($id)->getStatus();
- }
- /**
- * Order hold
- *
- * @param int $id
- * @return bool
- */
- public function hold($id)
- {
- $order = $this->orderRepository->get($id);
- $order->hold();
- return (bool)$this->orderRepository->save($order);
- }
- /**
- * Order un hold
- *
- * @param int $id
- * @return bool
- */
- public function unHold($id)
- {
- $object = $this->orderRepository->get($id);
- $object->unhold();
- return (bool)$this->orderRepository->save($object);
- }
- /**
- * @param \Magento\Sales\Api\Data\OrderInterface $order
- * @return \Magento\Sales\Api\Data\OrderInterface
- * @throws \Exception
- */
- public function place(\Magento\Sales\Api\Data\OrderInterface $order)
- {
- // transaction will be here
- //begin transaction
- try {
- $order->place();
- return $this->orderRepository->save($order);
- //commit
- } catch (\Exception $e) {
- if ($e instanceof CommandException) {
- $this->paymentFailures->handle((int)$order->getQuoteId(), __($e->getMessage()));
- }
- throw $e;
- //rollback;
- }
- }
- /**
- * Order state setter.
- *
- * If status is specified, will add order status history with specified comment
- * the setData() cannot be overridden because of compatibility issues with resource model
- * By default allows to set any state. Can also update status to default or specified value
- * Complete and closed states are encapsulated intentionally
- *
- * @param \Magento\Sales\Api\Data\OrderInterface $order
- * @param string $state
- * @param string|bool $status
- * @param string $comment
- * @param bool $isCustomerNotified
- * @param bool $shouldProtectState
- * @return \Magento\Sales\Model\Service\OrderService
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- public function setState(
- \Magento\Sales\Api\Data\OrderInterface $order,
- $state,
- $status = false,
- $comment = '',
- $isCustomerNotified = null,
- $shouldProtectState = true
- ) {
- // attempt to set the specified state
- if ($shouldProtectState) {
- if ($order->isStateProtected($state)) {
- throw new \Magento\Framework\Exception\LocalizedException(
- __('The Order State "%1" must not be set manually.', $state)
- );
- }
- }
- $transport = new \Magento\Framework\DataObject(
- [
- 'state' => $state,
- 'status' => $status,
- 'comment' => $comment,
- 'is_customer_notified' => $isCustomerNotified
- ]
- );
- $this->eventManager->dispatch(
- 'sales_order_state_change_before',
- ['order' => $this, 'transport' => $transport]
- );
- $status = $transport->getStatus();
- $order->setData('state', $transport->getState());
- // add status history
- if ($status) {
- if ($status === true) {
- $status = $order->getConfig()->getStateDefaultStatus($transport->getState());
- }
- $order->setStatus($status);
- $history = $order->addStatusHistoryComment($transport->getComment(), false);
- // no sense to set $status again
- $history->setIsCustomerNotified($transport->getIsCustomerNotified());
- }
- return $this;
- }
- }
|