How to create an invoice without updating total paid of order.

The following 2 steps help you to create an invoice without updating total paid of order in magento 2. Steps are:

  • Use event sales_order_invoice_pay
  • Write the observer class

In magento, the overall paid by Orders will be increased after shaped an invoice. In that way all time you built a new invoice, Magento records that you received money from customers. In case of using Partial Payment, customers can make the payment many times for an order.

So how can we create the invoice without updating the total paid of order,just follow the below steps :

 Create an invoice in magento

Step 1: use event sales_order_invoice_pay

State observer in event.xml for your custom

module: /app/code/[NameSpace]/[ModuleName]/etc/events.xml

<?xml version="1.0"?>

    <event name="sales_order_invoice_pay">

        <observer name="webpos_sales_order_invoice_pay" instance="[NameSapce]\[ModuleName]\Observer\SalesOrderInvoicePay" />

    </event>  

</config>

 Step 2: write the observer class

/app/code/[NameSpace]/[ModuleName]/Observer/SalesOrderInvoicePay.php
<?php

namespace [NameSpace]\[ModuleName]\Observer\Sales;

use Magento\Framework\Event\Observer as EventObserver;

use Magento\Framework\Event\ObserverInterface;

class SalesOrderInvoicePay implements ObserverInterface

{   

/**

* @param EventObserver $observer

* @return $this

* @SuppressWarnings(PHPMD.CyclomaticComplexity)

*/

public function execute(EventObserver $observer)

{

     $invoice = $observer->getEvent()->getInvoice();

     $order = $invoice->getOrder();

     /* reset total_paid & base_total_paid of order */

     $order->setTotalPaid($order->getTotalPaid() - $invoice->getGrandTotal());

     $order->setBaseTotalPaid($order->getBaseTotalPaid() - $invoice->getBaseGrandTotal());

}    

}

 I hope the following information will help you to create an invoice without updating total paid of order in magento 2.

 

Last Update: April 9, 2018  

February 5, 2018   181   Nandini R    Orders    
Total 1 Votes:
0

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Google+
http://www.webnexs.com/blog/kb/create-invoice-updating-total-paid-order
Twitter
LinkedIn