[fix] [minor] rename and merge for customer and supplier
diff --git a/accounts/utils.py b/accounts/utils.py
index 3ca1a8a..d4ff282 100644
--- a/accounts/utils.py
+++ b/accounts/utils.py
@@ -348,4 +348,28 @@
 		from `tabGL Entry`
 		where account='%(account)s' and cost_center='%(cost_center)s' 
 		and fiscal_year='%(fiscal_year)s' and company='%(company)s' %(condition)s
-	""" % (args))[0][0]
\ No newline at end of file
+	""" % (args))[0][0]
+	
+def rename_account_for(dt, olddn, newdn, merge):
+	old_account = get_account_for(dt, olddn)
+	if old_account:
+		new_account = None
+		if not merge:
+			if old_account == olddn:
+				new_account = webnotes.rename_doc("Account", olddn, newdn)
+		else:
+			existing_new_account = get_account_for(dt, newdn)
+			new_account = webnotes.rename_doc("Account", old_account, 
+				existing_new_account or newdn, merge=True if existing_new_account else False)
+
+		if new_account:
+			webnotes.conn.set_value("Account", new_account, "master_name", newdn)
+			
+def get_account_for(account_for_doctype, account_for):
+	if account_for_doctype in ["Customer", "Supplier"]:
+		account_for_field = "master_type"
+	elif account_for_doctype == "Warehouse":
+		account_for_field = "account_type"
+		
+	return webnotes.conn.get_value("Account", {account_for_field: account_for_doctype, 
+		"master_name": account_for})
\ No newline at end of file
diff --git a/buying/doctype/supplier/supplier.py b/buying/doctype/supplier/supplier.py
index f60d4dd..b2873c5 100644
--- a/buying/doctype/supplier/supplier.py
+++ b/buying/doctype/supplier/supplier.py
@@ -148,21 +148,13 @@
 		self.delete_supplier_contact()
 		self.delete_supplier_account()
 		
-	def after_rename(self, old, new, merge=False):
-		#update supplier_name if not naming series
+	def before_rename(self, olddn, newdn, merge=False):
+		from accounts.utils import rename_account_for
+		rename_account_for("Supplier", olddn, newdn, merge)
+		
+	def after_rename(self, olddn, newdn, merge=False):
 		if webnotes.defaults.get_global_default('supp_master_name') == 'Supplier Name':
-			webnotes.conn.set(self.doc, "supplier_name", new)
-				
-		for account in webnotes.conn.sql("""select name, account_name from 
-			tabAccount where master_name=%s and master_type='Supplier'""", old, as_dict=1):
-			if account.account_name != new:
-				merge_account = True if merge and webnotes.conn.get_value("Account", 
-					{"master_name": new, "master_type": "Supplier"}) else False
-				webnotes.rename_doc("Account", account.name, new, merge=merge_account)
-
-		#update master_name in doctype account
-		webnotes.conn.sql("""update `tabAccount` set master_name = %s, 
-			master_type = 'Supplier' where master_name = %s""" , (new, old))
+			webnotes.conn.set(self.doc, "supplier_name", newdn)
 
 @webnotes.whitelist()
 def get_dashboard_info(supplier):
diff --git a/selling/doctype/customer/customer.py b/selling/doctype/customer/customer.py
index 7e4a73a..d00926f 100644
--- a/selling/doctype/customer/customer.py
+++ b/selling/doctype/customer/customer.py
@@ -143,21 +143,13 @@
 		if self.doc.lead_name:
 			webnotes.conn.sql("update `tabLead` set status='Interested' where name=%s",self.doc.lead_name)
 			
-	def after_rename(self, old, new, merge=False):
-		#update customer_name if not naming series
+	def before_rename(self, olddn, newdn, merge=False):
+		from accounts.utils import rename_account_for
+		rename_account_for("Customer", olddn, newdn, merge)
+			
+	def after_rename(self, olddn, newdn, merge=False):
 		if webnotes.defaults.get_global_default('cust_master_name') == 'Customer Name':
-			webnotes.conn.set(self.doc, "customer_name", new)
-		
-		for account in webnotes.conn.sql("""select name, account_name from 
-			tabAccount where master_name=%s and master_type='Customer'""", old, as_dict=1):
-			if account.account_name != new:
-				merge_account = True if merge and webnotes.conn.get_value("Account", 
-					{"master_name": new, "master_type": "Customer"}) else False
-				webnotes.rename_doc("Account", account.name, new, merge=merge_account)
-
-		#update master_name in account record
-		webnotes.conn.sql("""update `tabAccount` set master_name = %s, 
-			master_type = 'Customer' where master_name = %s""", (new, old))
+			webnotes.conn.set(self.doc, "customer_name", newdn)
 
 @webnotes.whitelist()
 def get_dashboard_info(customer):
diff --git a/stock/doctype/warehouse/warehouse.py b/stock/doctype/warehouse/warehouse.py
index 4869d39..3559960 100644
--- a/stock/doctype/warehouse/warehouse.py
+++ b/stock/doctype/warehouse/warehouse.py
@@ -73,7 +73,8 @@
 		if warehouse_account:
 			webnotes.delete_doc("Account", warehouse_account)
 				
-		if sql("""select name from `tabStock Ledger Entry` where warehouse = %s""", self.doc.name):
+		if webnotes.conn.sql("""select name from `tabStock Ledger Entry` 
+				where warehouse = %s""", self.doc.name):
 			msgprint("""Warehouse can not be deleted as stock ledger entry 
 				exists for this warehouse.""", raise_exception=1)
 			
@@ -88,7 +89,8 @@
 				
 			webnotes.conn.sql("delete from `tabBin` where warehouse=%s", olddn)
 			
-		self.rename_account(olddn, new_warehouse, merge)
+		from accounts.utils import rename_account_for
+		rename_account_for("Warehouse", olddn, new_warehouse, merge)
 
 		return new_warehouse
 
@@ -109,21 +111,4 @@
 			
 		webnotes.conn.set_default("allow_negative_stock", 
 			webnotes.conn.get_value("Stock Settings", None, "allow_negative_stock"))
-		webnotes.conn.auto_commit_on_many_writes = 0
-			
-	def rename_account(self, olddn, newdn, merge):
-		old_account = webnotes.conn.get_value("Account", 
-			{"account_type": "Warehouse", "master_name": olddn})
-		if old_account:
-			new_account = None
-			if not merge:
-				if old_account == olddn:
-					new_account = webnotes.rename_doc("Account", olddn, newdn)
-			else:
-				existing_new_account = webnotes.conn.get_value("Account", 
-					{"account_type": "Warehouse", "master_name": newdn})
-				new_account = webnotes.rename_doc("Account", old_account, 
-					existing_new_account or newdn, merge=True if existing_new_account else False)
-
-			if new_account:
-				webnotes.conn.set_value("Account", new_account, "master_name", newdn)
\ No newline at end of file
+		webnotes.conn.auto_commit_on_many_writes = 0
\ No newline at end of file