InvoiceOrder.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model;
  7. use Magento\Framework\App\ResourceConnection;
  8. use Magento\Sales\Api\Data\InvoiceCommentCreationInterface;
  9. use Magento\Sales\Api\Data\InvoiceCreationArgumentsInterface;
  10. use Magento\Sales\Api\InvoiceOrderInterface;
  11. use Magento\Sales\Api\OrderRepositoryInterface;
  12. use Magento\Sales\Model\Order\Config as OrderConfig;
  13. use Magento\Sales\Model\Order\Invoice\NotifierInterface;
  14. use Magento\Sales\Model\Order\InvoiceDocumentFactory;
  15. use Magento\Sales\Model\Order\InvoiceRepository;
  16. use Magento\Sales\Model\Order\OrderStateResolverInterface;
  17. use Magento\Sales\Model\Order\PaymentAdapterInterface;
  18. use Magento\Sales\Model\Order\Validation\InvoiceOrderInterface as InvoiceOrderValidator;
  19. use Psr\Log\LoggerInterface;
  20. /**
  21. * Class InvoiceOrder
  22. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  23. */
  24. class InvoiceOrder implements InvoiceOrderInterface
  25. {
  26. /**
  27. * @var ResourceConnection
  28. */
  29. private $resourceConnection;
  30. /**
  31. * @var OrderRepositoryInterface
  32. */
  33. private $orderRepository;
  34. /**
  35. * @var InvoiceDocumentFactory
  36. */
  37. private $invoiceDocumentFactory;
  38. /**
  39. * @var PaymentAdapterInterface
  40. */
  41. private $paymentAdapter;
  42. /**
  43. * @var OrderStateResolverInterface
  44. */
  45. private $orderStateResolver;
  46. /**
  47. * @var OrderConfig
  48. */
  49. private $config;
  50. /**
  51. * @var InvoiceRepository
  52. */
  53. private $invoiceRepository;
  54. /**
  55. * @var InvoiceOrderValidator
  56. */
  57. private $invoiceOrderValidator;
  58. /**
  59. * @var NotifierInterface
  60. */
  61. private $notifierInterface;
  62. /**
  63. * @var LoggerInterface
  64. */
  65. private $logger;
  66. /**
  67. * InvoiceOrder constructor.
  68. * @param ResourceConnection $resourceConnection
  69. * @param OrderRepositoryInterface $orderRepository
  70. * @param InvoiceDocumentFactory $invoiceDocumentFactory
  71. * @param PaymentAdapterInterface $paymentAdapter
  72. * @param OrderStateResolverInterface $orderStateResolver
  73. * @param OrderConfig $config
  74. * @param InvoiceRepository $invoiceRepository
  75. * @param InvoiceOrderValidator $invoiceOrderValidator
  76. * @param NotifierInterface $notifierInterface
  77. * @param LoggerInterface $logger
  78. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  79. */
  80. public function __construct(
  81. ResourceConnection $resourceConnection,
  82. OrderRepositoryInterface $orderRepository,
  83. InvoiceDocumentFactory $invoiceDocumentFactory,
  84. PaymentAdapterInterface $paymentAdapter,
  85. OrderStateResolverInterface $orderStateResolver,
  86. OrderConfig $config,
  87. InvoiceRepository $invoiceRepository,
  88. InvoiceOrderValidator $invoiceOrderValidator,
  89. NotifierInterface $notifierInterface,
  90. LoggerInterface $logger
  91. ) {
  92. $this->resourceConnection = $resourceConnection;
  93. $this->orderRepository = $orderRepository;
  94. $this->invoiceDocumentFactory = $invoiceDocumentFactory;
  95. $this->paymentAdapter = $paymentAdapter;
  96. $this->orderStateResolver = $orderStateResolver;
  97. $this->config = $config;
  98. $this->invoiceRepository = $invoiceRepository;
  99. $this->invoiceOrderValidator = $invoiceOrderValidator;
  100. $this->notifierInterface = $notifierInterface;
  101. $this->logger = $logger;
  102. }
  103. /**
  104. * @param int $orderId
  105. * @param bool $capture
  106. * @param array $items
  107. * @param bool $notify
  108. * @param bool $appendComment
  109. * @param \Magento\Sales\Api\Data\InvoiceCommentCreationInterface|null $comment
  110. * @param \Magento\Sales\Api\Data\InvoiceCreationArgumentsInterface|null $arguments
  111. * @return int
  112. * @throws \Magento\Sales\Api\Exception\DocumentValidationExceptionInterface
  113. * @throws \Magento\Sales\Api\Exception\CouldNotInvoiceExceptionInterface
  114. * @throws \Magento\Framework\Exception\InputException
  115. * @throws \Magento\Framework\Exception\NoSuchEntityException
  116. * @throws \DomainException
  117. */
  118. public function execute(
  119. $orderId,
  120. $capture = false,
  121. array $items = [],
  122. $notify = false,
  123. $appendComment = false,
  124. InvoiceCommentCreationInterface $comment = null,
  125. InvoiceCreationArgumentsInterface $arguments = null
  126. ) {
  127. $connection = $this->resourceConnection->getConnection('sales');
  128. $order = $this->orderRepository->get($orderId);
  129. $invoice = $this->invoiceDocumentFactory->create(
  130. $order,
  131. $items,
  132. $comment,
  133. ($appendComment && $notify),
  134. $arguments
  135. );
  136. $errorMessages = $this->invoiceOrderValidator->validate(
  137. $order,
  138. $invoice,
  139. $capture,
  140. $items,
  141. $notify,
  142. $appendComment,
  143. $comment,
  144. $arguments
  145. );
  146. if ($errorMessages->hasMessages()) {
  147. throw new \Magento\Sales\Exception\DocumentValidationException(
  148. __("Invoice Document Validation Error(s):\n" . implode("\n", $errorMessages->getMessages()))
  149. );
  150. }
  151. $connection->beginTransaction();
  152. try {
  153. $order = $this->paymentAdapter->pay($order, $invoice, $capture);
  154. $order->setState(
  155. $this->orderStateResolver->getStateForOrder($order, [OrderStateResolverInterface::IN_PROGRESS])
  156. );
  157. $order->setStatus($this->config->getStateDefaultStatus($order->getState()));
  158. $invoice->setState(\Magento\Sales\Model\Order\Invoice::STATE_PAID);
  159. $this->invoiceRepository->save($invoice);
  160. $this->orderRepository->save($order);
  161. $connection->commit();
  162. } catch (\Exception $e) {
  163. $this->logger->critical($e);
  164. $connection->rollBack();
  165. throw new \Magento\Sales\Exception\CouldNotInvoiceException(
  166. __('Could not save an invoice, see error log for details')
  167. );
  168. }
  169. if ($notify) {
  170. if (!$appendComment) {
  171. $comment = null;
  172. }
  173. $this->notifierInterface->notify($order, $invoice, $comment);
  174. }
  175. return $invoice->getEntityId();
  176. }
  177. }