From bfe18d6085d4b64e66e003ad7837c1c1341fdc08 Mon Sep 17 00:00:00 2001 From: Pranav Nachanekar Date: Fri, 4 Oct 2019 14:41:54 +0530 Subject: [PATCH] feat:assign appointments from opportunity --- .../crm/doctype/appointment/appointment.py | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/erpnext/crm/doctype/appointment/appointment.py b/erpnext/crm/doctype/appointment/appointment.py index 2da4acc1f5..5615ae1969 100644 --- a/erpnext/crm/doctype/appointment/appointment.py +++ b/erpnext/crm/doctype/appointment/appointment.py @@ -106,6 +106,16 @@ class Appointment(Document): self.lead = lead.name def auto_assign(self): + # If the latest opportunity is assigned to someone + # Assign the appointment to the same + existing_assignee = self.get_assignee_from_latest_opportunity() + if existing_assignee: + add_assignemnt({ + 'doctype':self.doctype + 'name':self.name + 'assign_to':existing_assignee + }) + return if self._assign: return available_agents = _get_agents_sorted_by_asc_workload( @@ -120,6 +130,25 @@ class Appointment(Document): }) break + def get_assignee_from_latest_opportunity(self): + if not self.lead: + return None + if not frappe.db.exists('Lead', self.lead): + return None + opporutnities = frappe.get_list( + 'Opportunity', + filters={ + 'party_name': self.lead, + }, + order_by='creation desc') + latest_opportunity = frappe.get_doc( + 'Opportunity', opporutnities[0].name) + assignee = latest_opportunity._assign + if not assignee: + return None + assignee = frappe.parse_json(assignee)[0] + return assignee + def create_calendar_event(self): if self.calendar_event: return