Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
Anand Doshi | 330dae9 | 2013-09-10 13:46:15 +0530 | [diff] [blame] | 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
| 4 | from __future__ import unicode_literals |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 5 | |
| 6 | import frappe, json |
| 7 | from frappe import _ |
| 8 | from frappe.utils import cint, formatdate |
Anand Doshi | fb109ad | 2013-09-11 18:58:20 +0530 | [diff] [blame] | 9 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 10 | @frappe.whitelist(allow_guest=True) |
Anand Doshi | fb109ad | 2013-09-11 18:58:20 +0530 | [diff] [blame] | 11 | def send_message(subject="Website Query", message="", sender="", status="Open"): |
Makarand Bauskar | 98f9427 | 2017-02-20 10:25:25 +0530 | [diff] [blame] | 12 | from frappe.www.contact import send_message as website_send_message |
| 13 | lead = customer = None |
Anand Doshi | 5d591eb | 2014-04-18 16:15:31 +0530 | [diff] [blame] | 14 | |
Makarand Bauskar | 98f9427 | 2017-02-20 10:25:25 +0530 | [diff] [blame] | 15 | website_send_message(subject, message, sender) |
Anand Doshi | 5d591eb | 2014-04-18 16:15:31 +0530 | [diff] [blame] | 16 | |
Makarand Bauskar | 98f9427 | 2017-02-20 10:25:25 +0530 | [diff] [blame] | 17 | 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 Mehta | d51e158 | 2016-11-16 11:13:31 +0530 | [diff] [blame] | 20 | |
Makarand Bauskar | 98f9427 | 2017-02-20 10:25:25 +0530 | [diff] [blame] | 21 | if not customer: |
Britlog | 495a408 | 2017-06-01 15:38:04 +0200 | [diff] [blame] | 22 | 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 Mehta | d51e158 | 2016-11-16 11:13:31 +0530 | [diff] [blame] | 29 | |
Makarand Bauskar | 98f9427 | 2017-02-20 10:25:25 +0530 | [diff] [blame] | 30 | opportunity = frappe.get_doc(dict( |
Neil Trini Lasrado | 1f99bea | 2017-10-25 11:44:12 +0530 | [diff] [blame] | 31 | doctype ='Opportunity', |
Makarand Bauskar | 98f9427 | 2017-02-20 10:25:25 +0530 | [diff] [blame] | 32 | enquiry_from = 'Customer' if customer else 'Lead', |
| 33 | status = 'Open', |
| 34 | title = subject, |
Neil Trini Lasrado | 1f99bea | 2017-10-25 11:44:12 +0530 | [diff] [blame] | 35 | contact_email = sender, |
| 36 | to_discuss = message |
Makarand Bauskar | 98f9427 | 2017-02-20 10:25:25 +0530 | [diff] [blame] | 37 | )) |
Rushabh Mehta | d51e158 | 2016-11-16 11:13:31 +0530 | [diff] [blame] | 38 | |
Makarand Bauskar | 98f9427 | 2017-02-20 10:25:25 +0530 | [diff] [blame] | 39 | if customer: |
Aditya Hase | 6ccb656 | 2017-08-28 18:17:36 +0530 | [diff] [blame] | 40 | opportunity.customer = customer[0][0] |
Makarand Bauskar | 98f9427 | 2017-02-20 10:25:25 +0530 | [diff] [blame] | 41 | elif lead: |
Aditya Hase | 6ccb656 | 2017-08-28 18:17:36 +0530 | [diff] [blame] | 42 | opportunity.lead = lead |
Makarand Bauskar | 98f9427 | 2017-02-20 10:25:25 +0530 | [diff] [blame] | 43 | else: |
Aditya Hase | 6ccb656 | 2017-08-28 18:17:36 +0530 | [diff] [blame] | 44 | opportunity.lead = new_lead.name |
Rushabh Mehta | d51e158 | 2016-11-16 11:13:31 +0530 | [diff] [blame] | 45 | |
Makarand Bauskar | 98f9427 | 2017-02-20 10:25:25 +0530 | [diff] [blame] | 46 | opportunity.insert(ignore_permissions=True) |
Rushabh Mehta | ddd79f4 | 2015-08-25 14:03:43 +0530 | [diff] [blame] | 47 | |
Makarand Bauskar | 98f9427 | 2017-02-20 10:25:25 +0530 | [diff] [blame] | 48 | 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) |
| 58 | |
| 59 | return "okay" |