我在magento 2中创建发票时面临付款到期(发货和处理)问题。
如果付款方式是采购订单,我们会在发货时以编程方式创建发票。
但当时它只为物品成本创建发票,不包括运费和发货因此,处理金额显示应付总金额。
以下是我以编程方式创建发票的代码。 -
if ($order->getTotalDue() > 0)
{
try
{
sleep(2);
if (!$order->canInvoice())
{
// Mage::throwException(Mage::helper('core')->__('Cannot create an invoice.'));
throw new MagentoFrameworkExceptionLocalizedException(__('Cannot create an invoice.'));
}
$invoice = $invoiceService->prepareInvoice($order);
if (!$invoice->getTotalQty())
{
throw new MagentoFrameworkExceptionLocalizedException(__('Cannot create an invoice without products.'));
}
// $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
// $invoice = $order->prepareInvoice($qtyToInvoice);
if ($order->getTotalDue() > 0) $invoice->setRequestedCaptureCase(MagentoSalesModelOrderInvoice::CAPTURE_ONLINE);
else $invoice->setRequestedCaptureCase(MagentoSalesModelOrderInvoice::NOT_CAPTURE);
$invoice->register();
$invoice->save();
$transactionSave = $transaction->addObject($invoice)->addObject($invoice->getOrder());
$transactionSave->save();
// ### update order settlement status
$sql = "UPDATE sales_order_grid SET settlement_status = 'settled' WHERE entity_id = " . $order->getId();
// $connection->query($sql) ;
}
catch(Exception $e)
{
// ## send error notification
// $content = "File Name : ".$file." \n\n Error with invoice/Cybersource settelment, Order #".$orderId." \n\n Error Message : \n\n".$e->getMessage() ;
$content = "File Name : " . $file . " \n\n Error with invoice/" . $paymentMethod . " settelment, Order #" . $orderId . " \n\n Error Message : \n\n" . $e->getMessage();
$subject = "Order shipment/settelment error " . date("d-y-m H:i:s");
processErrorEmailNotify($content, $subject);
$donecomplete = false;
// ### make log for report
$status = "Error";
logDataFileStatusMessage($file, $content, $status, $vendorlist['username'], $connection);
}
}
如果到期或创建新发票到待定期限,我们如何强制创建完整的发票? 任何帮助都是适当的。
答案 0 :(得分:0)
如果我们需要以编程方式为订单创建发票,那么您可以按照以下方式创建。
// Load the order
$order = $this->_objectManager->create('Magento\Sales\Model\Order')
->loadByAttribute('increment_id', '000000009');
// OR
$order = $this->_objectManager->create('Magento\Sales\Model\Order')
->load('1');
if ($order->canInvoice()) {
// Create invoice for this order
$invoice = $this->_objectManager->create('Magento\Sales\Model\Service\InvoiceService')->prepareInvoice($order);
// Make sure there is a qty on the invoice
if (!$invoice->getTotalQty()) {
throw new \Magento\Framework\Exception\LocalizedException(
__('You can\'t create an invoice without products.')
);
}
// Register as invoice item
$invoice->setRequestedCaptureCase(\Magento\Sales\Model\Order\Invoice::CAPTURE_OFFLINE);
$invoice->register();
// Save the invoice to the order
$transaction = $this->_objectManager->create('Magento\Framework\DB\Transaction')
->addObject($invoice)
->addObject($invoice->getOrder());
$transaction->save();
// Magento\Sales\Model\Order\Email\Sender\InvoiceSender
$this->invoiceSender->send($invoice);
$order->addStatusHistoryComment(
__('Notified customer about invoice #%1.', $invoice->getId())
)
->setIsCustomerNotified(true)
->save();
}
我希望它会对你有所帮助。