Generating Invoice/Order's Next Increment ID in Magento
Magento has its own way of generating increment ID for new invoices and new orders after they are saved. But how about knowing them before hand, i.e before the invoice or the order is created! Lets first understand the working of those increment ID generation before jumping to the code. As all of us must have known there are “Entity Types” in Magento. And those “Entity Types” are stored in table named eav_entity_type. If you look into that table for entity_type_code = order or invoice, in increment_model column you can see the Model alias used to generate new increment ID code. Lets take the case of invoice..so the increment model is eav/entity_increment_numeric and additionally this increment is store dependent as for this row increment_per_store is set to 1.
Next place to look into will be .. Yes! the model eav/entity_increment_numeric. And if you see the model class Mage_Eav_Model_Entity_Increment_Numeric there is only one Method in this class the public function getNextId() which is responsible to the the Next increment ID, needless to say its parent has other methods as well since you might have already understood. So if some how you can trigger the right Increment Model for any entity type and the call this method, you must get the next increment ID of order/invoice/shippment.
That was the “prose” part now lets get down to the “code poetry”. Here’s how you first find the appropriate increment ID and then fetch the new Increment ID.
[source language=”php”]
//Get the Entity Type Increment Model you want, our case invoice
// @var $entity_type_model Mage_Eav_Model_Entity_Type
$entity_type_model=Mage::getSingleton(‘eav/config’)->getEntityType(‘invoice’);
//Triggering the getNextId() of the Increment Model
//@var $invoiceNew Mage_Sales_Model_Order_Invoice
$new_incr_id = $entity_type_model->fetchNewIncrementId($invoiceNew->getOrder()->getStoreId());
[/source]
Now, you have the generated Invoice Increment ID. This can be helpful in cases when you want to do something else before Invoice is created but you want to now the Increment Id before hand. Hope this helps.