123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Sales\Model;
- use Magento\Framework\App\ResourceConnection;
- use Magento\Sales\Api\Data\InvoiceCommentCreationInterface;
- use Magento\Sales\Api\Data\InvoiceCreationArgumentsInterface;
- use Magento\Sales\Api\InvoiceOrderInterface;
- use Magento\Sales\Api\OrderRepositoryInterface;
- use Magento\Sales\Model\Order\Config as OrderConfig;
- use Magento\Sales\Model\Order\Invoice\NotifierInterface;
- use Magento\Sales\Model\Order\InvoiceDocumentFactory;
- use Magento\Sales\Model\Order\InvoiceRepository;
- use Magento\Sales\Model\Order\OrderStateResolverInterface;
- use Magento\Sales\Model\Order\PaymentAdapterInterface;
- use Magento\Sales\Model\Order\Validation\InvoiceOrderInterface as InvoiceOrderValidator;
- use Psr\Log\LoggerInterface;
- /**
- * Class InvoiceOrder
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class InvoiceOrder implements InvoiceOrderInterface
- {
- /**
- * @var ResourceConnection
- */
- private $resourceConnection;
- /**
- * @var OrderRepositoryInterface
- */
- private $orderRepository;
- /**
- * @var InvoiceDocumentFactory
- */
- private $invoiceDocumentFactory;
- /**
- * @var PaymentAdapterInterface
- */
- private $paymentAdapter;
- /**
- * @var OrderStateResolverInterface
- */
- private $orderStateResolver;
- /**
- * @var OrderConfig
- */
- private $config;
- /**
- * @var InvoiceRepository
- */
- private $invoiceRepository;
- /**
- * @var InvoiceOrderValidator
- */
- private $invoiceOrderValidator;
- /**
- * @var NotifierInterface
- */
- private $notifierInterface;
- /**
- * @var LoggerInterface
- */
- private $logger;
- /**
- * InvoiceOrder constructor.
- * @param ResourceConnection $resourceConnection
- * @param OrderRepositoryInterface $orderRepository
- * @param InvoiceDocumentFactory $invoiceDocumentFactory
- * @param PaymentAdapterInterface $paymentAdapter
- * @param OrderStateResolverInterface $orderStateResolver
- * @param OrderConfig $config
- * @param InvoiceRepository $invoiceRepository
- * @param InvoiceOrderValidator $invoiceOrderValidator
- * @param NotifierInterface $notifierInterface
- * @param LoggerInterface $logger
- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
- */
- public function __construct(
- ResourceConnection $resourceConnection,
- OrderRepositoryInterface $orderRepository,
- InvoiceDocumentFactory $invoiceDocumentFactory,
- PaymentAdapterInterface $paymentAdapter,
- OrderStateResolverInterface $orderStateResolver,
- OrderConfig $config,
- InvoiceRepository $invoiceRepository,
- InvoiceOrderValidator $invoiceOrderValidator,
- NotifierInterface $notifierInterface,
- LoggerInterface $logger
- ) {
- $this->resourceConnection = $resourceConnection;
- $this->orderRepository = $orderRepository;
- $this->invoiceDocumentFactory = $invoiceDocumentFactory;
- $this->paymentAdapter = $paymentAdapter;
- $this->orderStateResolver = $orderStateResolver;
- $this->config = $config;
- $this->invoiceRepository = $invoiceRepository;
- $this->invoiceOrderValidator = $invoiceOrderValidator;
- $this->notifierInterface = $notifierInterface;
- $this->logger = $logger;
- }
- /**
- * @param int $orderId
- * @param bool $capture
- * @param array $items
- * @param bool $notify
- * @param bool $appendComment
- * @param \Magento\Sales\Api\Data\InvoiceCommentCreationInterface|null $comment
- * @param \Magento\Sales\Api\Data\InvoiceCreationArgumentsInterface|null $arguments
- * @return int
- * @throws \Magento\Sales\Api\Exception\DocumentValidationExceptionInterface
- * @throws \Magento\Sales\Api\Exception\CouldNotInvoiceExceptionInterface
- * @throws \Magento\Framework\Exception\InputException
- * @throws \Magento\Framework\Exception\NoSuchEntityException
- * @throws \DomainException
- */
- public function execute(
- $orderId,
- $capture = false,
- array $items = [],
- $notify = false,
- $appendComment = false,
- InvoiceCommentCreationInterface $comment = null,
- InvoiceCreationArgumentsInterface $arguments = null
- ) {
- $connection = $this->resourceConnection->getConnection('sales');
- $order = $this->orderRepository->get($orderId);
- $invoice = $this->invoiceDocumentFactory->create(
- $order,
- $items,
- $comment,
- ($appendComment && $notify),
- $arguments
- );
- $errorMessages = $this->invoiceOrderValidator->validate(
- $order,
- $invoice,
- $capture,
- $items,
- $notify,
- $appendComment,
- $comment,
- $arguments
- );
- if ($errorMessages->hasMessages()) {
- throw new \Magento\Sales\Exception\DocumentValidationException(
- __("Invoice Document Validation Error(s):\n" . implode("\n", $errorMessages->getMessages()))
- );
- }
- $connection->beginTransaction();
- try {
- $order = $this->paymentAdapter->pay($order, $invoice, $capture);
- $order->setState(
- $this->orderStateResolver->getStateForOrder($order, [OrderStateResolverInterface::IN_PROGRESS])
- );
- $order->setStatus($this->config->getStateDefaultStatus($order->getState()));
- $invoice->setState(\Magento\Sales\Model\Order\Invoice::STATE_PAID);
- $this->invoiceRepository->save($invoice);
- $this->orderRepository->save($order);
- $connection->commit();
- } catch (\Exception $e) {
- $this->logger->critical($e);
- $connection->rollBack();
- throw new \Magento\Sales\Exception\CouldNotInvoiceException(
- __('Could not save an invoice, see error log for details')
- );
- }
- if ($notify) {
- if (!$appendComment) {
- $comment = null;
- }
- $this->notifierInterface->notify($order, $invoice, $comment);
- }
- return $invoice->getEntityId();
- }
- }
|