Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UpdateQty.php
Go to the documentation of this file.
1 <?php
9 
10 use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
14 use Magento\Framework\Controller\Result\RawFactory;
19 
24 class UpdateQty extends AbstractView implements HttpPostActionInterface
25 {
29  protected $resultJsonFactory;
30 
34  protected $resultPageFactory;
35 
39  protected $resultRawFactory;
40 
44  private $invoiceService;
45 
55  public function __construct(
56  Context $context,
58  \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory,
61  RawFactory $resultRawFactory,
62  InvoiceService $invoiceService
63  ) {
64  $this->resultPageFactory = $resultPageFactory;
65  $this->resultJsonFactory = $resultJsonFactory;
66  $this->resultRawFactory = $resultRawFactory;
67  $this->invoiceService = $invoiceService;
68  parent::__construct($context, $registry, $resultForwardFactory);
69  }
70 
76  public function execute()
77  {
78  try {
79  $orderId = $this->getRequest()->getParam('order_id');
80  $invoiceData = $this->getRequest()->getParam('invoice', []);
81  $invoiceItems = isset($invoiceData['items']) ? $invoiceData['items'] : [];
83  $order = $this->_objectManager->create(\Magento\Sales\Model\Order::class)->load($orderId);
84  if (!$order->getId()) {
85  throw new \Magento\Framework\Exception\LocalizedException(__('The order no longer exists.'));
86  }
87 
88  if (!$order->canInvoice()) {
89  throw new \Magento\Framework\Exception\LocalizedException(
90  __('The order does not allow an invoice to be created.')
91  );
92  }
93 
94  $invoice = $this->invoiceService->prepareInvoice($order, $invoiceItems);
95 
96  if (!$invoice->getTotalQty()) {
97  throw new \Magento\Framework\Exception\LocalizedException(
98  __("The invoice can't be created without products. Add products and try again.")
99  );
100  }
101  $this->registry->register('current_invoice', $invoice);
102  // Save invoice comment text in current invoice object in order to display it in corresponding view
103  $invoiceRawCommentText = $invoiceData['comment_text'];
104  $invoice->setCommentText($invoiceRawCommentText);
105 
107  $resultPage = $this->resultPageFactory->create();
108  $resultPage->getConfig()->getTitle()->prepend(__('Invoices'));
109  $response = $resultPage->getLayout()->getBlock('order_items')->toHtml();
110  } catch (LocalizedException $e) {
111  $response = ['error' => true, 'message' => $e->getMessage()];
112  } catch (\Exception $e) {
113  $response = ['error' => true, 'message' => __('Cannot update item quantity.')];
114  }
115  if (is_array($response)) {
117  $resultJson = $this->resultJsonFactory->create();
118  $resultJson->setData($response);
119  return $resultJson;
120  } else {
122  $resultRaw = $this->resultRawFactory->create();
123  $resultRaw->setContents($response);
124  return $resultRaw;
125  }
126  }
127 }
$response
Definition: 404.php:11
$order
Definition: order.php:55
__()
Definition: __.php:13
$invoice
$invoiceService
__construct(Context $context, Registry $registry, \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory, PageFactory $resultPageFactory, JsonFactory $resultJsonFactory, RawFactory $resultRawFactory, InvoiceService $invoiceService)
Definition: UpdateQty.php:55