I am facing payment due (Shipping & Handling) issue while creating invoice in magento 2.
If payment method is Purchase order we create invoice at the time of shipment programtically.
But at that time it only creating invoice for item cost and excluding Shipping & Handling Amount therefore it shows Amount due in Total Due.
Below is my code to create invoice programtically. -
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);
}
}
How can we force to create complete invoice if there is due or create new invoice to pending due amout ? ANy help would be appricaited.