mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-20 21:49:18 +00:00
refactored communication and added to Lead / Contact
This commit is contained in:
@@ -38,6 +38,72 @@ def get_customer_supplier(args=None):
|
||||
}
|
||||
return {}
|
||||
|
||||
@webnotes.whitelist()
|
||||
def make(doctype=None, name=None, content=None, subject=None,
|
||||
sender=None, recipients=None, contact=None, lead=None,
|
||||
communication_medium="Email", send_email=False):
|
||||
# add to Communication
|
||||
|
||||
sent_via = None
|
||||
|
||||
d = webnotes.doc('Communication')
|
||||
d.subject = subject
|
||||
d.content = content
|
||||
d.sender = sender or webnotes.conn.get_value("Profile", webnotes.session.user, "email")
|
||||
d.recipients = recipients
|
||||
d.lead = lead
|
||||
d.contact = contact
|
||||
if doctype:
|
||||
sent_via = webnotes.get_obj(doctype, name)
|
||||
d.fields[doctype.replace(" ", "_").lower()] = name
|
||||
|
||||
set_lead_and_contact(d)
|
||||
d.communication_medium = communication_medium
|
||||
if send_email:
|
||||
send_comm_email(d, sent_via)
|
||||
d.save(1)
|
||||
|
||||
def send_comm_email(d, sent_via=None):
|
||||
from webnotes.utils.email_lib import sendmail
|
||||
|
||||
if sent_via:
|
||||
if hasattr(sent_via, "get_sender"):
|
||||
d.sender = sent_via.get_sender(d)
|
||||
if hasattr(sent_via, "get_subject"):
|
||||
d.subject = sent_via.get_subject(d)
|
||||
if hasattr(sent_via, "get_content"):
|
||||
d.content = sent_via.get_content(d)
|
||||
|
||||
sendmail(\
|
||||
recipients = d.recipients.split(","), \
|
||||
sender = d.sender, \
|
||||
subject = d.subject, \
|
||||
msg= d.content)
|
||||
|
||||
if sent_via and hasattr(sent_via, 'on_communication_sent'):
|
||||
sent_via.on_communication_sent(d)
|
||||
|
||||
def set_lead_and_contact(d):
|
||||
import email.utils
|
||||
email_addr = email.utils.parseaddr(d.sender)
|
||||
# set contact
|
||||
if not d.contact:
|
||||
d.contact = webnotes.conn.get_value("Contact", {"email_id": email_addr[1]}, "name") or None
|
||||
|
||||
if not d.lead:
|
||||
d.lead = webnotes.conn.get_value("Lead", {"email_id": email_addr[1]}, "name") or None
|
||||
|
||||
if not d.lead and not d.contact:
|
||||
d.lead = make_lead(d, email_addr[0])
|
||||
|
||||
def make_lead(d, real_name):
|
||||
lead = webnotes.doc("Lead")
|
||||
lead.lead_name = real_name or d.sender
|
||||
lead.email_id = d.sender
|
||||
lead.source = "Email"
|
||||
lead.save(1)
|
||||
return lead.name
|
||||
|
||||
class DocType():
|
||||
def __init__(self, doc, doclist=[]):
|
||||
self.doc = doc
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
"docstatus": 0,
|
||||
"creation": "2012-11-14 12:25:16",
|
||||
"modified_by": "Administrator",
|
||||
"modified": "2012-11-26 12:41:59"
|
||||
"modified": "2012-11-27 12:24:43"
|
||||
},
|
||||
{
|
||||
"autoname": "naming_series:",
|
||||
@@ -121,7 +121,6 @@
|
||||
{
|
||||
"colour": "White:FFF",
|
||||
"doctype": "DocField",
|
||||
"label": "Related To",
|
||||
"fieldname": "column_break3",
|
||||
"fieldtype": "Column Break",
|
||||
"permlevel": 0
|
||||
@@ -189,6 +188,20 @@
|
||||
"fieldtype": "Column Break",
|
||||
"permlevel": 0
|
||||
},
|
||||
{
|
||||
"doctype": "DocField",
|
||||
"label": "Recipients",
|
||||
"fieldname": "recipients",
|
||||
"fieldtype": "Data",
|
||||
"permlevel": 0
|
||||
},
|
||||
{
|
||||
"doctype": "DocField",
|
||||
"label": "Sender",
|
||||
"fieldname": "sender",
|
||||
"fieldtype": "Data",
|
||||
"permlevel": 0
|
||||
},
|
||||
{
|
||||
"doctype": "DocField",
|
||||
"label": "Communication Medium",
|
||||
@@ -204,13 +217,6 @@
|
||||
"fieldtype": "Data",
|
||||
"permlevel": 0
|
||||
},
|
||||
{
|
||||
"doctype": "DocField",
|
||||
"label": "Email Address",
|
||||
"fieldname": "email_address",
|
||||
"fieldtype": "Data",
|
||||
"permlevel": 0
|
||||
},
|
||||
{
|
||||
"doctype": "DocField",
|
||||
"options": "simple",
|
||||
|
||||
@@ -79,13 +79,13 @@ class SupportMailbox(POP3Mailbox):
|
||||
WHERE name=%s AND raised_by REGEXP %s
|
||||
""" , (thread_id, '(' + email_id + ')'))
|
||||
if exists and exists[0] and exists[0][0]:
|
||||
from webnotes.model.code import get_obj
|
||||
st = webnotes.get_obj('Support Ticket', thread_id)
|
||||
|
||||
st = get_obj('Support Ticket', thread_id)
|
||||
st.make_response_record(content, full_email_id, content_type)
|
||||
from support.doctype.communication.communication import make
|
||||
|
||||
make(content=content, sender=full_email_id, doctype="Support Ticket",
|
||||
name=thread_id, lead = st.doc.lead, contact=st.doc.contact)
|
||||
|
||||
# to update modified date
|
||||
#webnotes.conn.set(st.doc, 'status', 'Open')
|
||||
st.doc.status = 'Open'
|
||||
st.doc.save()
|
||||
|
||||
|
||||
@@ -58,13 +58,15 @@ $.extend(cur_frm.cscript, {
|
||||
|
||||
var comm_list = wn.model.get("Communication", {"support_ticket": doc.name})
|
||||
comm_list.push({
|
||||
"email_address": doc.raised_by,
|
||||
"sender": doc.raised_by,
|
||||
"modified": doc.creation,
|
||||
"content": doc.description});
|
||||
|
||||
new erpnext.CommunicationView({
|
||||
cur_frm.communication_view = new erpnext.CommunicationView({
|
||||
list: comm_list,
|
||||
parent: wrapper
|
||||
parent: wrapper,
|
||||
doc: doc,
|
||||
email: doc.raised_by
|
||||
})
|
||||
|
||||
},
|
||||
|
||||
@@ -28,105 +28,26 @@ class DocType(TransactionBase):
|
||||
|
||||
def onload(self):
|
||||
self.add_communication_list()
|
||||
|
||||
def send_response(self):
|
||||
"""
|
||||
Adds a new response to the ticket and sends an email to the sender
|
||||
"""
|
||||
if not self.doc.new_response:
|
||||
webnotes.msgprint("Please write something as a response", raise_exception=1)
|
||||
|
||||
import markdown2
|
||||
self.doc.new_response = markdown2.markdown(self.doc.new_response)
|
||||
|
||||
subject = '[' + self.doc.name + '] ' + (self.doc.subject or 'No Subject Specified')
|
||||
|
||||
response = self.doc.new_response + '<p>[Please do not change the subject while responding.]</p>'
|
||||
|
||||
# add last response to new response
|
||||
response += self.last_response()
|
||||
|
||||
|
||||
def get_sender(self, comm):
|
||||
return webnotes.conn.get_value('Email Settings',None,'support_email')
|
||||
|
||||
def get_subject(self, comm):
|
||||
return '[' + self.doc.name + '] ' + (comm.doc.subject or 'No Subject Specified')
|
||||
|
||||
def get_content(self, comm):
|
||||
signature = webnotes.conn.get_value('Email Settings',None,'support_signature')
|
||||
content = comm.doc.content
|
||||
if signature:
|
||||
response += '<p>' + signature + '</p>'
|
||||
|
||||
from webnotes.utils.email_lib import sendmail
|
||||
content += '<p>' + signature + '</p>'
|
||||
return content
|
||||
|
||||
sendmail(\
|
||||
recipients = [self.doc.raised_by], \
|
||||
sender=webnotes.conn.get_value('Email Settings',None,'support_email'), \
|
||||
subject=subject, \
|
||||
msg=response)
|
||||
|
||||
self.doc.new_response = None
|
||||
def on_communication_sent(self, comm):
|
||||
webnotes.conn.set(self.doc, 'status', 'Waiting for Customer')
|
||||
self.make_response_record(response)
|
||||
self.add_communication_list()
|
||||
|
||||
def last_response(self):
|
||||
"""return last response"""
|
||||
tmp = webnotes.conn.sql("""select content from `tabCommunication`
|
||||
where support_ticket = %s order by creation desc limit 1
|
||||
""", self.doc.name)
|
||||
|
||||
if not tmp:
|
||||
tmp = webnotes.conn.sql("""
|
||||
SELECT description from `tabSupport Ticket`
|
||||
where name = %s
|
||||
""", self.doc.name)
|
||||
|
||||
response_title = "=== In response to ==="
|
||||
|
||||
if tmp and tmp[0][0]:
|
||||
return "\n\n" + response_title + "\n\n" + tmp[0][0].split(response_title)[0]
|
||||
else:
|
||||
return ""
|
||||
|
||||
|
||||
def make_response_record(self, response, from_email = None, content_type='text/plain'):
|
||||
"""
|
||||
Creates a new Communication record
|
||||
"""
|
||||
# add to Communication
|
||||
d = webnotes.doc('Communication')
|
||||
d.subject = self.doc.subject
|
||||
d.email_address = from_email or webnotes.user.name
|
||||
self.set_lead_and_contact(d)
|
||||
d.support_ticket = self.doc.name
|
||||
d.content = response
|
||||
d.communication_medium = "Email"
|
||||
d.save(1)
|
||||
|
||||
def set_lead_and_contact(self, d):
|
||||
import email.utils
|
||||
email_addr = email.utils.parseaddr(d.email_address)
|
||||
# set contact
|
||||
if self.doc.contact:
|
||||
d.contact = self.doc.contact
|
||||
else:
|
||||
d.contact = webnotes.conn.get_value("Contact", {"email_id": email_addr[1]}, "name") or None
|
||||
if d.contact:
|
||||
webnotes.conn.set(self.doc, "contact", d.contact)
|
||||
|
||||
if self.doc.lead:
|
||||
d.lead = self.doc.lead
|
||||
else:
|
||||
d.lead = webnotes.conn.get_value("Lead", {"email_id": email_addr[1]}, "name") or None
|
||||
if d.lead:
|
||||
webnotes.conn.set(self.doc, "lead", d.lead)
|
||||
|
||||
# not linked to any lead / contact, create new lead
|
||||
if not d.lead and not d.contact:
|
||||
d.lead = self.make_lead(d, email_addr[0])
|
||||
webnotes.conn.set(self.doc, "lead", d.lead)
|
||||
|
||||
def make_lead(self, d, real_name):
|
||||
d = webnotes.doc("Lead")
|
||||
d.lead_name = real_name or d.email_address
|
||||
d.email_id = d.email_address
|
||||
d.source = "Email"
|
||||
d.save(1)
|
||||
return d.name
|
||||
if comm.doc.lead and not self.doc.lead:
|
||||
webnotes.conn.set(self.doc, 'lead', comm.doc.lead)
|
||||
if comm.doc.contact and not self.doc.contact:
|
||||
webnotes.conn.set(self.doc, 'contact', comm.doc.contact)
|
||||
|
||||
def close_ticket(self):
|
||||
webnotes.conn.set(self.doc,'status','Closed')
|
||||
@@ -137,5 +58,5 @@ class DocType(TransactionBase):
|
||||
update_feed(self.doc)
|
||||
|
||||
def on_trash(self):
|
||||
webnotes.conn.sql("""update `tabCommunication set support_ticket=""
|
||||
webnotes.conn.sql("""update `tabCommunication` set support_ticket=""
|
||||
where support_ticket=%s`""", self.doc.name)
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
"docstatus": 0,
|
||||
"creation": "2012-11-02 17:17:05",
|
||||
"modified_by": "Administrator",
|
||||
"modified": "2012-11-26 12:54:25"
|
||||
"modified": "2012-11-27 18:21:06"
|
||||
},
|
||||
{
|
||||
"autoname": "naming_series:",
|
||||
@@ -97,22 +97,6 @@
|
||||
"reqd": 0,
|
||||
"permlevel": 0
|
||||
},
|
||||
{
|
||||
"depends_on": "eval:!doc.__islocal",
|
||||
"doctype": "DocField",
|
||||
"label": "New Response",
|
||||
"fieldname": "new_response",
|
||||
"fieldtype": "Text",
|
||||
"permlevel": 0
|
||||
},
|
||||
{
|
||||
"depends_on": "eval:!doc.__islocal",
|
||||
"doctype": "DocField",
|
||||
"label": "Send",
|
||||
"fieldname": "send",
|
||||
"fieldtype": "Button",
|
||||
"permlevel": 0
|
||||
},
|
||||
{
|
||||
"depends_on": "eval:!doc.__islocal",
|
||||
"doctype": "DocField",
|
||||
|
||||
Reference in New Issue
Block a user