The invoice number is relevant not only for the company but also for the government if needed. At least here in Spain I think all valid invoices from a company have to have a correlative number with no gaps, and they have to be in order.
As such, the “creation” of the number should not be done when it’s state is “Draft” because a number of things can happen before the draft is validated, and this could create a gap in the Valid Invoice Series.
Is there a way to force this behaviour? Am I missing something?
Naming a document with a unique serie after you first save is a necesary step in order for the framework to render it to you next time it is requested. So, I guess you don’t have many choices about this topic. But, one thing you can do, and that’s what I recommend, is adding an additional field for your serie (the one for the government). So, you will only set the value to this field when the document is submitted (or validated). You can do that in the on_submit event. If you don’t need the other serie (the one that comes with the framework) you can hide it in from print format.
Let me know if you need more info about this topic.
Can this additional field be associated to a naming series, so it’s automatically set?
The way I was thinking this would work is by having two different series: one for the draft invoices and another for the valid invoices.
That is correct. You can have two different series in the same doc, but only with another addicional field. The only thing is you will have to set the value by your self to the field. You can achieve this with the make_autoname function. Something like this:

Of course, you will need to create the naming_serie first.
1 Like
Is that a “custom script” or do I need to edit the filesystem source files (it seems Sublime Text)? If so, can you tip me where to create that file?
It should be already created. You said earlier that it has to do with the Sales Invoice. I would not recomend to modify the Sales Invoice python file. Instead I suggest using a custom script (JavaScript) and overriding the before_submit event. Something like this:
Then you call a python script that can handle that for you. The make_autoname function will be very usefull for you.
1 Like
I might try this eventually, but I think the approach suggested in the documentation about the “Pro Forma” invoice at the bottom of https://frappe.github.io/erpnext/user/manual/en/accounts/sales-invoice is enough and that way I don’t need to mess with the code.
Thanks for the help though, this will probably be useful another time so I’m bookmarking it.