From eb1192771fbf5d0b3e41b6b6af8fe5ccc6f194e6 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Thu, 13 Mar 2014 16:09:07 +0530 Subject: [PATCH] set root type from parent or child --- erpnext/accounts/doctype/account/account.py | 14 ++++----- .../chart_of_accounts/chart_of_accounts.py | 29 ++++++++++++------- .../charts/import_from_openerp.py | 2 +- erpnext/setup/doctype/company/test_company.py | 4 +-- erpnext/stock/doctype/item/item.txt | 8 +++-- 5 files changed, 33 insertions(+), 24 deletions(-) diff --git a/erpnext/accounts/doctype/account/account.py b/erpnext/accounts/doctype/account/account.py index aec889cd93..7ba57ab270 100644 --- a/erpnext/accounts/doctype/account/account.py +++ b/erpnext/accounts/doctype/account/account.py @@ -48,17 +48,17 @@ class DocType: def validate_parent(self): """Fetch Parent Details and validation for account not to be created under ledger""" if self.doc.parent_account: - par = frappe.db.sql("""select name, group_or_ledger, is_pl_account - from tabAccount where name =%s""", self.doc.parent_account) + par = frappe.db.sql("""select name, group_or_ledger, root_type + from tabAccount where name =%s""", self.doc.parent_account, as_dict=1) if not par: throw(_("Parent account does not exists")) - elif par[0][0] == self.doc.name: + elif par[0]["name"] == self.doc.name: throw(_("You can not assign itself as parent account")) - elif par[0][1] != 'Group': + elif par[0]["group_or_ledger"] != 'Group': throw(_("Parent account can not be a ledger")) - - if not self.doc.is_pl_account: - self.doc.is_pl_account = par[0][2] + + if par[0]["root_type"]: + self.doc.root_type = par[0]["root_type"] def validate_duplicate_account(self): if self.doc.fields.get('__islocal') or not self.doc.name: diff --git a/erpnext/accounts/doctype/chart_of_accounts/chart_of_accounts.py b/erpnext/accounts/doctype/chart_of_accounts/chart_of_accounts.py index 819adb489f..050cc3fdca 100644 --- a/erpnext/accounts/doctype/chart_of_accounts/chart_of_accounts.py +++ b/erpnext/accounts/doctype/chart_of_accounts/chart_of_accounts.py @@ -10,6 +10,7 @@ from unidecode import unidecode class DocType: def __init__(self, d, dl): self.doc, self.doclist = d, dl + self.no_root_type = False def create_accounts(self, company): chart = {} @@ -35,22 +36,28 @@ class DocType: "parent_account": parent, "group_or_ledger": "Group" if child.get("children") else "Ledger", "root_type": child.get("root_type"), - "is_pl_account": "Yes" if child.get("root_type") in ["Expense", "Income"] \ - else "No", "account_type": child.get("account_type") }).insert() accounts.append(account_name_in_db) - # print account.doc.lft, account.doc.rgt, account.doc.root_type - + + # set root_type for all parents where blank + if not account.doc.root_type or account.doc.root_type == 'None': + self.no_root_type = True + elif self.no_root_type: + frappe.db.sql("""update tabAccount set root_type=%s + where lft<=%s and rgt>=%s and ifnull(root_type, '')=''""", + (account.doc.root_type, account.doc.lft, account.doc.rgt)) + if child.get("children"): _import_accounts(child.get("children"), account.doc.name) _import_accounts(chart.get("root").get("children"), None) - - # set root_type from parent or child if not set - # root_types = frappe.db.sql("""select lft, rgt, distinct root_type from tabAccount - # where ifnull(root_type, '') != '' order by lft desc""") - # print root_types - - \ No newline at end of file + + # set root_type for root accounts + for acc in frappe.db.sql("""select name, lft, rgt from `tabAccount` + where ifnull(parent_account, '')=''""", as_dict=1): + root_types = frappe.db.sql_list("""select distinct root_type from tabAccount + where lft>%s and rgt<%s""", (acc.lft, acc.rgt)) + if len(root_types) > 1: + frappe.db.set_value("Account", acc.name, "root_type", None) \ No newline at end of file diff --git a/erpnext/accounts/doctype/chart_of_accounts/charts/import_from_openerp.py b/erpnext/accounts/doctype/chart_of_accounts/charts/import_from_openerp.py index f8cecc8042..c37cdef9a4 100644 --- a/erpnext/accounts/doctype/chart_of_accounts/charts/import_from_openerp.py +++ b/erpnext/accounts/doctype/chart_of_accounts/charts/import_from_openerp.py @@ -220,7 +220,7 @@ def make_account_trees(): accounts[account["parent_id"]]["children"].append(account) del account["parent_id"] else: - print account.get("name") + print account.get("name") + " deleted, parent node not found" del accounts[id] # remove empty children diff --git a/erpnext/setup/doctype/company/test_company.py b/erpnext/setup/doctype/company/test_company.py index 1fd87af1bf..482ba1d684 100644 --- a/erpnext/setup/doctype/company/test_company.py +++ b/erpnext/setup/doctype/company/test_company.py @@ -10,8 +10,8 @@ class TestCompany(unittest.TestCase): def test_coa(self): for country, chart_name in frappe.db.sql("""select country, chart_name from `tabChart of Accounts` order by country""", as_list=1): - print country - # print "Chart Name: ", chart_name + print "Country: ", country + print "Chart Name: ", chart_name company_bean = frappe.bean({ "doctype": "Company", diff --git a/erpnext/stock/doctype/item/item.txt b/erpnext/stock/doctype/item/item.txt index 51abc22807..229fa9718b 100644 --- a/erpnext/stock/doctype/item/item.txt +++ b/erpnext/stock/doctype/item/item.txt @@ -2,7 +2,7 @@ { "creation": "2013-05-03 10:45:46", "docstatus": 0, - "modified": "2014-03-05 15:20:59", + "modified": "2014-03-13 15:54:09", "modified_by": "Administrator", "owner": "Administrator" }, @@ -31,7 +31,6 @@ }, { "cancel": 0, - "delete": 0, "doctype": "DocPerm", "email": 1, "name": "__common__", @@ -274,7 +273,7 @@ "doctype": "DocField", "fieldname": "is_asset_item", "fieldtype": "Select", - "label": "Is Asset Item", + "label": "Is Fixed Asset Item", "oldfieldname": "is_asset_item", "oldfieldtype": "Select", "options": "Yes\nNo", @@ -868,6 +867,7 @@ }, { "create": 1, + "delete": 1, "doctype": "DocPerm", "import": 1, "role": "Material Master Manager", @@ -876,6 +876,7 @@ { "amend": 0, "create": 0, + "delete": 0, "doctype": "DocPerm", "role": "Material Manager", "write": 0 @@ -883,6 +884,7 @@ { "amend": 0, "create": 0, + "delete": 0, "doctype": "DocPerm", "role": "Material User", "write": 0