2015-03-03 09:25:30 +00:00
|
|
|
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
2013-09-10 08:16:15 +00:00
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
2014-10-21 10:46:30 +00:00
|
|
|
|
2019-01-22 18:58:37 +00:00
|
|
|
import frappe
|
|
|
|
|
2013-09-11 13:28:20 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
@frappe.whitelist(allow_guest=True)
|
2023-04-05 06:34:36 +00:00
|
|
|
def send_message(sender, message, subject="Website Query"):
|
2017-11-16 11:33:52 +00:00
|
|
|
from frappe.www.contact import send_message as website_send_message
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2023-04-05 06:34:36 +00:00
|
|
|
website_send_message(sender, message, subject)
|
2017-02-20 04:55:25 +00:00
|
|
|
|
2023-04-05 06:34:36 +00:00
|
|
|
lead = customer = None
|
2022-03-28 13:22:46 +00:00
|
|
|
customer = frappe.db.sql(
|
|
|
|
"""select distinct dl.link_name from `tabDynamic Link` dl
|
2017-11-16 11:33:52 +00:00
|
|
|
left join `tabContact` c on dl.parent=c.name where dl.link_doctype='Customer'
|
2022-03-28 13:22:46 +00:00
|
|
|
and c.email_id = %s""",
|
|
|
|
sender,
|
|
|
|
)
|
2017-02-20 04:55:25 +00:00
|
|
|
|
2017-11-16 11:33:52 +00:00
|
|
|
if not customer:
|
2022-03-28 13:22:46 +00:00
|
|
|
lead = frappe.db.get_value("Lead", dict(email_id=sender))
|
2017-06-01 13:38:04 +00:00
|
|
|
if not lead:
|
2022-03-28 13:22:46 +00:00
|
|
|
new_lead = frappe.get_doc(
|
|
|
|
dict(doctype="Lead", email_id=sender, lead_name=sender.split("@")[0].title())
|
|
|
|
).insert(ignore_permissions=True)
|
|
|
|
|
|
|
|
opportunity = frappe.get_doc(
|
|
|
|
dict(
|
|
|
|
doctype="Opportunity",
|
|
|
|
opportunity_from="Customer" if customer else "Lead",
|
|
|
|
status="Open",
|
|
|
|
title=subject,
|
|
|
|
contact_email=sender,
|
|
|
|
)
|
|
|
|
)
|
2017-11-16 11:33:52 +00:00
|
|
|
|
|
|
|
if customer:
|
2019-07-03 05:04:31 +00:00
|
|
|
opportunity.party_name = customer[0][0]
|
2017-11-16 11:33:52 +00:00
|
|
|
elif lead:
|
2019-07-03 05:04:31 +00:00
|
|
|
opportunity.party_name = lead
|
2017-11-16 11:33:52 +00:00
|
|
|
else:
|
2019-07-03 05:04:31 +00:00
|
|
|
opportunity.party_name = new_lead.name
|
2017-11-16 11:33:52 +00:00
|
|
|
|
|
|
|
opportunity.insert(ignore_permissions=True)
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
comm = frappe.get_doc(
|
|
|
|
{
|
|
|
|
"doctype": "Communication",
|
|
|
|
"subject": subject,
|
|
|
|
"content": message,
|
|
|
|
"sender": sender,
|
|
|
|
"sent_or_received": "Received",
|
|
|
|
"reference_doctype": "Opportunity",
|
|
|
|
"reference_name": opportunity.name,
|
|
|
|
}
|
|
|
|
)
|
2017-11-16 11:33:52 +00:00
|
|
|
comm.insert(ignore_permissions=True)
|