Merge branch 'master' into edge
diff --git a/accounts/doctype/account/account.py b/accounts/doctype/account/account.py
index eb65604..bdc26e4 100644
--- a/accounts/doctype/account/account.py
+++ b/accounts/doctype/account/account.py
@@ -187,7 +187,7 @@
 		sql("""delete from `tabGL Entry` where account = %s and 
 			ifnull(is_cancelled, 'No') = 'Yes'""", self.doc.name)
 
-	def on_rename(self, new, old):
+	def on_rename(self, new, old, merge=False):
 		company_abbr = webnotes.conn.get_value("Company", self.doc.company, "abbr")		
 		parts = new.split(" - ")	
 
diff --git a/accounts/doctype/cost_center/cost_center.py b/accounts/doctype/cost_center/cost_center.py
index a767245..bf09188 100644
--- a/accounts/doctype/cost_center/cost_center.py
+++ b/accounts/doctype/cost_center/cost_center.py
@@ -87,7 +87,7 @@
 		self.validate_mandatory()
 		self.validate_budget_details()
 		
-	def on_rename(self, new, old):
+	def on_rename(self, new, old, merge=False):
 		company_abbr = webnotes.conn.get_value("Company", self.doc.company_name, "abbr")		
 		parts = new.split(" - ")	
 
diff --git a/buying/doctype/supplier/supplier.py b/buying/doctype/supplier/supplier.py
index 0137504..d41b86c 100644
--- a/buying/doctype/supplier/supplier.py
+++ b/buying/doctype/supplier/supplier.py
@@ -168,7 +168,7 @@
 		self.delete_supplier_communication()
 		self.delete_supplier_account()
 		
-	def on_rename(self, new, old):
+	def on_rename(self, new, old, merge=False):
 		#update supplier_name if not naming series
 		if webnotes.defaults.get_global_default('supp_master_name') == 'Supplier Name':
 			update_fields = [
@@ -186,7 +186,7 @@
 		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:
-				webnotes.rename_doc("Account", account.name, new)
+				webnotes.rename_doc("Account", account.name, new, merge=merge)
 
 		#update master_name in doctype account
 		webnotes.conn.sql("""update `tabAccount` set master_name = %s, 
diff --git a/selling/doctype/customer/customer.py b/selling/doctype/customer/customer.py
index 7e16341..6f54ef9 100644
--- a/selling/doctype/customer/customer.py
+++ b/selling/doctype/customer/customer.py
@@ -216,7 +216,7 @@
 		if self.doc.lead_name:
 			sql("update `tabLead` set status='Interested' where name=%s",self.doc.lead_name)
 			
-	def on_rename(self, new, old):
+	def on_rename(self, new, old, merge=False):
 		#update customer_name if not naming series
 		if webnotes.defaults.get_global_default('cust_master_name') == 'Customer Name':
 			update_fields = [
@@ -244,7 +244,7 @@
 		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:
-				webnotes.rename_doc("Account", account.name, new)
+				webnotes.rename_doc("Account", account.name, new, merge=merge)
 
 		#update master_name in doctype account
 		webnotes.conn.sql("""update `tabAccount` set master_name = %s, 
diff --git a/selling/doctype/customer/test_customer.py b/selling/doctype/customer/test_customer.py
index 551b03f..806585f 100644
--- a/selling/doctype/customer/test_customer.py
+++ b/selling/doctype/customer/test_customer.py
@@ -1,4 +1,60 @@
+from __future__ import unicode_literals
 
+import webnotes
+import unittest
+
+class TestCustomer(unittest.TestCase):
+	def test_rename(self):
+		self.assertEqual(webnotes.conn.exists("Customer", "_Test Customer 1"), 
+			(("_Test Customer 1",),))
+			
+		webnotes.rename_doc("Customer", "_Test Customer 1", "_Test Customer 1 Renamed")
+
+		self.assertEqual(webnotes.conn.exists("Customer", "_Test Customer 1 Renamed"), 
+			(("_Test Customer 1 Renamed",),))
+		self.assertEqual(webnotes.conn.exists("Customer", "_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
+		webnotes.conn.sql("""delete from `tabSales Invoice` where customer='_Test Customer 1'""")
+		
+		# check if they exist
+		self.assertEqual(webnotes.conn.exists("Customer", "_Test Customer"), 
+			(("_Test Customer",),))
+		self.assertEqual(webnotes.conn.exists("Customer", "_Test Customer 1"), 
+			(("_Test Customer 1",),))
+		self.assertEqual(webnotes.conn.exists("Account", "_Test Customer - _TC"), 
+			(("_Test Customer - _TC",),))
+		self.assertEqual(webnotes.conn.exists("Account", "_Test Customer 1 - _TC"), 
+			(("_Test Customer 1 - _TC",),))
+			
+		# check if transactions exists
+		self.assertNotEquals(webnotes.conn.sql("""select count(*) from `tabSales Invoice` 
+			where customer='_Test Customer'""", )[0][0], 0)
+		self.assertNotEquals(webnotes.conn.sql("""select count(*) from `tabSales Invoice` 
+			where debit_to='_Test Customer - _TC'""", )[0][0], 0)
+		
+		webnotes.rename_doc("Customer", "_Test Customer", "_Test Customer 1", merge=True)
+		
+		# check that no transaction exists for old name
+		self.assertNotEquals(webnotes.conn.sql("""select count(*) from `tabSales Invoice` 
+			where customer='_Test Customer 1'""", )[0][0], 0)
+		self.assertNotEquals(webnotes.conn.sql("""select count(*) from `tabSales Invoice` 
+			where debit_to='_Test Customer 1 - _TC'""", )[0][0], 0)
+		
+		# check that transactions exist for new name
+		self.assertEquals(webnotes.conn.sql("""select count(*) from `tabSales Invoice` 
+			where customer='_Test Customer'""", )[0][0], 0)
+		self.assertEquals(webnotes.conn.sql("""select count(*) from `tabSales Invoice` 
+			where debit_to='_Test Customer - _TC'""", )[0][0], 0)
+			
+		# check that old name doesn't exist
+		self.assertEqual(webnotes.conn.exists("Customer", "_Test Customer"), ())
+		self.assertEqual(webnotes.conn.exists("Account", "_Test Customer - _TC"), ())
+			
 test_records = [
 	[{
 		"doctype": "Customer",
@@ -7,5 +63,13 @@
 		"customer_group": "_Test Customer Group",
 		"territory": "_Test Territory",
 		"company": "_Test Company"
+	}],
+	[{
+		"doctype": "Customer",
+		"customer_name": "_Test Customer 1",
+		"customer_type": "Individual",
+		"customer_group": "_Test Customer Group",
+		"territory": "_Test Territory",
+		"company": "_Test Company"
 	}]
 ]
\ No newline at end of file
diff --git a/setup/doctype/company/company.py b/setup/doctype/company/company.py
index 78be538..e383fb1 100644
--- a/setup/doctype/company/company.py
+++ b/setup/doctype/company/company.py
@@ -287,7 +287,7 @@
 			where doctype='Global Defaults' and field='default_company' 
 			and value=%s""", self.doc.name)
 			
-	def on_rename(self,newdn,olddn):
+	def on_rename(self,newdn,olddn, merge=False):
 		webnotes.conn.sql("""update `tabCompany` set company_name=%s
 			where name=%s""", (newdn, olddn))
 		
diff --git a/stock/doctype/item/item.py b/stock/doctype/item/item.py
index fde532c..bc438a8 100644
--- a/stock/doctype/item/item.py
+++ b/stock/doctype/item/item.py
@@ -272,7 +272,7 @@
 			from webnotes.webutils import clear_cache
 			clear_cache(self.doc.page_name)
 
-	def on_rename(self,newdn,olddn):
+	def on_rename(self,newdn,olddn, merge=False):
 		webnotes.conn.sql("update tabItem set item_code = %s where name = %s", (newdn, olddn))
 		if self.doc.page_name:
 			from webnotes.webutils import clear_cache
diff --git a/stock/doctype/serial_no/serial_no.py b/stock/doctype/serial_no/serial_no.py
index bbf55b3..e85a947 100644
--- a/stock/doctype/serial_no/serial_no.py
+++ b/stock/doctype/serial_no/serial_no.py
@@ -117,7 +117,7 @@
 		self.make_stock_ledger_entry(1)
 		self.make_gl_entries()
 	
-	def on_rename(self, new, old):
+	def on_rename(self, new, old, merge=False):
 		"""rename serial_no text fields"""
 		for dt in webnotes.conn.sql("""select parent from tabDocField 
 			where fieldname='serial_no' and fieldtype='Text'"""):