diff --git a/aumms/aumms/doc_events/user.py b/aumms/aumms/doc_events/user.py new file mode 100644 index 00000000..df12f064 --- /dev/null +++ b/aumms/aumms/doc_events/user.py @@ -0,0 +1,35 @@ +import frappe +from frappe.utils import has_common, get_fullname + + +def create_smith_warehouse(doc, method = None): + """ + method to create personal warehouse for a smith user + args: + doc (class object): object of User + """ + # checking if the user has Smith roles or if the user is administrator + user_roles = frappe.get_roles(doc.name) + required_roles = ["Smith", "Head of Smith"] + if not has_common(user_roles, required_roles) or doc.name == 'Administrator': + return + + # generating the warehouse name + user_fullname = get_fullname(doc.name) + new_warehouse = frappe.new_doc("Warehouse") + req_warehouse_name = f"{user_fullname} - Smith" + + # checking if the warehouse already exist + warehouse = frappe.db.exists("Warehouse", {"email_id": doc.name, 'parent_warehouse':'All smith Warehouse - EG'}) + if warehouse: + warehouse_doc = frappe.get_doc("Warehouse", warehouse) + # renaming the warehouse if the user's full name was changed + if not warehouse_doc.warehouse_name == req_warehouse_name: + warehouse_doc.warehouse_name = req_warehouse_name + warehouse_doc.save() + else: + # creating a new warehouse + new_warehouse.warehouse_name = req_warehouse_name + new_warehouse.email_id = doc.name + new_warehouse.parent_warehouse = 'All smith Warehouse - EG' + new_warehouse.save() diff --git a/aumms/hooks.py b/aumms/hooks.py index 46828b55..a90c494d 100644 --- a/aumms/hooks.py +++ b/aumms/hooks.py @@ -148,6 +148,9 @@ }, 'Payment Entry':{ 'on_submit': 'aumms.aumms.doc_events.payment_entry.payment_entry_on_submit' + }, + 'User':{ + 'on_update': 'aumms.aumms.doc_events.user.create_smith_warehouse' } }