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 | |
Aditya Hase | f79937d | 2019-01-23 00:28:37 +0530 | [diff] [blame] | 6 | import frappe |
| 7 | |
Anand Doshi | fb109ad | 2013-09-11 18:58:20 +0530 | [diff] [blame] | 8 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 9 | @frappe.whitelist(allow_guest=True) |
Anand Doshi | fb109ad | 2013-09-11 18:58:20 +0530 | [diff] [blame] | 10 | def send_message(subject="Website Query", message="", sender="", status="Open"): |
Rushabh Mehta | a5ebebd | 2017-11-16 17:03:52 +0530 | [diff] [blame] | 11 | from frappe.www.contact import send_message as website_send_message |
| 12 | lead = customer = None |
Anand Doshi | 5d591eb | 2014-04-18 16:15:31 +0530 | [diff] [blame] | 13 | |
Rushabh Mehta | a5ebebd | 2017-11-16 17:03:52 +0530 | [diff] [blame] | 14 | website_send_message(subject, message, sender) |
Anand Doshi | 5d591eb | 2014-04-18 16:15:31 +0530 | [diff] [blame] | 15 | |
Rushabh Mehta | a5ebebd | 2017-11-16 17:03:52 +0530 | [diff] [blame] | 16 | customer = frappe.db.sql("""select distinct dl.link_name from `tabDynamic Link` dl |
| 17 | left join `tabContact` c on dl.parent=c.name where dl.link_doctype='Customer' |
Aditya Hase | 9acb885 | 2019-01-29 10:52:37 +0530 | [diff] [blame] | 18 | and c.email_id = %s""", sender) |
Rushabh Mehta | d51e158 | 2016-11-16 11:13:31 +0530 | [diff] [blame] | 19 | |
Rushabh Mehta | a5ebebd | 2017-11-16 17:03:52 +0530 | [diff] [blame] | 20 | if not customer: |
Britlog | 495a408 | 2017-06-01 15:38:04 +0200 | [diff] [blame] | 21 | lead = frappe.db.get_value('Lead', dict(email_id=sender)) |
| 22 | if not lead: |
| 23 | new_lead = frappe.get_doc(dict( |
| 24 | doctype='Lead', |
| 25 | email_id = sender, |
| 26 | lead_name = sender.split('@')[0].title() |
| 27 | )).insert(ignore_permissions=True) |
Rushabh Mehta | d51e158 | 2016-11-16 11:13:31 +0530 | [diff] [blame] | 28 | |
Rushabh Mehta | a5ebebd | 2017-11-16 17:03:52 +0530 | [diff] [blame] | 29 | opportunity = frappe.get_doc(dict( |
| 30 | doctype ='Opportunity', |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 31 | opportunity_from = 'Customer' if customer else 'Lead', |
Rushabh Mehta | a5ebebd | 2017-11-16 17:03:52 +0530 | [diff] [blame] | 32 | status = 'Open', |
| 33 | title = subject, |
| 34 | contact_email = sender, |
| 35 | to_discuss = message |
| 36 | )) |
Rushabh Mehta | d51e158 | 2016-11-16 11:13:31 +0530 | [diff] [blame] | 37 | |
Rushabh Mehta | a5ebebd | 2017-11-16 17:03:52 +0530 | [diff] [blame] | 38 | if customer: |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 39 | opportunity.party_name = customer[0][0] |
Rushabh Mehta | a5ebebd | 2017-11-16 17:03:52 +0530 | [diff] [blame] | 40 | elif lead: |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 41 | opportunity.party_name = lead |
Rushabh Mehta | a5ebebd | 2017-11-16 17:03:52 +0530 | [diff] [blame] | 42 | else: |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 43 | opportunity.party_name = new_lead.name |
Rushabh Mehta | d51e158 | 2016-11-16 11:13:31 +0530 | [diff] [blame] | 44 | |
Rushabh Mehta | a5ebebd | 2017-11-16 17:03:52 +0530 | [diff] [blame] | 45 | opportunity.insert(ignore_permissions=True) |
Rushabh Mehta | ddd79f4 | 2015-08-25 14:03:43 +0530 | [diff] [blame] | 46 | |
Rushabh Mehta | a5ebebd | 2017-11-16 17:03:52 +0530 | [diff] [blame] | 47 | 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) |
Makarand Bauskar | 98f9427 | 2017-02-20 10:25:25 +0530 | [diff] [blame] | 57 | |
Rushabh Mehta | a5ebebd | 2017-11-16 17:03:52 +0530 | [diff] [blame] | 58 | return "okay" |