From 429bfcfd83d3b46bb2cf725307889f96a6660407 Mon Sep 17 00:00:00 2001 From: Suraj Shetty Date: Thu, 8 Aug 2019 19:16:32 +0530 Subject: [PATCH] fix: Make requested changes --- .../communication/doctype/call_log/call_log.py | 2 ++ erpnext/crm/doctype/utils.py | 15 ++++++++------- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/erpnext/communication/doctype/call_log/call_log.py b/erpnext/communication/doctype/call_log/call_log.py index d29794ebc8..fdbdb634a0 100644 --- a/erpnext/communication/doctype/call_log/call_log.py +++ b/erpnext/communication/doctype/call_log/call_log.py @@ -12,6 +12,8 @@ from erpnext.crm.doctype.lead.lead import get_lead_with_phone_number class CallLog(Document): def before_insert(self): + # strip 0 from the start of the number for proper number comparisions + # eg. 07888383332 should match with 7888383332 number = self.get('from').lstrip('0') self.contact = get_contact_with_phone_number(number) self.lead = get_lead_with_phone_number(number) diff --git a/erpnext/crm/doctype/utils.py b/erpnext/crm/doctype/utils.py index 1cfd89c4ea..55532761c2 100644 --- a/erpnext/crm/doctype/utils.py +++ b/erpnext/crm/doctype/utils.py @@ -11,11 +11,13 @@ def get_last_interaction(contact=None, lead=None): last_issue = None if contact: query_condition = '' + values = [] contact = frappe.get_doc('Contact', contact) for link in contact.links: if link.link_doctype == 'Customer': last_issue = get_last_issue_from_customer(link.link_name) - query_condition += "(`reference_doctype`='{}' AND `reference_name`='{}') OR".format(link.link_doctype, link.link_name) + query_condition += "(`reference_doctype`=%s AND `reference_name`=%s) OR" + values += [link_link_doctype, link_link_name] if query_condition: # remove extra appended 'OR' @@ -23,19 +25,18 @@ def get_last_interaction(contact=None, lead=None): last_communication = frappe.db.sql(""" SELECT `name`, `content` FROM `tabCommunication` - WHERE - `sent_or_received`='Received' AND - ({}) + WHERE `sent_or_received`='Received' + AND ({}) ORDER BY `modified` LIMIT 1 - """.format(query_condition), as_dict=1) # nosec + """.format(query_condition), values, as_dict=1) # nosec if lead: last_communication = frappe.get_all('Communication', filters={ 'reference_doctype': 'Lead', 'reference_name': lead, 'sent_or_received': 'Received' - }, fields=['name', 'content'], limit=1) + }, fields=['name', 'content'], order_by='`creation` DESC', limit=1) last_communication = last_communication[0] if last_communication else None @@ -47,7 +48,7 @@ def get_last_interaction(contact=None, lead=None): def get_last_issue_from_customer(customer_name): issues = frappe.get_all('Issue', { 'customer': customer_name - }, ['name', 'subject', 'customer'], limit=1) + }, ['name', 'subject', 'customer'], order_by='`creation` DESC', limit=1) return issues[0] if issues else None