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"): |
Rushabh Mehta | a334062 | 2016-06-23 18:25:50 +0530 | [diff] [blame] | 12 | from frappe.www.contact import send_message as website_send_message |
Rushabh Mehta | d51e158 | 2016-11-16 11:13:31 +0530 | [diff] [blame] | 13 | lead = customer = None |
Anand Doshi | 5d591eb | 2014-04-18 16:15:31 +0530 | [diff] [blame] | 14 | |
Rushabh Mehta | 5cdc8e5 | 2014-09-15 16:59:38 +0530 | [diff] [blame] | 15 | website_send_message(subject, message, sender) |
Anand Doshi | 5d591eb | 2014-04-18 16:15:31 +0530 | [diff] [blame] | 16 | |
Rushabh Mehta | d51e158 | 2016-11-16 11:13:31 +0530 | [diff] [blame] | 17 | 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 Orellana | ac81440 | 2017-01-12 12:08:20 -0500 | [diff] [blame] | 37 | elif lead: |
| 38 | opportunity.lead = lead |
Rushabh Mehta | d51e158 | 2016-11-16 11:13:31 +0530 | [diff] [blame] | 39 | else: |
| 40 | opportunity.lead = new_lead.name |
| 41 | |
| 42 | opportunity.insert(ignore_permissions=True) |
| 43 | |
Rushabh Mehta | 5cdc8e5 | 2014-09-15 16:59:38 +0530 | [diff] [blame] | 44 | comm = frappe.get_doc({ |
| 45 | "doctype":"Communication", |
| 46 | "subject": subject, |
| 47 | "content": message, |
| 48 | "sender": sender, |
Rushabh Mehta | d51e158 | 2016-11-16 11:13:31 +0530 | [diff] [blame] | 49 | "sent_or_received": "Received", |
| 50 | 'reference_doctype': 'Opportunity', |
| 51 | 'reference_name': opportunity.name |
Rushabh Mehta | 5cdc8e5 | 2014-09-15 16:59:38 +0530 | [diff] [blame] | 52 | }) |
| 53 | comm.insert(ignore_permissions=True) |
Rushabh Mehta | ddd79f4 | 2015-08-25 14:03:43 +0530 | [diff] [blame] | 54 | |
| 55 | return "okay" |