/** * @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->invoiceMock->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->invoiceMock->expects($this->once())->method('getCustomerNoteNotify')->willReturn($customerNoteNotify); $this->invoiceMock->expects($this->any())->method('getCustomerNote')->willReturn($comment); $this->templateContainerMock->expects($this->once())->method('setTemplateVars')->with(['order' => $this->orderMock, 'invoice' => $this->invoiceMock, '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->invoiceMock->expects($this->once())->method('setEmailSent')->with(true); $this->invoiceResourceMock->expects($this->once())->method('saveAttribute')->with($this->invoiceMock, ['send_email', 'email_sent']); $this->assertTrue($this->sender->send($this->invoiceMock)); } else { $this->invoiceResourceMock->expects($this->once())->method('saveAttribute')->with($this->invoiceMock, 'send_email'); $this->assertFalse($this->sender->send($this->invoiceMock)); } } else { $this->invoiceResourceMock->expects($this->once())->method('saveAttribute')->with($this->invoiceMock, 'send_email'); $this->assertFalse($this->sender->send($this->invoiceMock)); } }
/** * @param bool $isVirtualOrder * @param int $formatCallCount * @param string|null $expectedShippingAddress * @dataProvider sendVirtualOrderDataProvider */ public function testSendVirtualOrder($isVirtualOrder, $formatCallCount, $expectedShippingAddress) { $billingAddress = 'address_test'; $this->orderMock->setData(\Magento\Sales\Api\Data\OrderInterface::IS_VIRTUAL, $isVirtualOrder); $this->invoiceMock->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($billingAddress); $this->stepAddressFormat($addressMock, $isVirtualOrder); $this->invoiceMock->expects($this->once())->method('getCustomerNoteNotify')->willReturn(false); $this->templateContainerMock->expects($this->once())->method('setTemplateVars')->with(['order' => $this->orderMock, 'invoice' => $this->invoiceMock, 'comment' => '', 'billing' => $addressMock, 'payment_html' => 'payment', 'store' => $this->storeMock, 'formattedShippingAddress' => $expectedShippingAddress, 'formattedBillingAddress' => $billingAddress]); $this->identityContainerMock->expects($this->once())->method('isEnabled')->willReturn(false); $this->invoiceResourceMock->expects($this->once())->method('saveAttribute')->with($this->invoiceMock, 'send_email'); $this->assertFalse($this->sender->send($this->invoiceMock)); }
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, 'invoice' => $this->invoiceMock, '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->invoiceMock, false, $comment); $this->assertTrue($result); }
protected function _processInvoice() { if ($this->_order->canInvoice()) { $invoice = $this->_order->prepareInvoice(); switch ($this->_paymentInst->getConfigPaymentAction()) { case \Magento\Payment\Model\Method\AbstractMethod::ACTION_AUTHORIZE: $invoice->register(); $this->_order->setState(\Magento\Sales\Model\Order::STATE_PROCESSING, 'authorized'); break; case \Magento\Payment\Model\Method\AbstractMethod::ACTION_AUTHORIZE_CAPTURE: $this->_paymentInst->setAssistCaptureResponse(true); $invoice->register()->capture(); break; } /** @var \Magento\Framework\DB\Transaction $transaction */ $transaction = $this->_transactionFactory->create(); $transaction->addObject($invoice)->addObject($invoice->getOrder())->save(); $this->_invoiceSender->send($invoice); } elseif ($this->_order->isCanceled()) { throw new \Magento\Framework\Exception\LocalizedException(__('Order canceled')); } else { throw new \Magento\Framework\Exception\LocalizedException(__('Order paid')); } }
/** * 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]); }
protected function _createInvoice($order, $message) { if (!$order->hasInvoices()) { $invoice = $order->prepareInvoice(); $invoice->register(); $invoice->pay(); $this->_transactionFactory->create()->addObject($invoice)->addObject($invoice->getOrder())->save(); $this->_invoiceSender->send($invoice, true, $message); } }