From 3a0f96bd009dfdf66809e00ec4fc1aba73f2e71a Mon Sep 17 00:00:00 2001 From: Saurabh Date: Thu, 23 Jun 2016 12:50:09 +0530 Subject: [PATCH] [patch] patch to move all warehouses under all warehouse group --- erpnext/accounts/doctype/account/account.py | 2 +- .../v7_0/create_warehouse_nestedset.py | 55 +++++++++++++++++-- erpnext/patches/v7_0/group_warehouses.py | 45 --------------- 3 files changed, 50 insertions(+), 52 deletions(-) delete mode 100644 erpnext/patches/v7_0/group_warehouses.py diff --git a/erpnext/accounts/doctype/account/account.py b/erpnext/accounts/doctype/account/account.py index f5000f44ae..918917c56a 100644 --- a/erpnext/accounts/doctype/account/account.py +++ b/erpnext/accounts/doctype/account/account.py @@ -182,7 +182,7 @@ class Account(Document): lft, rgt = frappe.db.get_value("Warehouse", warehouse, ["lft", "rgt"]) if frappe.db.sql_list("""select sle.name from `tabStock Ledger Entry` sle where exists (select wh.name from - tabWarehouse wh where lft >= %s and rgt <= %s and sle.warehouse = wh.warehouse)""", (lft, rgt)): + tabWarehouse wh where lft >= %s and rgt <= %s and sle.warehouse = wh.name)""", (lft, rgt)): throw(_("Stock entries exist against warehouse {0}, hence you cannot re-assign or modify Warehouse").format(warehouse)) def update_nsm_model(self): diff --git a/erpnext/patches/v7_0/create_warehouse_nestedset.py b/erpnext/patches/v7_0/create_warehouse_nestedset.py index 78cc3999a2..c313f31456 100644 --- a/erpnext/patches/v7_0/create_warehouse_nestedset.py +++ b/erpnext/patches/v7_0/create_warehouse_nestedset.py @@ -2,9 +2,52 @@ import frappe from frappe import _ def execute(): - for warehouse in frappe.db.sql_list("""select name from tabWarehouse - order by company asc, name asc"""): - warehouse = frappe.get_doc("Warehouse", warehouse) - warehouse.is_group = "No" - warehouse.parent_warehouse = "" - warehouse.save(ignore_permissions=True) \ No newline at end of file + frappe.reload_doc("stock", "doctype", "warehouse") + + for company in frappe.get_all("Company", fields=["name", "abbr"]): + if not frappe.db.get_value("Warehouse", "{0} - {1}".format(_("All Warehouses"), company.abbr)): + create_default_warehouse_group(company) + + for warehouse in frappe.get_all("Warehouse", filters={"company": company.name}, fields=["name", "create_account_under", + "parent_warehouse", "is_group"]): + set_parent_to_warehouses(warehouse, company) + set_parent_to_warehouse_acounts(warehouse, company) + frappe.db.commit() + +def set_parent_to_warehouses(warehouse, company): + warehouse = frappe.get_doc("Warehouse", warehouse.name) + warehouse.is_group = "Yes" if warehouse.is_group == "Yes" else "No" + + if not warehouse.parent_warehouse and warehouse.name != "{0} - {1}".format(_("All Warehouses"), company.abbr): + warehouse.parent_warehouse = "{0} - {1}".format(_("All Warehouses"), company.abbr) + + warehouse.save(ignore_permissions=True) + +def set_parent_to_warehouse_acounts(warehouse, company): + account = frappe.db.get_value("Account", {"warehouse": warehouse.name}) + stock_group = frappe.db.get_value("Account", {"account_type": "Stock", + "is_group": 1, "company": company.name}) + + if account and account != "{0} - {1}".format(_("All Warehouses"), company.abbr): + account = frappe.get_doc("Account", account) + + if warehouse.is_group == "Yes": + account.is_group = 1 + account.account_type = "" + + if warehouse.create_account_under == stock_group or not warehouse.create_account_under: + if not warehouse.parent_warehouse: + account.parent_account = "{0} - {1}".format(_("All Warehouses"), company.abbr) + else: + account.parent_account = frappe.db.get_value("Account", warehouse.parent_warehouse) + + account.save(ignore_permissions=True) + +def create_default_warehouse_group(company): + frappe.get_doc({ + "doctype": "Warehouse", + "warehouse_name": _("All Warehouses"), + "is_group": "Yes", + "company": company.name, + "parent_warehouse": "" + }).insert(ignore_permissions=True) \ No newline at end of file diff --git a/erpnext/patches/v7_0/group_warehouses.py b/erpnext/patches/v7_0/group_warehouses.py deleted file mode 100644 index 8987d58561..0000000000 --- a/erpnext/patches/v7_0/group_warehouses.py +++ /dev/null @@ -1,45 +0,0 @@ -import frappe -from frappe import _ - -def execute(): - frappe.reload_doc("stock", "doctype", "warehouse") - for company in frappe.get_all("Company", fields=["name", "abbr"]): - if not frappe.db.get_value("Warehouse", "{0} - {1}".format(_("All Warehouses"), company.abbr)): - create_default_warehouse_group(company) - - for warehouse in frappe.get_all("Warehouse", {"company": company}, ["name", "create_account_under", "parent_warehouse"]): - set_parent_to_warehouses(warehouse, company) - set_parent_to_warehouse_acounts(warehouse, company) - -def set_parent_to_warehouses(warehouse, company): - warehouse = frappe.get_doc("Warehouse", warehouse.name) - warehouse.is_group = "No" - - if not warehouse.parent_warehouse: - warehouse.parent_warehouse = "{0} - {1}".format(_("All Warehouses"), company.abbr) - - warehouse.save() - -def set_parent_to_warehouse_acounts(warehouse, company): - account = frappe.db.get_value("Account", {"warehouse": warehouse.name}) - stock_group = frappe.db.get_value("Account", {"account_type": "Stock", - "is_group": 1, "company": company.name}) - - if account: - account = frappe.get_doc("Account", account) - - if warehouse.create_account_under == stock_group or not warehouse.create_account_under: - if not warehouse.parent_warehouse: - account.parent_account = "{0} - {1}".format(_("All Warehouses"), company.abbr) - else: - account.parent_account = frappe.db.get_value("Account", {"warehouse": warehouse.parent_warehouse}) - account.save(ignore_permissions=True) - -def create_default_warehouse_group(company): - frappe.get_doc({ - "doctype": "Warehouse", - "warehouse_name": _("All Warehouses"), - "is_group": "Yes", - "company": company.name, - "parent_warehouse": "" - }).insert(ignore_permissions=True) \ No newline at end of file