From bbe5fa22498b4af1cd721d956ea5090d39b97b1a Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Sat, 15 Jun 2024 17:52:53 +0530 Subject: [PATCH] patch: added patch to create default sidebar fields layout from customize form --- crm/patches.txt | 3 +- .../create_default_sidebar_fields_layout.py | 63 +++++++++++++++++++ frontend/src/pages/Deal.vue | 6 +- 3 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 crm/patches/v1_0/create_default_sidebar_fields_layout.py diff --git a/crm/patches.txt b/crm/patches.txt index c0204ec5..32c4485a 100644 --- a/crm/patches.txt +++ b/crm/patches.txt @@ -6,4 +6,5 @@ crm.patches.v1_0.move_crm_note_data_to_fcrm_note [post_model_sync] # Patches added in this section will be executed after doctypes are migrated crm.patches.v1_0.create_email_template_custom_fields -crm.patches.v1_0.create_default_fields_layout \ No newline at end of file +crm.patches.v1_0.create_default_fields_layout +crm.patches.v1_0.create_default_sidebar_fields_layout \ No newline at end of file diff --git a/crm/patches/v1_0/create_default_sidebar_fields_layout.py b/crm/patches/v1_0/create_default_sidebar_fields_layout.py new file mode 100644 index 00000000..04b65fee --- /dev/null +++ b/crm/patches/v1_0/create_default_sidebar_fields_layout.py @@ -0,0 +1,63 @@ +import json +import frappe + +def execute(): + if not frappe.db.exists("CRM Fields Layout", {"dt": "CRM Lead", "type": "Side Panel"}): + create_doctype_fields_layout("CRM Lead") + + if not frappe.db.exists("CRM Fields Layout", {"dt": "CRM Deal", "type": "Side Panel"}): + create_doctype_fields_layout("CRM Deal") + +def create_doctype_fields_layout(doctype): + not_allowed_fieldtypes = [ + "Section Break", + "Column Break", + ] + + fields = frappe.get_meta(doctype).fields + fields = [field for field in fields if field.fieldtype not in not_allowed_fieldtypes] + + sections = {} + section_fields = [] + last_section = None + + for field in fields: + if field.fieldtype == "Tab Break" and last_section: + sections[last_section]["fields"] = section_fields + last_section = None + if field.read_only: + section_fields = [] + continue + if field.fieldtype == "Tab Break": + if field.read_only: + section_fields = [] + continue + section_fields = [] + last_section = field.fieldname + sections[field.fieldname] = { + "label": field.label, + "name": field.fieldname, + "opened": True, + "fields": [], + } + if field.fieldname == "contacts_tab": + sections[field.fieldname]["editable"] = False + sections[field.fieldname]["contacts"] = [] + else: + section_fields.append(field.fieldname) + + section_fields = [] + for section in sections: + if section == "contacts_tab": + sections[section]["name"] = "contacts_section" + sections[section].pop("fields", None) + section_fields.append(sections[section]) + + frappe.get_doc({ + "doctype": "CRM Fields Layout", + "dt": doctype, + "type": "Side Panel", + "layout": json.dumps(section_fields), + }).insert(ignore_permissions=True) + + return section_fields \ No newline at end of file diff --git a/frontend/src/pages/Deal.vue b/frontend/src/pages/Deal.vue index adbf0c6e..fd8fbb85 100644 --- a/frontend/src/pages/Deal.vue +++ b/frontend/src/pages/Deal.vue @@ -583,9 +583,11 @@ const deal_contacts = createResource({ cache: ['deal_contacts', props.dealId], auto: true, onSuccess: (data) => { - fieldsLayout.data.find( + let contactSection = fieldsLayout.data.find( (section) => section.name == 'contacts_section', - ).contacts = data.map((contact) => { + ) + if (!contactSection) return + contactSection.contacts = data.map((contact) => { return { name: contact.name, full_name: contact.full_name,