diff --git a/msp/hooks.py b/msp/hooks.py index 5df4a86..1a24064 100644 --- a/msp/hooks.py +++ b/msp/hooks.py @@ -87,11 +87,11 @@ jenv = { # --------------- # Hook on document methods and events -# doc_events = { -# "Customer": { -# "on_update": "msp.msp.customer_quick_entry.custom_customer_info" -# } -# } +doc_events = { + "Location": { + "before_save": "msp.tools.hooks_methods.build_full_location_path" + } +} # Scheduled Tasks # --------------- diff --git a/msp/tools/hooks_methods.py b/msp/tools/hooks_methods.py new file mode 100644 index 0000000..962d83f --- /dev/null +++ b/msp/tools/hooks_methods.py @@ -0,0 +1,27 @@ +import frappe +from frappe.utils import cstr + +def build_full_location_path(doctype, method=None): + parent_location_name = doctype.parent_location + full_path = '' + html_full_path = '' + has_parent_location = True if parent_location_name else False + + while has_parent_location: + result = frappe.db.get_value('Location', {'name': parent_location_name}, ['name', 'location_name', 'parent_location'], as_dict=True) + if not result: + has_parent_location = False + continue + + full_path = f"{result['location_name']} --> {full_path}" + html_full_path = f"{result['location_name']} --> {html_full_path}" + parent_location_name = result['parent_location'] + + if not parent_location_name: + has_parent_location = False + + full_path = f"{full_path} {doctype.location_name}" if full_path != '' else doctype.location_name + html_full_path = f"{html_full_path} {doctype.location_name}" if html_full_path != '' else f"{doctype.location_name}" + + doctype.full_path = full_path + doctype.html_full_path = html_full_path