From a65ad2c901e7ad25f0069dc99538c5e439d1faf3 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Fri, 8 Jun 2012 18:21:35 +0530 Subject: [PATCH] fetch customer/supplier given a contact in communication --- .../doctype/communication/communication.js | 19 ++++++++++++++++++ .../doctype/communication/communication.py | 20 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/erpnext/support/doctype/communication/communication.js b/erpnext/support/doctype/communication/communication.js index 36e5c907f0..9b7b896a7b 100644 --- a/erpnext/support/doctype/communication/communication.js +++ b/erpnext/support/doctype/communication/communication.js @@ -91,4 +91,23 @@ cur_frm.cscript.render_list = function(doc, doctype, wrapper, ListView, make_new var record_list_view = new RecordListView(doctype, wrapper, ListView); }); +} + + +cur_frm.cscript.contact = function(doc, dt, dn) { + if (doc.contact) { + wn.call({ + method: 'support.doctype.communication.communication.get_customer_supplier', + args: { + contact: doc.contact + }, + callback: function(r, rt) { + if (!r.exc && r.message) { + doc = locals[doc.doctype][doc.name]; + doc[r.message['fieldname']] = r.message['value']; + refresh_field(r.message['fieldname']); + } + }, + }); + } } \ No newline at end of file diff --git a/erpnext/support/doctype/communication/communication.py b/erpnext/support/doctype/communication/communication.py index c99422da9b..8382eb7bbe 100644 --- a/erpnext/support/doctype/communication/communication.py +++ b/erpnext/support/doctype/communication/communication.py @@ -17,6 +17,26 @@ import webnotes from webnotes.model.doc import make_autoname +@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