user1540714
user1540714

Reputation: 2079

Magento: Create an invoice with invoice number = order number?

I am using this code to create an invoice in Magento:

$invoiceId = Mage::getModel('sales/order_invoice_api')->create($order->getIncrementId(), array());

This automatically assigns a number (increment_id) to the invoice, for example 100016050. I want to create an invoice where the increment_id of the invoice = the increment_id of the order.

How can that be done?

Thanks!

Upvotes: 4

Views: 2683

Answers (1)

Jürgen Thelen
Jürgen Thelen

Reputation: 12727

This would require coding a complete custom module, so I'll just explain some basics.

In Magento, entities like order, invoice, creditmemo and shipping each have its own and independant number group per store_id.

These number groups can be defined in the table eav_entity_store:

entity_store_id  entity_type_id  store_id  increment_prefix  increment_last_id
              1               5         1                 1          100000000
              2               6         1                 2          200000000
              3               7         1                 3          300000000
              4               8         1                 4          400000000

To know which entity_type_id refers to which entity, check your eav_entity_type table:

entity_type_id  entity_type_code  entity_model
             5  order             sales/order
             6  invoice           sales/order_invoice
             7  creditmemo        sales/order_creditmemo
             8  shipment          sales/order_shipment

Note that your entity_type_id's may (or may not) vary from that.

Magento usually increments each of this entities by one, see eav_entity_type.increment_per_store.

This happens the time such entity is created. But, the creation of an order doesn't always mean, that an invoice for it will be created, too. For example the user could cancel the payment while order placing, or the payment will not be authorized by the payment provider, so no invoice would be created.

This may lead to gaps, e.g. order already at 100000005, while invoice still at 200000002.

Your code would need to manage this gap in a way that keeps order and invoice in sync.

To do this, you could create an observer for the sales_order_invoice_save_before event, for example.

app/code/local/Mycompany/Mymodule/etc/config.xml:

<config>
    <modules>
        <Mycompany_Mymodule>
            <version>0.1.0</version>
        </Mycompany_Mymodule>
    </modules>
    <global>
        <models>
            <mymodule>
                <class>Mycompany_Mymodule_Model</class>
            </mymodule>
        </models>
        <events>
            <sales_order_invoice_save_before>
                <observers>
                    <myobserver>
                        <type>singleton</type>
                        <class>mymodule/observer</class>
                        <method>salesOrderInvoiceSaveBefore</method>
                    </myobserver>
                </observers>
            </sales_order_invoice_save_before>
        </events>
    </global>
</config>

app/code/local/Mycompany/Mymodule/Model/Observer.php:

class Mycompany_Mymodule_Model_Observer
{

    /**
     * Hook to observe `sales_order_invoice_save_before` event
     *
     * @param Varien_Event_Observer $oObserver
     */

    public function salesOrderInvoiceSaveBefore($oObserver)
    {
        $oInvoice = $oObserver->getInvoice();
    }

}

Magento passes the invoice object to this observer, before the invoice object is saved. This would allow you to retrieve the related order object (and therefore the order's increment_id) using this invoice object.

Having retrieved the order.increment_id you could search the invoices to find out whether or not an invoice with that order.increment_id already exists.

If it doesn't exist yet, you can assign the value of order.increment_id to invoice.increment_id before leaving the observer and are done.

Please note, that these are only the basics. There are some more pitfalls to it.

For example, multiple and/or duplicate invoices per order cases are not handled yet.

For example, in some countries the fiscal/tax authorities require invoice numbers to be continuously increasing. It must be 1, 2, 3, 4, 5, but 1, 2, 3, 4 is missing, 5 is not acceptable. Using the technique above, such gaps can still happen, because of payment cancellations by the user, etc.

However, this should get you on the right track.

Upvotes: 4

Related Questions