set root type from parent or child

This commit is contained in:
Nabin Hait 2014-03-13 16:09:07 +05:30
parent e71cf6d6a1
commit eb1192771f
5 changed files with 33 additions and 24 deletions

View File

@ -48,17 +48,17 @@ class DocType:
def validate_parent(self): def validate_parent(self):
"""Fetch Parent Details and validation for account not to be created under ledger""" """Fetch Parent Details and validation for account not to be created under ledger"""
if self.doc.parent_account: if self.doc.parent_account:
par = frappe.db.sql("""select name, group_or_ledger, is_pl_account par = frappe.db.sql("""select name, group_or_ledger, root_type
from tabAccount where name =%s""", self.doc.parent_account) from tabAccount where name =%s""", self.doc.parent_account, as_dict=1)
if not par: if not par:
throw(_("Parent account does not exists")) 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")) 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")) throw(_("Parent account can not be a ledger"))
if not self.doc.is_pl_account: if par[0]["root_type"]:
self.doc.is_pl_account = par[0][2] self.doc.root_type = par[0]["root_type"]
def validate_duplicate_account(self): def validate_duplicate_account(self):
if self.doc.fields.get('__islocal') or not self.doc.name: if self.doc.fields.get('__islocal') or not self.doc.name:

View File

@ -10,6 +10,7 @@ from unidecode import unidecode
class DocType: class DocType:
def __init__(self, d, dl): def __init__(self, d, dl):
self.doc, self.doclist = d, dl self.doc, self.doclist = d, dl
self.no_root_type = False
def create_accounts(self, company): def create_accounts(self, company):
chart = {} chart = {}
@ -35,22 +36,28 @@ class DocType:
"parent_account": parent, "parent_account": parent,
"group_or_ledger": "Group" if child.get("children") else "Ledger", "group_or_ledger": "Group" if child.get("children") else "Ledger",
"root_type": child.get("root_type"), "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") "account_type": child.get("account_type")
}).insert() }).insert()
accounts.append(account_name_in_db) 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"): if child.get("children"):
_import_accounts(child.get("children"), account.doc.name) _import_accounts(child.get("children"), account.doc.name)
_import_accounts(chart.get("root").get("children"), None) _import_accounts(chart.get("root").get("children"), None)
# set root_type from parent or child if not set # set root_type for root accounts
# root_types = frappe.db.sql("""select lft, rgt, distinct root_type from tabAccount for acc in frappe.db.sql("""select name, lft, rgt from `tabAccount`
# where ifnull(root_type, '') != '' order by lft desc""") where ifnull(parent_account, '')=''""", as_dict=1):
# print root_types 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)

View File

@ -220,7 +220,7 @@ def make_account_trees():
accounts[account["parent_id"]]["children"].append(account) accounts[account["parent_id"]]["children"].append(account)
del account["parent_id"] del account["parent_id"]
else: else:
print account.get("name") print account.get("name") + " deleted, parent node not found"
del accounts[id] del accounts[id]
# remove empty children # remove empty children

View File

@ -10,8 +10,8 @@ class TestCompany(unittest.TestCase):
def test_coa(self): def test_coa(self):
for country, chart_name in frappe.db.sql("""select country, chart_name for country, chart_name in frappe.db.sql("""select country, chart_name
from `tabChart of Accounts` order by country""", as_list=1): from `tabChart of Accounts` order by country""", as_list=1):
print country print "Country: ", country
# print "Chart Name: ", chart_name print "Chart Name: ", chart_name
company_bean = frappe.bean({ company_bean = frappe.bean({
"doctype": "Company", "doctype": "Company",

View File

@ -2,7 +2,7 @@
{ {
"creation": "2013-05-03 10:45:46", "creation": "2013-05-03 10:45:46",
"docstatus": 0, "docstatus": 0,
"modified": "2014-03-05 15:20:59", "modified": "2014-03-13 15:54:09",
"modified_by": "Administrator", "modified_by": "Administrator",
"owner": "Administrator" "owner": "Administrator"
}, },
@ -31,7 +31,6 @@
}, },
{ {
"cancel": 0, "cancel": 0,
"delete": 0,
"doctype": "DocPerm", "doctype": "DocPerm",
"email": 1, "email": 1,
"name": "__common__", "name": "__common__",
@ -274,7 +273,7 @@
"doctype": "DocField", "doctype": "DocField",
"fieldname": "is_asset_item", "fieldname": "is_asset_item",
"fieldtype": "Select", "fieldtype": "Select",
"label": "Is Asset Item", "label": "Is Fixed Asset Item",
"oldfieldname": "is_asset_item", "oldfieldname": "is_asset_item",
"oldfieldtype": "Select", "oldfieldtype": "Select",
"options": "Yes\nNo", "options": "Yes\nNo",
@ -868,6 +867,7 @@
}, },
{ {
"create": 1, "create": 1,
"delete": 1,
"doctype": "DocPerm", "doctype": "DocPerm",
"import": 1, "import": 1,
"role": "Material Master Manager", "role": "Material Master Manager",
@ -876,6 +876,7 @@
{ {
"amend": 0, "amend": 0,
"create": 0, "create": 0,
"delete": 0,
"doctype": "DocPerm", "doctype": "DocPerm",
"role": "Material Manager", "role": "Material Manager",
"write": 0 "write": 0
@ -883,6 +884,7 @@
{ {
"amend": 0, "amend": 0,
"create": 0, "create": 0,
"delete": 0,
"doctype": "DocPerm", "doctype": "DocPerm",
"role": "Material User", "role": "Material User",
"write": 0 "write": 0