From 270d4e9f4e4c163abe376d8324a575d28f63b133 Mon Sep 17 00:00:00 2001 From: Rohit Waghchaure Date: Fri, 19 Apr 2019 11:20:41 +0530 Subject: [PATCH] fix: moved erpnext related methods from frappe to erpnext --- erpnext/crm/doctype/lead/lead.py | 27 +++++++++++++++++++ .../crm/doctype/opportunity/opportunity.py | 21 +++++++++++++++ erpnext/public/js/communication.js | 6 ++--- erpnext/support/doctype/issue/issue.py | 17 ++++++++++++ 4 files changed, 68 insertions(+), 3 deletions(-) diff --git a/erpnext/crm/doctype/lead/lead.py b/erpnext/crm/doctype/lead/lead.py index 442b6c2db2..d42502dc3b 100644 --- a/erpnext/crm/doctype/lead/lead.py +++ b/erpnext/crm/doctype/lead/lead.py @@ -10,6 +10,7 @@ from frappe.model.mapper import get_mapped_doc from erpnext.controllers.selling_controller import SellingController from frappe.contacts.address_and_contact import load_address_and_contact from erpnext.accounts.party import set_taxes +from frappe.email.inbox import link_communication_to_document sender_field = "email_id" @@ -199,3 +200,29 @@ def get_lead_details(lead, posting_date=None, company=None): out['taxes_and_charges'] = taxes_and_charges return out + +@frappe.whitelist() +def make_lead_from_communication(communication, ignore_communication_links=False): + """ raise a issue from email """ + + doc = frappe.get_doc("Communication", communication) + lead_name = None + if doc.sender: + lead_name = frappe.db.get_value("Lead", {"email_id": doc.sender}) + if not lead_name and doc.phone_no: + lead_name = frappe.db.get_value("Lead", {"mobile_no": doc.phone_no}) + if not lead_name: + lead = frappe.get_doc({ + "doctype": "Lead", + "lead_name": doc.sender_full_name, + "email_id": doc.sender, + "mobile_no": doc.phone_no + }) + lead.flags.ignore_mandatory = True + lead.flags.ignore_permissions = True + lead.insert() + + lead_name = lead.name + + link_communication_to_document(doc, "Lead", lead_name, ignore_communication_links) + return lead_name \ No newline at end of file diff --git a/erpnext/crm/doctype/opportunity/opportunity.py b/erpnext/crm/doctype/opportunity/opportunity.py index 2e02e75b73..4d4593641a 100644 --- a/erpnext/crm/doctype/opportunity/opportunity.py +++ b/erpnext/crm/doctype/opportunity/opportunity.py @@ -10,6 +10,7 @@ from erpnext.setup.utils import get_exchange_rate from erpnext.utilities.transaction_base import TransactionBase from erpnext.accounts.party import get_party_account_currency from frappe.desk.form import assign_to +from frappe.email.inbox import link_communication_to_document subject_field = "title" sender_field = "contact_email" @@ -349,3 +350,23 @@ def assign_to_user(doc, subject_field): "name": doc.name, "description": doc.get(subject_field) }) +@frappe.whitelist() +def make_opportunity_from_communication(communication, ignore_communication_links=False): + from erpnext.crm.doctype.lead.lead import make_lead_from_communication + doc = frappe.get_doc("Communication", communication) + + lead = doc.reference_name if doc.reference_doctype == "Lead" else None + if not lead: + lead = make_lead_from_communication(communication, ignore_communication_links=True) + + enquiry_from = "Lead" + + opportunity = frappe.get_doc({ + "doctype": "Opportunity", + "enquiry_from": enquiry_from, + "lead": lead + }).insert(ignore_permissions=True) + + link_communication_to_document(doc, "Opportunity", opportunity.name, ignore_communication_links) + + return opportunity.name diff --git a/erpnext/public/js/communication.js b/erpnext/public/js/communication.js index 49701b88b6..5316eb45b5 100644 --- a/erpnext/public/js/communication.js +++ b/erpnext/public/js/communication.js @@ -33,7 +33,7 @@ frappe.ui.form.on("Communication", { make_lead_from_communication: (frm) => { return frappe.call({ - method: "frappe.email.inbox.make_lead_from_communication", + method: "erpnext.crm.doctype.lead.lead.make_lead_from_communication", args: { communication: frm.doc.name }, @@ -48,7 +48,7 @@ frappe.ui.form.on("Communication", { make_issue_from_communication: (frm) => { return frappe.call({ - method: "frappe.email.inbox.make_issue_from_communication", + method: "erpnext.support.doctype.issue.issue.make_issue_from_communication", args: { communication: frm.doc.name }, @@ -63,7 +63,7 @@ frappe.ui.form.on("Communication", { make_opportunity_from_communication: (frm) => { return frappe.call({ - method: "frappe.email.inbox.make_opportunity_from_communication", + method: "erpnext.crm.doctype.opportunity.opportunity.make_opportunity_from_communication", args: { communication: frm.doc.name }, diff --git a/erpnext/support/doctype/issue/issue.py b/erpnext/support/doctype/issue/issue.py index 01677ae34c..d626def665 100644 --- a/erpnext/support/doctype/issue/issue.py +++ b/erpnext/support/doctype/issue/issue.py @@ -13,6 +13,7 @@ from frappe.model.mapper import get_mapped_doc from frappe.utils.user import is_website_user from ..service_level_agreement.service_level_agreement import get_active_service_level_agreement_for from erpnext.crm.doctype.opportunity.opportunity import assign_to_user +from frappe.email.inbox import link_communication_to_document sender_field = "raised_by" @@ -294,3 +295,19 @@ def make_task(source_name, target_doc=None): "doctype": "Task" } }, target_doc) +@frappe.whitelist() +def make_issue_from_communication(communication, ignore_communication_links=False): + """ raise a issue from email """ + + doc = frappe.get_doc("Communication", communication) + issue = frappe.get_doc({ + "doctype": "Issue", + "subject": doc.subject, + "communication_medium": doc.communication_medium, + "raised_by": doc.sender or "", + "raised_by_phone": doc.phone_no or "" + }).insert(ignore_permissions=True) + + link_communication_to_document(doc, "Issue", issue.name, ignore_communication_links) + + return issue.name