2

I'm new to this posting business so please be patient.

I have 6 stores set up in magento 1.9 representing the 6 different departments of the charity, they have individual order number prefixes, however what I really need is the ability for the 6 stores to share invoice numbers. Allowing the accounted to see one long list of invoices from all the stores.

When I have tried to set this up however I get conflicts 'invoice can't save' because another store has already used the invoice number. If I have not been clear or if you require more information please ask.

Does any one know of a way to implement this, I would be very grateful

1 Answer 1

2

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.

12
  • Hello, thank you for your reply, it sounds excellent however I'm really no developer, I've got the system running at 99% now and my invoice issue is the last hurdle and I don't want to mess it up, do you work on a freelance basis? could you implement the changes on a cost basis and we pay you via paypal? Commented May 12, 2015 at 15:23
  • @DanielTyas. Sorry I'm not available for hiring. But I will try to help you (for free). I will come back tomorrow with a fully working module and post it here. I will give you the exact files you have to create and you can create them. I will also provide a fallback solution. In case my module does not work you can disable it and be back at square one. Does this sound OK?
    – Marius
    Commented May 12, 2015 at 15:26
  • That sounds superb! Commented May 12, 2015 at 15:26
  • Amazing! Go Marius ;)
    – snh_nl
    Commented May 12, 2015 at 17:36
  • @DanielTyas. I've updated the answer with the fully functional module that is needed to get your desired behavior. Let me know if it works. If it does and you still feel like giving money away, just donate what you think this is worth to charity.
    – Marius
    Commented May 12, 2015 at 21:13

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.