diff --git a/crm/fcrm/doctype/crm_contacts/crm_contacts.json b/crm/fcrm/doctype/crm_contacts/crm_contacts.json index e6044d67..5bca79f9 100644 --- a/crm/fcrm/doctype/crm_contacts/crm_contacts.json +++ b/crm/fcrm/doctype/crm_contacts/crm_contacts.json @@ -12,7 +12,8 @@ "column_break_uvny", "gender", "mobile_no", - "phone" + "phone", + "is_primary" ], "fields": [ { @@ -55,7 +56,6 @@ "fetch_from": "contact.phone", "fieldname": "phone", "fieldtype": "Data", - "in_list_view": 1, "label": "Phone", "options": "Phone", "read_only": 1 @@ -64,16 +64,22 @@ "fetch_from": "contact.gender", "fieldname": "gender", "fieldtype": "Link", - "in_list_view": 1, "label": "Gender", "options": "Gender", "read_only": 1 + }, + { + "default": "0", + "fieldname": "is_primary", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Is Primary" } ], "index_web_pages_for_search": 1, "istable": 1, "links": [], - "modified": "2023-08-25 19:19:27.813526", + "modified": "2023-11-12 14:58:18.846919", "modified_by": "Administrator", "module": "FCRM", "name": "CRM Contacts", diff --git a/crm/fcrm/doctype/crm_deal/crm_deal.py b/crm/fcrm/doctype/crm_deal/crm_deal.py index df1c8413..927c8baa 100644 --- a/crm/fcrm/doctype/crm_deal/crm_deal.py +++ b/crm/fcrm/doctype/crm_deal/crm_deal.py @@ -7,6 +7,44 @@ from frappe.model.document import Document class CRMDeal(Document): + def validate(self): + self.set_primary_contact() + self.set_primary_email_mobile_no() + + def set_primary_contact(self, contact=None): + if not self.contacts: + return + + if not contact and len(self.contacts) == 1: + self.contacts[0].is_primary = 1 + elif contact: + for d in self.contacts: + if d.contact == contact: + d.is_primary = 1 + else: + d.is_primary = 0 + + def set_primary_email_mobile_no(self): + if not self.contacts: + self.email = "" + self.mobile_no = "" + return + + if len([contact for contact in self.contacts if contact.is_primary]) > 1: + frappe.throw(_("Only one {0} can be set as primary.").format(frappe.bold("Contact"))) + + primary_contact_exists = False + for d in self.contacts: + if d.is_primary == 1: + primary_contact_exists = True + self.email = d.email.strip() + self.mobile_no = d.mobile_no.strip() + break + + if not primary_contact_exists: + self.email = "" + self.mobile_no = "" + @staticmethod def sort_options(): return [