0
votes

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.

1

1 Answers

0
votes

If we need to create invoice programmatically for an order then you can create by following way.

// 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();
}

I hope it will help you.