Ankush Menat | a1b7a79 | 2022-05-31 15:35:40 +0530 | [diff] [blame] | 1 | import frappe |
| 2 | from frappe.model.naming import get_default_naming_series |
| 3 | |
| 4 | |
| 5 | class NamingSeriesNotSetError(frappe.ValidationError): |
| 6 | pass |
| 7 | |
| 8 | |
| 9 | def set_by_naming_series( |
| 10 | doctype, fieldname, naming_series, hide_name_field=True, make_mandatory=1 |
| 11 | ): |
| 12 | """Change a doctype's naming to user naming series""" |
| 13 | from frappe.custom.doctype.property_setter.property_setter import make_property_setter |
| 14 | |
| 15 | if naming_series: |
| 16 | make_property_setter( |
| 17 | doctype, "naming_series", "hidden", 0, "Check", validate_fields_for_doctype=False |
| 18 | ) |
| 19 | make_property_setter( |
| 20 | doctype, "naming_series", "reqd", make_mandatory, "Check", validate_fields_for_doctype=False |
| 21 | ) |
| 22 | |
| 23 | # set values for mandatory |
| 24 | try: |
| 25 | frappe.db.sql( |
| 26 | """update `tab{doctype}` set naming_series={s} where |
| 27 | ifnull(naming_series, '')=''""".format( |
| 28 | doctype=doctype, s="%s" |
| 29 | ), |
| 30 | get_default_naming_series(doctype), |
| 31 | ) |
| 32 | except NamingSeriesNotSetError: |
| 33 | pass |
| 34 | |
| 35 | if hide_name_field: |
| 36 | make_property_setter(doctype, fieldname, "reqd", 0, "Check", validate_fields_for_doctype=False) |
| 37 | make_property_setter( |
| 38 | doctype, fieldname, "hidden", 1, "Check", validate_fields_for_doctype=False |
| 39 | ) |
| 40 | else: |
| 41 | make_property_setter( |
| 42 | doctype, "naming_series", "reqd", 0, "Check", validate_fields_for_doctype=False |
| 43 | ) |
| 44 | make_property_setter( |
| 45 | doctype, "naming_series", "hidden", 1, "Check", validate_fields_for_doctype=False |
| 46 | ) |
| 47 | |
| 48 | if hide_name_field: |
| 49 | make_property_setter( |
| 50 | doctype, fieldname, "hidden", 0, "Check", validate_fields_for_doctype=False |
| 51 | ) |
| 52 | make_property_setter(doctype, fieldname, "reqd", 1, "Check", validate_fields_for_doctype=False) |
| 53 | |
| 54 | # set values for mandatory |
| 55 | frappe.db.sql( |
| 56 | """update `tab{doctype}` set `{fieldname}`=`name` where |
| 57 | ifnull({fieldname}, '')=''""".format( |
| 58 | doctype=doctype, fieldname=fieldname |
| 59 | ) |
| 60 | ) |