123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- <?php
- /**
- *
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Sales\Controller\Adminhtml\Order\Invoice;
- use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
- use Magento\Backend\App\Action;
- use Magento\Framework\Exception\LocalizedException;
- use Magento\Framework\Registry;
- use Magento\Sales\Model\Order\Email\Sender\InvoiceSender;
- use Magento\Sales\Model\Order\Email\Sender\ShipmentSender;
- use Magento\Sales\Model\Order\ShipmentFactory;
- use Magento\Sales\Model\Order\Invoice;
- use Magento\Sales\Model\Service\InvoiceService;
- /**
- * Save invoice controller.
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class Save extends \Magento\Backend\App\Action implements HttpPostActionInterface
- {
- /**
- * Authorization level of a basic admin session
- *
- * @see _isAllowed()
- */
- const ADMIN_RESOURCE = 'Magento_Sales::sales_invoice';
- /**
- * @var InvoiceSender
- */
- protected $invoiceSender;
- /**
- * @var ShipmentSender
- */
- protected $shipmentSender;
- /**
- * @var ShipmentFactory
- */
- protected $shipmentFactory;
- /**
- * @var Registry
- */
- protected $registry;
- /**
- * @var InvoiceService
- */
- private $invoiceService;
- /**
- * @param Action\Context $context
- * @param Registry $registry
- * @param InvoiceSender $invoiceSender
- * @param ShipmentSender $shipmentSender
- * @param ShipmentFactory $shipmentFactory
- * @param InvoiceService $invoiceService
- */
- public function __construct(
- Action\Context $context,
- Registry $registry,
- InvoiceSender $invoiceSender,
- ShipmentSender $shipmentSender,
- ShipmentFactory $shipmentFactory,
- InvoiceService $invoiceService
- ) {
- $this->registry = $registry;
- $this->invoiceSender = $invoiceSender;
- $this->shipmentSender = $shipmentSender;
- $this->shipmentFactory = $shipmentFactory;
- $this->invoiceService = $invoiceService;
- parent::__construct($context);
- }
- /**
- * Prepare shipment
- *
- * @param \Magento\Sales\Model\Order\Invoice $invoice
- * @return \Magento\Sales\Model\Order\Shipment|false
- */
- protected function _prepareShipment($invoice)
- {
- $invoiceData = $this->getRequest()->getParam('invoice');
- $shipment = $this->shipmentFactory->create(
- $invoice->getOrder(),
- isset($invoiceData['items']) ? $invoiceData['items'] : [],
- $this->getRequest()->getPost('tracking')
- );
- if (!$shipment->getTotalQty()) {
- return false;
- }
- return $shipment->register();
- }
- /**
- * Save invoice
- *
- * We can save only new invoice. Existing invoices are not editable
- *
- * @return \Magento\Framework\Controller\ResultInterface
- *
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- * @SuppressWarnings(PHPMD.NPathComplexity)
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- public function execute()
- {
- /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
- $resultRedirect = $this->resultRedirectFactory->create();
- $formKeyIsValid = $this->_formKeyValidator->validate($this->getRequest());
- $isPost = $this->getRequest()->isPost();
- if (!$formKeyIsValid || !$isPost) {
- $this->messageManager
- ->addErrorMessage(__("The invoice can't be saved at this time. Please try again later."));
- return $resultRedirect->setPath('sales/order/index');
- }
- $data = $this->getRequest()->getPost('invoice');
- $orderId = $this->getRequest()->getParam('order_id');
- if (!empty($data['comment_text'])) {
- $this->_objectManager->get(\Magento\Backend\Model\Session::class)->setCommentText($data['comment_text']);
- }
- try {
- $invoiceData = $this->getRequest()->getParam('invoice', []);
- $invoiceItems = isset($invoiceData['items']) ? $invoiceData['items'] : [];
- /** @var \Magento\Sales\Model\Order $order */
- $order = $this->_objectManager->create(\Magento\Sales\Model\Order::class)->load($orderId);
- if (!$order->getId()) {
- throw new \Magento\Framework\Exception\LocalizedException(__('The order no longer exists.'));
- }
- if (!$order->canInvoice()) {
- throw new \Magento\Framework\Exception\LocalizedException(
- __('The order does not allow an invoice to be created.')
- );
- }
- $invoice = $this->invoiceService->prepareInvoice($order, $invoiceItems);
- if (!$invoice) {
- throw new LocalizedException(__("The invoice can't be saved at this time. Please try again later."));
- }
- if (!$invoice->getTotalQty()) {
- throw new \Magento\Framework\Exception\LocalizedException(
- __("The invoice can't be created without products. Add products and try again.")
- );
- }
- $this->registry->register('current_invoice', $invoice);
- if (!empty($data['capture_case'])) {
- $invoice->setRequestedCaptureCase($data['capture_case']);
- }
- if (!empty($data['comment_text'])) {
- $invoice->addComment(
- $data['comment_text'],
- isset($data['comment_customer_notify']),
- isset($data['is_visible_on_front'])
- );
- $invoice->setCustomerNote($data['comment_text']);
- $invoice->setCustomerNoteNotify(isset($data['comment_customer_notify']));
- }
- $invoice->register();
- $invoice->getOrder()->setCustomerNoteNotify(!empty($data['send_email']));
- $invoice->getOrder()->setIsInProcess(true);
- $transactionSave = $this->_objectManager->create(
- \Magento\Framework\DB\Transaction::class
- )->addObject(
- $invoice
- )->addObject(
- $invoice->getOrder()
- );
- $shipment = false;
- if (!empty($data['do_shipment']) || (int)$invoice->getOrder()->getForcedShipmentWithInvoice()) {
- $shipment = $this->_prepareShipment($invoice);
- if ($shipment) {
- $transactionSave->addObject($shipment);
- }
- }
- $transactionSave->save();
- // send invoice/shipment emails
- try {
- if (!empty($data['send_email'])) {
- $this->invoiceSender->send($invoice);
- }
- } catch (\Exception $e) {
- $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
- $this->messageManager->addErrorMessage(__('We can\'t send the invoice email right now.'));
- }
- if ($shipment) {
- try {
- if (!empty($data['send_email'])) {
- $this->shipmentSender->send($shipment);
- }
- } catch (\Exception $e) {
- $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
- $this->messageManager->addErrorMessage(__('We can\'t send the shipment right now.'));
- }
- }
- if (!empty($data['do_shipment'])) {
- $this->messageManager->addSuccessMessage(__('You created the invoice and shipment.'));
- } else {
- $this->messageManager->addSuccessMessage(__('The invoice has been created.'));
- }
- $this->_objectManager->get(\Magento\Backend\Model\Session::class)->getCommentText(true);
- return $resultRedirect->setPath('sales/order/view', ['order_id' => $orderId]);
- } catch (LocalizedException $e) {
- $this->messageManager->addErrorMessage($e->getMessage());
- } catch (\Exception $e) {
- $this->messageManager->addErrorMessage(
- __("The invoice can't be saved at this time. Please try again later.")
- );
- $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
- }
- return $resultRedirect->setPath('sales/*/new', ['order_id' => $orderId]);
- }
- }
|