blob: e46fed6bb6faea1f94d12858f8afa64a0d617434 [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"):
Makarand Bauskar98f94272017-02-20 10:25:25 +053012 from frappe.www.contact import send_message as website_send_message
13 lead = customer = None
Anand Doshi5d591eb2014-04-18 16:15:31 +053014
Makarand Bauskar98f94272017-02-20 10:25:25 +053015 website_send_message(subject, message, sender)
Anand Doshi5d591eb2014-04-18 16:15:31 +053016
Makarand Bauskar98f94272017-02-20 10:25:25 +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
Makarand Bauskar98f94272017-02-20 10:25:25 +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
Makarand Bauskar98f94272017-02-20 10:25:25 +053030 opportunity = frappe.get_doc(dict(
31 doctype='Opportunity',
32 enquiry_from = 'Customer' if customer else 'Lead',
33 status = 'Open',
34 title = subject,
35 to_discuss=message
36 ))
Rushabh Mehtad51e1582016-11-16 11:13:31 +053037
Makarand Bauskar98f94272017-02-20 10:25:25 +053038 if customer:
39 opportunity.customer = customer[0][0]
40 elif lead:
41 opportunity.lead = lead
42 else:
43 opportunity.lead = new_lead.name
Rushabh Mehtad51e1582016-11-16 11:13:31 +053044
Makarand Bauskar98f94272017-02-20 10:25:25 +053045 opportunity.insert(ignore_permissions=True)
Rushabh Mehtaddd79f42015-08-25 14:03:43 +053046
Makarand Bauskar98f94272017-02-20 10:25:25 +053047 comm = frappe.get_doc({
48 "doctype":"Communication",
49 "subject": subject,
50 "content": message,
51 "sender": sender,
52 "sent_or_received": "Received",
53 'reference_doctype': 'Opportunity',
54 'reference_name': opportunity.name
55 })
56 comm.insert(ignore_permissions=True)
57
58 return "okay"