started cleanup of address / contact calls - partly fixed for customer, to fix shipping_address, get_pos_values, for all supplier transactions
diff --git a/erpnext/accounts/doctype/account/test_account.py b/erpnext/accounts/doctype/account/test_account.py
index 2d15b62..3221901 100644
--- a/erpnext/accounts/doctype/account/test_account.py
+++ b/erpnext/accounts/doctype/account/test_account.py
@@ -4,7 +4,7 @@
 from __future__ import unicode_literals
 import webnotes
 
-def make_test_records(verbose):
+def _make_test_records(verbose):
 	from webnotes.test_runner import make_test_objects
 		
 	accounts = [
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
index 9c6a9b2..010ef63 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
@@ -154,6 +154,19 @@
 		}
 	},
 	
+	customer: function() {
+		if(this.frm.updating_customer_details)
+			return;
+		erpnext.selling.get_customer_details(this.frm, 
+			"erpnext.accounts.doctype.sales_invoice.sales_invoice.get_customer_details",
+			{
+				posting_date: this.frm.doc.posting_date,
+				company: this.frm.doc.company,
+				customer: this.frm.doc.customer,
+				debit_to: this.frm.doc.debit_to
+			})
+	},
+	
 	debit_to: function() {
 		this.customer();
 	},
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
index f77abfe..10255fa 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
@@ -147,23 +147,13 @@
 		self.set_pos_fields(for_validate)
 		
 		if not self.doc.debit_to:
-			self.doc.debit_to = self.get_customer_account()
+			self.doc.debit_to = get_customer_account(self.doc.company, self.doc.customer)
 		if not self.doc.due_date:
-			self.doc.due_date = self.get_due_date()
+			self.doc.due_date = get_due_date(self.doc.posting_date, self.doc.customer, 
+				self.doc.debit_to, self.doc.company)
 		
 		super(DocType, self).set_missing_values(for_validate)
-		
-	def set_customer_defaults(self):
-		# TODO cleanup these methods
-		if self.doc.customer:
-			self.doc.debit_to = self.get_customer_account()
-		elif self.doc.debit_to:
-			self.doc.customer = webnotes.conn.get_value('Account', self.doc.debit_to, 'master_name')
-		
-		self.doc.due_date = self.get_due_date()
-		
-		super(DocType, self).set_customer_defaults()
-			
+					
 	def update_time_log_batch(self, sales_invoice):
 		for d in self.doclist.get({"doctype":"Sales Invoice Item"}):
 			if d.time_log_batch:
@@ -190,7 +180,7 @@
 		if pos:
 			if not for_validate and not self.doc.customer:
 				self.doc.customer = pos.customer
-				self.set_customer_defaults()
+				# self.set_customer_defaults()
 
 			for fieldname in ('territory', 'naming_series', 'currency', 'charge', 'letter_head', 'tc_name',
 				'selling_price_list', 'company', 'select_print_heading', 'cash_bank_account'):
@@ -214,44 +204,6 @@
 			# fetch charges
 			if self.doc.charge and not len(self.doclist.get({"parentfield": "other_charges"})):
 				self.set_taxes("other_charges", "charge")
-
-	def get_customer_account(self):
-		"""Get Account Head to which amount needs to be Debited based on Customer"""
-		if not self.doc.company:
-			msgprint("Please select company first and re-select the customer after doing so",
-			 	raise_exception=1)
-		if self.doc.customer:
-			acc_head = webnotes.conn.sql("""select name from `tabAccount` 
-				where (name = %s or (master_name = %s and master_type = 'customer')) 
-				and docstatus != 2 and company = %s""", 
-				(cstr(self.doc.customer) + " - " + self.get_company_abbr(), 
-				self.doc.customer, self.doc.company))
-			
-			if acc_head and acc_head[0][0]:
-				return acc_head[0][0]
-			else:
-				msgprint("%s does not have an Account Head in %s. \
-					You must first create it from the Customer Master" % 
-					(self.doc.customer, self.doc.company))
-
-	def get_due_date(self):
-		"""Set Due Date = Posting Date + Credit Days"""
-		due_date = None
-		if self.doc.posting_date:
-			credit_days = 0
-			if self.doc.debit_to:
-				credit_days = webnotes.conn.get_value("Account", self.doc.debit_to, "credit_days")
-			if self.doc.customer and not credit_days:
-				credit_days = webnotes.conn.get_value("Customer", self.doc.customer, "credit_days")
-			if self.doc.company and not credit_days:
-				credit_days = webnotes.conn.get_value("Company", self.doc.company, "credit_days")
-				
-			if credit_days:
-				due_date = add_days(self.doc.posting_date, credit_days)
-			else:
-				due_date = self.doc.posting_date
-
-		return due_date	
 	
 	def get_advances(self):
 		super(DocType, self).get_advances(self.doc.debit_to, 
@@ -894,6 +846,59 @@
 		assign_to.add(args)
 
 @webnotes.whitelist()
+def get_customer_details(company, customer, debit_to, posting_date):
+	if not webnotes.has_permission("Customer", "read", customer):
+		webnotes.throw("No Permission")
+		
+	from erpnext.selling.doctype.customer.customer import get_customer_details
+	
+	if customer:
+		debit_to = get_customer_account(company, customer)
+	elif debit_to:
+		customer = webnotes.conn.get_value('Account',debit_to, 'master_name')
+
+	out = {
+		"customer": customer,
+		"debit_to": debit_to,
+		"due_date": get_due_date(posting_date, customer, debit_to, company)
+	}	
+	out.update(get_customer_details(customer))
+	return out
+	
+def get_customer_account(company, customer):
+	if not company:
+		webnotes.throw(_("Please select company first."))
+
+	if customer:
+		acc_head = webnotes.conn.get_value("Account", {"master_name":customer,
+			"master_type":"Customer", "company": company})
+
+		if not acc_head:
+			webnotes.throw(_("Customer has no account head in selected Company. Open the customer record and create an Account Head first."))
+	
+		return acc_head		
+
+def get_due_date(posting_date, customer, debit_to, company):
+	"""Set Due Date = Posting Date + Credit Days"""
+	due_date = None
+	if posting_date:
+		credit_days = 0
+		if debit_to:
+			credit_days = webnotes.conn.get_value("Account", debit_to, "credit_days")
+		if customer and not credit_days:
+			credit_days = webnotes.conn.get_value("Customer", customer, "credit_days")
+		if company and not credit_days:
+			credit_days = webnotes.conn.get_value("Company", company, "credit_days")
+			
+		if credit_days:
+			due_date = add_days(posting_date, credit_days)
+		else:
+			due_date = posting_date
+
+	return due_date	
+
+
+@webnotes.whitelist()
 def get_bank_cash_account(mode_of_payment):
 	val = webnotes.conn.get_value("Mode of Payment", mode_of_payment, "default_account")
 	if not val:
diff --git a/erpnext/buying/doctype/supplier/supplier.py b/erpnext/buying/doctype/supplier/supplier.py
index 99c7352..ec60428 100644
--- a/erpnext/buying/doctype/supplier/supplier.py
+++ b/erpnext/buying/doctype/supplier/supplier.py
@@ -194,4 +194,27 @@
 	out["total_billing"] = billing[0][0]
 	out["total_unpaid"] = billing[0][1]
 	
-	return out
\ No newline at end of file
+	return out
+	
+@webnotes.whitelist()
+def get_supplier_details(supplier):
+	if not webnotes.has_permission("Supplier", "read", supplier):
+		webnotes.msgprint("No Permission", raise_exception=webnotes.PermissionError)
+
+	supplier = webnotes.doc("Supplier", supplier)
+	
+	out = webnotes._dict({
+		"supplier_address": webnotes.conn.get_value("Address", 
+			{"supplier": supplier.name, "is_primary_address":1}, "name"),
+		"contact_person": webnotes.conn.get_value("Contact", 
+			{"supplier":supplier.name, "is_primary_contact":1}, "name")
+	})
+	
+	out.supplier_name = supplier.supplier_name
+	out.currency = supplier.default_currency
+	out.buying_price_list = supplier.default_price_list
+		
+	return out
+		
+	
+	
\ No newline at end of file
diff --git a/erpnext/controllers/selling_controller.py b/erpnext/controllers/selling_controller.py
index f950f28..7b1be18 100644
--- a/erpnext/controllers/selling_controller.py
+++ b/erpnext/controllers/selling_controller.py
@@ -33,9 +33,10 @@
 			self.set_taxes("other_charges", "charge")
 					
 	def set_missing_lead_customer_details(self):
+		from erpnext.selling.doctype.customer.customer import get_customer_details
 		if self.doc.customer:
 			if not (self.doc.contact_person and self.doc.customer_address and self.doc.customer_name):
-				for fieldname, val in self.get_customer_defaults().items():
+				for fieldname, val in get_customer_details(self.doc.customer).iteritems():
 					if not self.doc.fields.get(fieldname) and self.meta.get_field(fieldname):
 						self.doc.fields[fieldname] = val
 		
diff --git a/erpnext/public/build.json b/erpnext/public/build.json
index ea9808a..bb81a58 100644
--- a/erpnext/public/build.json
+++ b/erpnext/public/build.json
@@ -11,6 +11,7 @@
 		"public/js/toolbar.js",
 		"public/js/feature_setup.js",
 		"public/js/utils.js",
-		"public/js/queries.js"
+		"public/js/queries.js",
+		"public/js/utils/customer_supplier.js"
 	]
 }
\ No newline at end of file
diff --git a/erpnext/public/js/utils/customer_supplier.js b/erpnext/public/js/utils/customer_supplier.js
new file mode 100644
index 0000000..ccfbfc9
--- /dev/null
+++ b/erpnext/public/js/utils/customer_supplier.js
@@ -0,0 +1,46 @@
+// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
+// License: GNU General Public License v3. See license.txt
+
+wn.provide("erpnext.utils");
+erpnext.utils.get_customer_details = function(frm, method, args) {
+	if(!method) 
+		method = "erpnext.selling.doctype.customer.customer.get_customer_details";
+	if(!args) 
+		args = { customer: frm.doc.customer };
+	wn.call({
+		method: method,
+		args: args,
+		callback: function(r) {
+			if(r.message) {
+				frm.updating_customer_details = true;
+				frm.set_value(r.message);
+				frm.updating_customer_details = false;
+			}
+		}
+	});
+}
+
+erpnext.utils.get_address_display = function(frm, address_field) {
+	if(frm.updating_customer_details) return;
+	if(!address_field) address_field = "customer_address";
+	wn.call({
+		method: "erpnext.utilities.doctype.address.address.get_address_display",
+		args: {address: frm.doc[address_field] },
+		callback: function(r) {
+			if(r.message)
+				frm.set_value("address_display", r.message)
+		}
+	})
+}
+
+erpnext.utils.get_contact_details = function(frm) {
+	if(frm.updating_customer_details) return;
+	wn.call({
+		method: "erpnext.utilities.doctype.contact.contact.get_contact_details",
+		args: {address: frm.doc.contact_person },
+		callback: function(r) {
+			if(r.message)
+				frm.set_value(r.message);
+		}
+	})
+}
\ No newline at end of file
diff --git a/erpnext/selling/doctype/customer/customer.py b/erpnext/selling/doctype/customer/customer.py
index ee54f93..05e8112 100644
--- a/erpnext/selling/doctype/customer/customer.py
+++ b/erpnext/selling/doctype/customer/customer.py
@@ -10,6 +10,8 @@
 
 
 from erpnext.utilities.transaction_base import TransactionBase
+from erpnext.utilities.doctype.address.address import get_address_display
+from erpnext.utilities.doctype.contact.contact import get_contact_details
 
 class DocType(TransactionBase):
 	def __init__(self, doc, doclist=[]):
@@ -189,4 +191,51 @@
 	out["total_billing"] = billing[0][0]
 	out["total_unpaid"] = billing[0][1]
 	
-	return out
\ No newline at end of file
+	return out
+
+@webnotes.whitelist()
+def get_customer_details(customer):
+	if not webnotes.has_permission("Customer", "read", customer):
+		webnotes.throw("Not Permitted", webnotes.PermissionError)
+		
+	out = {}
+	customer_bean = webnotes.bean("Customer", customer)
+	customer = customer_bean.doc
+
+	out = webnotes._dict({
+		"customer_address": webnotes.conn.get_value("Address", 
+			{"customer": customer.name, "is_primary_address":1}, "name"),
+		"contact_person": webnotes.conn.get_value("Contact", 
+			{"customer":customer.name, "is_primary_contact":1}, "name")
+	})
+
+	# address display
+	out.address_display = get_address_display(out.customer_address)
+	
+	# primary contact details
+	out.update(get_contact_details(out.contact_person))
+
+	# copy
+	for f in ['customer_name', 'customer_group', 'territory']:
+		out[f] = customer.get(f)
+	
+	# fields prepended with default in Customer doctype
+	for f in ['sales_partner', 'commission_rate', 'currency', 'price_list']:
+		if customer.get("default_" + f):
+			out[f] = customer.get("default_" + f)
+
+	# price list
+	out.selling_price_list = customer.price_list or webnotes.conn.get_value("Customer Group",
+		customer.customer_group, "default_price_list")
+	
+	if out.selling_price_list:
+		out.price_list_currency = webnotes.conn.get_value("Price List", out.selling_price_list, "currency")
+	
+	# sales team
+	out.sales_team = [{
+		"sales_person": d.sales_person, 
+		"sales_designation": d.sales_designation
+	} for d in customer_bean.doclist.get({"doctype":"Sales Team"})]
+	
+	return out
+	
\ No newline at end of file
diff --git a/erpnext/selling/doctype/customer/test_customer.py b/erpnext/selling/doctype/customer/test_customer.py
index 707d38a..b3e0372 100644
--- a/erpnext/selling/doctype/customer/test_customer.py
+++ b/erpnext/selling/doctype/customer/test_customer.py
@@ -6,7 +6,38 @@
 import webnotes
 import unittest
 
+from webnotes.test_runner import make_test_records
+
+
 class TestCustomer(unittest.TestCase):
+	def test_get_customer_details(self):
+		from erpnext.selling.doctype.customer.customer import get_customer_details
+		
+		to_check = {
+			'address_display': '_Test Address Line 1\n_Test City\nIndia\nPhone: +91 0000000000', 
+			'selling_price_list': None, 
+			'customer_group': '_Test Customer Group', 
+			'contact_designation': None, 
+			'customer_address': '_Test Address-Office', 
+			'contact_department': None, 
+			'contact_email': 'test_contact_customer@example.com', 
+			'contact_mobile': None, 
+			'_sales_team': [], 
+			'contact_display': '_Test Contact For _Test Customer', 
+			'contact_person': '_Test Contact For _Test Customer-_Test Customer', 
+			'territory': u'_Test Territory', 
+			'contact_phone': '+91 0000000000', 
+			'customer_name': '_Test Customer'
+		}
+		
+		make_test_records("Address")
+		make_test_records("Contact")
+				
+		details = get_customer_details("_Test Customer")
+
+		for key, value in to_check.iteritems():
+			self.assertEquals(value, details.get(key))
+		
 	def test_rename(self):
 		self.assertEqual(webnotes.conn.exists("Customer", "_Test Customer 1"), 
 			(("_Test Customer 1",),))
@@ -20,7 +51,6 @@
 		webnotes.rename_doc("Customer", "_Test Customer 1 Renamed", "_Test Customer 1")
 		
 	def test_merge(self):
-		from webnotes.test_runner import make_test_records
 		make_test_records("Sales Invoice")
 		
 		# clear transactions for new name
diff --git a/erpnext/selling/doctype/installation_note/installation_note.js b/erpnext/selling/doctype/installation_note/installation_note.js
index 223bd8d..c666247 100644
--- a/erpnext/selling/doctype/installation_note/installation_note.js
+++ b/erpnext/selling/doctype/installation_note/installation_note.js
@@ -4,6 +4,16 @@
 cur_frm.cscript.tname = "Installation Note Item";
 cur_frm.cscript.fname = "installed_item_details";
 
+
+wn.ui.form.on_change("Installation Note", "customer", 
+	function(frm) { erpnext.utils.get_customer_details(frm); });
+
+wn.ui.form.on_change("Installation Note", "customer_address", 
+	function(frm) { erpnext.utils.get_address_display(frm); });
+
+wn.ui.form.on_change("Installation Note", "contact_person", 
+	function(frm) { erpnext.utils.get_contact_details(frm); });
+
 wn.provide("erpnext.selling");
 // TODO commonify this code
 erpnext.selling.InstallationNote = wn.ui.form.Controller.extend({
@@ -11,11 +21,6 @@
 		if(!this.frm.doc.status) set_multiple(dt,dn,{ status:'Draft'});
 		if(this.frm.doc.__islocal) set_multiple(this.frm.doc.doctype, this.frm.doc.name, 
 				{inst_date: get_today()});
-				
-		fields = ['customer_address', 'contact_person','customer_name', 'address_display', 
-			'contact_display', 'contact_mobile', 'contact_email', 'territory', 'customer_group']
-		if(this.frm.doc.customer) unhide_field(fields);
-		else hide_field(fields)
 		
 		this.setup_queries();
 	},
@@ -60,42 +65,7 @@
 				}
 			);
 		}
-	},
-	
-	customer: function() {
-		var me = this;
-		if(this.frm.doc.customer) {
-			return this.frm.call({
-				doc: this.frm.doc,
-				method: "set_customer_defaults",
-			});
-			
-			// TODO shift this to depends_on
-			unhide_field(['customer_address', 'contact_person', 'customer_name',
-				'address_display', 'contact_display', 'contact_mobile', 'contact_email', 
-				'territory', 'customer_group']);
-		}
-	}, 
-	
-	customer_address: function() {
-		var me = this;
-		if(this.frm.doc.customer) {
-			return this.frm.call({
-				doc: this.frm.doc,
-				args: {
-					customer: this.frm.doc.customer, 
-					address: this.frm.doc.customer_address, 
-					contact: this.frm.doc.contact_person
-				},
-				method: "get_customer_address",
-				freeze: true,
-			});
-		}
-	},
-	
-	contact_person: function() {
-		this.customer_address();
-	},
+	},	
 });
 
 $.extend(cur_frm.cscript, new erpnext.selling.InstallationNote({frm: cur_frm}));
\ No newline at end of file
diff --git a/erpnext/selling/doctype/installation_note/installation_note.txt b/erpnext/selling/doctype/installation_note/installation_note.txt
index d23a68a..d8d8b6d 100644
--- a/erpnext/selling/doctype/installation_note/installation_note.txt
+++ b/erpnext/selling/doctype/installation_note/installation_note.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-04-30 13:13:06", 
   "docstatus": 0, 
-  "modified": "2013-12-20 19:24:08", 
+  "modified": "2014-01-02 10:25:49", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -74,6 +74,7 @@
   "search_index": 1
  }, 
  {
+  "depends_on": "customer", 
   "doctype": "DocField", 
   "fieldname": "customer_address", 
   "fieldtype": "Link", 
@@ -82,6 +83,7 @@
   "print_hide": 1
  }, 
  {
+  "depends_on": "customer", 
   "doctype": "DocField", 
   "fieldname": "contact_person", 
   "fieldtype": "Link", 
@@ -104,6 +106,7 @@
   "doctype": "DocField", 
   "fieldname": "address_display", 
   "fieldtype": "Small Text", 
+  "hidden": 1, 
   "label": "Address", 
   "read_only": 1
  }, 
@@ -111,10 +114,12 @@
   "doctype": "DocField", 
   "fieldname": "contact_display", 
   "fieldtype": "Small Text", 
+  "hidden": 1, 
   "label": "Contact", 
   "read_only": 1
  }, 
  {
+  "depends_on": "customer", 
   "doctype": "DocField", 
   "fieldname": "contact_mobile", 
   "fieldtype": "Text", 
@@ -122,6 +127,7 @@
   "read_only": 1
  }, 
  {
+  "depends_on": "customer", 
   "doctype": "DocField", 
   "fieldname": "contact_email", 
   "fieldtype": "Text", 
@@ -130,6 +136,7 @@
   "read_only": 1
  }, 
  {
+  "depends_on": "customer", 
   "description": "<a href=\"#Sales Browser/Territory\">Add / Edit</a>", 
   "doctype": "DocField", 
   "fieldname": "territory", 
@@ -142,6 +149,7 @@
   "search_index": 1
  }, 
  {
+  "depends_on": "customer", 
   "description": "<a href=\"#Sales Browser/Customer Group\">Add / Edit</a>", 
   "doctype": "DocField", 
   "fieldname": "customer_group", 
diff --git a/erpnext/selling/doctype/opportunity/opportunity.js b/erpnext/selling/doctype/opportunity/opportunity.js
index 396def8..fd449fa 100644
--- a/erpnext/selling/doctype/opportunity/opportunity.js
+++ b/erpnext/selling/doctype/opportunity/opportunity.js
@@ -3,6 +3,12 @@
 
 {% include 'utilities/doctype/sms_control/sms_control.js' %};
 
+wn.ui.form.on_change("Opportunity", "customer", function(frm) { 
+	erpnext.utils.get_customer_details(frm) });
+wn.ui.form.on_change("Opportunity", "customer_address", erpnext.utils.get_address_display);
+wn.ui.form.on_change("Opportunity", "contact_person", erpnext.utils.get_contact_details);	
+
+
 wn.provide("erpnext.selling");
 // TODO commonify this code
 erpnext.selling.Opportunity = wn.ui.form.Controller.extend({
@@ -12,8 +18,6 @@
 		if(!this.frm.doc.enquiry_from && this.frm.doc.lead)
 			this.frm.doc.enquiry_from = "Lead";
 
-		if(!this.frm.doc.enquiry_from) 
-			hide_field(['customer', 'customer_address', 'contact_person', 'customer_name','lead', 'address_display', 'contact_display', 'contact_mobile', 'contact_email', 'territory', 'customer_group']);
 		if(!this.frm.doc.status) 
 			set_multiple(cdt,cdn,{status:'Draft'});
 		if(!this.frm.doc.date) 
@@ -23,14 +27,6 @@
 		if(!this.frm.doc.fiscal_year && sys_defaults.fiscal_year) 
 			set_multiple(cdt,cdn,{fiscal_year:sys_defaults.fiscal_year});		
 	
-		if(this.frm.doc.enquiry_from) {
-			if(this.frm.doc.enquiry_from == 'Customer') {
-				hide_field('lead');
-			}
-			else if (this.frm.doc.enquiry_from == 'Lead') {
-				hide_field(['customer', 'customer_address', 'contact_person', 'customer_group']);
-			}
-		} 
 
 		if(!this.frm.doc.__islocal) {
 			cur_frm.communication_view = new wn.views.CommunicationList({
@@ -73,22 +69,7 @@
 				me.frm.set_query(opts[0], erpnext.queries[opts[1]]);
 			});
 	},
-	
-	customer: function() {
-		var me = this;
-		if(this.frm.doc.customer) {
-			// TODO shift this to depends_on
-			unhide_field(['customer_address', 'contact_person', 'customer_name',
-				'address_display', 'contact_display', 'contact_mobile', 'contact_email', 
-				'territory', 'customer_group']);
-				
-			return this.frm.call({
-				doc: this.frm.doc,
-				method: "set_customer_defaults",
-			});
-		}
-	}, 
-	
+		
 	create_quotation: function() {
 		wn.model.open_mapped_doc({
 			method: "erpnext.selling.doctype.opportunity.opportunity.make_quotation",
@@ -109,10 +90,7 @@
 			cur_frm.add_custom_button(wn._('Opportunity Lost'), cur_frm.cscript['Declare Opportunity Lost']);
 		}
 		cur_frm.add_custom_button(wn._('Send SMS'), cur_frm.cscript.send_sms, "icon-mobile-phone");
-	}
-	
-	cur_frm.toggle_display("contact_info", doc.customer || doc.lead);
-	
+	}	
 }
 
 cur_frm.cscript.onload_post_render = function(doc, cdt, cdn) {
@@ -128,46 +106,13 @@
 	}
 }
 
-// hide - unhide fields on basis of enquiry_from lead or customer
-cur_frm.cscript.enquiry_from = function(doc,cdt,cdn){
-	cur_frm.cscript.lead_cust_show(doc,cdt,cdn);
-}
-
-// hide - unhide fields based on lead or customer
-cur_frm.cscript.lead_cust_show = function(doc,cdt,cdn){	
-	if(doc.enquiry_from == 'Lead'){
-		unhide_field(['lead']);
-		hide_field(['customer','customer_address','contact_person','customer_name','address_display','contact_display','contact_mobile','contact_email','territory','customer_group']);
-		doc.lead = doc.customer = doc.customer_address = doc.contact_person = doc.address_display = doc.contact_display = doc.contact_mobile = doc.contact_email = doc.territory = doc.customer_group = "";
-	}
-	else if(doc.enquiry_from == 'Customer'){		
-		unhide_field(['customer']);
-		hide_field(['lead', 'address_display', 'contact_display', 'contact_mobile', 
-			'contact_email', 'territory', 'customer_group']);		
-		doc.lead = doc.customer = doc.customer_address = doc.contact_person = doc.address_display = doc.contact_display = doc.contact_mobile = doc.contact_email = doc.territory = doc.customer_group = "";
-	}
-}
-
-cur_frm.cscript.customer_address = cur_frm.cscript.contact_person = function(doc, dt, dn) {
-	args = {
-		address: doc.customer_address, 
-		contact: doc.contact_person
-	}
-	if(doc.customer) args.update({customer: doc.customer});
-	
-	return get_server_fields('get_customer_address', JSON.stringify(args),'', doc, dt, dn, 1);
-}
 
 cur_frm.cscript.lead = function(doc, cdt, cdn) {
 	cur_frm.toggle_display("contact_info", doc.customer || doc.lead);
-	
 	wn.model.map_current_doc({
 		method: "erpnext.selling.doctype.lead.lead.make_opportunity",
 		source_name: cur_frm.doc.lead
 	})
-	
-	unhide_field(['customer_name', 'address_display','contact_mobile', 'customer_address', 
-		'contact_email', 'territory']);	
 }
 
 
diff --git a/erpnext/selling/doctype/opportunity/opportunity.txt b/erpnext/selling/doctype/opportunity/opportunity.txt
index d0dc0f5..b0fe782 100644
--- a/erpnext/selling/doctype/opportunity/opportunity.txt
+++ b/erpnext/selling/doctype/opportunity/opportunity.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-03-07 18:50:30", 
   "docstatus": 0, 
-  "modified": "2013-12-20 19:24:15", 
+  "modified": "2014-01-02 11:10:51", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -80,6 +80,7 @@
   "reqd": 1
  }, 
  {
+  "depends_on": "eval:doc.enquiry_from===\"Customer\"", 
   "doctype": "DocField", 
   "fieldname": "customer", 
   "fieldtype": "Link", 
@@ -96,6 +97,7 @@
   "search_index": 0
  }, 
  {
+  "depends_on": "eval:doc.enquiry_from===\"Lead\"", 
   "doctype": "DocField", 
   "fieldname": "lead", 
   "fieldtype": "Link", 
@@ -183,6 +185,7 @@
   "read_only": 0
  }, 
  {
+  "depends_on": "eval:doc.lead || doc.customer", 
   "doctype": "DocField", 
   "fieldname": "contact_info", 
   "fieldtype": "Section Break", 
@@ -191,6 +194,7 @@
   "read_only": 0
  }, 
  {
+  "depends_on": "eval:doc.customer || doc.lead", 
   "doctype": "DocField", 
   "fieldname": "customer_address", 
   "fieldtype": "Link", 
@@ -204,13 +208,14 @@
   "doctype": "DocField", 
   "fieldname": "address_display", 
   "fieldtype": "Small Text", 
-  "hidden": 0, 
+  "hidden": 1, 
   "label": "Address", 
   "oldfieldname": "address", 
   "oldfieldtype": "Small Text", 
   "read_only": 1
  }, 
  {
+  "depends_on": "customer", 
   "description": "<a href=\"#Sales Browser/Territory\">Add / Edit</a>", 
   "doctype": "DocField", 
   "fieldname": "territory", 
@@ -224,7 +229,7 @@
   "search_index": 1
  }, 
  {
-  "depends_on": "eval:doc.enquiry_from==\"Customer\"", 
+  "depends_on": "customer", 
   "description": "<a href=\"#Sales Browser/Customer Group\">Add / Edit</a>", 
   "doctype": "DocField", 
   "fieldname": "customer_group", 
@@ -247,6 +252,7 @@
   "read_only": 0
  }, 
  {
+  "depends_on": "customer", 
   "doctype": "DocField", 
   "fieldname": "customer_name", 
   "fieldtype": "Data", 
@@ -255,6 +261,7 @@
   "read_only": 1
  }, 
  {
+  "depends_on": "eval:doc.lead || doc.customer", 
   "doctype": "DocField", 
   "fieldname": "contact_person", 
   "fieldtype": "Link", 
@@ -265,6 +272,7 @@
   "read_only": 0
  }, 
  {
+  "depends_on": "customer", 
   "doctype": "DocField", 
   "fieldname": "contact_display", 
   "fieldtype": "Small Text", 
@@ -272,6 +280,7 @@
   "read_only": 1
  }, 
  {
+  "depends_on": "eval:doc.lead || doc.customer", 
   "doctype": "DocField", 
   "fieldname": "contact_email", 
   "fieldtype": "Text", 
@@ -279,6 +288,7 @@
   "read_only": 1
  }, 
  {
+  "depends_on": "eval:doc.lead || doc.customer", 
   "doctype": "DocField", 
   "fieldname": "contact_mobile", 
   "fieldtype": "Text", 
diff --git a/erpnext/selling/sales_common.js b/erpnext/selling/sales_common.js
index 1c8aed3..8ca076d 100644
--- a/erpnext/selling/sales_common.js
+++ b/erpnext/selling/sales_common.js
@@ -10,6 +10,7 @@
 
 wn.provide("erpnext.selling");
 wn.require("assets/erpnext/js/transaction.js");
+
 {% include "public/js/controllers/accounts.js" %}
 
 erpnext.selling.SellingController = erpnext.TransactionController.extend({
@@ -103,47 +104,15 @@
 	},
 	
 	customer: function() {
-		var me = this;
-		if(this.frm.doc.customer || this.frm.doc.debit_to) {
-			if(!this.frm.doc.company) {
-				this.frm.set_value("customer", null);
-				msgprint(wn._("Please specify Company"));
-			} else {
-				var selling_price_list = this.frm.doc.selling_price_list;
-				return this.frm.call({
-					doc: this.frm.doc,
-					method: "set_customer_defaults",
-					freeze: true,
-					callback: function(r) {
-						if(!r.exc) {
-							(me.frm.doc.selling_price_list !== selling_price_list) ? 
-								me.selling_price_list() :
-								me.price_list_currency();
-						}
-					}
-				});
-			}
-		}
+		erpnext.utils.get_customer_details(this.frm);
 	},
 	
 	customer_address: function() {
-		var me = this;
-		if(this.frm.doc.customer) {
-			return this.frm.call({
-				doc: this.frm.doc,
-				args: {
-					customer: this.frm.doc.customer, 
-					address: this.frm.doc.customer_address, 
-					contact: this.frm.doc.contact_person
-				},
-				method: "set_customer_address",
-				freeze: true,
-			});
-		}
+		erpnext.utils.get_address_display(this.frm, "customer_address");
 	},
 	
 	contact_person: function() {
-		this.customer_address();
+		erpnext.utils.get_contact_details(this.frm);
 	},
 	
 	barcode: function(doc, cdt, cdn) {
diff --git a/erpnext/support/doctype/customer_issue/customer_issue.js b/erpnext/support/doctype/customer_issue/customer_issue.js
index 0ff3f17..d4cdbc2 100644
--- a/erpnext/support/doctype/customer_issue/customer_issue.js
+++ b/erpnext/support/doctype/customer_issue/customer_issue.js
@@ -2,27 +2,21 @@
 // License: GNU General Public License v3. See license.txt
 
 wn.provide("erpnext.support");
-// TODO commonify this code
+
+wn.ui.form.on_change("Customer Issue", "customer", function(frm) { 
+	erpnext.utils.get_customer_details(frm) });
+wn.ui.form.on_change("Customer Issue", "customer_address", 
+	erpnext.utils.get_address_display);
+wn.ui.form.on_change("Customer Issue", "contact_person", 
+	erpnext.utils.get_contact_details);	
+
 erpnext.support.CustomerIssue = wn.ui.form.Controller.extend({
 	refresh: function() {
 		if((cur_frm.doc.status=='Open' || cur_frm.doc.status == 'Work In Progress')) {
 			cur_frm.add_custom_button(wn._('Make Maintenance Visit'), this.make_maintenance_visit)
 		}
 	}, 
-	
-	customer: function() {
-		var me = this;
-		if(this.frm.doc.customer) {
-			// TODO shift this to depends_on
-			unhide_field(['customer_address', 'contact_person']);
-			
-			return this.frm.call({
-				doc: this.frm.doc,
-				method: "set_customer_defaults",
-			});
-		}
-	}, 
-	
+		
 	make_maintenance_visit: function() {
 		wn.model.open_mapped_doc({
 			method: "erpnext.support.doctype.customer_issue.customer_issue.make_maintenance_visit",
@@ -35,16 +29,7 @@
 
 cur_frm.cscript.onload = function(doc,cdt,cdn){
 	if(!doc.status) 
-		set_multiple(dt,dn,{status:'Open'});	
-	if(doc.__islocal){		
-		hide_field(['customer_address','contact_person']);
-	} 
-}
-
-cur_frm.cscript.customer_address = cur_frm.cscript.contact_person = function(doc,dt,dn) {		
-	if(doc.customer) 
-		return get_server_fields('get_customer_address', 
-			JSON.stringify({customer: doc.customer, address: doc.customer_address, contact: doc.contact_person}),'', doc, dt, dn, 1);
+		set_multiple(dt,dn,{status:'Open'}); 
 }
 
 cur_frm.fields_dict['customer_address'].get_query = function(doc, cdt, cdn) {
@@ -82,6 +67,8 @@
 cur_frm.add_fetch('serial_no', 'customer', 'customer');
 cur_frm.add_fetch('serial_no', 'customer_name', 'customer_name');
 cur_frm.add_fetch('serial_no', 'delivery_address', 'customer_address');
+cur_frm.add_fetch('item_code', 'item_name', 'item_name');
+cur_frm.add_fetch('item_code', 'description', 'description');
 
 cur_frm.fields_dict['item_code'].get_query = function(doc, cdt, cdn) {
 	if(doc.serial_no) {
@@ -98,8 +85,6 @@
 	}
 }
 
-cur_frm.add_fetch('item_code', 'item_name', 'item_name');
-cur_frm.add_fetch('item_code', 'description', 'description');
 
 
 cur_frm.fields_dict.customer.get_query = function(doc,cdt,cdn) {
diff --git a/erpnext/support/doctype/customer_issue/customer_issue.txt b/erpnext/support/doctype/customer_issue/customer_issue.txt
index ae16c2c..cee27d5 100644
--- a/erpnext/support/doctype/customer_issue/customer_issue.txt
+++ b/erpnext/support/doctype/customer_issue/customer_issue.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-01-10 16:34:30", 
   "docstatus": 0, 
-  "modified": "2013-12-20 19:24:02", 
+  "modified": "2014-01-02 11:20:49", 
   "modified_by": "Administrator", 
   "owner": "harshada@webnotestech.com"
  }, 
@@ -274,6 +274,7 @@
   "width": "50%"
  }, 
  {
+  "depends_on": "customer", 
   "doctype": "DocField", 
   "fieldname": "customer_name", 
   "fieldtype": "Data", 
@@ -281,6 +282,7 @@
   "read_only": 1
  }, 
  {
+  "depends_on": "customer", 
   "description": "<a href=\"#Sales Browser/Customer Group\">Add / Edit</a>", 
   "doctype": "DocField", 
   "fieldname": "customer_group", 
@@ -291,6 +293,7 @@
   "reqd": 0
  }, 
  {
+  "depends_on": "customer", 
   "description": "<a href=\"#Sales Browser/Territory\">Add / Edit</a>", 
   "doctype": "DocField", 
   "fieldname": "territory", 
@@ -305,10 +308,27 @@
   "search_index": 1
  }, 
  {
+  "depends_on": "customer", 
   "doctype": "DocField", 
-  "fieldname": "address_display", 
+  "fieldname": "contact_display", 
   "fieldtype": "Small Text", 
-  "label": "Address", 
+  "label": "Contact", 
+  "read_only": 1
+ }, 
+ {
+  "depends_on": "customer", 
+  "doctype": "DocField", 
+  "fieldname": "contact_mobile", 
+  "fieldtype": "Data", 
+  "label": "Mobile No", 
+  "read_only": 1
+ }, 
+ {
+  "depends_on": "customer", 
+  "doctype": "DocField", 
+  "fieldname": "contact_email", 
+  "fieldtype": "Data", 
+  "label": "Contact Email", 
   "read_only": 1
  }, 
  {
@@ -318,27 +338,8 @@
   "width": "50%"
  }, 
  {
-  "doctype": "DocField", 
-  "fieldname": "contact_display", 
-  "fieldtype": "Small Text", 
-  "label": "Contact", 
-  "read_only": 1
- }, 
- {
-  "doctype": "DocField", 
-  "fieldname": "contact_mobile", 
-  "fieldtype": "Data", 
-  "label": "Mobile No", 
-  "read_only": 1
- }, 
- {
-  "doctype": "DocField", 
-  "fieldname": "contact_email", 
-  "fieldtype": "Data", 
-  "label": "Contact Email", 
-  "read_only": 1
- }, 
- {
+  "depends_on": "customer", 
+  "description": "If different than customer address", 
   "doctype": "DocField", 
   "fieldname": "service_address", 
   "fieldtype": "Small Text", 
@@ -347,6 +348,15 @@
   "oldfieldtype": "Small Text"
  }, 
  {
+  "depends_on": "customer", 
+  "doctype": "DocField", 
+  "fieldname": "address_display", 
+  "fieldtype": "Small Text", 
+  "hidden": 0, 
+  "label": "Address", 
+  "read_only": 1
+ }, 
+ {
   "doctype": "DocField", 
   "fieldname": "more_info", 
   "fieldtype": "Section Break", 
diff --git a/erpnext/support/doctype/maintenance_schedule/maintenance_schedule.js b/erpnext/support/doctype/maintenance_schedule/maintenance_schedule.js
index 75773e0..4cdf56f 100644
--- a/erpnext/support/doctype/maintenance_schedule/maintenance_schedule.js
+++ b/erpnext/support/doctype/maintenance_schedule/maintenance_schedule.js
@@ -2,6 +2,14 @@
 // License: GNU General Public License v3. See license.txt
 
 wn.provide("erpnext.support");
+
+wn.ui.form.on_change("Maintenance Schedule", "customer", function(frm) { 
+	erpnext.utils.get_customer_details(frm) });
+wn.ui.form.on_change("Maintenance Schedule", "customer_address", 
+	erpnext.utils.get_address_display);
+wn.ui.form.on_change("Maintenance Schedule", "contact_person", 
+	erpnext.utils.get_contact_details);	
+
 // TODO commonify this code
 erpnext.support.MaintenanceSchedule = wn.ui.form.Controller.extend({
 	refresh: function() {
@@ -28,15 +36,6 @@
 			})
 		}
 	},
-	customer: function() {
-		var me = this;
-		if(this.frm.doc.customer) {
-			return this.frm.call({
-				doc: this.frm.doc,
-				method: "set_customer_defaults",
-			});
-		}		
-	}, 
 });
 
 $.extend(cur_frm.cscript, new erpnext.support.MaintenanceSchedule({frm: cur_frm}));
@@ -46,14 +45,9 @@
   
   if(doc.__islocal){
     set_multiple(dt,dn,{transaction_date:get_today()});
-    hide_field(['customer_address','contact_person','customer_name','address_display','contact_display','contact_mobile','contact_email','territory','customer_group']);
   }   
 }
 
-cur_frm.cscript.customer_address = cur_frm.cscript.contact_person = function(doc,dt,dn) {    
-  if(doc.customer) return get_server_fields('get_customer_address', JSON.stringify({customer: doc.customer, address: doc.customer_address, contact: doc.contact_person}),'', doc, dt, dn, 1);
-}
-
 cur_frm.fields_dict['customer_address'].get_query = function(doc, cdt, cdn) {
   return{
     filters:{ 'customer': doc.customer}
diff --git a/erpnext/support/doctype/maintenance_schedule/maintenance_schedule.txt b/erpnext/support/doctype/maintenance_schedule/maintenance_schedule.txt
index ff97b95..6c3508a 100644
--- a/erpnext/support/doctype/maintenance_schedule/maintenance_schedule.txt
+++ b/erpnext/support/doctype/maintenance_schedule/maintenance_schedule.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-01-10 16:34:30", 
   "docstatus": 0, 
-  "modified": "2013-12-20 19:24:13", 
+  "modified": "2014-01-02 11:24:51", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -55,12 +55,6 @@
  }, 
  {
   "doctype": "DocField", 
-  "fieldname": "column_break0", 
-  "fieldtype": "Column Break", 
-  "oldfieldtype": "Column Break"
- }, 
- {
-  "doctype": "DocField", 
   "fieldname": "customer", 
   "fieldtype": "Link", 
   "in_filter": 1, 
@@ -75,84 +69,11 @@
  }, 
  {
   "doctype": "DocField", 
-  "fieldname": "customer_address", 
-  "fieldtype": "Link", 
-  "label": "Customer Address", 
-  "options": "Address", 
-  "print_hide": 1
- }, 
- {
-  "doctype": "DocField", 
-  "fieldname": "contact_person", 
-  "fieldtype": "Link", 
-  "label": "Contact Person", 
-  "options": "Contact", 
-  "print_hide": 1
- }, 
- {
-  "doctype": "DocField", 
-  "fieldname": "customer_name", 
-  "fieldtype": "Data", 
-  "in_filter": 1, 
-  "in_list_view": 1, 
-  "label": "Customer Name", 
-  "oldfieldname": "customer_name", 
-  "oldfieldtype": "Data", 
-  "read_only": 1, 
-  "reqd": 0, 
-  "search_index": 0
- }, 
- {
-  "doctype": "DocField", 
-  "fieldname": "address_display", 
-  "fieldtype": "Small Text", 
-  "hidden": 1, 
-  "label": "Address", 
-  "read_only": 1
- }, 
- {
-  "doctype": "DocField", 
-  "fieldname": "contact_display", 
-  "fieldtype": "Small Text", 
-  "hidden": 1, 
-  "label": "Contact", 
-  "read_only": 1
- }, 
- {
-  "doctype": "DocField", 
-  "fieldname": "contact_mobile", 
-  "fieldtype": "Data", 
-  "hidden": 1, 
-  "label": "Mobile No", 
-  "read_only": 1
- }, 
- {
-  "doctype": "DocField", 
-  "fieldname": "contact_email", 
-  "fieldtype": "Data", 
-  "hidden": 1, 
-  "label": "Contact Email", 
-  "print_hide": 1, 
-  "read_only": 1
- }, 
- {
-  "doctype": "DocField", 
-  "fieldname": "column_break1", 
+  "fieldname": "column_break0", 
   "fieldtype": "Column Break", 
   "oldfieldtype": "Column Break"
  }, 
  {
-  "doctype": "DocField", 
-  "fieldname": "transaction_date", 
-  "fieldtype": "Date", 
-  "in_filter": 1, 
-  "label": "Transaction Date", 
-  "oldfieldname": "transaction_date", 
-  "oldfieldtype": "Date", 
-  "reqd": 1, 
-  "search_index": 0
- }, 
- {
   "default": "Draft", 
   "doctype": "DocField", 
   "fieldname": "status", 
@@ -169,50 +90,16 @@
  }, 
  {
   "doctype": "DocField", 
-  "fieldname": "amended_from", 
-  "fieldtype": "Data", 
-  "ignore_restrictions": 1, 
-  "label": "Amended From", 
-  "no_copy": 1, 
-  "oldfieldname": "amended_from", 
-  "oldfieldtype": "Data", 
-  "read_only": 1
- }, 
- {
-  "doctype": "DocField", 
-  "fieldname": "company", 
-  "fieldtype": "Link", 
+  "fieldname": "transaction_date", 
+  "fieldtype": "Date", 
   "in_filter": 1, 
-  "label": "Company", 
-  "oldfieldname": "company", 
-  "oldfieldtype": "Link", 
-  "options": "Company", 
+  "label": "Transaction Date", 
+  "oldfieldname": "transaction_date", 
+  "oldfieldtype": "Date", 
   "reqd": 1, 
   "search_index": 0
  }, 
  {
-  "description": "<a href=\"#Sales Browser/Territory\">Add / Edit</a>", 
-  "doctype": "DocField", 
-  "fieldname": "territory", 
-  "fieldtype": "Link", 
-  "in_filter": 1, 
-  "label": "Territory", 
-  "oldfieldname": "territory", 
-  "oldfieldtype": "Link", 
-  "options": "Territory", 
-  "reqd": 1, 
-  "search_index": 0
- }, 
- {
-  "description": "<a href=\"#Sales Browser/Customer Group\">Add / Edit</a>", 
-  "doctype": "DocField", 
-  "fieldname": "customer_group", 
-  "fieldtype": "Link", 
-  "label": "Customer Group", 
-  "options": "Customer Group", 
-  "reqd": 1
- }, 
- {
   "doctype": "DocField", 
   "fieldname": "items", 
   "fieldtype": "Section Break", 
@@ -255,6 +142,120 @@
   "read_only": 1
  }, 
  {
+  "doctype": "DocField", 
+  "fieldname": "contact_info", 
+  "fieldtype": "Section Break", 
+  "label": "Contact Info"
+ }, 
+ {
+  "depends_on": "customer", 
+  "doctype": "DocField", 
+  "fieldname": "customer_name", 
+  "fieldtype": "Data", 
+  "in_filter": 1, 
+  "in_list_view": 1, 
+  "label": "Customer Name", 
+  "oldfieldname": "customer_name", 
+  "oldfieldtype": "Data", 
+  "read_only": 1, 
+  "reqd": 0, 
+  "search_index": 0
+ }, 
+ {
+  "depends_on": "customer", 
+  "doctype": "DocField", 
+  "fieldname": "contact_person", 
+  "fieldtype": "Link", 
+  "label": "Contact Person", 
+  "options": "Contact", 
+  "print_hide": 1
+ }, 
+ {
+  "depends_on": "customer", 
+  "doctype": "DocField", 
+  "fieldname": "contact_mobile", 
+  "fieldtype": "Data", 
+  "hidden": 1, 
+  "label": "Mobile No", 
+  "read_only": 1
+ }, 
+ {
+  "depends_on": "customer", 
+  "doctype": "DocField", 
+  "fieldname": "contact_email", 
+  "fieldtype": "Data", 
+  "hidden": 1, 
+  "label": "Contact Email", 
+  "print_hide": 1, 
+  "read_only": 1
+ }, 
+ {
+  "doctype": "DocField", 
+  "fieldname": "contact_display", 
+  "fieldtype": "Small Text", 
+  "hidden": 1, 
+  "label": "Contact", 
+  "read_only": 1
+ }, 
+ {
+  "doctype": "DocField", 
+  "fieldname": "column_break_17", 
+  "fieldtype": "Column Break"
+ }, 
+ {
+  "depends_on": "customer", 
+  "doctype": "DocField", 
+  "fieldname": "customer_address", 
+  "fieldtype": "Link", 
+  "label": "Customer Address", 
+  "options": "Address", 
+  "print_hide": 1
+ }, 
+ {
+  "doctype": "DocField", 
+  "fieldname": "address_display", 
+  "fieldtype": "Small Text", 
+  "hidden": 1, 
+  "label": "Address", 
+  "read_only": 1
+ }, 
+ {
+  "depends_on": "customer", 
+  "description": "<a href=\"#Sales Browser/Territory\">Add / Edit</a>", 
+  "doctype": "DocField", 
+  "fieldname": "territory", 
+  "fieldtype": "Link", 
+  "in_filter": 1, 
+  "label": "Territory", 
+  "oldfieldname": "territory", 
+  "oldfieldtype": "Link", 
+  "options": "Territory", 
+  "reqd": 1, 
+  "search_index": 0
+ }, 
+ {
+  "depends_on": "customer", 
+  "description": "<a href=\"#Sales Browser/Customer Group\">Add / Edit</a>", 
+  "doctype": "DocField", 
+  "fieldname": "customer_group", 
+  "fieldtype": "Link", 
+  "label": "Customer Group", 
+  "options": "Customer Group", 
+  "reqd": 1
+ }, 
+ {
+  "doctype": "DocField", 
+  "fieldname": "company", 
+  "fieldtype": "Link", 
+  "in_filter": 1, 
+  "label": "Company", 
+  "oldfieldname": "company", 
+  "oldfieldtype": "Link", 
+  "options": "Company", 
+  "reqd": 1, 
+  "search_index": 0
+ }, 
+ {
   "doctype": "DocPerm"
  }
 ]
\ No newline at end of file
diff --git a/erpnext/support/doctype/maintenance_visit/maintenance_visit.js b/erpnext/support/doctype/maintenance_visit/maintenance_visit.js
index f571b9a..5d1aa54 100644
--- a/erpnext/support/doctype/maintenance_visit/maintenance_visit.js
+++ b/erpnext/support/doctype/maintenance_visit/maintenance_visit.js
@@ -2,6 +2,14 @@
 // License: GNU General Public License v3. See license.txt
 
 wn.provide("erpnext.support");
+
+wn.ui.form.on_change("Maintenance Visit", "customer", function(frm) { 
+	erpnext.utils.get_customer_details(frm) });
+wn.ui.form.on_change("Maintenance Visit", "customer_address", 
+	erpnext.utils.get_address_display);
+wn.ui.form.on_change("Maintenance Visit", "contact_person", 
+	erpnext.utils.get_contact_details);	
+
 // TODO commonify this code
 erpnext.support.MaintenanceVisit = wn.ui.form.Controller.extend({
 	refresh: function() {
@@ -44,20 +52,7 @@
 					})
 				});
 		}
-		cur_frm.cscript.hide_contact_info();			
 	},
-	customer: function() {
-		var me = this;
-		if(this.frm.doc.customer) {
-			// TODO shift this to depends_on
-			cur_frm.cscript.hide_contact_info();
-			
-			return this.frm.call({
-				doc: this.frm.doc,
-				method: "set_customer_defaults",
-			});
-		}
-	}, 
 });
 
 $.extend(cur_frm.cscript, new erpnext.support.MaintenanceVisit({frm: cur_frm}));
@@ -65,15 +60,6 @@
 cur_frm.cscript.onload = function(doc, dt, dn) {
 	if(!doc.status) set_multiple(dt,dn,{status:'Draft'});
 	if(doc.__islocal) set_multiple(dt,dn,{mntc_date:get_today()});
-	cur_frm.cscript.hide_contact_info();			
-}
-
-cur_frm.cscript.hide_contact_info = function() {
-	cur_frm.toggle_display("contact_info_section", cur_frm.doc.customer ? true : false);
-}
-
-cur_frm.cscript.customer_address = cur_frm.cscript.contact_person = function(doc,dt,dn) {		
-	if(doc.customer) return get_server_fields('get_customer_address', JSON.stringify({customer: doc.customer, address: doc.customer_address, contact: doc.contact_person}),'', doc, dt, dn, 1);
 }
 
 cur_frm.fields_dict['customer_address'].get_query = function(doc, cdt, cdn) {
diff --git a/erpnext/support/doctype/maintenance_visit/maintenance_visit.txt b/erpnext/support/doctype/maintenance_visit/maintenance_visit.txt
index 2b2986e..314960a 100644
--- a/erpnext/support/doctype/maintenance_visit/maintenance_visit.txt
+++ b/erpnext/support/doctype/maintenance_visit/maintenance_visit.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-01-10 16:34:31", 
   "docstatus": 0, 
-  "modified": "2013-12-20 19:24:13", 
+  "modified": "2014-01-02 11:28:44", 
   "modified_by": "Administrator", 
   "owner": "ashwini@webnotestech.com"
  }, 
@@ -268,6 +268,7 @@
   "search_index": 0
  }, 
  {
+  "depends_on": "customer", 
   "doctype": "DocField", 
   "fieldname": "contact_info_section", 
   "fieldtype": "Section Break", 
diff --git a/erpnext/support/doctype/support_ticket/support_ticket.js b/erpnext/support/doctype/support_ticket/support_ticket.js
index b5224e7..a276e36 100644
--- a/erpnext/support/doctype/support_ticket/support_ticket.js
+++ b/erpnext/support/doctype/support_ticket/support_ticket.js
@@ -5,20 +5,8 @@
 	return{	query: "erpnext.controllers.queries.customer_query" } }
 
 wn.provide("erpnext.support");
-// TODO commonify this code
-erpnext.support.SupportTicket = wn.ui.form.Controller.extend({
-	customer: function() {
-		var me = this;
-		if(this.frm.doc.customer) {
-			return this.frm.call({
-				doc: this.frm.doc,
-				method: "set_customer_defaults",
-			});			
-		}
-	}
-});
 
-$.extend(cur_frm.cscript, new erpnext.support.SupportTicket({frm: cur_frm}));
+cur_frm.add_fetch("customer", "customer_name", "customer_name")
 
 $.extend(cur_frm.cscript, {
 	onload: function(doc, dt, dn) {
diff --git a/erpnext/utilities/doctype/address/address.py b/erpnext/utilities/doctype/address/address.py
index ad6e049..ea21c21 100644
--- a/erpnext/utilities/doctype/address/address.py
+++ b/erpnext/utilities/doctype/address/address.py
@@ -51,3 +51,26 @@
 				webnotes.conn.sql("""update `tabAddress` set `%s`=0 where `%s`=%s and name!=%s""" %
 					(is_address_type, fieldname, "%s", "%s"), (self.doc.fields[fieldname], self.doc.name))
 				break
+
+@webnotes.whitelist()
+def get_address_display(address_dict):
+	if not isinstance(address_dict, dict):
+		address_dict = webnotes.conn.get_value("Address", address_dict, "*", as_dict=True) or {}
+	
+	meta = webnotes.get_doctype("Address")
+	sequence = (("", "address_line1"), 
+		("\n", "address_line2"), 
+		("\n", "city"),
+		("\n", "state"), 
+		("\n" + meta.get_label("pincode") + ": ", "pincode"), 
+		("\n", "country"),
+		("\n" + meta.get_label("phone") + ": ", "phone"), 
+		("\n" + meta.get_label("fax") + ": ", "fax"))
+	
+	display = ""
+	for separator, fieldname in sequence:
+		if address_dict.get(fieldname):
+			display += separator + address_dict.get(fieldname)
+		
+	return display.strip()
+
diff --git a/erpnext/utilities/doctype/contact/contact.py b/erpnext/utilities/doctype/contact/contact.py
index 301d7fd..9dcb30d 100644
--- a/erpnext/utilities/doctype/contact/contact.py
+++ b/erpnext/utilities/doctype/contact/contact.py
@@ -5,9 +5,9 @@
 import webnotes
 from webnotes.utils import cstr, extract_email_id
 
-from erpnext.utilities.transaction_base import TransactionBase
+from erpnext.controllers.status_updater import StatusUpdater
 
-class DocType(TransactionBase):
+class DocType(StatusUpdater):
 	def __init__(self, doc, doclist=[]):
 		self.doc = doc
 		self.doclist = doclist
@@ -49,3 +49,20 @@
 	def on_trash(self):
 		webnotes.conn.sql("""update `tabSupport Ticket` set contact='' where contact=%s""",
 			self.doc.name)
+
+@webnotes.whitelist()
+def get_contact_details(contact):
+	contact = webnotes.doc("Contact", contact)
+	out = {
+		"contact_person": contact.get("name"),
+		"contact_display": " ".join(filter(None, 
+			[contact.get("first_name"), contact.get("last_name")])),
+		"contact_email": contact.get("email_id"),
+		"contact_mobile": contact.get("mobile_no"),
+		"contact_phone": contact.get("phone"),
+		"contact_designation": contact.get("designation"),
+		"contact_department": contact.get("department")
+	}
+	
+	return out
+	
\ No newline at end of file
diff --git a/erpnext/utilities/transaction_base.py b/erpnext/utilities/transaction_base.py
index eed4639..d58a453 100644
--- a/erpnext/utilities/transaction_base.py
+++ b/erpnext/utilities/transaction_base.py
@@ -9,16 +9,8 @@
 
 from erpnext.controllers.status_updater import StatusUpdater
 
-class TransactionBase(StatusUpdater):
-	def get_default_address_and_contact(self, party_field, party_name=None):
-		"""get a dict of default field values of address and contact for a given party type
-			party_type can be one of: customer, supplier"""
-		if not party_name:
-			party_name = self.doc.fields.get(party_field)
-		
-		return get_default_address_and_contact(party_field, party_name,
-			fetch_shipping_address=True if self.meta.get_field("shipping_address_name") else False)
-			
+
+class TransactionBase(StatusUpdater):			
 	def set_address_fields(self):
 		party_type, party_name = self.get_party_type_and_name()
 		
@@ -53,80 +45,14 @@
 					break
 
 		return self._party_type_and_name
-			
-	def get_customer_defaults(self):
-		if not self.doc.customer: return {}
-		
-		out = self.get_default_address_and_contact("customer")
-
-		customer = webnotes.doc("Customer", self.doc.customer)
-		for f in ['customer_name', 'customer_group', 'territory']:
-			out[f] = customer.fields.get(f)
-		
-		# fields prepended with default in Customer doctype
-		for f in ['sales_partner', 'commission_rate', 'currency', 'price_list']:
-			if customer.fields.get("default_" + f):
-				out[f] = customer.fields.get("default_" + f)
-			
-		return out
-				
-	def set_customer_defaults(self):
-		"""
-			For a customer:
-			1. Sets default address and contact
-			2. Sets values like Territory, Customer Group, etc.
-			3. Clears existing Sales Team and fetches the one mentioned in Customer
-		"""
-		customer_defaults = self.get_customer_defaults()
-					
-		customer_defaults["selling_price_list"] = customer_defaults.get("price_list") or \
-			webnotes.conn.get_value("Customer Group", self.doc.customer_group, "default_price_list") or \
-			self.doc.selling_price_list
-			
-		for fieldname, val in customer_defaults.items():
-			if self.meta.get_field(fieldname):
-				self.doc.fields[fieldname] = val
-			
-		if self.meta.get_field("sales_team") and self.doc.customer:
-			self.set_sales_team_for_customer()
-			
-	def set_sales_team_for_customer(self):
-		from webnotes.model import default_fields
-		
-		# clear table
-		self.doclist = self.doc.clear_table(self.doclist, "sales_team")
-
-		sales_team = webnotes.conn.sql("""select * from `tabSales Team`
-			where parenttype="Customer" and parent=%s""", self.doc.customer, as_dict=True)
-		for i, sales_person in enumerate(sales_team):
-			# remove default fields
-			for fieldname in default_fields:
-				if fieldname in sales_person:
-					del sales_person[fieldname]
-			
-			sales_person.update({
-				"doctype": "Sales Team",
-				"parentfield": "sales_team",
-				"idx": i+1
-			})
-			
-			# add child
-			self.doclist.append(sales_person)
-			
+						
 	def get_supplier_defaults(self):
-		out = self.get_default_address_and_contact("supplier")
-
-		supplier = webnotes.doc("Supplier", self.doc.supplier)
-		out["supplier_name"] = supplier.supplier_name
-		if supplier.default_currency:
-			out["currency"] = supplier.default_currency
-		if supplier.default_price_list:
-			out["buying_price_list"] = supplier.default_price_list
-		
-		return out
+		from erpnext.buying.doctype.supplier.supplier import get_supplier_details
+		return get_supplier_details(self.doc.supplier)
 		
 	def set_supplier_defaults(self):
-		for fieldname, val in self.get_supplier_defaults().items():
+		from erpnext.buying.doctype.supplier.supplier import get_supplier_details
+		for fieldname, val in get_supplier_details(self.doc.supplier).iteritems():
 			if self.meta.get_field(fieldname):
 				self.doc.fields[fieldname] = val
 				
@@ -143,21 +69,7 @@
 		
 	def set_lead_defaults(self):
 		self.doc.fields.update(self.get_lead_defaults())
-	
-	def get_customer_address(self, args):
-		args = json.loads(args)
-		ret = {
-			'customer_address' : args["address"],
-			'address_display' : get_address_display(args["address"]),
-		}
-		if args.get('contact'):
-			ret.update(map_party_contact_details(args['contact']))
-		
-		return ret
-		
-	def set_customer_address(self, args):
-		self.doc.fields.update(self.get_customer_address(args))
-		
+			
 	# TODO deprecate this - used only in sales_order.js
 	def get_shipping_address(self, name):
 		shipping_address = get_default_address("customer", name, is_shipping_address=True)
@@ -306,34 +218,6 @@
 				for field, condition in fields:
 					if prevdoc_values[field] is not None:
 						self.validate_value(field, condition, prevdoc_values[field], doc)
-
-def get_default_address_and_contact(party_field, party_name, fetch_shipping_address=False):
-	out = {}
-	
-	# get addresses
-	billing_address = get_default_address(party_field, party_name)
-	if billing_address:
-		out[party_field + "_address"] = billing_address
-		out["address_display"] = get_address_display(billing_address)
-	else:
-		out[party_field + "_address"] = out["address_display"] = None
-	
-	if fetch_shipping_address:
-		shipping_address = get_default_address(party_field, party_name, is_shipping_address=True)
-		if shipping_address:
-			out["shipping_address_name"] = shipping_address
-			out["shipping_address"] = get_address_display(shipping_address)
-		else:
-			out["shipping_address_name"] = out["shipping_address"] = None
-	
-	# get contact
-	if party_field == "lead":
-		out["customer_address"] = out.get("lead_address")
-		out.update(map_lead_contact_details(party_name))
-	else:
-		out.update(map_party_contact_details(None, party_field, party_name))
-	
-	return out
 	
 def get_default_address(party_field, party_name, is_shipping_address=False):
 	if is_shipping_address:
@@ -352,23 +236,7 @@
 		(party_name,))
 		
 	return contact[0][0] if contact else None
-	
-def get_address_display(address_dict):
-	if not isinstance(address_dict, dict):
-		address_dict = webnotes.conn.get_value("Address", address_dict, "*", as_dict=True) or {}
-	
-	meta = webnotes.get_doctype("Address")
-	sequence = (("", "address_line1"), ("\n", "address_line2"), ("\n", "city"),
-		("\n", "state"), ("\n" + meta.get_label("pincode") + ": ", "pincode"), ("\n", "country"),
-		("\n" + meta.get_label("phone") + ": ", "phone"), ("\n" + meta.get_label("fax") + ": ", "fax"))
-	
-	display = ""
-	for separator, fieldname in sequence:
-		if address_dict.get(fieldname):
-			display += separator + address_dict.get(fieldname)
 		
-	return display.strip()
-	
 def map_lead_contact_details(party_name):
 	out = {}
 	for fieldname in ["contact_display", "contact_email", "contact_mobile", "contact_phone"]: