NewAction.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Sales\Controller\Adminhtml\Order\Invoice;
  8. use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
  9. use Magento\Backend\App\Action;
  10. use Magento\Framework\Registry;
  11. use Magento\Framework\View\Result\PageFactory;
  12. use Magento\Sales\Model\Service\InvoiceService;
  13. class NewAction extends \Magento\Backend\App\Action implements HttpGetActionInterface
  14. {
  15. /**
  16. * Authorization level of a basic admin session
  17. *
  18. * @see _isAllowed()
  19. */
  20. const ADMIN_RESOURCE = 'Magento_Sales::sales_invoice';
  21. /**
  22. * @var Registry
  23. */
  24. protected $registry;
  25. /**
  26. * @var PageFactory
  27. */
  28. protected $resultPageFactory;
  29. /**
  30. * @var InvoiceService
  31. */
  32. private $invoiceService;
  33. /**
  34. * @param Action\Context $context
  35. * @param Registry $registry
  36. * @param PageFactory $resultPageFactory
  37. * @param InvoiceService $invoiceService
  38. */
  39. public function __construct(
  40. Action\Context $context,
  41. Registry $registry,
  42. PageFactory $resultPageFactory,
  43. InvoiceService $invoiceService
  44. ) {
  45. $this->registry = $registry;
  46. $this->resultPageFactory = $resultPageFactory;
  47. parent::__construct($context);
  48. $this->invoiceService = $invoiceService;
  49. }
  50. /**
  51. * Redirect to order view page
  52. *
  53. * @param int $orderId
  54. * @return \Magento\Backend\Model\View\Result\Redirect
  55. */
  56. protected function _redirectToOrder($orderId)
  57. {
  58. /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
  59. $resultRedirect = $this->resultRedirectFactory->create();
  60. $resultRedirect->setPath('sales/order/view', ['order_id' => $orderId]);
  61. return $resultRedirect;
  62. }
  63. /**
  64. * Invoice create page
  65. *
  66. * @return \Magento\Framework\Controller\ResultInterface
  67. */
  68. public function execute()
  69. {
  70. $orderId = $this->getRequest()->getParam('order_id');
  71. $invoiceData = $this->getRequest()->getParam('invoice', []);
  72. $invoiceItems = isset($invoiceData['items']) ? $invoiceData['items'] : [];
  73. try {
  74. /** @var \Magento\Sales\Model\Order $order */
  75. $order = $this->_objectManager->create(\Magento\Sales\Model\Order::class)->load($orderId);
  76. if (!$order->getId()) {
  77. throw new \Magento\Framework\Exception\LocalizedException(__('The order no longer exists.'));
  78. }
  79. if (!$order->canInvoice()) {
  80. throw new \Magento\Framework\Exception\LocalizedException(
  81. __('The order does not allow an invoice to be created.')
  82. );
  83. }
  84. $invoice = $this->invoiceService->prepareInvoice($order, $invoiceItems);
  85. if (!$invoice->getTotalQty()) {
  86. throw new \Magento\Framework\Exception\LocalizedException(
  87. __("The invoice can't be created without products. Add products and try again.")
  88. );
  89. }
  90. $this->registry->register('current_invoice', $invoice);
  91. $comment = $this->_objectManager->get(\Magento\Backend\Model\Session::class)->getCommentText(true);
  92. if ($comment) {
  93. $invoice->setCommentText($comment);
  94. }
  95. /** @var \Magento\Backend\Model\View\Result\Page $resultPage */
  96. $resultPage = $this->resultPageFactory->create();
  97. $resultPage->setActiveMenu('Magento_Sales::sales_order');
  98. $resultPage->getConfig()->getTitle()->prepend(__('Invoices'));
  99. $resultPage->getConfig()->getTitle()->prepend(__('New Invoice'));
  100. return $resultPage;
  101. } catch (\Magento\Framework\Exception\LocalizedException $exception) {
  102. $this->messageManager->addErrorMessage($exception->getMessage());
  103. return $this->_redirectToOrder($orderId);
  104. } catch (\Exception $exception) {
  105. $this->messageManager->addExceptionMessage($exception, 'Cannot create an invoice.');
  106. return $this->_redirectToOrder($orderId);
  107. }
  108. }
  109. }