From 131939a7421b182ab1e2d213e4aab3c75e6e0b37 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Tue, 26 Nov 2013 15:03:51 +0530 Subject: [PATCH] [fix] [minor] rename and merge for customer and supplier --- accounts/utils.py | 26 +++++++++++++++++++++++++- buying/doctype/supplier/supplier.py | 20 ++++++-------------- selling/doctype/customer/customer.py | 20 ++++++-------------- stock/doctype/warehouse/warehouse.py | 25 +++++-------------------- 4 files changed, 42 insertions(+), 49 deletions(-) diff --git a/accounts/utils.py b/accounts/utils.py index 3ca1a8a294..d4ff282498 100644 --- a/accounts/utils.py +++ b/accounts/utils.py @@ -348,4 +348,28 @@ def get_actual_expense(args): 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 f60d4dd191..b2873c5661 100644 --- a/buying/doctype/supplier/supplier.py +++ b/buying/doctype/supplier/supplier.py @@ -148,21 +148,13 @@ class DocType(TransactionBase): 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 7e4a73a1b1..d00926f43c 100644 --- a/selling/doctype/customer/customer.py +++ b/selling/doctype/customer/customer.py @@ -143,21 +143,13 @@ class DocType(TransactionBase): 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 4869d3939c..3559960d22 100644 --- a/stock/doctype/warehouse/warehouse.py +++ b/stock/doctype/warehouse/warehouse.py @@ -73,7 +73,8 @@ class DocType: 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 @@ class DocType: 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 @@ class DocType: 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