blob: eb84bcc8d85bdd4b56660bfb8bc8c336d36a2633 [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
4from __future__ import unicode_literals
Rushabh Mehta3daa49a2014-10-21 16:16:30 +05305
6import frappe, json
7from frappe import _
8from frappe.utils import cint, formatdate
Anand Doshifb109ad2013-09-11 18:58:20 +05309
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053010@frappe.whitelist(allow_guest=True)
Anand Doshifb109ad2013-09-11 18:58:20 +053011def send_message(subject="Website Query", message="", sender="", status="Open"):
Rushabh Mehtaa5ebebd2017-11-16 17:03:52 +053012 from frappe.www.contact import send_message as website_send_message
13 lead = customer = None
Anand Doshi5d591eb2014-04-18 16:15:31 +053014
Rushabh Mehtaa5ebebd2017-11-16 17:03:52 +053015 website_send_message(subject, message, sender)
Anand Doshi5d591eb2014-04-18 16:15:31 +053016
Rushabh Mehtaa5ebebd2017-11-16 17:03:52 +053017 customer = frappe.db.sql("""select distinct dl.link_name from `tabDynamic Link` dl
18 left join `tabContact` c on dl.parent=c.name where dl.link_doctype='Customer'
19 and c.email_id='{email_id}'""".format(email_id=sender))
Rushabh Mehtad51e1582016-11-16 11:13:31 +053020
Rushabh Mehtaa5ebebd2017-11-16 17:03:52 +053021 if not customer:
Britlog495a4082017-06-01 15:38:04 +020022 lead = frappe.db.get_value('Lead', dict(email_id=sender))
23 if not lead:
24 new_lead = frappe.get_doc(dict(
25 doctype='Lead',
26 email_id = sender,
27 lead_name = sender.split('@')[0].title()
28 )).insert(ignore_permissions=True)
Rushabh Mehtad51e1582016-11-16 11:13:31 +053029
Rushabh Mehtaa5ebebd2017-11-16 17:03:52 +053030 opportunity = frappe.get_doc(dict(
31 doctype ='Opportunity',
32 enquiry_from = 'Customer' if customer else 'Lead',
33 status = 'Open',
34 title = subject,
35 contact_email = sender,
36 to_discuss = message
37 ))
Rushabh Mehtad51e1582016-11-16 11:13:31 +053038
Rushabh Mehtaa5ebebd2017-11-16 17:03:52 +053039 if customer:
40 opportunity.customer = customer[0][0]
41 elif lead:
42 opportunity.lead = lead
43 else:
44 opportunity.lead = 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
Rushabh Mehtaa5ebebd2017-11-16 17:03:52 +053048 comm = frappe.get_doc({
49 "doctype":"Communication",
50 "subject": subject,
51 "content": message,
52 "sender": sender,
53 "sent_or_received": "Received",
54 'reference_doctype': 'Opportunity',
55 'reference_name': opportunity.name
56 })
57 comm.insert(ignore_permissions=True)
Makarand Bauskar98f94272017-02-20 10:25:25 +053058
Rushabh Mehtaa5ebebd2017-11-16 17:03:52 +053059 return "okay"