resultPageFactory = $resultPageFactory; $this->resultJsonFactory = $resultJsonFactory; $this->resultRawFactory = $resultRawFactory; $this->invoiceService = $invoiceService; parent::__construct($context, $registry, $resultForwardFactory); } /** * Update items qty action * * @return \Magento\Framework\Controller\ResultInterface */ public function execute() { try { $orderId = $this->getRequest()->getParam('order_id'); $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->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); // Save invoice comment text in current invoice object in order to display it in corresponding view $invoiceRawCommentText = $invoiceData['comment_text']; $invoice->setCommentText($invoiceRawCommentText); /** @var \Magento\Backend\Model\View\Result\Page $resultPage */ $resultPage = $this->resultPageFactory->create(); $resultPage->getConfig()->getTitle()->prepend(__('Invoices')); $response = $resultPage->getLayout()->getBlock('order_items')->toHtml(); } catch (LocalizedException $e) { $response = ['error' => true, 'message' => $e->getMessage()]; } catch (\Exception $e) { $response = ['error' => true, 'message' => __('Cannot update item quantity.')]; } if (is_array($response)) { /** @var \Magento\Framework\Controller\Result\Json $resultJson */ $resultJson = $this->resultJsonFactory->create(); $resultJson->setData($response); return $resultJson; } else { /** @var \Magento\Framework\Controller\Result\Raw $resultRaw */ $resultRaw = $this->resultRawFactory->create(); $resultRaw->setContents($response); return $resultRaw; } } }