Exemple #1
0
 /**
  * Add comment to invoice action
  *
  * @return void
  */
 public function execute()
 {
     try {
         $this->getRequest()->setParam('invoice_id', $this->getRequest()->getParam('id'));
         $data = $this->getRequest()->getPost('comment');
         if (empty($data['comment'])) {
             throw new Exception(__('The Comment Text field cannot be empty.'));
         }
         $this->_title->add(__('Invoices'));
         $invoice = $this->invoiceLoader->load($this->_request);
         $invoice->addComment($data['comment'], isset($data['is_customer_notified']), isset($data['is_visible_on_front']));
         $invoice->sendUpdateEmail(!empty($data['is_customer_notified']), $data['comment']);
         $invoice->save();
         $this->_view->loadLayout();
         $response = $this->_view->getLayout()->getBlock('invoice_comments')->toHtml();
     } catch (Exception $e) {
         $response = array('error' => true, 'message' => $e->getMessage());
     } catch (\Exception $e) {
         $response = array('error' => true, 'message' => __('Cannot add new comment.'));
     }
     if (is_array($response)) {
         $response = $this->_objectManager->get('Magento\\Core\\Helper\\Data')->jsonEncode($response);
         $this->getResponse()->representJson($response);
     } else {
         $this->getResponse()->setBody($response);
     }
 }
Exemple #2
0
 /**
  * Update items qty action
  *
  * @return void
  */
 public function execute()
 {
     try {
         $this->_title->add(__('Invoices'));
         $orderId = $this->getRequest()->getParam('order_id');
         $invoiceId = $this->getRequest()->getParam('invoice_id');
         $invoiceData = $this->getRequest()->getParam('invoice', []);
         $invoiceItems = isset($invoiceData['items']) ? $invoiceData['items'] : [];
         $invoice = $this->invoiceLoader->load($orderId, $invoiceId, $invoiceItems);
         // Save invoice comment text in current invoice object in order to display it in corresponding view
         $invoiceRawCommentText = $invoiceData['comment_text'];
         $invoice->setCommentText($invoiceRawCommentText);
         $this->_view->loadLayout();
         $response = $this->_view->getLayout()->getBlock('order_items')->toHtml();
     } catch (Exception $e) {
         $response = array('error' => true, 'message' => $e->getMessage());
     } catch (\Exception $e) {
         $response = array('error' => true, 'message' => __('Cannot update item quantity.'));
     }
     if (is_array($response)) {
         $response = $this->_objectManager->get('Magento\\Core\\Helper\\Data')->jsonEncode($response);
         $this->getResponse()->representJson($response);
     } else {
         $this->getResponse()->setBody($response);
     }
 }
Exemple #3
0
 /**
  * Create pdf for current invoice
  *
  * @return ResponseInterface|void
  */
 public function execute()
 {
     $orderId = $this->getRequest()->getParam('order_id');
     $invoiceId = $this->getRequest()->getParam('invoice_id');
     $invoiceData = $this->getRequest()->getParam('invoice', []);
     $invoiceData = isset($invoiceData['items']) ? $invoiceData['items'] : [];
     $this->invoiceLoader->load($orderId, $invoiceId, $invoiceData);
     parent::execute();
 }
Exemple #4
0
 /**
  * @param Invoice $dataObject
  * @return \Magento\Sales\Model\Order\Invoice
  * @throws \Exception
  */
 public function getModel(Invoice $dataObject)
 {
     $items = [];
     /** @var InvoiceItem $item */
     foreach ($dataObject->getItems() as $item) {
         $items[$item->getOrderItemId()] = $item->getQty();
     }
     return $this->invoiceLoader->setOrderId($dataObject->getOrderId())->setInvoiceId($dataObject->getEntityId())->setInvoiceItems($items)->create();
 }
Exemple #5
0
 /**
  * Invoice information page
  *
  * @return void
  */
 public function execute()
 {
     $this->_title->add(__('Invoices'));
     $invoice = $this->invoiceLoader->load($this->_request);
     if ($invoice) {
         $this->_title->add(sprintf("#%s", $invoice->getIncrementId()));
         $this->_view->loadLayout();
         $this->_setActiveMenu('Magento_Sales::sales_order');
         $this->_view->getLayout()->getBlock('sales_invoice_view')->updateBackButtonUrl($this->getRequest()->getParam('come_from'));
         $this->_view->renderLayout();
     } else {
         $this->_forward('noroute');
     }
 }
Exemple #6
0
 /**
  * Invoice create page
  *
  * @return void
  */
 public function execute()
 {
     $this->_title->add(__('Invoices'));
     $invoice = $this->invoiceLoader->load($this->_request);
     if ($invoice) {
         $this->_title->add(__('New Invoice'));
         $comment = $this->_objectManager->get('Magento\\Backend\\Model\\Session')->getCommentText(true);
         if ($comment) {
             $invoice->setCommentText($comment);
         }
         $this->_view->loadLayout();
         $this->_setActiveMenu('Magento_Sales::sales_order');
         $this->_view->renderLayout();
     } else {
         $this->_redirect('sales/order/view', array('order_id' => $this->getRequest()->getParam('order_id')));
     }
 }
Exemple #7
0
 public function testLoad()
 {
     $orderId = 1;
     $invoiceId = 2;
     $invoiceMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Invoice')->disableOriginalConstructor()->setMethods(['load', 'getId'])->getMock();
     $invoiceMock->expects($this->once())->method('load')->will($this->returnSelf());
     $invoiceMock->expects($this->once())->method('getId')->will($this->returnValue($invoiceId));
     $this->objectManagerMock->expects($this->once())->method('create')->with('Magento\\Sales\\Model\\Order\\Invoice')->will($this->returnValue($invoiceMock));
     $this->assertInstanceOf('Magento\\Sales\\Model\\Order\\Invoice', $this->loader->load($orderId, $invoiceId));
 }
Exemple #8
0
 /**
  * Capture invoice action
  *
  * @return void
  */
 public function execute()
 {
     $invoice = $this->invoiceLoader->load($this->_request);
     if ($invoice) {
         try {
             $invoice->capture();
             $invoice->getOrder()->setIsInProcess(true);
             $this->_objectManager->create('Magento\\Framework\\DB\\Transaction')->addObject($invoice)->addObject($invoice->getOrder())->save();
             $this->messageManager->addSuccess(__('The invoice has been captured.'));
         } catch (Exception $e) {
             $this->messageManager->addError($e->getMessage());
         } catch (\Exception $e) {
             $this->messageManager->addError(__('Invoice capturing error'));
         }
         $this->_redirect('sales/*/view', array('invoice_id' => $invoice->getId()));
     } else {
         $this->_forward('noroute');
     }
 }
Exemple #9
0
 /**
  * Cancel invoice action
  *
  * @return void
  */
 public function execute()
 {
     $orderId = $this->getRequest()->getParam('order_id');
     $invoiceId = $this->getRequest()->getParam('invoice_id');
     $invoiceData = $this->getRequest()->getParam('invoice', []);
     $invoiceData = isset($invoiceData['items']) ? $invoiceData['items'] : [];
     $invoice = $this->invoiceLoader->load($orderId, $invoiceId, $invoiceData);
     if ($invoice) {
         try {
             $invoice->cancel();
             $invoice->getOrder()->setIsInProcess(true);
             $this->_objectManager->create('Magento\\Framework\\DB\\Transaction')->addObject($invoice)->addObject($invoice->getOrder())->save();
             $this->messageManager->addSuccess(__('You canceled the invoice.'));
         } catch (Exception $e) {
             $this->messageManager->addError($e->getMessage());
         } catch (\Exception $e) {
             $this->messageManager->addError(__('Invoice canceling error'));
         }
         $this->_redirect('sales/*/view', array('invoice_id' => $invoice->getId()));
     } else {
         $this->_forward('noroute');
     }
 }
Exemple #10
0
 /**
  * test for Invoice converter
  */
 public function testGetModel()
 {
     $orderId = 1;
     $invoiceId = 2;
     $itemId = 3;
     $itemQty = 4;
     $this->invoiceMock->expects($this->once())->method('getOrderId')->will($this->returnValue($orderId));
     $this->invoiceMock->expects($this->once())->method('getEntityId')->will($this->returnValue($invoiceId));
     $this->invoiceMock->expects($this->once())->method('getItems')->will($this->returnValue([$this->invoiceItemMock]));
     $this->invoiceItemMock->expects($this->once())->method('getOrderItemId')->will($this->returnValue($itemId));
     $this->invoiceItemMock->expects($this->once())->method('getQty')->will($this->returnValue($itemQty));
     $this->invoiceLoaderMock->expects($this->once())->method('setOrderId')->with($this->equalTo($orderId))->will($this->returnSelf());
     $this->invoiceLoaderMock->expects($this->once())->method('setInvoiceId')->with($this->equalTo($invoiceId))->will($this->returnSelf());
     $this->invoiceLoaderMock->expects($this->once())->method('setInvoiceItems')->with($this->equalTo([$itemId => $itemQty]))->will($this->returnSelf());
     $this->invoiceLoaderMock->expects($this->once())->method('create')->will($this->returnValue($this->modelInvoiceMock));
     $this->invoiceLoaderMock->expects($this->once())->method('create')->will($this->returnValue($this->modelInvoiceMock));
     $this->assertInstanceOf('Magento\\Sales\\Model\\Order\\Invoice', $this->converter->getModel($this->invoiceMock));
 }
Exemple #11
0
 /**
  * Save invoice
  * We can save only new invoice. Existing invoices are not editable
  *
  * @return void
  */
 public function execute()
 {
     $data = $this->getRequest()->getPost('invoice');
     $orderId = $this->getRequest()->getParam('order_id');
     if (!empty($data['comment_text'])) {
         $this->_objectManager->get('Magento\\Backend\\Model\\Session')->setCommentText($data['comment_text']);
     }
     try {
         $invoiceId = $this->getRequest()->getParam('invoice_id');
         $invoiceData = $this->getRequest()->getParam('invoice', []);
         $invoiceData = isset($invoiceData['items']) ? $invoiceData['items'] : [];
         /** @var Invoice $invoice */
         $invoice = $this->invoiceLoader->load($orderId, $invoiceId, $invoiceData);
         if ($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->register();
             if (!empty($data['send_email'])) {
                 $invoice->setEmailSent(true);
             }
             $invoice->getOrder()->setCustomerNoteNotify(!empty($data['send_email']));
             $invoice->getOrder()->setIsInProcess(true);
             $transactionSave = $this->_objectManager->create('Magento\\Framework\\DB\\Transaction')->addObject($invoice)->addObject($invoice->getOrder());
             $shipment = false;
             if (!empty($data['do_shipment']) || (int) $invoice->getOrder()->getForcedShipmentWithInvoice()) {
                 $shipment = $this->_prepareShipment($invoice);
                 if ($shipment) {
                     $shipment->setEmailSent($invoice->getEmailSent());
                     $transactionSave->addObject($shipment);
                 }
             }
             $transactionSave->save();
             if (isset($shippingResponse) && $shippingResponse->hasErrors()) {
                 $this->messageManager->addError(__('The invoice and the shipment  have been created. ' . 'The shipping label cannot be created now.'));
             } elseif (!empty($data['do_shipment'])) {
                 $this->messageManager->addSuccess(__('You created the invoice and shipment.'));
             } else {
                 $this->messageManager->addSuccess(__('The invoice has been created.'));
             }
             // send invoice/shipment emails
             $comment = '';
             if (isset($data['comment_customer_notify'])) {
                 $comment = $data['comment_text'];
             }
             try {
                 $this->invoiceCommentSender->send($invoice, !empty($data['send_email']), $comment);
             } catch (\Exception $e) {
                 $this->_objectManager->get('Magento\\Framework\\Logger')->logException($e);
                 $this->messageManager->addError(__('We can\'t send the invoice email.'));
             }
             if ($shipment) {
                 try {
                     $this->shipmentSender->send($shipment, !empty($data['send_email']));
                 } catch (\Exception $e) {
                     $this->_objectManager->get('Magento\\Framework\\Logger')->logException($e);
                     $this->messageManager->addError(__('We can\'t send the shipment.'));
                 }
             }
             $this->_objectManager->get('Magento\\Backend\\Model\\Session')->getCommentText(true);
             $this->_redirect('sales/order/view', array('order_id' => $orderId));
         } else {
             $this->_redirect('sales/*/new', array('order_id' => $orderId));
         }
         return;
     } catch (Exception $e) {
         $this->messageManager->addError($e->getMessage());
     } catch (\Exception $e) {
         $this->messageManager->addError(__('We can\'t save the invoice.'));
         $this->_objectManager->get('Magento\\Framework\\Logger')->logException($e);
     }
     $this->_redirect('sales/*/new', array('order_id' => $orderId));
 }
Exemple #12
0
 /**
  * Create pdf for current invoice
  *
  * @return ResponseInterface|void
  */
 public function execute()
 {
     $this->invoiceLoader->load($this->_request);
     parent::execute();
 }