diff --git a/erpnext/crm/doctype/utils.py b/erpnext/crm/doctype/utils.py index 5c338174bc..36cb0c1f23 100644 --- a/erpnext/crm/doctype/utils.py +++ b/erpnext/crm/doctype/utils.py @@ -1,4 +1,5 @@ import frappe +import json @frappe.whitelist() def get_document_with_phone_number(number): @@ -19,7 +20,49 @@ def get_document_with_phone_number(number): if leads: return frappe.get_doc('Lead', leads[0].name) +@frappe.whitelist() +def get_last_interaction(number, reference_doc): + reference_doc = json.loads(reference_doc) if reference_doc else get_document_with_phone_number(number) -def get_customer_last_interaction(contact_doc): - # - pass \ No newline at end of file + if not reference_doc: return + + reference_doc = frappe._dict(reference_doc) + + last_communication = {} + last_issue = {} + if reference_doc.doctype == 'Contact': + customer_name = '' + query_condition = '' + for link in reference_doc.links: + link = frappe._dict(link) + if link.link_doctype == 'Customer': + customer_name = link.link_name + query_condition += "(`reference_doctype`='{}' AND `reference_name`='{}') OR".format(link.link_doctype, link.link_name) + + if query_condition: + query_condition = query_condition[:-2] + + last_communication = frappe.db.sql(""" + SELECT `name`, `content` + FROM `tabCommunication` + WHERE {} + ORDER BY `modified` + LIMIT 1 + """.format(query_condition)) + + if customer_name: + last_issue = frappe.get_all('Issue', { + 'customer': customer_name + }, ['name', 'subject'], limit=1) + + elif reference_doc.doctype == 'Lead': + last_communication = frappe.get_all('Communication', { + 'reference_doctype': reference_doc.doctype, + 'reference_name': reference_doc.name, + 'sent_or_received': 'Received' + }, fields=['name', 'content'], limit=1) + + return { + 'last_communication': last_communication[0] if last_communication else None, + 'last_issue': last_issue[0] if last_issue else None + } \ No newline at end of file diff --git a/erpnext/public/js/call_popup/call_popup.js b/erpnext/public/js/call_popup/call_popup.js index 7236f9e828..38464785db 100644 --- a/erpnext/public/js/call_popup/call_popup.js +++ b/erpnext/public/js/call_popup/call_popup.js @@ -4,8 +4,6 @@ class CallPopup { this.call_log = call_log; this.call_status_method = call_status_method; this.make(); - this.make_customer_contact(); - this.setup_call_status_updater(); } make() { @@ -16,29 +14,68 @@ class CallPopup { 'fieldname': 'customer_info', 'fieldtype': 'HTML' }, { - 'fieldtype': 'Section Break' + 'label': 'Last Interaction', + 'fielname': 'last_interaction', + 'fieldtype': 'Section Break', + // 'hidden': true }, { 'fieldtype': 'Small Text', 'label': "Last Communication", 'fieldname': 'last_communication', - 'read_only': true }, { - 'fieldtype': 'Column Break' + 'fieldname': 'last_communication_link', + 'fieldtype': 'HTML', + }, { + 'fieldtype': 'Small Text', + 'label': "Last Issue", + 'fieldname': 'last_issue', + }, { + 'fieldname': 'last_issue_link', + 'fieldtype': 'HTML', + }, { + 'label': 'Enter Call Summary', + 'fieldtype': 'Section Break', }, { 'fieldtype': 'Small Text', 'label': 'Call Summary', - 'fieldname': 'call_communication', + 'fieldname': 'call_summary', + }, { + 'label': 'Append To', + 'fieldtype': 'Select', + 'fieldname': 'doctype', + 'options': ['Issue', 'Lead', 'Communication'], + 'default': this.call_log.doctype + }, { + 'label': 'Document', + 'fieldtype': 'Dynamic Link', + 'fieldname': 'docname', + 'options': 'doctype', + 'default': this.call_log.name }, { 'fieldtype': 'Button', 'label': 'Submit', 'click': () => { - this.dialog.get_value(); + const values = this.dialog.get_values(); + frappe.xcall('frappe.desk.form.utils.add_comment', { + 'reference_doctype': values.doctype, + 'reference_name': values.docname, + 'content': `${__('Call Summary')}: ${values.call_summary}`, + 'comment_email': frappe.session.user + }).then(() => { + this.dialog.set_value('call_summary', ''); + }); } }] }); this.set_call_status(this.call_log.call_status); - this.dialog.show(); + this.make_customer_contact(); this.dialog.get_close_btn().show(); + this.setup_call_status_updater(); + this.dialog.set_secondary_action(() => { + clearInterval(this.updater); + this.dialog.hide(); + }); + this.dialog.show(); } make_customer_contact() { @@ -48,10 +85,16 @@ class CallPopup { 'number': this.number }).then(contact_doc => { wrapper.empty(); - const contact = contact_doc; + const contact = this.contact = contact_doc; if (!contact) { - wrapper.append('