/** * 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')); $orderId = $this->getRequest()->getParam('order_id'); $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); $invoice->addComment($data['comment'], isset($data['is_customer_notified']), isset($data['is_visible_on_front'])); $this->invoiceCommentSender->send($invoice, !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); } }
public function testSendVirtualOrder() { $isVirtualOrder = true; $this->orderMock->setData(\Magento\Sales\Api\Data\OrderInterface::IS_VIRTUAL, $isVirtualOrder); $this->stepAddressFormat($this->addressMock, $isVirtualOrder); $this->identityContainerMock->expects($this->once())->method('isEnabled')->will($this->returnValue(false)); $this->templateContainerMock->expects($this->once())->method('setTemplateVars')->with($this->equalTo(['order' => $this->orderMock, 'invoice' => $this->invoiceMock, 'billing' => $this->addressMock, 'comment' => '', 'store' => $this->storeMock, 'formattedShippingAddress' => null, 'formattedBillingAddress' => 1])); $this->assertFalse($this->sender->send($this->invoiceMock)); }
public function testSendTrueWithoutCustomerCopy() { $billingAddress = $this->addressMock; $this->stepAddressFormat($billingAddress); $comment = 'comment_test'; $this->orderMock->expects($this->once())->method('getCustomerIsGuest')->will($this->returnValue(false)); $this->identityContainerMock->expects($this->once())->method('isEnabled')->will($this->returnValue(true)); $this->templateContainerMock->expects($this->once())->method('setTemplateVars')->with($this->equalTo(['order' => $this->orderMock, 'invoice' => $this->invoiceMock, 'billing' => $billingAddress, 'comment' => $comment, 'store' => $this->storeMock, 'formattedShippingAddress' => 1, 'formattedBillingAddress' => 1])); $this->stepSendWithCallSendCopyTo(); $result = $this->sender->send($this->invoiceMock, false, $comment); $this->assertTrue($result); }
public function testSendTrueWithoutCustomerCopy() { $billingAddress = 'billing_address'; $comment = 'comment_test'; $this->orderMock->expects($this->once())->method('getCustomerIsGuest')->will($this->returnValue(false)); $this->orderMock->expects($this->any())->method('getBillingAddress')->will($this->returnValue($billingAddress)); $this->identityContainerMock->expects($this->once())->method('isEnabled')->will($this->returnValue(true)); $this->templateContainerMock->expects($this->once())->method('setTemplateVars')->with($this->equalTo(['order' => $this->orderMock, 'creditmemo' => $this->creditmemoMock, 'billing' => $billingAddress, 'comment' => $comment, 'store' => $this->storeMock])); $senderMock = $this->getMock('Magento\\Sales\\Model\\Order\\Email\\Sender', ['send', 'sendCopyTo'], [], '', false); $senderMock->expects($this->never())->method('send'); $senderMock->expects($this->once())->method('sendCopyTo'); $this->senderBuilderFactoryMock->expects($this->once())->method('create')->will($this->returnValue($senderMock)); $result = $this->sender->send($this->creditmemoMock, false, $comment); $this->assertTrue($result); }
/** * Add comment to invoice action * * @return \Magento\Framework\Controller\ResultInterface */ public function execute() { try { $this->getRequest()->setParam('invoice_id', $this->getRequest()->getParam('id')); $data = $this->getRequest()->getPost('comment'); if (empty($data['comment'])) { throw new LocalizedException(__('Please enter a comment.')); } $invoice = $this->getInvoice(); if (!$invoice) { /** @var \Magento\Backend\Model\View\Result\Forward $resultForward */ $resultForward = $this->resultForwardFactory->create(); return $resultForward->forward('noroute'); } $invoice->addComment($data['comment'], isset($data['is_customer_notified']), isset($data['is_visible_on_front'])); $this->invoiceCommentSender->send($invoice, !empty($data['is_customer_notified']), $data['comment']); $invoice->save(); /** @var \Magento\Backend\Model\View\Result\Page $resultPage */ $resultPage = $this->resultPageFactory->create(); $resultPage->getConfig()->getTitle()->prepend(__('Invoices')); $response = $resultPage->getLayout()->getBlock('invoice_comments')->toHtml(); } catch (LocalizedException $e) { $response = ['error' => true, 'message' => $e->getMessage()]; } catch (\Exception $e) { $response = ['error' => true, 'message' => __('Cannot add new comment.')]; } 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; } }
/** * Save invoice * We can save only new invoice. Existing invoices are not editable * * @return \Magento\Framework\Controller\ResultInterface * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ 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']); } /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultRedirectFactory->create(); try { $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')->load($orderId); if (!$order->getId()) { throw new \Magento\Framework\Exception(__('The order no longer exists.')); } if (!$order->canInvoice()) { throw new \Magento\Framework\Exception(__('The order does not allow an invoice to be created.')); } /** @var \Magento\Sales\Model\Order\Invoice $invoice */ $invoice = $this->_objectManager->create('Magento\\Sales\\Model\\Service\\Order', ['order' => $order])->prepareInvoice($invoiceItems); if (!$invoice) { throw new LocalizedException(__('We can\'t save the invoice.')); } if (!$invoice->getTotalQty()) { throw new \Magento\Framework\Exception(__('Cannot create an invoice without products.')); } $this->registry->register('current_invoice', $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('Psr\\Log\\LoggerInterface')->critical($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('Psr\\Log\\LoggerInterface')->critical($e); $this->messageManager->addError(__('We can\'t send the shipment.')); } } $this->_objectManager->get('Magento\\Backend\\Model\\Session')->getCommentText(true); return $resultRedirect->setPath('sales/order/view', ['order_id' => $orderId]); } catch (LocalizedException $e) { $this->messageManager->addError($e->getMessage()); } catch (\Exception $e) { $this->messageManager->addError(__('We can\'t save the invoice.')); $this->_objectManager->get('Psr\\Log\\LoggerInterface')->critical($e); } return $resultRedirect->setPath('sales/*/new', ['order_id' => $orderId]); }
/** * 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)); }