Rename duplicate account

This commit is contained in:
Nabin Hait 2014-03-11 17:54:33 +05:30
parent fc37fd8b6e
commit 3dfe854108

View File

@ -3,32 +3,54 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import frappe, os, json import frappe, os, json
from frappe.utils import cstr
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
def create_accounts(self, company): def create_accounts(self, company):
chart = {}
with open(os.path.join(os.path.dirname(__file__), "charts", with open(os.path.join(os.path.dirname(__file__), "charts",
self.doc.source_file), "r") as f: self.doc.source_file), "r") as f:
chart = json.loads(f.read()) chart = json.loads(f.read())
def _import_accounts(children, parent): if chart:
for child in children: accounts = []
print child.get("name"), parent def _import_accounts(children, parent):
account = frappe.bean({ for child in children:
"doctype": "Account", account_name = child.get("name")
"account_name": child.get("name"), account_name_in_db = unidecode(account_name.strip().lower())
"company": company,
"parent_account": parent, if account_name_in_db in accounts:
"group_or_ledger": "Group" if child.get("children") else "Ledger", count = accounts.count(account_name_in_db)
"root_type": child.get("root_type"), account_name = account_name + " " + cstr(count)
"is_pl_account": "Yes" if child.get("root_type") in ["Expense", "Income"] \
else "No", account = frappe.bean({
"account_type": child.get("account_type") "doctype": "Account",
}).insert() "account_name": account_name,
"company": company,
"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
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
if child.get("children"):
_import_accounts(child.get("children"), account.doc.name)
_import_accounts(chart.get("root").get("children"), None)