Merge branch 'master' of github.com:webnotes/erpnext
diff --git a/accounts/doctype/c_form/c_form.js b/accounts/doctype/c_form/c_form.js
index d9e5c68..adb989d 100644
--- a/accounts/doctype/c_form/c_form.js
+++ b/accounts/doctype/c_form/c_form.js
@@ -26,4 +26,6 @@
 cur_frm.cscript.invoice_no = function(doc, cdt, cdn) {
 	var d = locals[cdt][cdn];
 	get_server_fields('get_invoice_details', d.invoice_no, 'invoice_details', doc, cdt, cdn, 1);
-}
\ No newline at end of file
+}
+
+cur_frm.fields_dict.customer.get_query = erpnext.utils.customer_query;
\ No newline at end of file
diff --git a/accounts/page/general_ledger/general_ledger.js b/accounts/page/general_ledger/general_ledger.js
index ecc8626..b6fa3a0 100644
--- a/accounts/page/general_ledger/general_ledger.js
+++ b/accounts/page/general_ledger/general_ledger.js
@@ -69,7 +69,8 @@
 			filter: function(val, item, opts) {
 				return item.company == val || val == opts.default_value;
 			}},
-		{fieldtype:"Select", label: "Account", link:"Account", default_value: "Select Account...",
+		{fieldtype:"Select", label: "Account", link:"Account", 
+			default_value: "Select Account...", chosen: true,
 			filter: function(val, item, opts, me) {
 				if(val == opts.default_value) {
 					return true;
@@ -104,23 +105,42 @@
 			var $filter = me.filter_inputs.account;
 			var company = $(this).val();
 			var default_company = me.filter_inputs.company.get(0).opts.default_value;
-			$filter.empty().add_options([$filter.get(0).opts.default_value].concat(
+			var default_account = $filter.get(0).opts.default_value;
+			var new_options = [default_account].concat(
 				$.map(wn.report_dump.data["Account"], function(ac) {
 					return (company===default_company || 
 						accounts_by_company[company].indexOf(ac.name)!=-1) ? 
 						ac.name : null;
-				})));
+				}));
+			var old_account = me.filter_inputs.account.val();
+
+			$filter.empty().add_options(new_options);
+			
+			if((old_account != default_account) && new_options.indexOf(old_account)!=-1) {
+				$filter.val(old_account);
+			}
+			
+			// chosen
+			// if(me.filter_inputs.company.get(0).opts.chosen) {
+			// 	$filter.trigger("liszt:updated");
+			// }
+			
 			me.filter_inputs.refresh.click();
 		});
 		
 		this.filter_inputs.account && this.filter_inputs.account.change(function() {
 			me.filter_inputs.refresh.click();
 		});
+		
 	},
 	init_filter_values: function() {
 		this._super();
 		this.filter_inputs.company.change();
 	},
+	apply_filters_from_route: function() {
+		this._super();
+		this.filter_inputs.company.change();
+	},
 	make_accounts_by_company: function() {
 		var accounts_by_company = {};
 		var me = this;
@@ -134,7 +154,7 @@
 		account = this.account_by_name[account];
 		item_account = this.account_by_name[item_account];
 		
-		return (item_account.lft >= account.lft && item_account.rgt <= account.rgt)			
+		return ((item_account.lft >= account.lft) && (item_account.rgt <= account.rgt));
 	},
 	prepare_data: function() {
 		// add Opening, Closing, Totals rows
diff --git a/accounts/page/voucher_import_tool/voucher_import_tool.py b/accounts/page/voucher_import_tool/voucher_import_tool.py
index 4fb033b..4a94ad6 100644
--- a/accounts/page/voucher_import_tool/voucher_import_tool.py
+++ b/accounts/page/voucher_import_tool/voucher_import_tool.py
@@ -90,8 +90,12 @@
 	
 	webnotes.conn.commit()
 	try:
+		jv = Document("Journal Voucher")
+		
 		webnotes.conn.begin()
 		for i in xrange(len(data)):
+			jv = Document("Journal Voucher")
+			
 			d = data[i][0]
 			if import_type == "Voucher Import: Two Accounts" and flt(d.get("amount")) == 0:
 				webnotes.message_log = ["Amount not specified"]
@@ -112,7 +116,6 @@
 				
 				d.company = common_values.company
 						
-				jv = Document("Journal Voucher")
 				map_fields(["voucher_type", "posting_date", "naming_series", 
 					"remarks:user_remark", "ref_number:cheque_no", "ref_date:cheque_date",
 					"is_opening", "due_date", "company"], d, jv.fields)
diff --git a/buying/doctype/purchase_common/purchase_common.js b/buying/doctype/purchase_common/purchase_common.js
index f295aa0..7553a49 100644
--- a/buying/doctype/purchase_common/purchase_common.js
+++ b/buying/doctype/purchase_common/purchase_common.js
@@ -668,4 +668,6 @@
 			});
 		refresh_field(cur_frm.cscript.fname);
 	}
-}
\ No newline at end of file
+}
+
+cur_frm.fields_dict.supplier.get_query = erpnext.utils.supplier_query;
\ No newline at end of file
diff --git a/buying/page/purchase_analytics/purchase_analytics.js b/buying/page/purchase_analytics/purchase_analytics.js
index ac0e41f..23db139 100644
--- a/buying/page/purchase_analytics/purchase_analytics.js
+++ b/buying/page/purchase_analytics/purchase_analytics.js
@@ -32,7 +32,7 @@
 			parent: $(wrapper).find('.layout-main'),
 			appframe: wrapper.appframe,
 			doctypes: ["Item", "Item Group", "Supplier", "Supplier Type", "Company",
-				"Purchase Invoice", "Purchase Invoice Item"],
+				"Purchase Invoice", "Purchase Invoice Item", "Fiscal Year"],
 			tree_grid: { show: true }
 		});
 		
diff --git a/home/page/attributions/attributions.html b/home/page/attributions/attributions.html
index 9952908..113b969 100644
--- a/home/page/attributions/attributions.html
+++ b/home/page/attributions/attributions.html
@@ -40,10 +40,14 @@
 			<li>Ace - code editor</li>
 			<li>Slick Grid - report grid</li>
 			<li>jQPlot - graphs</li>
-			<li><a href="http://taitems.github.com/jQuery.Gantt/">JQuery.Gantt</a> - Gantt Chart</li>
+			<li><a href="http://taitems.github.com/jQuery.Gantt/" target="_blank">
+				JQuery.Gantt</a> - Gantt Chart</li>
 			<li>JSON2 - JSON builder, parser</li>
 			<li>JSColor - color picker</li>
-			<li><a href="https://github.com/dcneiner/Downloadify">Downloadify</a> - Export CSV files from the browser</li>
+			<li><a href="https://github.com/dcneiner/Downloadify" target="_blank">
+				Downloadify</a> - Export CSV files from the browser</li>
+			<li><a href="https://github.com/harvesthq/chosen" target="_blank">
+				Chosen</a> - a searchable select dropdown</li>
 		</ul>
 	</ul>
 
diff --git a/home/page/latest_updates/latest_updates.js b/home/page/latest_updates/latest_updates.js
index 38ff40f..dbd9f8c 100644
--- a/home/page/latest_updates/latest_updates.js
+++ b/home/page/latest_updates/latest_updates.js
@@ -1,4 +1,22 @@
 erpnext.updates = [
+	["22nd November 2012", [
+		"Support Ticket: Compose a reply using Markdown",
+		"Supplier Link Field: Search by Supplier Name instead of ID",
+		"Supplier Link Field: Show only ID in auto-suggest \
+			if ID created using Supplier Name (as defined in Global Defaults)",
+	]],
+	["21st November 2012", [
+		"Tree Report: Added missing expand / collapse buttons.",
+		"List View: Do not show restricted records, as defined in Permission Manager.",
+		"Customer Link Field: Search by Customer Name instead of ID",
+		"Customer Link Field: Show only ID in auto-suggest \
+			if ID created using Customer Name (as defined in Global Defaults)",
+		"Letter Head: Fixed bug causing cursor position to reset in Content",
+	]],
+	["20th November 2012", [
+		"Auto-suggest: Show main label in bold",
+		"Data Import Tool: Fixed #Name error faced by MS Excel users in import template",
+	]],
 	["19th November 2012", [
 		"Sales Order: Bugfix - Shipping Address should be a Link field.",
 		"Link Fields: Search Profile, Employee and Lead using Full Names instead of ID.",
diff --git a/production/doctype/production_planning_tool/production_planning_tool.js b/production/doctype/production_planning_tool/production_planning_tool.js
index 11d6e44..9caf829 100644
--- a/production/doctype/production_planning_tool/production_planning_tool.js
+++ b/production/doctype/production_planning_tool/production_planning_tool.js
@@ -48,3 +48,8 @@
   var d = locals[this.doctype][this.docname];
   return 'SELECT DISTINCT `tabBOM`.`name` FROM `tabBOM` WHERE `tabBOM`.`item` = "' + d.item_code + '" AND `tabBOM`.`is_active` = "Yes" AND `tabBOM`.docstatus = 1 AND `tabBOM`.`name` like "%s" ORDER BY `tabBOM`.`name` LIMIT 50';
 }
+
+cur_frm.fields_dict.customer.get_query = erpnext.utils.customer_query;
+
+cur_frm.fields_dict.pp_so_details.grid.get_field("customer").get_query =
+	erpnext.utils.customer_query;
\ No newline at end of file
diff --git a/projects/doctype/project/project.js b/projects/doctype/project/project.js
index 5605441..7f847b9 100644
--- a/projects/doctype/project/project.js
+++ b/projects/doctype/project/project.js
@@ -27,4 +27,6 @@
 		cur_frm.gantt_area.empty();
 		erpnext.show_task_gantt(cur_frm.gantt_area, cur_frm.docname);		
 	}
-}
\ No newline at end of file
+}
+
+cur_frm.fields_dict.customer.get_query = erpnext.utils.customer_query;
\ No newline at end of file
diff --git a/projects/doctype/task/task.js b/projects/doctype/task/task.js
index 295c58e..18c1653 100644
--- a/projects/doctype/task/task.js
+++ b/projects/doctype/task/task.js
@@ -27,6 +27,8 @@
   if(doc.project) get_server_fields('get_project_details', '','', doc, cdt, cdn, 1);
 }
 
+
+// TODO: remove these? field doesn't exist, but custom field could exist
 cur_frm.fields_dict['customer'].get_query = function(doc,cdt,cdn){
   var cond='';
   if(doc.project) cond = 'ifnull(`tabProject`.customer, "") = `tabCustomer`.name AND ifnull(`tabProject`.name, "") = "'+doc.project+'" AND';
diff --git a/projects/doctype/task/task.py b/projects/doctype/task/task.py
index 423d343..175379f 100644
--- a/projects/doctype/task/task.py
+++ b/projects/doctype/task/task.py
@@ -41,7 +41,8 @@
 		if cust:
 			ret = {'customer': cust and cust[0][0] or '', 'customer_name': cust and cust[0][1] or ''}
 			return ret
-	
+
+	# TODO: Remove these? as the field customer doesn't exists
 	def get_customer_details(self):
 		cust = sql("select customer_name from `tabCustomer` where name=%s", self.doc.customer)
 		if cust:
diff --git a/projects/doctype/timesheet/timesheet.js b/projects/doctype/timesheet/timesheet.js
index 81f0420..dc973fe 100644
--- a/projects/doctype/timesheet/timesheet.js
+++ b/projects/doctype/timesheet/timesheet.js
@@ -44,4 +44,7 @@
   if(d.project_name) cond = 'ifnull(`tabTask`.project, "") = "'+d.project_name+'" AND';
   
   return repl('SELECT distinct `tabTask`.`subject` FROM `tabTask` WHERE %(cond)s `tabTask`.`subject` LIKE "%s" ORDER BY `tabTask`.`subject` ASC LIMIT 50', {cond:cond});
-}
\ No newline at end of file
+}
+
+cur_frm.fields_dict.timesheet_details.grid.get_field("customer_name").get_query = 
+	erpnext.utils.customer_query;
\ No newline at end of file
diff --git a/public/js/account_tree_grid.js b/public/js/account_tree_grid.js
index cf33ee4..10e1dac 100644
--- a/public/js/account_tree_grid.js
+++ b/public/js/account_tree_grid.js
@@ -105,7 +105,7 @@
 				}
 				
 				me.init_account(d);
-			});			
+			});
 		}
 		this.set_indent();
 		this.prepare_balances();
diff --git a/public/js/utils.js b/public/js/utils.js
index 6cd2c19..4398498 100644
--- a/public/js/utils.js
+++ b/public/js/utils.js
@@ -22,7 +22,11 @@
 		from `tabProfile` where ifnull(enabled, 0)=1 and docstatus < 2 and \
 		name not in ('Administrator', 'Guest') and (%(key)s like \"%s\" or \
 		concat_ws(' ', first_name, middle_name, last_name) like \"%%%s\") \
-		order by name asc limit 50";
+		order by \
+		case when name like \"%s%%\" then 0 else 1 end, \
+		case when concat_ws(' ', first_name, middle_name, last_name) like \"%s%%\" \
+			then 0 else 1 end, \
+		name asc limit 50";
 };
 
 // searches for active employees
@@ -30,7 +34,10 @@
 	return "select name, employee_name from `tabEmployee` \
 		where status = 'Active' and docstatus < 2 and \
 		(%(key)s like \"%s\" or employee_name like \"%%%s\") \
-		order by name asc limit 50";
+		order by \
+		case when name like \"%s%%\" then 0 else 1 end, \
+		case when employee_name like \"%s%%\" then 0 else 1 end, \
+		name limit 50";
 };
 
 // searches for leads which are not converted
@@ -38,5 +45,41 @@
 	return "select name, lead_name, company_name from `tabLead` \
 		where docstatus < 2 and ifnull(status, '') != 'Converted' and \
 		(%(key)s like \"%s\" or lead_name like \"%%%s\" or company_name like \"%%%s\") \
-		order by lead_name asc limit 50";
+		order by \
+		case when name like \"%s%%\" then 0 else 1 end, \
+		case when lead_name like \"%s%%\" then 0 else 1 end, \
+		case when company_name like \"%s%%\" then 0 else 1 end, \
+		lead_name asc limit 50";
 };
+
+// searches for customer
+erpnext.utils.customer_query = function() {
+	if(sys_defaults.cust_master_name == "Customer Name") {
+		var fields = ["name", "customer_group", "country", "territory"];
+	} else {
+		var fields = ["name", "customer_name", "customer_group", "country", "territory"];
+	}
+	
+	return "select " + fields.join(", ") + " from `tabCustomer` where docstatus < 2 and \
+		(%(key)s like \"%s\" or customer_name like \"%%%s\") \
+		order by \
+		case when name like \"%s%%\" then 0 else 1 end, \
+		case when customer_name like \"%s%%\" then 0 else 1 end, \
+		name, customer_name limit 50";
+};
+
+// searches for supplier
+erpnext.utils.supplier_query = function() {
+	if(sys_defaults.supp_master_name == "Supplier Name") {
+		var fields = ["name", "supplier_type"];
+	} else {
+		var fields = ["name", "supplier_name", "supplier_type"];
+	}
+	
+	return "select " + fields.join(", ") + " from `tabSupplier` where docstatus < 2 and \
+		(%(key)s like \"%s\" or supplier_name like \"%%%s\") \
+		order by \
+		case when name like \"%s%%\" then 0 else 1 end, \
+		case when supplier_name like \"%s%%\" then 0 else 1 end, \
+		name, supplier_name limit 50";
+};
\ No newline at end of file
diff --git a/selling/doctype/installation_note/installation_note.js b/selling/doctype/installation_note/installation_note.js
index 44f6ac8..f298eda 100644
--- a/selling/doctype/installation_note/installation_note.js
+++ b/selling/doctype/installation_note/installation_note.js
@@ -83,3 +83,4 @@
   return 'SELECT name,CONCAT(first_name," ",ifnull(last_name,"")) As FullName,department,designation FROM tabContact WHERE customer = "'+ doc.customer +'" AND docstatus != 2 AND name LIKE "%s" ORDER BY name ASC LIMIT 50';
 }
 
+cur_frm.fields_dict.customer.get_query = erpnext.utils.customer_query;
\ No newline at end of file
diff --git a/selling/doctype/lead/lead.js b/selling/doctype/lead/lead.js
index 78182f2..8c940bc 100644
--- a/selling/doctype/lead/lead.js
+++ b/selling/doctype/lead/lead.js
@@ -148,4 +148,6 @@
 //get query select Territory
 cur_frm.fields_dict['territory'].get_query = function(doc,cdt,cdn) {
 	return 'SELECT `tabTerritory`.`name`,`tabTerritory`.`parent_territory` FROM `tabTerritory` WHERE `tabTerritory`.`is_group` = "No" AND `tabTerritory`.`docstatus`!= 2 AND `tabTerritory`.%(key)s LIKE "%s" ORDER BY	`tabTerritory`.`name` ASC LIMIT 50';
-}
\ No newline at end of file
+}
+
+cur_frm.fields_dict.customer.get_query = erpnext.utils.customer_query;
\ No newline at end of file
diff --git a/selling/doctype/lead/lead.txt b/selling/doctype/lead/lead.txt
index b41fc51..666d702 100644
--- a/selling/doctype/lead/lead.txt
+++ b/selling/doctype/lead/lead.txt
@@ -3,9 +3,9 @@
 
 	# These values are common in all dictionaries
 	{
-		u'creation': '2012-08-08 10:41:31',
+		u'creation': '2012-11-19 12:06:56',
 		u'docstatus': 0,
-		u'modified': '2012-11-16 12:26:14',
+		u'modified': '2012-11-21 17:12:54',
 		u'modified_by': u'Administrator',
 		u'owner': u'Administrator'
 	},
@@ -40,12 +40,14 @@
 
 	# These values are common for all DocPerm
 	{
+		'amend': 0,
 		u'doctype': u'DocPerm',
 		u'name': u'__common__',
 		'parent': u'Lead',
 		'parentfield': u'permissions',
 		'parenttype': u'DocType',
-		'read': 1
+		'read': 1,
+		'submit': 0
 	},
 
 	# DocType, Lead
@@ -146,7 +148,7 @@
 	{
 		'colour': u'White:FFF',
 		'depends_on': u"eval:doc.source == 'Existing Customer'",
-		'description': u'Source of th',
+		'description': u'Source of the Lead',
 		u'doctype': u'DocField',
 		'fieldname': u'customer',
 		'fieldtype': u'Link',
@@ -626,77 +628,81 @@
 
 	# DocPerm
 	{
+		'cancel': 0,
+		'create': 0,
+		u'doctype': u'DocPerm',
+		'permlevel': 1,
+		'role': u'All',
+		'write': 0
+	},
+
+	# DocPerm
+	{
+		'cancel': 0,
 		'create': 1,
 		u'doctype': u'DocPerm',
 		'permlevel': 0,
-		'role': u'Guest',
+		'role': u'Sales User',
 		'write': 1
 	},
 
 	# DocPerm
 	{
-		'amend': 0,
-		'cancel': 0,
-		'create': 0,
-		u'doctype': u'DocPerm',
-		'permlevel': 1,
-		'role': u'Sales User',
-		'submit': 0,
-		'write': 0
-	},
-
-	# DocPerm
-	{
-		'amend': 0,
-		'cancel': 0,
-		'create': 0,
-		u'doctype': u'DocPerm',
-		'permlevel': 1,
-		'role': u'Sales Manager',
-		'submit': 0,
-		'write': 0
-	},
-
-	# DocPerm
-	{
-		'amend': 0,
 		'cancel': 1,
 		'create': 1,
 		u'doctype': u'DocPerm',
 		'permlevel': 0,
 		'role': u'Sales Manager',
-		'submit': 0,
 		'write': 1
 	},
 
 	# DocPerm
 	{
-		'amend': 0,
-		'cancel': 0,
-		'create': 1,
-		u'doctype': u'DocPerm',
-		'permlevel': 0,
-		'role': u'Sales User',
-		'submit': 0,
-		'write': 1
-	},
-
-	# DocPerm
-	{
-		u'doctype': u'DocPerm',
-		'permlevel': 1,
-		'role': u'All'
-	},
-
-	# DocPerm
-	{
-		'amend': 0,
 		'cancel': 0,
 		'create': 1,
 		u'doctype': u'DocPerm',
 		'permlevel': 0,
 		'role': u'System Manager',
-		'submit': 0,
+		'write': 1
+	},
+
+	# DocPerm
+	{
+		'cancel': 0,
+		'create': 0,
+		u'doctype': u'DocPerm',
+		'permlevel': 1,
+		'role': u'Sales Manager',
+		'write': 0
+	},
+
+	# DocPerm
+	{
+		'cancel': 0,
+		'create': 0,
+		u'doctype': u'DocPerm',
+		'permlevel': 1,
+		'role': u'Sales User',
+		'write': 0
+	},
+
+	# DocPerm
+	{
+		'cancel': 0,
+		'create': 1,
+		u'doctype': u'DocPerm',
+		'permlevel': 0,
+		'role': u'System Manager',
+		'write': 1
+	},
+
+	# DocPerm
+	{
+		'cancel': 0,
+		'create': 1,
+		u'doctype': u'DocPerm',
+		'permlevel': 0,
+		'role': u'Guest',
 		'write': 1
 	}
 ]
\ No newline at end of file
diff --git a/selling/doctype/opportunity/opportunity.js b/selling/doctype/opportunity/opportunity.js
index 4070f5e..a729092 100644
--- a/selling/doctype/opportunity/opportunity.js
+++ b/selling/doctype/opportunity/opportunity.js
@@ -210,4 +210,6 @@
 cur_frm.fields_dict['territory'].get_query = function(doc,cdt,cdn) {
 	return 'SELECT `tabTerritory`.`name`,`tabTerritory`.`parent_territory` FROM `tabTerritory` WHERE `tabTerritory`.`is_group` = "No" AND `tabTerritory`.`docstatus`!= 2 AND `tabTerritory`.%(key)s LIKE "%s"	ORDER BY	`tabTerritory`.`name` ASC LIMIT 50';}
 	
-cur_frm.fields_dict.lead.get_query = erpnext.utils.lead_query;
\ No newline at end of file
+cur_frm.fields_dict.lead.get_query = erpnext.utils.lead_query;
+
+cur_frm.fields_dict.customer.get_query = erpnext.utils.customer_query;
\ No newline at end of file
diff --git a/selling/doctype/sales_common/sales_common.js b/selling/doctype/sales_common/sales_common.js
index be6f3f1..65112b5 100644
--- a/selling/doctype/sales_common/sales_common.js
+++ b/selling/doctype/sales_common/sales_common.js
@@ -857,3 +857,5 @@
 		validated = false;
 	}
 }
+
+cur_frm.fields_dict.customer.get_query = erpnext.utils.customer_query;
\ No newline at end of file
diff --git a/selling/doctype/shipping_address/shipping_address.js b/selling/doctype/shipping_address/shipping_address.js
index 0ac173a..deea4ed 100755
--- a/selling/doctype/shipping_address/shipping_address.js
+++ b/selling/doctype/shipping_address/shipping_address.js
@@ -18,3 +18,5 @@
 // =====================================================================
 cur_frm.add_fetch('customer','customer_name','customer_name');
 cur_frm.add_fetch('customer','address','customer_address');
+
+cur_frm.fields_dict.customer.get_query = erpnext.utils.customer_query;
\ No newline at end of file
diff --git a/stock/doctype/bin/bin.py b/stock/doctype/bin/bin.py
index 2f78f9c..fdf9eeb 100644
--- a/stock/doctype/bin/bin.py
+++ b/stock/doctype/bin/bin.py
@@ -273,7 +273,7 @@
 		self.exc_list = []
 		for sle in sll:
 			# block if stock level goes negative on any date
-			if val_method != 'Moving Average' or flt(allow_negative_stock) == 0:
+			if (val_method != 'Moving Average') or (cint(allow_negative_stock) == 0):
 				if self.validate_negative_stock(cqty, sle):
 					cqty += sle['actual_qty']
 					continue
diff --git a/stock/doctype/delivery_note/delivery_note.js b/stock/doctype/delivery_note/delivery_note.js
index e1f8b37..4a3910a 100644
--- a/stock/doctype/delivery_note/delivery_note.js
+++ b/stock/doctype/delivery_note/delivery_note.js
@@ -332,4 +332,4 @@
 		doctype: 'Delivery Note'
 	}
 	cur_frm.cscript.notify(doc, args);
-}
+}
\ No newline at end of file
diff --git a/stock/doctype/item/item.js b/stock/doctype/item/item.js
index 332a3f0..828beab 100644
--- a/stock/doctype/item/item.js
+++ b/stock/doctype/item/item.js
@@ -141,4 +141,10 @@
 cur_frm.fields_dict['ref_rate_details'].grid.onrowadd = function(doc, cdt, cdn){
 	locals[cdt][cdn].ref_currency = sys_defaults.currency;
 	refresh_field('ref_currency',cdn,'ref_rate_details');
-}
\ No newline at end of file
+}
+
+cur_frm.fields_dict.item_customer_details.grid.get_field("customer_name").get_query = 
+	erpnext.utils.customer_query;
+	
+cur_frm.fields_dict.item_supplier_details.grid.get_field("supplier").get_query = 
+	erpnext.utils.supplier_query;
\ No newline at end of file
diff --git a/stock/doctype/serial_no/serial_no.js b/stock/doctype/serial_no/serial_no.js
index 901acd2..49e88e2 100644
--- a/stock/doctype/serial_no/serial_no.js
+++ b/stock/doctype/serial_no/serial_no.js
@@ -67,4 +67,8 @@
 	WHERE `tabItem`.`docstatus`!= 2 AND ifnull(`tabItem`.`has_serial_no`, "No") = "Yes" \
 	AND (ifnull(`tabItem`.`end_of_life`,"") = "" OR `tabItem`.`end_of_life` > NOW() OR `tabItem`.`end_of_life`="0000-00-00") \
 	AND `tabItem`.%(key)s LIKE "%s"  ORDER BY  `tabItem`.`name` ASC LIMIT 50';
-}
\ No newline at end of file
+}
+
+cur_frm.fields_dict.customer.get_query = erpnext.utils.customer_query;
+
+cur_frm.fields_dict.supplier.get_query = erpnext.utils.supplier_query;
\ No newline at end of file
diff --git a/stock/doctype/stock_entry/stock_entry.js b/stock/doctype/stock_entry/stock_entry.js
index b40a413..bf75826 100644
--- a/stock/doctype/stock_entry/stock_entry.js
+++ b/stock/doctype/stock_entry/stock_entry.js
@@ -230,3 +230,7 @@
     validated = false;
   }
 }
+
+cur_frm.fields_dict.customer.get_query = erpnext.utils.customer_query;
+
+cur_frm.fields_dict.supplier.get_query = erpnext.utils.supplier_query;
\ No newline at end of file
diff --git a/support/doctype/communication/communication.js b/support/doctype/communication/communication.js
index 76161ab..f56b7a1 100644
--- a/support/doctype/communication/communication.js
+++ b/support/doctype/communication/communication.js
@@ -136,4 +136,8 @@
 cur_frm.cscript.hide_dialog = function() {
 	if(cur_frm.communication_list)
 		cur_frm.communication_list.run();
-}
\ No newline at end of file
+}
+
+cur_frm.fields_dict.customer.get_query = erpnext.utils.customer_query;
+
+cur_frm.fields_dict.supplier.get_query = erpnext.utils.supplier_query;
\ No newline at end of file
diff --git a/support/doctype/customer_issue/customer_issue.js b/support/doctype/customer_issue/customer_issue.js
index 545ff56..81c10d4 100644
--- a/support/doctype/customer_issue/customer_issue.js
+++ b/support/doctype/customer_issue/customer_issue.js
@@ -144,3 +144,5 @@
 		WHERE `tabTerritory`.`is_group` = "No" AND `tabTerritory`.`docstatus`!= 2 \
 		AND `tabTerritory`.%(key)s LIKE "%s"	ORDER BY	`tabTerritory`.`name` ASC LIMIT 50';
 }
+
+cur_frm.fields_dict.customer.get_query = erpnext.utils.customer_query;
\ No newline at end of file
diff --git a/support/doctype/maintenance_schedule/maintenance_schedule.js b/support/doctype/maintenance_schedule/maintenance_schedule.js
index efa3eac..21d2e62 100644
--- a/support/doctype/maintenance_schedule/maintenance_schedule.js
+++ b/support/doctype/maintenance_schedule/maintenance_schedule.js
@@ -131,3 +131,5 @@
 cur_frm.fields_dict['territory'].get_query = function(doc,cdt,cdn) {
   return 'SELECT `tabTerritory`.`name`,`tabTerritory`.`parent_territory` FROM `tabTerritory` WHERE `tabTerritory`.`is_group` = "No" AND `tabTerritory`.`docstatus`!= 2 AND `tabTerritory`.%(key)s LIKE "%s"  ORDER BY  `tabTerritory`.`name` ASC LIMIT 50';
 }
+
+cur_frm.fields_dict.customer.get_query = erpnext.utils.customer_query;
\ No newline at end of file
diff --git a/support/doctype/maintenance_visit/maintenance_visit.js b/support/doctype/maintenance_visit/maintenance_visit.js
index 1e5e9c8..a53c090 100644
--- a/support/doctype/maintenance_visit/maintenance_visit.js
+++ b/support/doctype/maintenance_visit/maintenance_visit.js
@@ -114,3 +114,5 @@
 cur_frm.fields_dict['territory'].get_query = function(doc,cdt,cdn) {
   return 'SELECT `tabTerritory`.`name`,`tabTerritory`.`parent_territory` FROM `tabTerritory` WHERE `tabTerritory`.`is_group` = "No" AND `tabTerritory`.`docstatus`!= 2 AND `tabTerritory`.%(key)s LIKE "%s"  ORDER BY  `tabTerritory`.`name` ASC LIMIT 50';
 }
+
+cur_frm.fields_dict.customer.get_query = erpnext.utils.customer_query;
\ No newline at end of file
diff --git a/support/doctype/support_ticket/support_ticket.js b/support/doctype/support_ticket/support_ticket.js
index 50f49b0..6ab2c68 100644
--- a/support/doctype/support_ticket/support_ticket.js
+++ b/support/doctype/support_ticket/support_ticket.js
@@ -189,4 +189,6 @@
 	this.make();
 }
 
-cur_frm.fields_dict.allocated_to.get_query = erpnext.utils.profile_query;
\ No newline at end of file
+cur_frm.fields_dict.allocated_to.get_query = erpnext.utils.profile_query;
+
+cur_frm.fields_dict.customer.get_query = erpnext.utils.customer_query;
\ No newline at end of file
diff --git a/support/doctype/support_ticket/support_ticket.py b/support/doctype/support_ticket/support_ticket.py
index d04f473..3968618 100644
--- a/support/doctype/support_ticket/support_ticket.py
+++ b/support/doctype/support_ticket/support_ticket.py
@@ -36,16 +36,19 @@
 		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 + '\n\n[Please do not change the subject while responding.]'
+		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()
 
 		signature = webnotes.conn.get_value('Email Settings',None,'support_signature')
 		if signature:
-			response += '\n\n' + signature
+			response += '<p>' + signature + '</p>'
 
 		from webnotes.utils.email_lib import sendmail
 		
@@ -56,7 +59,7 @@
 			msg=response)
 
 		self.doc.new_response = None
-		webnotes.conn.set(self.doc,'status','Waiting for Customer')
+		webnotes.conn.set(self.doc, 'status', 'Waiting for Customer')
 		self.make_response_record(response)
 	
 	def last_response(self):
diff --git a/utilities/doctype/address/address.js b/utilities/doctype/address/address.js
index c4631f8..034806e 100644
--- a/utilities/doctype/address/address.js
+++ b/utilities/doctype/address/address.js
@@ -15,8 +15,8 @@
 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 cur_frm.cscript.onload = function(doc, cdt, cdn) {
-	if(doc.customer) cur_frm.add_fetch('customer', 'customer_name', 'customer_name');
-	if(doc.supplier) cur_frm.add_fetch('supplier', 'supplier_name', 'supplier_name');
+	cur_frm.add_fetch('customer', 'customer_name', 'customer_name');
+	cur_frm.add_fetch('supplier', 'supplier_name', 'supplier_name');
 	
 	var route = wn.get_route();
 	if(route[1]=='Supplier') {
@@ -41,4 +41,6 @@
 		cur_frm.address_list.run();
 }
 
+cur_frm.fields_dict.customer.get_query = erpnext.utils.customer_query;
 
+cur_frm.fields_dict.supplier.get_query = erpnext.utils.supplier_query;
\ No newline at end of file
diff --git a/utilities/doctype/contact/contact.js b/utilities/doctype/contact/contact.js
index 1b0894e..43bba1a 100644
--- a/utilities/doctype/contact/contact.js
+++ b/utilities/doctype/contact/contact.js
@@ -42,4 +42,6 @@
 		cur_frm.contact_list.run();
 }
 
+cur_frm.fields_dict.customer.get_query = erpnext.utils.customer_query;
 
+cur_frm.fields_dict.supplier.get_query = erpnext.utils.supplier_query;
\ No newline at end of file