blob: 57750a56f6f9336efb71de610402a6a4eeb73d28 [file] [log] [blame]
Anand Doshi885e0742015-03-03 14:55:30 +05301# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
Anand Doshi330dae92013-09-10 13:46:15 +05302# License: GNU General Public License v3. See license.txt
3
Rushabh Mehta3daa49a2014-10-21 16:16:30 +05304
Aditya Hasef79937d2019-01-23 00:28:37 +05305import frappe
6
Anand Doshifb109ad2013-09-11 18:58:20 +05307
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05308@frappe.whitelist(allow_guest=True)
Ritwik Purif1933932023-04-05 12:04:36 +05309def send_message(sender, message, subject="Website Query"):
Rushabh Mehtaa5ebebd2017-11-16 17:03:52 +053010 from frappe.www.contact import send_message as website_send_message
Ankush Menat494bd9e2022-03-28 18:52:46 +053011
Ritwik Purif1933932023-04-05 12:04:36 +053012 website_send_message(sender, message, subject)
13
Rushabh Mehtaa5ebebd2017-11-16 17:03:52 +053014 lead = customer = None
Ankush Menat494bd9e2022-03-28 18:52:46 +053015 customer = frappe.db.sql(
16 """select distinct dl.link_name from `tabDynamic Link` dl
Rushabh Mehtaa5ebebd2017-11-16 17:03:52 +053017 left join `tabContact` c on dl.parent=c.name where dl.link_doctype='Customer'
Ankush Menat494bd9e2022-03-28 18:52:46 +053018 and c.email_id = %s""",
19 sender,
20 )
Rushabh Mehtad51e1582016-11-16 11:13:31 +053021
Rushabh Mehtaa5ebebd2017-11-16 17:03:52 +053022 if not customer:
Ankush Menat494bd9e2022-03-28 18:52:46 +053023 lead = frappe.db.get_value("Lead", dict(email_id=sender))
Britlog495a4082017-06-01 15:38:04 +020024 if not lead:
Ankush Menat494bd9e2022-03-28 18:52:46 +053025 new_lead = frappe.get_doc(
26 dict(doctype="Lead", email_id=sender, lead_name=sender.split("@")[0].title())
27 ).insert(ignore_permissions=True)
Rushabh Mehtad51e1582016-11-16 11:13:31 +053028
Ankush Menat494bd9e2022-03-28 18:52:46 +053029 opportunity = frappe.get_doc(
30 dict(
31 doctype="Opportunity",
32 opportunity_from="Customer" if customer else "Lead",
33 status="Open",
34 title=subject,
35 contact_email=sender,
Ankush Menat494bd9e2022-03-28 18:52:46 +053036 )
37 )
Rushabh Mehtad51e1582016-11-16 11:13:31 +053038
Rushabh Mehtaa5ebebd2017-11-16 17:03:52 +053039 if customer:
Nabin Hait34c551d2019-07-03 10:34:31 +053040 opportunity.party_name = customer[0][0]
Rushabh Mehtaa5ebebd2017-11-16 17:03:52 +053041 elif lead:
Nabin Hait34c551d2019-07-03 10:34:31 +053042 opportunity.party_name = lead
Rushabh Mehtaa5ebebd2017-11-16 17:03:52 +053043 else:
Nabin Hait34c551d2019-07-03 10:34:31 +053044 opportunity.party_name = new_lead.name
Rushabh Mehtad51e1582016-11-16 11:13:31 +053045
Rushabh Mehtaa5ebebd2017-11-16 17:03:52 +053046 opportunity.insert(ignore_permissions=True)
Rushabh Mehtaddd79f42015-08-25 14:03:43 +053047
Ankush Menat494bd9e2022-03-28 18:52:46 +053048 comm = frappe.get_doc(
49 {
50 "doctype": "Communication",
51 "subject": subject,
52 "content": message,
53 "sender": sender,
54 "sent_or_received": "Received",
55 "reference_doctype": "Opportunity",
56 "reference_name": opportunity.name,
57 }
58 )
Rushabh Mehtaa5ebebd2017-11-16 17:03:52 +053059 comm.insert(ignore_permissions=True)