/** * Add comment to shipment history * * @return void */ public function execute() { try { $this->getRequest()->setParam('shipment_id', $this->getRequest()->getParam('id')); $data = $this->getRequest()->getPost('comment'); if (empty($data['comment'])) { throw new \Magento\Framework\Model\Exception(__("The comment text field cannot be empty.")); } $this->_title->add(__('Shipments')); $this->shipmentLoader->setOrderId($this->getRequest()->getParam('order_id')); $this->shipmentLoader->setShipmentId($this->getRequest()->getParam('shipment_id')); $this->shipmentLoader->setShipment($this->getRequest()->getParam('shipment')); $this->shipmentLoader->setTracking($this->getRequest()->getParam('tracking')); $shipment = $this->shipmentLoader->load(); $shipment->addComment($data['comment'], isset($data['is_customer_notified']), isset($data['is_visible_on_front'])); $this->shipmentSender->send($shipment, !empty($data['is_customer_notified']), $data['comment']); $shipment->save(); $this->_view->loadLayout(false); $response = $this->_view->getLayout()->getBlock('shipment_comments')->toHtml(); } catch (\Magento\Framework\Model\Exception $e) { $response = ['error' => true, 'message' => $e->getMessage()]; } catch (\Exception $e) { $response = ['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); } }
/** * @param int $configValue * @param bool|null $forceSyncMode * @param bool|null $customerNoteNotify * @param bool|null $emailSendingResult * @dataProvider sendDataProvider * @return void * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testSend($configValue, $forceSyncMode, $customerNoteNotify, $emailSendingResult) { $comment = 'comment_test'; $address = 'address_test'; $configPath = 'sales_email/general/async_sending'; $this->shipmentMock->expects($this->once())->method('setSendEmail')->with(true); $this->globalConfig->expects($this->once())->method('getValue')->with($configPath)->willReturn($configValue); if (!$configValue || $forceSyncMode) { $addressMock = $this->getMock('Magento\\Sales\\Model\\Order\\Address', [], [], '', false); $this->addressRenderer->expects($this->any())->method('format')->with($addressMock, 'html')->willReturn($address); $this->orderMock->expects($this->any())->method('getBillingAddress')->willReturn($addressMock); $this->orderMock->expects($this->any())->method('getShippingAddress')->willReturn($addressMock); $this->shipmentMock->expects($this->once())->method('getCustomerNoteNotify')->willReturn($customerNoteNotify); $this->shipmentMock->expects($this->any())->method('getCustomerNote')->willReturn($comment); $this->templateContainerMock->expects($this->once())->method('setTemplateVars')->with(['order' => $this->orderMock, 'shipment' => $this->shipmentMock, 'comment' => $customerNoteNotify ? $comment : '', 'billing' => $addressMock, 'payment_html' => 'payment', 'store' => $this->storeMock, 'formattedShippingAddress' => $address, 'formattedBillingAddress' => $address]); $this->identityContainerMock->expects($this->once())->method('isEnabled')->willReturn($emailSendingResult); if ($emailSendingResult) { $this->senderBuilderFactoryMock->expects($this->once())->method('create')->willReturn($this->senderMock); $this->senderMock->expects($this->once())->method('send'); $this->senderMock->expects($this->once())->method('sendCopyTo'); $this->shipmentMock->expects($this->once())->method('setEmailSent')->with(true); $this->shipmentResourceMock->expects($this->once())->method('saveAttribute')->with($this->shipmentMock, ['send_email', 'email_sent']); $this->assertTrue($this->sender->send($this->shipmentMock)); } else { $this->shipmentResourceMock->expects($this->once())->method('saveAttribute')->with($this->shipmentMock, 'send_email'); $this->assertFalse($this->sender->send($this->shipmentMock)); } } else { $this->shipmentResourceMock->expects($this->once())->method('saveAttribute')->with($this->shipmentMock, 'send_email'); $this->assertFalse($this->sender->send($this->shipmentMock)); } }
/** * @param bool $isVirtualOrder * @param int $formatCallCount * @param string|null $expectedShippingAddress * @dataProvider sendVirtualOrderDataProvider */ public function testSendVirtualOrder($isVirtualOrder, $formatCallCount, $expectedShippingAddress) { $address = 'address_test'; $this->orderMock->setData(\Magento\Sales\Api\Data\OrderInterface::IS_VIRTUAL, $isVirtualOrder); $this->shipmentMock->expects($this->once())->method('setSendEmail')->with(true); $this->globalConfig->expects($this->once())->method('getValue')->with('sales_email/general/async_sending')->willReturn(false); $addressMock = $this->getMock('Magento\\Sales\\Model\\Order\\Address', [], [], '', false); $this->addressRenderer->expects($this->exactly($formatCallCount))->method('format')->with($addressMock, 'html')->willReturn($address); $this->stepAddressFormat($addressMock, $isVirtualOrder); $this->shipmentMock->expects($this->once())->method('getCustomerNoteNotify')->willReturn(false); $this->templateContainerMock->expects($this->once())->method('setTemplateVars')->with(['order' => $this->orderMock, 'shipment' => $this->shipmentMock, 'comment' => '', 'billing' => $addressMock, 'payment_html' => 'payment', 'store' => $this->storeMock, 'formattedShippingAddress' => $expectedShippingAddress, 'formattedBillingAddress' => $address]); $this->identityContainerMock->expects($this->once())->method('isEnabled')->willReturn(false); $this->shipmentResourceMock->expects($this->once())->method('saveAttribute')->with($this->shipmentMock, 'send_email'); $this->assertFalse($this->sender->send($this->shipmentMock)); }
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, 'shipment' => $this->shipmentMock, 'billing' => $billingAddress, 'payment_html' => 'payment', '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->shipmentMock, false, $comment); $this->assertTrue($result); }
/** * 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\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.')); } /** @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\LocalizedException(__('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->setCustomerNote($data['comment_text']); $invoice->setCustomerNoteNotify(isset($data['comment_customer_notify'])); } $invoice->register(); $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) { $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 try { if (!empty($data['send_email'])) { $this->invoiceSender->send($invoice); } } catch (\Exception $e) { $this->_objectManager->get('Psr\\Log\\LoggerInterface')->critical($e); $this->messageManager->addError(__('We can\'t send the invoice email.')); } if ($shipment) { try { if (!empty($data['send_email'])) { $this->shipmentSender->send($shipment); } } 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 shipment * We can save only new shipment. Existing shipments are not editable * * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function execute() { /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultRedirectFactory->create(); $formKeyIsValid = $this->_formKeyValidator->validate($this->getRequest()); $isPost = $this->getRequest()->isPost(); if (!$formKeyIsValid || !$isPost) { $this->messageManager->addError(__('We can\'t save the shipment right now.')); return $resultRedirect->setPath('sales/order/index'); } $data = $this->getRequest()->getParam('shipment'); if (!empty($data['comment_text'])) { $this->_objectManager->get('Magento\\Backend\\Model\\Session')->setCommentText($data['comment_text']); } try { $this->shipmentLoader->setOrderId($this->getRequest()->getParam('order_id')); $this->shipmentLoader->setShipmentId($this->getRequest()->getParam('shipment_id')); $this->shipmentLoader->setShipment($data); $this->shipmentLoader->setTracking($this->getRequest()->getParam('tracking')); $shipment = $this->shipmentLoader->load(); if (!$shipment) { $this->_forward('noroute'); return; } if (!empty($data['comment_text'])) { $shipment->addComment($data['comment_text'], isset($data['comment_customer_notify']), isset($data['is_visible_on_front'])); $shipment->setCustomerNote($data['comment_text']); $shipment->setCustomerNoteNotify(isset($data['comment_customer_notify'])); } $shipment->register(); $shipment->getOrder()->setCustomerNoteNotify(!empty($data['send_email'])); $responseAjax = new \Magento\Framework\DataObject(); $isNeedCreateLabel = isset($data['create_shipping_label']) && $data['create_shipping_label']; if ($isNeedCreateLabel) { $this->labelGenerator->create($shipment, $this->_request); $responseAjax->setOk(true); } $this->_saveShipment($shipment); if (!empty($data['send_email'])) { $this->shipmentSender->send($shipment); } $shipmentCreatedMessage = __('The shipment has been created.'); $labelCreatedMessage = __('You created the shipping label.'); $this->messageManager->addSuccess($isNeedCreateLabel ? $shipmentCreatedMessage . ' ' . $labelCreatedMessage : $shipmentCreatedMessage); $this->_objectManager->get('Magento\\Backend\\Model\\Session')->getCommentText(true); } catch (\Magento\Framework\Exception\LocalizedException $e) { if ($isNeedCreateLabel) { $responseAjax->setError(true); $responseAjax->setMessage($e->getMessage()); } else { $this->messageManager->addError($e->getMessage()); $this->_redirect('*/*/new', ['order_id' => $this->getRequest()->getParam('order_id')]); } } catch (\Exception $e) { $this->_objectManager->get('Psr\\Log\\LoggerInterface')->critical($e); if ($isNeedCreateLabel) { $responseAjax->setError(true); $responseAjax->setMessage(__('An error occurred while creating shipping label.')); } else { $this->messageManager->addError(__('Cannot save shipment.')); $this->_redirect('*/*/new', ['order_id' => $this->getRequest()->getParam('order_id')]); } } if ($isNeedCreateLabel) { $this->getResponse()->representJson($responseAjax->toJson()); } else { $this->_redirect('sales/order/view', ['order_id' => $shipment->getOrderId()]); } }
/** * Save shipment * We can save only new shipment. Existing shipments are not editable * * @return void */ public function execute() { $data = $this->getRequest()->getParam('shipment'); if (!empty($data['comment_text'])) { $this->_objectManager->get('Magento\\Backend\\Model\\Session')->setCommentText($data['comment_text']); } try { $this->shipmentLoader->setOrderId($this->getRequest()->getParam('order_id')); $this->shipmentLoader->setShipmentId($this->getRequest()->getParam('shipment_id')); $this->shipmentLoader->setShipment($data); $this->shipmentLoader->setTracking($this->getRequest()->getParam('tracking')); $shipment = $this->shipmentLoader->load(); if (!$shipment) { $this->_forward('noroute'); return; } $shipment->register(); $comment = ''; if (!empty($data['comment_text'])) { $shipment->addComment($data['comment_text'], isset($data['comment_customer_notify']), isset($data['is_visible_on_front'])); if (isset($data['comment_customer_notify'])) { $comment = $data['comment_text']; } } if (!empty($data['send_email'])) { $shipment->setEmailSent(true); } $shipment->getOrder()->setCustomerNoteNotify(!empty($data['send_email'])); $responseAjax = new \Magento\Framework\Object(); $isNeedCreateLabel = isset($data['create_shipping_label']) && $data['create_shipping_label']; if ($isNeedCreateLabel) { $this->labelGenerator->create($shipment, $this->_request); $responseAjax->setOk(true); } $this->_saveShipment($shipment); $this->shipmentSender->send($shipment, !empty($data['send_email']), $comment); $shipmentCreatedMessage = __('The shipment has been created.'); $labelCreatedMessage = __('You created the shipping label.'); $this->messageManager->addSuccess($isNeedCreateLabel ? $shipmentCreatedMessage . ' ' . $labelCreatedMessage : $shipmentCreatedMessage); $this->_objectManager->get('Magento\\Backend\\Model\\Session')->getCommentText(true); } catch (\Magento\Framework\Model\Exception $e) { if ($isNeedCreateLabel) { $responseAjax->setError(true); $responseAjax->setMessage($e->getMessage()); } else { $this->messageManager->addError($e->getMessage()); $this->_redirect('*/*/new', array('order_id' => $this->getRequest()->getParam('order_id'))); } } catch (\Exception $e) { $this->_objectManager->get('Magento\\Framework\\Logger')->logException($e); if ($isNeedCreateLabel) { $responseAjax->setError(true); $responseAjax->setMessage(__('An error occurred while creating shipping label.')); } else { $this->messageManager->addError(__('Cannot save shipment.')); $this->_redirect('*/*/new', array('order_id' => $this->getRequest()->getParam('order_id'))); } } if ($isNeedCreateLabel) { $this->getResponse()->representJson($responseAjax->toJson()); } else { $this->_redirect('sales/order/view', ['order_id' => $shipment->getOrderId()]); } }
/** * 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)); }