diff --git a/erpnext/crm/doctype/lead/lead.py b/erpnext/crm/doctype/lead/lead.py index 29ca71bd88e..2fa6b68ee20 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 e8da4e6436b..c5aae9cd5f3 100644 --- a/erpnext/crm/doctype/opportunity/opportunity.py +++ b/erpnext/crm/doctype/opportunity/opportunity.py @@ -9,6 +9,7 @@ from frappe.model.mapper import get_mapped_doc 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.email.inbox import link_communication_to_document subject_field = "title" sender_field = "contact_email" @@ -321,3 +322,24 @@ def auto_close_opportunity(): doc.flags.ignore_permissions = True doc.flags.ignore_mandatory = True doc.save() + +@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 e85107e77aa..02357ef086c 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 0b5eb539c8d..2e74b997f61 100644 --- a/erpnext/support/doctype/issue/issue.py +++ b/erpnext/support/doctype/issue/issue.py @@ -9,6 +9,7 @@ from frappe import _ from frappe.model.document import Document from frappe.utils import now from frappe.utils.user import is_website_user +from frappe.email.inbox import link_communication_to_document sender_field = "raised_by" @@ -160,3 +161,20 @@ def has_website_permission(doc, ptype, user, verbose=False): def update_issue(contact, method): """Called when Contact is deleted""" frappe.db.sql("""UPDATE `tabIssue` set contact='' where contact=%s""", contact.name) + +@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 \ No newline at end of file