blob: 4295188dc0bef44191cfaae311ed7a97c2069718 [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)
Anand Doshifb109ad2013-09-11 18:58:20 +05309def send_message(subject="Website Query", message="", sender="", status="Open"):
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
Rushabh Mehtaa5ebebd2017-11-16 17:03:52 +053012 lead = customer = None
Anand Doshi5d591eb2014-04-18 16:15:31 +053013
Rushabh Mehtaa5ebebd2017-11-16 17:03:52 +053014 website_send_message(subject, message, sender)
Anand Doshi5d591eb2014-04-18 16:15:31 +053015
Ankush Menat494bd9e2022-03-28 18:52:46 +053016 customer = frappe.db.sql(
17 """select distinct dl.link_name from `tabDynamic Link` dl
Rushabh Mehtaa5ebebd2017-11-16 17:03:52 +053018 left join `tabContact` c on dl.parent=c.name where dl.link_doctype='Customer'
Ankush Menat494bd9e2022-03-28 18:52:46 +053019 and c.email_id = %s""",
20 sender,
21 )
Rushabh Mehtad51e1582016-11-16 11:13:31 +053022
Rushabh Mehtaa5ebebd2017-11-16 17:03:52 +053023 if not customer:
Ankush Menat494bd9e2022-03-28 18:52:46 +053024 lead = frappe.db.get_value("Lead", dict(email_id=sender))
Britlog495a4082017-06-01 15:38:04 +020025 if not lead:
Ankush Menat494bd9e2022-03-28 18:52:46 +053026 new_lead = frappe.get_doc(
27 dict(doctype="Lead", email_id=sender, lead_name=sender.split("@")[0].title())
28 ).insert(ignore_permissions=True)
Rushabh Mehtad51e1582016-11-16 11:13:31 +053029
Ankush Menat494bd9e2022-03-28 18:52:46 +053030 opportunity = frappe.get_doc(
31 dict(
32 doctype="Opportunity",
33 opportunity_from="Customer" if customer else "Lead",
34 status="Open",
35 title=subject,
36 contact_email=sender,
37 to_discuss=message,
38 )
39 )
Rushabh Mehtad51e1582016-11-16 11:13:31 +053040
Rushabh Mehtaa5ebebd2017-11-16 17:03:52 +053041 if customer:
Nabin Hait34c551d2019-07-03 10:34:31 +053042 opportunity.party_name = customer[0][0]
Rushabh Mehtaa5ebebd2017-11-16 17:03:52 +053043 elif lead:
Nabin Hait34c551d2019-07-03 10:34:31 +053044 opportunity.party_name = lead
Rushabh Mehtaa5ebebd2017-11-16 17:03:52 +053045 else:
Nabin Hait34c551d2019-07-03 10:34:31 +053046 opportunity.party_name = new_lead.name
Rushabh Mehtad51e1582016-11-16 11:13:31 +053047
Rushabh Mehtaa5ebebd2017-11-16 17:03:52 +053048 opportunity.insert(ignore_permissions=True)
Rushabh Mehtaddd79f42015-08-25 14:03:43 +053049
Ankush Menat494bd9e2022-03-28 18:52:46 +053050 comm = frappe.get_doc(
51 {
52 "doctype": "Communication",
53 "subject": subject,
54 "content": message,
55 "sender": sender,
56 "sent_or_received": "Received",
57 "reference_doctype": "Opportunity",
58 "reference_name": opportunity.name,
59 }
60 )
Rushabh Mehtaa5ebebd2017-11-16 17:03:52 +053061 comm.insert(ignore_permissions=True)
Makarand Bauskar98f94272017-02-20 10:25:25 +053062
Rushabh Mehtaa5ebebd2017-11-16 17:03:52 +053063 return "okay"