Temporary sales invoice number before submit

Coming back to this thread with an almost working solution.
What I did is create a custom app and add these hooks :
hooks.py

doc_events = {
	"Sales Invoice": {
		"autoname": "numi.sales_invoice.draft_autoname",
		"on_submit": "numi.sales_invoice.draft_on_submit"
	}
}

sales_invoice.py

import frappe
from frappe.model.naming import make_autoname
from frappe.model.rename_doc import rename_doc

@frappe.whitelist()
def draft_autoname(doc, method):
    doc.name = make_autoname("hash", doc.doctype)


@frappe.whitelist()
    def draft_on_submit(doc, method):
        new_name = make_autoname(doc.naming_series, doc.doctype)
        rename_doc(doc.doctype, doc.name, new_name, force = True, ignore_permissions = False, show_alert = False)

This creates an invoice with a hash (4d1c91c3b3) and when submitted it is renamed correctly to ACC-SINV-2022-00003 for example. The only problem is after clicking on submit the form tries to reload on the same name (4d1c91c3b3) and gives an error Sales Invoice 4d1c91c3b3 not found because obviously it was changed in the on_submit method to ACC-SINV-2022-00003.
How can we deal with this ? Is there anything we can add to reload the form with the new name.

EDIT :

I finally found how Dokos did this, it wasn’t at ERPNext level but in the Core of Frappe giving the possibility to check a checkbox at Doctype creation to attribute naming series only after submission. I think this should be included in Frappe as the main behavior.

This is the commit that changed it :
https://gitlab.com/dokos/dodock/-/commit/e3983b7565441c60732f2cc1fb2f2f7a8cf158d1

2 Likes

Thanks @Merabtene

Here I made a PR with the solution provided by Dokos

https://github.com/frappe/frappe/pull/16144

4 Likes

:+1: this really good news. Would love to see it in v14.