blob: c1405c374fd5c9c5cce57858f050cc5bbb4fb348 [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 Mehtaa3340622016-06-23 18:25:50 +053012 from frappe.www.contact import send_message as website_send_message
Rushabh Mehtad51e1582016-11-16 11:13:31 +053013 lead = customer = None
Anand Doshi5d591eb2014-04-18 16:15:31 +053014
Rushabh Mehta5cdc8e52014-09-15 16:59:38 +053015 website_send_message(subject, message, sender)
Anand Doshi5d591eb2014-04-18 16:15:31 +053016
Rushabh Mehtad51e1582016-11-16 11:13:31 +053017 customer = frappe.db.get_value('Contact', dict(email_id=sender), 'customer')
18 if not customer:
19 lead = frappe.db.get_value('Lead', dict(email_id=sender))
20 if not lead:
21 new_lead = frappe.get_doc(dict(
22 doctype='Lead',
23 email_id = sender,
24 lead_name = sender.split('@')[0].title()
25 )).insert(ignore_permissions=True)
26
27 opportunity = frappe.get_doc(dict(
28 doctype='Opportunity',
29 enquiry_from = 'Customer' if customer else 'Lead',
30 status = 'Open',
31 title = subject,
32 to_discuss=message
33 ))
34
35 if customer:
36 opportunity.customer = customer
Felipe Orellanaac814402017-01-12 12:08:20 -050037 elif lead:
38 opportunity.lead = lead
Rushabh Mehtad51e1582016-11-16 11:13:31 +053039 else:
40 opportunity.lead = new_lead.name
41
42 opportunity.insert(ignore_permissions=True)
43
Rushabh Mehta5cdc8e52014-09-15 16:59:38 +053044 comm = frappe.get_doc({
45 "doctype":"Communication",
46 "subject": subject,
47 "content": message,
48 "sender": sender,
Rushabh Mehtad51e1582016-11-16 11:13:31 +053049 "sent_or_received": "Received",
50 'reference_doctype': 'Opportunity',
51 'reference_name': opportunity.name
Rushabh Mehta5cdc8e52014-09-15 16:59:38 +053052 })
53 comm.insert(ignore_permissions=True)
Rushabh Mehtaddd79f42015-08-25 14:03:43 +053054
55 return "okay"