From bddd5d9b0c3a761121d41c49457e4b1d4402094f Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Thu, 9 May 2013 12:45:18 +0530 Subject: [PATCH] [rename] [fix] merge should be passed to on_rename method of controller for further processing --- accounts/doctype/account/account.py | 2 +- accounts/doctype/cost_center/cost_center.py | 2 +- buying/doctype/supplier/supplier.py | 4 +- selling/doctype/customer/customer.py | 4 +- selling/doctype/customer/test_customer.py | 64 +++++++++++++++++++++ setup/doctype/company/company.py | 2 +- stock/doctype/item/item.py | 2 +- stock/doctype/serial_no/serial_no.py | 2 +- 8 files changed, 73 insertions(+), 9 deletions(-) diff --git a/accounts/doctype/account/account.py b/accounts/doctype/account/account.py index eb65604e02..bdc26e46ce 100644 --- a/accounts/doctype/account/account.py +++ b/accounts/doctype/account/account.py @@ -187,7 +187,7 @@ class DocType: 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 a7672452aa..bf0918879f 100644 --- a/accounts/doctype/cost_center/cost_center.py +++ b/accounts/doctype/cost_center/cost_center.py @@ -87,7 +87,7 @@ class DocType(DocTypeNestedSet): 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 0137504b30..d41b86c32c 100644 --- a/buying/doctype/supplier/supplier.py +++ b/buying/doctype/supplier/supplier.py @@ -168,7 +168,7 @@ class DocType(TransactionBase): 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 @@ class DocType(TransactionBase): 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 7e16341a4a..6f54ef9641 100644 --- a/selling/doctype/customer/customer.py +++ b/selling/doctype/customer/customer.py @@ -216,7 +216,7 @@ class DocType(TransactionBase): 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 @@ class DocType(TransactionBase): 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 551b03f0be..806585f1e1 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 @@ test_records = [ "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 78be5380b2..e383fb1bc8 100644 --- a/setup/doctype/company/company.py +++ b/setup/doctype/company/company.py @@ -287,7 +287,7 @@ class DocType: 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 fde532c96c..bc438a877a 100644 --- a/stock/doctype/item/item.py +++ b/stock/doctype/item/item.py @@ -272,7 +272,7 @@ class DocType(DocListController): 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 bbf55b35e3..e85a947899 100644 --- a/stock/doctype/serial_no/serial_no.py +++ b/stock/doctype/serial_no/serial_no.py @@ -117,7 +117,7 @@ class DocType(StockController): 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'"""):