diff --git a/core/doctype/communication/communication.py b/core/doctype/communication/communication.py index aa95d97f7e..14daa88691 100644 --- a/core/doctype/communication/communication.py +++ b/core/doctype/communication/communication.py @@ -4,25 +4,32 @@ from __future__ import unicode_literals import webnotes -@webnotes.whitelist() -def get_customer_supplier(args=None): - """ - Get Customer/Supplier, given a contact, if a unique match exists - """ - import webnotes - if not args: args = webnotes.form_dict - if not args.get('contact'): - raise Exception, "Please specify a contact to fetch Customer/Supplier" - result = webnotes.conn.sql("""\ - select customer, supplier - from `tabContact` - where name = %s""", args.get('contact'), as_dict=1) - if result and len(result)==1 and (result[0]['customer'] or result[0]['supplier']): - return { - 'fieldname': result[0]['customer'] and 'customer' or 'supplier', - 'value': result[0]['customer'] or result[0]['supplier'] - } - return {} +class DocType(): + def __init__(self, doc, doclist=[]): + self.doc = doc + self.doclist = doclist + + def get_parent_bean(self): + if self.doc.contact: + parent_doctype = "Contact" + parent_name = self.doc.contact + else: + parent_doctype = "Lead" + parent_name = self.doc.lead + + return webnotes.bean(parent_doctype, parent_name) + + def on_update(self): + """update status of parent Lead or Contact based on who is replying""" + parent = self.get_parent_bean() + + if webnotes.conn.get_value("Profile", self.doc.sender, "user_type")=="System User": + parent.doc.status = "Replied" + else: + parent.doc.status = "Open" + + parent.bean.ignore_permissions = True + parent.bean.save() @webnotes.whitelist() def make(doctype=None, name=None, content=None, subject=None, @@ -44,7 +51,8 @@ def make(doctype=None, name=None, content=None, subject=None, from email.utils import formataddr sender = formataddr(sender) - d = webnotes.doc('Communication') + comm = webnotes.new_bean('Communication') + d = comm.doc d.subject = subject d.content = content d.sender = sender or webnotes.conn.get_value("Profile", webnotes.session.user, "email") @@ -63,7 +71,27 @@ def make(doctype=None, name=None, content=None, subject=None, d.communication_medium = communication_medium if send_email: send_comm_email(d, name, sent_via, print_html, attachments, send_me_a_copy) - d.save(1, ignore_fields=True) + comm.insert() + +@webnotes.whitelist() +def get_customer_supplier(args=None): + """ + Get Customer/Supplier, given a contact, if a unique match exists + """ + import webnotes + if not args: args = webnotes.form_dict + if not args.get('contact'): + raise Exception, "Please specify a contact to fetch Customer/Supplier" + result = webnotes.conn.sql("""\ + select customer, supplier + from `tabContact` + where name = %s""", args.get('contact'), as_dict=1) + if result and len(result)==1 and (result[0]['customer'] or result[0]['supplier']): + return { + 'fieldname': result[0]['customer'] and 'customer' or 'supplier', + 'value': result[0]['customer'] or result[0]['supplier'] + } + return {} def send_comm_email(d, name, sent_via=None, print_html=None, attachments='[]', send_me_a_copy=False): from json import loads @@ -114,11 +142,6 @@ def set_lead_and_contact(d): d.company = webnotes.conn.get_value("Lead", d.lead, "company") or \ webnotes.conn.get_default("company") -class DocType(): - def __init__(self, doc, doclist=[]): - self.doc = doc - self.doclist = doclist - def get_user(doctype, txt, searchfield, start, page_len, filters): from controllers.queries import get_match_cond return webnotes.conn.sql("""select name, concat_ws(' ', first_name, middle_name, last_name)