Jump to content

Reset Invoices Number At The Beginning Of The Year


Recommended Posts

I use Prestashop 1.4.3

There is no method to reset to 1 the invoice number at the beginning of the year?

For me it is important to start with the invoice FA000001.

 

With BO is not possible to make the change.

The invoices start again from the last number of 2011.

 

Thanks in advance. is urgent!

Link to comment
Share on other sites

Test this Tips:

 

• the file to edit it ... your dir... / classes / Order.php

• open the file with your favorite editor and look for the string: getLastInvoiceNumber

• you should find it at line 906 (approximately) and 923 (if you have other versions of well-controlled content)

 

At line 906 changed this sequence:

 

public static function getLastInvoiceNumber()

{

return (int)Db::getInstance()->getValue('

SELECT MAX(`invoice_number`) AS `invoice_number`

FROM `'._DB_PREFIX_.'orders`

');

}

 

which becomes:

 

public static function getLastInvoiceNumber()

{

return (int)Db::getInstance()->getValue('

SELECT MAX(`invoice_number`) AS `invoice_number`

FROM `'._DB_PREFIX_.'orders` WHERE year(invoice_date) = year(current_date)

');

}

 

 

Then at the line 926 (approximately), edit the query:

 

$number = '(SELECT `invoice_number`

FROM (

SELECT MAX(`invoice_number`) + 1 AS `invoice_number`

FROM `'._DB_PREFIX_.'orders`)

tmp )';

 

 

Changes in this

 

 

$number = '(SELECT `invoice_number`

FROM (

SELECT MAX(`invoice_number`) + 1 AS `invoice_number`

FROM `'._DB_PREFIX_.'orders`) WHERE year(invoice_date) = year(current_date)

tmp )';

 

 

Save the changes you just made ​​and you will find that in the "Invoices" the counter is reset.

 

Now you can also change the prefix to your liking such as "FA-2012 /"

 

*** If you issued the invoice, do not worry, just change the table "orders" with the correct invoice number and re-print the invoice.

 

 

Arco



Link to comment
Share on other sites

Hi,

 

I tried your advices, but it doesn't work for me. It is strange. I try it for Prestashop 1.4.6.2. As you wrote I had those codes on line 906 and 923. But when I make those changes I can't see invoice icon in list of orders and when I open order, there isn't everything.

When I have opened order detail window, I can click on invoice icon, it is possible to open pdf, but there is old numbers. Also if I try to change in BO Orders->Invoices number of invoice to 1 (bellow prefix), but it is writing invalid number of invoice, must be higher than 77 (77 is last number of invoice - order in previous year).

 

290.jpg

 

291m.jpg

 

Thanks for any idea.

Link to comment
Share on other sites

  • 1 year later...
  • 2 weeks later...
  • 10 months later...

How to do this in 1.5?

 

 

Anyone has a solution for 1.5?

 

I did this in the getLastInvoiceNumber() function in Order Class:

return Db::getInstance()->getValue('
	SELECT MAX(`number`) 
	FROM `ps_order_invoice`
	WHERE `date_add` > MAKEDATE( EXTRACT( YEAR FROM CURDATE() ) , 1 )
');

and it works. Hovewer I'm not sure this is the best way to reset.

Link to comment
Share on other sites

  • 1 month later...

for administration reasons i use the invoice year as start of my invoice numbers, this gives me the perfect opportunity to reset the counter, simpel find the current year in the latest invoice number if not found make a new invoice number starting the current year.

 

i copied /classes/order/Order.php to override/classes/order/Order.php and i editted 2 functions:

 

Line 1085:

    public static function getLastInvoiceNumber()
    {
        return Db::getInstance()->getValue('
            SELECT MAX(`number`)
            FROM `'._DB_PREFIX_.'order_invoice` WHERE `date_add` > MAKEDATE( EXTRACT( YEAR FROM CURDATE() ) , 1 )
        ');
    }
public function setInvoice($use_existing_payment = false)
    {
        if (!$this->hasInvoice())
        {
            $order_invoice = new OrderInvoice();
            $order_invoice->id_order = $this->id;
            $order_invoice->number = Configuration::get('PS_INVOICE_START_NUMBER', null, null, $this->id_shop);
            // If invoice start number has been set, you clean the value of this configuration
            if ($order_invoice->number)
            {
                
                Configuration::updateValue('PS_INVOICE_START_NUMBER', false, false, null, $this->id_shop);
            }
            else
            {
                /* get invoice number */
                $n = Order::getLastInvoiceNumber();
                
                /* get current year */
                $dateYear = date('Y');
                
                /* find year in string */
                $findDate = strpos($n, $dateYear);
                
                if ($findDate !== false)
                {
                    $order_invoice->number = Order::getLastInvoiceNumber() + 1;
                }
                else
                {
                    $order_invoice->number = date('Y');
                    $order_invoice->number .= "0001";
                }
            }
            
            $invoice_address = new Address((int)$this->id_address_invoice);
            $carrier = new Carrier((int)$this->id_carrier);
            $tax_calculator = $carrier->getTaxCalculator($invoice_address);

            $order_invoice->total_discount_tax_excl = $this->total_discounts_tax_excl;
            $order_invoice->total_discount_tax_incl = $this->total_discounts_tax_incl;
            $order_invoice->total_paid_tax_excl = $this->total_paid_tax_excl;
            $order_invoice->total_paid_tax_incl = $this->total_paid_tax_incl;
            $order_invoice->total_products = $this->total_products;
            $order_invoice->total_products_wt = $this->total_products_wt;
            $order_invoice->total_shipping_tax_excl = $this->total_shipping_tax_excl;
            $order_invoice->total_shipping_tax_incl = $this->total_shipping_tax_incl;
            $order_invoice->shipping_tax_computation_method = $tax_calculator->computation_method;
            $order_invoice->total_wrapping_tax_excl = $this->total_wrapping_tax_excl;
            $order_invoice->total_wrapping_tax_incl = $this->total_wrapping_tax_incl;

            // Save Order invoice
            $order_invoice->add();

            $order_invoice->saveCarrierTaxCalculator($tax_calculator->getTaxesAmount($order_invoice->total_shipping_tax_excl));

            // Update order_carrier
            $id_order_carrier = Db::getInstance()->getValue('
                SELECT `id_order_carrier`
                FROM `'._DB_PREFIX_.'order_carrier`
                WHERE `id_order` = '.(int)$order_invoice->id_order.'
                AND (`id_order_invoice` IS NULL OR `id_order_invoice` = 0)');
            
            if ($id_order_carrier)
            {
                $order_carrier = new OrderCarrier($id_order_carrier);
                $order_carrier->id_order_invoice = (int)$order_invoice->id;
                $order_carrier->update();
            }

            // Update order detail
            Db::getInstance()->execute('
                UPDATE `'._DB_PREFIX_.'order_detail`
                SET `id_order_invoice` = '.(int)$order_invoice->id.'
                WHERE `id_order` = '.(int)$order_invoice->id_order);

            // Update order payment
            if ($use_existing_payment)
            {
                $id_order_payments = Db::getInstance()->executeS('
                    SELECT DISTINCT op.id_order_payment
                    FROM `'._DB_PREFIX_.'order_payment` op
                    INNER JOIN `'._DB_PREFIX_.'orders` o ON (o.reference = op.order_reference)
                    LEFT JOIN `'._DB_PREFIX_.'order_invoice_payment` oip ON (oip.id_order_payment = op.id_order_payment)                    
                    WHERE (oip.id_order != '.(int)$order_invoice->id_order.' OR oip.id_order IS NULL) AND o.id_order = '.(int)$order_invoice->id_order);

                if (count($id_order_payments))
                {
                    foreach ($id_order_payments as $order_payment)
                        Db::getInstance()->execute('
                            INSERT INTO `'._DB_PREFIX_.'order_invoice_payment`
                            SET
                                `id_order_invoice` = '.(int)$order_invoice->id.',
                                `id_order_payment` = '.(int)$order_payment['id_order_payment'].',
                                `id_order` = '.(int)$order_invoice->id_order);
                    // Clear cache
                    Cache::clean('order_invoice_paid_*');
                }
            }

            // Update order cart rule
            Db::getInstance()->execute('
                UPDATE `'._DB_PREFIX_.'order_cart_rule`
                SET `id_order_invoice` = '.(int)$order_invoice->id.'
                WHERE `id_order` = '.(int)$order_invoice->id_order);

            // Keep it for backward compatibility, to remove on 1.6 version
            $this->invoice_date = $order_invoice->date_add;
            $this->invoice_number = $order_invoice->number;
            $this->update();
        }
    }

This changes 20141234 to 20150001 if 2014 is not the current year. it might be dirty as hell, but hey! IT WORKS :D

  • Like 1
Link to comment
Share on other sites

  • 11 months later...

Thank you for the type. I use date format on my invoices as YYMMDDXX  (XX is number order)

 

 

I change this things in bold and it works perfectly. Don't need to change public static function getLastInvoiceNumber() :).

 

 

 

public function setInvoice($use_existing_payment = false)
    {
        if (!$this->hasInvoice())
        {
            $order_invoice = new OrderInvoice();
            $order_invoice->id_order = $this->id;
            $order_invoice->number = Configuration::get('PS_INVOICE_START_NUMBER', null, null, $this->id_shop);
            // If invoice start number has been set, you clean the value of this configuration
            if ($order_invoice->number)
            {
                
                
Configuration::updateValue('PS_INVOICE_START_NUMBER', false, false, null, $this->id_shop);
            }
            else
            {
                /* get invoice number */
                $n = Order::getLastInvoiceNumber();
                
                
/* get current year-month-day */
                $dateYear = date('ymd');

                
                
/* find year-month-day in string */
                $findDate = strpos($n, $dateYear);

                
                
if ($findDate !== false)
                {
                    $order_invoice->number = Order::getLastInvoiceNumber() + 1;
                }
                else
                {
                    $order_invoice->number = date('ymd');
                    $order_invoice->number .= "01";

                }
            }
Link to comment
Share on other sites

  • 1 year later...
×
×
  • Create New...