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 | |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 9 | def set_by_naming_series(doctype, fieldname, naming_series, hide_name_field=True, make_mandatory=1): |
Ankush Menat | a1b7a79 | 2022-05-31 15:35:40 +0530 | [diff] [blame] | 10 | """Change a doctype's naming to user naming series""" |
| 11 | from frappe.custom.doctype.property_setter.property_setter import make_property_setter |
| 12 | |
| 13 | if naming_series: |
| 14 | make_property_setter( |
| 15 | doctype, "naming_series", "hidden", 0, "Check", validate_fields_for_doctype=False |
| 16 | ) |
| 17 | make_property_setter( |
| 18 | doctype, "naming_series", "reqd", make_mandatory, "Check", validate_fields_for_doctype=False |
| 19 | ) |
| 20 | |
| 21 | # set values for mandatory |
| 22 | try: |
| 23 | frappe.db.sql( |
| 24 | """update `tab{doctype}` set naming_series={s} where |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 25 | ifnull(naming_series, '')=''""".format(doctype=doctype, s="%s"), |
Ankush Menat | a1b7a79 | 2022-05-31 15:35:40 +0530 | [diff] [blame] | 26 | get_default_naming_series(doctype), |
| 27 | ) |
| 28 | except NamingSeriesNotSetError: |
| 29 | pass |
| 30 | |
| 31 | if hide_name_field: |
| 32 | make_property_setter(doctype, fieldname, "reqd", 0, "Check", validate_fields_for_doctype=False) |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 33 | make_property_setter(doctype, fieldname, "hidden", 1, "Check", validate_fields_for_doctype=False) |
Ankush Menat | a1b7a79 | 2022-05-31 15:35:40 +0530 | [diff] [blame] | 34 | else: |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 35 | make_property_setter(doctype, "naming_series", "reqd", 0, "Check", validate_fields_for_doctype=False) |
Ankush Menat | a1b7a79 | 2022-05-31 15:35:40 +0530 | [diff] [blame] | 36 | make_property_setter( |
| 37 | doctype, "naming_series", "hidden", 1, "Check", validate_fields_for_doctype=False |
| 38 | ) |
| 39 | |
| 40 | if hide_name_field: |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 41 | make_property_setter(doctype, fieldname, "hidden", 0, "Check", validate_fields_for_doctype=False) |
Ankush Menat | a1b7a79 | 2022-05-31 15:35:40 +0530 | [diff] [blame] | 42 | make_property_setter(doctype, fieldname, "reqd", 1, "Check", validate_fields_for_doctype=False) |
| 43 | |
| 44 | # set values for mandatory |
| 45 | frappe.db.sql( |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 46 | f"""update `tab{doctype}` set `{fieldname}`=`name` where |
| 47 | ifnull({fieldname}, '')=''""" |
Ankush Menat | a1b7a79 | 2022-05-31 15:35:40 +0530 | [diff] [blame] | 48 | ) |