UpdateQty.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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\HttpPostActionInterface as HttpPostActionInterface;
  9. use Magento\Framework\Exception\LocalizedException;
  10. use Magento\Framework\Controller\Result\JsonFactory;
  11. use Magento\Framework\View\Result\PageFactory;
  12. use Magento\Framework\Controller\Result\RawFactory;
  13. use Magento\Backend\App\Action\Context;
  14. use Magento\Framework\Registry;
  15. use Magento\Sales\Model\Service\InvoiceService;
  16. use Magento\Sales\Controller\Adminhtml\Invoice\AbstractInvoice\View as AbstractView;
  17. /**
  18. * Class UpdateQty
  19. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  20. */
  21. class UpdateQty extends AbstractView implements HttpPostActionInterface
  22. {
  23. /**
  24. * @var JsonFactory
  25. */
  26. protected $resultJsonFactory;
  27. /**
  28. * @var PageFactory
  29. */
  30. protected $resultPageFactory;
  31. /**
  32. * @var RawFactory
  33. */
  34. protected $resultRawFactory;
  35. /**
  36. * @var InvoiceService
  37. */
  38. private $invoiceService;
  39. /**
  40. * @param Context $context
  41. * @param Registry $registry
  42. * @param \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory
  43. * @param PageFactory $resultPageFactory
  44. * @param JsonFactory $resultJsonFactory
  45. * @param RawFactory $resultRawFactory
  46. * @param InvoiceService $invoiceService
  47. */
  48. public function __construct(
  49. Context $context,
  50. Registry $registry,
  51. \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory,
  52. PageFactory $resultPageFactory,
  53. JsonFactory $resultJsonFactory,
  54. RawFactory $resultRawFactory,
  55. InvoiceService $invoiceService
  56. ) {
  57. $this->resultPageFactory = $resultPageFactory;
  58. $this->resultJsonFactory = $resultJsonFactory;
  59. $this->resultRawFactory = $resultRawFactory;
  60. $this->invoiceService = $invoiceService;
  61. parent::__construct($context, $registry, $resultForwardFactory);
  62. }
  63. /**
  64. * Update items qty action
  65. *
  66. * @return \Magento\Framework\Controller\ResultInterface
  67. */
  68. public function execute()
  69. {
  70. try {
  71. $orderId = $this->getRequest()->getParam('order_id');
  72. $invoiceData = $this->getRequest()->getParam('invoice', []);
  73. $invoiceItems = isset($invoiceData['items']) ? $invoiceData['items'] : [];
  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. // Save invoice comment text in current invoice object in order to display it in corresponding view
  92. $invoiceRawCommentText = $invoiceData['comment_text'];
  93. $invoice->setCommentText($invoiceRawCommentText);
  94. /** @var \Magento\Backend\Model\View\Result\Page $resultPage */
  95. $resultPage = $this->resultPageFactory->create();
  96. $resultPage->getConfig()->getTitle()->prepend(__('Invoices'));
  97. $response = $resultPage->getLayout()->getBlock('order_items')->toHtml();
  98. } catch (LocalizedException $e) {
  99. $response = ['error' => true, 'message' => $e->getMessage()];
  100. } catch (\Exception $e) {
  101. $response = ['error' => true, 'message' => __('Cannot update item quantity.')];
  102. }
  103. if (is_array($response)) {
  104. /** @var \Magento\Framework\Controller\Result\Json $resultJson */
  105. $resultJson = $this->resultJsonFactory->create();
  106. $resultJson->setData($response);
  107. return $resultJson;
  108. } else {
  109. /** @var \Magento\Framework\Controller\Result\Raw $resultRaw */
  110. $resultRaw = $this->resultRawFactory->create();
  111. $resultRaw->setContents($response);
  112. return $resultRaw;
  113. }
  114. }
  115. }