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