How to Generate Invoice Automatically When Creating Shipment in Magento

invoiceshipment

I want to generate invoice automatically when I create the shipment for the order manually.
How can I create a system like this. Does anyone know how to do this?
Any assistance will be helpful.

Best Answer

I'd go the route of creating an event-observer for sales_order_shipment_save_after:

So in app/code/local/Namespace/Module/etc/config.xml:

...
<events>
    <sales_order_shipment_save_after>
    <observers>
        <namespace_module>
        <type>singleton</type>
        <class>Namespace_Module_Model_Observer</class>
        <method>autoInvoice</method>
        </namespace_module>
    </observers>
    </sales_order_shipment_save_after>
</events>
....

and in app/code/local/Namespace/Module/Model/Observer.php:

<?php
class Namespace_Module_Model_Observer
{
   public function autoInvoice($observer) {
       $shipment = $observer->getEvent()->getShipment();
       $order = $shipment->getOrder();
       if($order->canInvoice())
       {
           $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
           $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
           $invoice->register();
           $transactionSave = Mage::getModel('core/resource_transaction')
           ->addObject($invoice)
           ->addObject($invoice->getOrder());
           $transactionSave->save();
       }
   }
}

Then you should be all set.

Related Topic