2

Magento updates an order as paid after invoicing.I guess,that's the default behaviour. However, in Cash of Delivery orders, we need to send the invoice along with the shipment while the order is still unpaid. How can we generate an invoice while the order is still unpaid. We want to generate invoice before payment.

4
  • are you need to generate invoice before payment only off line payments methods or only particular payment methods? for eg.COD
    – Abdul
    Commented Jan 11, 2016 at 9:30
  • I need to generate Invoice before payment for offline payment methods. The only offline method we have as of now is COD. I am not concerned about online at this moment. Commented Jan 11, 2016 at 9:51
  • are you know admin side generate invoice in to particulate order?
    – Abdul
    Commented Jan 11, 2016 at 10:12
  • I am sorry. I didn't understand. Please rephrase. Commented Jan 11, 2016 at 10:19

3 Answers 3

2

Follow bellow steps

Step : 1 Create config.xml file (app/code/community/AR/Autoinvoice/etc/config.xml) copy and paste the code below:

<?xml version="1.0"?>
<config>
    <modules>
        <AR_Autoinvoice>
            <version>0.1.0</version>
        </AR_Autoinvoice>
    </modules>
    <global>
        <models>
            <autoinvoice>
                <class>AR_Autoinvoice_Model</class>
            </autoinvoice>
        </models>
        <events>
            <sales_order_save_commit_after>
                <observers>
                    <ar_autoinvoice>
                        <class>AR_Autoinvoice_Model_Observer</class>
                        <method>afterSalesOrderSaveCommitAfter</method>
                    </ar_autoinvoice>
                </observers>
            </sales_order_save_commit_after>
        </events>
        <helpers>
            <autoinvoice>
                <class>AR_Autoinvoice_Helper</class>
            </autoinvoice>
        </helpers>
    </global>
</config>

Step : 2 Create Helper file (app/code/community/AR/Autoinvoice/Helper/Data.php) copy and paste the code below:

<?php
class AR_Autoinvoice_Helper_Data extends Mage_Core_Helper_Abstract {

}

?>

Step : 3 Create Observer.php file (app/code/community/AR/Autoinvoice/Model/Observer.php) copy and paste the code below:

<?php

class AR_Autoinvoice_Model_Observer
{

    public $order;

    function afterSalesOrderSaveCommitAfter(&$event) {
        return $this->__process($event);
    }

    protected function __process($event) {
        $this->order = $event->getEvent()->getOrder();
        if (!$this->order->getId()) {
            return $this;
        }
        else {
            $this->createInvoice();
        }
    }

    protected function createInvoice() {
        $orderState = $this->order->getState();
        if ($orderState === Mage_Sales_Model_Order::STATE_NEW) { // Check for state new.
            if ($this->order->canInvoice()) {
                $this->order->getPayment()->setSkipTransactionCreation(false);
                $invoice = $this->order->prepareInvoice();
                $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
                $invoice->register();
                Mage::getModel('core/resource_transaction')
                   ->addObject($invoice)
                   ->addObject($this->order)
                   ->save();
            }
            else {
            }
        }
    }
}

?>

Step : 4 Create AR_Autoinvoice.xml file (app/etc/modules/AR_Autoinvoice.xml) Copy and paste the code below:

<config>
    <modules>
        <AR_Autoinvoice>
            <active>true</active>
            <codePool>community</codePool>
        </AR_Autoinvoice>
    </modules>
</config>

Note : Make sure all files are saved in their locations as ans and now go into Magento admin and CLEAR ALL CACHE.

3
  • I am yet to try out this. Commented Jan 11, 2016 at 13:46
  • Now, the invoice is generated automatically, but the invoice is marked as "Paid" I am okay with the invoice created automatically, but the invoice should be marked as paid. I need something like "UnPaid " or something. Sorry to say. My issue sits unresolved. Commented Jan 11, 2016 at 15:02
  • If use full my ans then accept & vote me...
    – Abdul
    Commented Jan 11, 2016 at 16:35
1

Well you can achieve that using Observer as below:

app/etc/modules/My_Invoicer.xml:

<config>
    <modules>
        <My_Invoicer>
            <active>true</active>
            <codePool>local</codePool>
        </My_Invoicer>
    </modules>
</config>

app/code/local/My/Invoicer/etc/config.xml:

<config>
    <modules>
        <My_Invoicer>
            <version>1.0.0.0</version>
        </My_Invoicer>
    </modules>
    <global>
        <models>
            <my_invoicer>
                <class>My_Invoicer_Model</class>
            </my_invoicer>
        </models>        
        <events>
            <sales_order_save_after>
                <observers>
                    <my_invoicer_automatically_complete_order>
                        <class>my_invoicer/observer</class>
                        <method>automaticallyInvoiceShipCompleteOrder</method>
                    </my_invoicer_automatically_complete_order>
                </observers>                
            </sales_order_save_after>
        </events>        
    </global>
</config>

and app/code/local/My/Invoicer/Model/Observer.php:

<?php
class My_Invoicer_Model_Observer
{
    /**
     * Mage::dispatchEvent($this->_eventPrefix.'_save_after', $this->_getEventData());
     * protected $_eventPrefix = 'sales_order';
     * protected $_eventObject = 'order';
     * event: sales_order_save_after
     */
    public function automaticallyInvoiceShipCompleteOrder($observer)
    {
        $order = $observer->getEvent()->getOrder();

        $orders = Mage::getModel('sales/order_invoice')->getCollection()
                        ->addAttributeToFilter('order_id', array('eq'=>$order->getId()));
        $orders->getSelect()->limit(1);  

        if ((int)$orders->count() !== 0) {
            return $this;
        }

        if ($order->getState() == Mage_Sales_Model_Order::STATE_NEW) {

            try {
                if(!$order->canInvoice()) {
                    $order->addStatusHistoryComment('My_Invoicer: Order cannot be invoiced.', false);
                    $order->save();  
                }

                //START Handle Invoice
                $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();

                $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
                $invoice->register();

                $invoice->getOrder()->setCustomerNoteNotify(true);          
                $invoice->getOrder()->setIsInProcess(true);
                $invoice->sendEmail(true, '');
                $order->addStatusHistoryComment('Automatically INVOICED by My_Invoicer.', false);

                $transactionSave = Mage::getModel('core/resource_transaction')
                    ->addObject($invoice)
                    ->addObject($invoice->getOrder());

                $transactionSave->save();

                //END Handle Invoice

                //START Handle Shipment
                //~ $shipment = $order->prepareShipment();
                //~ $shipment->register();
 //~ 
                //~ $order->setIsInProcess(true);
                //~ $order->addStatusHistoryComment('Automatically SHIPPED by My_Invoicer.', false);
 //~ 
                //~ $transactionSave = Mage::getModel('core/resource_transaction')
                    //~ ->addObject($shipment)
                    //~ ->addObject($shipment->getOrder())
                    //~ ->save();
                //END Handle Shipment
            } catch (Exception $e) {
                $order->addStatusHistoryComment('My_Invoicer: Exception occurred during automaticallyInvoiceShipCompleteOrder action. Exception message: '.$e->getMessage(), false);
                $order->save();
            }                
        }

    return $this;        
    }
}

Don't forget to clear caches after above files are added.

Hope this works for you

1
  • Okay what it did, it automactically generated the invoice & marked it as paid. We need to figure out a way to generate the invoice & the invoice shows as something like " Payment Pending ". The moment the invoice shows as paid, it's gameover. Commented Jan 11, 2016 at 13:45
1

Vicky's and Abdul's solutions doesn't solved the issue. I have found out the solution here.
Alexandre have done exactly what i am looking for Bank Transfer. I need it for COD. My Magento Knowledge is limited. Could someone have a look at this.

What did I do now to solve my problem?

protected $_canCapture = true; protected $_canCapturePartial= true;

I found these variables at app/code/core/Mage/Payment/Model/Method/Abstract.php. Since this is an abstract class, this would effect all payment method NOT just COD. Creating a module for the same would be more advisable, however I don't know how to write the module for the same. I wish I could get this done through a module.

1
  • Alright, this is what I have, with my limited magento skill. Commented Jan 16, 2016 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.