PrestaShop and its careless relation invoice number - invoice date

2314 readings
2020-05-20 (updated on 2022-05-07)
Something that PrestaShop has been doing wrong since the beginning of time is the mismatch between invoice number and invoice date. This is because the invoice date can be saved days before the invoice number is generated. Fortunately, it is very easy to fix.

A basic rule in the invoicing of our business is that there must be a correlation between the numbers and the invoice dates.

It is a legal requirement: there cannot be an invoice with a number after another but with an earlier date. There may be jumps in the invoice numbers, for example if we have canceled and eliminated one, but it cannot be that an invoice with a later number than another has a date prior to that of the same.

This is absolutely neglected in PrestaShop. It seems incredible that after so much time, and having a really simple solution, it's still just as bad (at least as of today).

It must be said that, fortunately, many stores do not notice this problem. For example, if in your store there is only the card payment method (through a bank gateway), it is very rare that any case of discrepancy occurs.

The problem is that PrestaShop registers the data for the invoice, in the order_invoice table, before the invoice needs to be generated, that is, before the order goes to an order status that has the invoice option checked. And, when a state finally passes that causes the generation of the invoice, actually the generation of the invoice number, it does not save the current date as the invoice date, but continues to use the date that registered the data for the invoice (that is, the date_add date from the order_invoice table). And it is that in the order_invoice table there is no invoice date data, only that date_add, which is generally used in all the tables to save the moment of insertion of the record, but here it is the date_add. PrestaShop also uses it as the invoice date, which is wrong.

In the orders table, yes. we find an invoice date data (invoice_date), like this: as an invoice number (invoice_number), but this data doesn't seem to be used (filters for printing invoices don't look at it at all), and in particular invoice_date is just as wrong as date_add in invoice_date.

The best solution would be to have a clear and specific data for the invoice date, but if we want to solve our PrestaShop store NOW, the simplest thing is to do it with an override of the Order class, where it is made that, at the time an invoice is numbered, the date_add date is updated with the current one.

This leaves our PrestaShop ready so that the mismatch error between the invoice number and date does not occur again, but what? What happens if I already have unbalanced invoices? They're wrong, I can't present them, how do I solve it? Well, his thing is to try to correct the invoices by putting the correct date on them, that is, the date on which the invoice was actually generated. the invoice number. But can it be done? Is there that date somewhere where we can retrieve it?

The answer is yes, we have that date in the order_history table, we have to search there, for the order corresponding to the invoice that we want to fix, the oldest record of passing to a order status with the invoice option. The date of that record is the date it was generated. the invoice number and thus the correct invoice date.

Of course, doing this task by hand can be difficult and cumbersome, even more so if it is not one invoice but several that we have to fix. Therefore, I have developed a script that checks the invoices for us and corrects those that have the wrong invoice number.

If you need my help on this issue of wrong invoice dates in PrestaShop, don't hesitate to contact me. I'll be there glad to help you.