The increment id is attached to the invoice (orders, shipments and creditmemos) through this method Mage_Sales_Model_Resource_Order_Abstract::_beforeSave
.
protected function _beforeSave(Mage_Core_Model_Abstract $object)
{
if ($this->_useIncrementId && !$object->getIncrementId()) {
/* @var $entityType Mage_Eav_Model_Entity_Type */
$entityType = Mage::getModel('eav/entity_type')->loadByCode($this->_entityTypeForIncrementId);
$object->setIncrementId($entityType->fetchNewIncrementId($object->getStoreId()));
}
parent::_beforeSave($object);
return $this;
}
The invoice resource model extends the Mage_Sales_Model_Resource_Order_Abstract
class.
As you can see there is this line $object->setIncrementId($entityType->fetchNewIncrementId($object->getStoreId()))
that generates the increment id based on $object->getStoreId()
. If you replace that with a number you will always get the same increment prefix and the numbers will be continued independent of the store.
But don't edit the core. You will screw up the orders and shipments also.
You need to rewrite the Mage_Sales_Model_Resource_Order_Invoice
and make your changes to the _beforeSave()
method.
Something like this.
Create a new module. Let's call it StackExchange_Invoice
.
For this new module you willneed the following files.
app/code/local/StackExchange/Invoice/etc/config.xml
- the configuration file for the modules
<?xml version="1.0"?>
<config>
<modules>
<StackExchange_Invoice>
<version>1.0.0</version>
</StackExchange_Invoice>
</modules>
<global>
<models>
<sales_resource>
<rewrite>
<order_invoice>StackExchange_Invoice_Model_Resource_Order_Invoice</order_invoice>
</rewrite>
</sales_resource>
</models>
<helpers>
<stackexchange_invoice>
<class>StackExchange_Invoice_Helper</class>
</stackexchange_invoice>
</helpers>
</global>
<adminhtml>
<translate>
<modules>
<StackExchange_Invoice>
<files>
<default>StackExchange_Invoice.csv</default>
</files>
</StackExchange_Invoice>
</modules>
</translate>
</adminhtml>
<default>
<sales>
<stackexchange_invoice>
<enabled>1</enabled>
<prefix>1</prefix>
</stackexchange_invoice>
</sales>
</default>
</config>
app/code/local/StackExchange/Invoice/etc/system.xml
- the xml for declaring configuration fields
<?xml version="1.0"?>
<config>
<sections>
<sales>
<groups>
<stackexchange_invoice translate="label" module="stackexchange_invoice">
<label>Invoice numbering</label>
<frontend_type>text</frontend_type>
<sort_order>500</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
<fields>
<enabled translate="label">
<label>Enable same invoice prefix for all stores</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
<sort_order>10</sort_order>
</enabled>
<prefix translate="label">
<label>Invoices prefix</label>
<frontend_type>text</frontend_type>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
<sort_order>20</sort_order>
<depends>
<enabled>1</enabled>
</depends>
</prefix>
</fields>
</stackexchange_invoice>
</groups>
</sales>
</sections>
</config>
app/code/local/StackExchange/Invoice/Helper/Data.php
- the module main helper
<?php
class StackExchange_Invoice_Helper_Data extends Mage_Core_Helper_Abstract
{
const ENABLED_XML_PATH = 'sales/stackexchange_invoice/enabled';
public function isEnabled()
{
return Mage::getStoreConfigFlag(self::ENABLED_XML_PATH, 0);
}
}
app/code/local/StackExchange/Invoice/Model/Resource/Order/Invoice.php
- the rewrite of the invoice resource model
<?php
class StackExchange_Invoice_Model_Resource_Order_Invoice extends Mage_Sales_Model_Resource_Order_Invoice
{
const XML_PREFIX_PATH = 'sales/stackexchange_invoice/prefix';
protected function _beforeSave(Mage_Core_Model_Abstract $object)
{
if (!Mage::helper('stackexchange_invoice')->isEnabled()) {
return parent::_beforeSave($object);
}
if ($this->_useIncrementId && !$object->getIncrementId()) {
$prefix = Mage::getStoreConfig(self::XML_PREFIX_PATH, 0);
/* @var $entityType Mage_Eav_Model_Entity_Type */
$entityType = Mage::getModel('eav/entity_type')->loadByCode($this->_entityTypeForIncrementId);
$object->setIncrementId($entityType->fetchNewIncrementId($prefix));
}
parent::_beforeSave($object);
return $this;
}
}
app/locale/en_US/StackExchange_Invoice.csv
- the language file. Not necessary but nice to have.
"Invoice numbering","Invoice numbering"
"Enable same invoice prefix for all stores","Enable same invoice prefix for all stores"
"Invoices prefix","Invoices prefix"
app/etc/modules/StackExchange_Invoice.xml
- the module declaration file.
<?xml version="1.0"?>
<config>
<modules>
<StackExchange_Invoice>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Sales />
</depends>
</StackExchange_Invoice>
</modules>
</config>
That's it. Clear the cache and you are done. All your invoices will start with 1 now.
In case you want to change the invoice prefix you need to go in the backend in System->Configuration->Sales->Invoice numbering
and change the value of the field Invoices prefix
. but be careful...invoices that have already been created will remain with the previous prefix.
From the same config section you can disable the functionality and come back to the default behavior by setting the field Enable same invoice prefix for all stores
to No
.
If for some reason the module does not behave properly or you want to remove it, just delete the files created above.
That's it. Good luck. Please test a lot before going into live mode with this.