Minor changes in import-from-odoo
This commit is contained in:
parent
c68646c694
commit
712aa5809d
@ -10,11 +10,9 @@ import os, json
|
|||||||
import ast
|
import ast
|
||||||
from xml.etree import ElementTree as ET
|
from xml.etree import ElementTree as ET
|
||||||
from frappe.utils.csvutils import read_csv_content
|
from frappe.utils.csvutils import read_csv_content
|
||||||
from frappe.utils import cstr
|
|
||||||
import frappe
|
import frappe
|
||||||
|
|
||||||
|
path = "/Users/nabinhait/projects/odoo/addons"
|
||||||
path = "/Users/nabinhait/Documents/openerp/openerp/addons"
|
|
||||||
|
|
||||||
accounts = {}
|
accounts = {}
|
||||||
charts = {}
|
charts = {}
|
||||||
@ -23,13 +21,13 @@ all_account_types = []
|
|||||||
def go():
|
def go():
|
||||||
global accounts, charts
|
global accounts, charts
|
||||||
default_account_types = get_default_account_types()
|
default_account_types = get_default_account_types()
|
||||||
|
|
||||||
country_dirs = []
|
country_dirs = []
|
||||||
for basepath, folders, files in os.walk(path):
|
for basepath, folders, files in os.walk(path):
|
||||||
basename = os.path.basename(basepath)
|
basename = os.path.basename(basepath)
|
||||||
if basename.startswith("l10n_"):
|
if basename.startswith("l10n_"):
|
||||||
country_dirs.append(basename)
|
country_dirs.append(basename)
|
||||||
|
|
||||||
for country_dir in country_dirs:
|
for country_dir in country_dirs:
|
||||||
accounts, charts = {}, {}
|
accounts, charts = {}, {}
|
||||||
country_path = os.path.join(path, country_dir)
|
country_path = os.path.join(path, country_dir)
|
||||||
@ -40,10 +38,10 @@ def go():
|
|||||||
xml_roots = get_xml_roots(files_path)
|
xml_roots = get_xml_roots(files_path)
|
||||||
csv_content = get_csv_contents(files_path)
|
csv_content = get_csv_contents(files_path)
|
||||||
prefix = country_dir if csv_content else None
|
prefix = country_dir if csv_content else None
|
||||||
account_types = get_account_types(xml_roots.get("account.account.type", []),
|
account_types = get_account_types(xml_roots.get("account.account.type", []),
|
||||||
csv_content.get("account.account.type", []), prefix)
|
csv_content.get("account.account.type", []), prefix)
|
||||||
account_types.update(default_account_types)
|
account_types.update(default_account_types)
|
||||||
|
|
||||||
if xml_roots:
|
if xml_roots:
|
||||||
make_maps_for_xml(xml_roots, account_types, country_dir)
|
make_maps_for_xml(xml_roots, account_types, country_dir)
|
||||||
|
|
||||||
@ -51,14 +49,13 @@ def go():
|
|||||||
make_maps_for_csv(csv_content, account_types, country_dir)
|
make_maps_for_csv(csv_content, account_types, country_dir)
|
||||||
make_account_trees()
|
make_account_trees()
|
||||||
make_charts()
|
make_charts()
|
||||||
|
|
||||||
def get_default_account_types():
|
def get_default_account_types():
|
||||||
default_types_root = []
|
default_types_root = []
|
||||||
for file in ["data_account_type.xml"]:
|
default_types_root.append(ET.parse(os.path.join(path, "account", "data",
|
||||||
default_types_root.append(ET.parse(os.path.join(path, "account", "data",
|
|
||||||
"data_account_type.xml")).getroot())
|
"data_account_type.xml")).getroot())
|
||||||
return get_account_types(default_types_root, None, prefix="account")
|
return get_account_types(default_types_root, None, prefix="account")
|
||||||
|
|
||||||
def get_xml_roots(files_path):
|
def get_xml_roots(files_path):
|
||||||
xml_roots = frappe._dict()
|
xml_roots = frappe._dict()
|
||||||
for filepath in files_path:
|
for filepath in files_path:
|
||||||
@ -67,17 +64,17 @@ def get_xml_roots(files_path):
|
|||||||
tree = ET.parse(filepath)
|
tree = ET.parse(filepath)
|
||||||
root = tree.getroot()
|
root = tree.getroot()
|
||||||
for node in root[0].findall("record"):
|
for node in root[0].findall("record"):
|
||||||
if node.get("model") in ["account.account.template",
|
if node.get("model") in ["account.account.template",
|
||||||
"account.chart.template", "account.account.type"]:
|
"account.chart.template", "account.account.type"]:
|
||||||
xml_roots.setdefault(node.get("model"), []).append(root)
|
xml_roots.setdefault(node.get("model"), []).append(root)
|
||||||
break
|
break
|
||||||
return xml_roots
|
return xml_roots
|
||||||
|
|
||||||
def get_csv_contents(files_path):
|
def get_csv_contents(files_path):
|
||||||
csv_content = {}
|
csv_content = {}
|
||||||
for filepath in files_path:
|
for filepath in files_path:
|
||||||
fname = os.path.basename(filepath)
|
fname = os.path.basename(filepath)
|
||||||
for file_type in ["account.account.template", "account.account.type",
|
for file_type in ["account.account.template", "account.account.type",
|
||||||
"account.chart.template"]:
|
"account.chart.template"]:
|
||||||
if fname.startswith(file_type) and fname.endswith(".csv"):
|
if fname.startswith(file_type) and fname.endswith(".csv"):
|
||||||
with open(filepath, "r") as csvfile:
|
with open(filepath, "r") as csvfile:
|
||||||
@ -87,27 +84,27 @@ def get_csv_contents(files_path):
|
|||||||
except Exception, e:
|
except Exception, e:
|
||||||
continue
|
continue
|
||||||
return csv_content
|
return csv_content
|
||||||
|
|
||||||
def get_account_types(root_list, csv_content, prefix=None):
|
def get_account_types(root_list, csv_content, prefix=None):
|
||||||
types = {}
|
types = {}
|
||||||
account_type_map = {
|
account_type_map = {
|
||||||
'cash': 'Cash',
|
'cash': 'Cash',
|
||||||
'bank': 'Bank',
|
'bank': 'Bank',
|
||||||
'tr_cash': 'Cash',
|
'tr_cash': 'Cash',
|
||||||
'tr_bank': 'Bank',
|
'tr_bank': 'Bank',
|
||||||
'receivable': 'Receivable',
|
'receivable': 'Receivable',
|
||||||
'tr_receivable': 'Receivable',
|
'tr_receivable': 'Receivable',
|
||||||
'account rec': 'Receivable',
|
'account rec': 'Receivable',
|
||||||
'payable': 'Payable',
|
'payable': 'Payable',
|
||||||
'tr_payable': 'Payable',
|
'tr_payable': 'Payable',
|
||||||
'equity': 'Equity',
|
'equity': 'Equity',
|
||||||
'stocks': 'Stock',
|
'stocks': 'Stock',
|
||||||
'stock': 'Stock',
|
'stock': 'Stock',
|
||||||
'tax': 'Tax',
|
'tax': 'Tax',
|
||||||
'tr_tax': 'Tax',
|
'tr_tax': 'Tax',
|
||||||
'tax-out': 'Tax',
|
'tax-out': 'Tax',
|
||||||
'tax-in': 'Tax',
|
'tax-in': 'Tax',
|
||||||
'charges_personnel': 'Chargeable',
|
'charges_personnel': 'Chargeable',
|
||||||
'fixed asset': 'Fixed Asset',
|
'fixed asset': 'Fixed Asset',
|
||||||
'cogs': 'Cost of Goods Sold',
|
'cogs': 'Cost of Goods Sold',
|
||||||
|
|
||||||
@ -122,10 +119,10 @@ def get_account_types(root_list, csv_content, prefix=None):
|
|||||||
if field.get("name")=="code" and field.text.lower() != "none" \
|
if field.get("name")=="code" and field.text.lower() != "none" \
|
||||||
and account_type_map.get(field.text):
|
and account_type_map.get(field.text):
|
||||||
data["account_type"] = account_type_map[field.text]
|
data["account_type"] = account_type_map[field.text]
|
||||||
|
|
||||||
node_id = prefix + "." + node.get("id") if prefix else node.get("id")
|
node_id = prefix + "." + node.get("id") if prefix else node.get("id")
|
||||||
types[node_id] = data
|
types[node_id] = data
|
||||||
|
|
||||||
if csv_content and csv_content[0][0]=="id":
|
if csv_content and csv_content[0][0]=="id":
|
||||||
for row in csv_content[1:]:
|
for row in csv_content[1:]:
|
||||||
row_dict = dict(zip(csv_content[0], row))
|
row_dict = dict(zip(csv_content[0], row))
|
||||||
@ -138,7 +135,7 @@ def get_account_types(root_list, csv_content, prefix=None):
|
|||||||
node_id = prefix + "." + data.get("id") if prefix else data.get("id")
|
node_id = prefix + "." + data.get("id") if prefix else data.get("id")
|
||||||
types[node_id] = data
|
types[node_id] = data
|
||||||
return types
|
return types
|
||||||
|
|
||||||
def get_report_type(report_type):
|
def get_report_type(report_type):
|
||||||
report_type_map = {
|
report_type_map = {
|
||||||
"asset": "Balance Sheet",
|
"asset": "Balance Sheet",
|
||||||
@ -147,13 +144,13 @@ def get_report_type(report_type):
|
|||||||
"expense": "Profit and Loss",
|
"expense": "Profit and Loss",
|
||||||
"income": "Profit and Loss"
|
"income": "Profit and Loss"
|
||||||
}
|
}
|
||||||
|
|
||||||
for d in report_type_map:
|
for d in report_type_map:
|
||||||
if d in report_type.lower():
|
if d in report_type.lower():
|
||||||
return report_type_map[d]
|
return report_type_map[d]
|
||||||
|
|
||||||
def make_maps_for_xml(xml_roots, account_types, country_dir):
|
def make_maps_for_xml(xml_roots, account_types, country_dir):
|
||||||
"""make maps for `charts` and `accounts`"""
|
"""make maps for `charts` and `accounts`"""
|
||||||
for model, root_list in xml_roots.iteritems():
|
for model, root_list in xml_roots.iteritems():
|
||||||
for root in root_list:
|
for root in root_list:
|
||||||
for node in root[0].findall("record"):
|
for node in root[0].findall("record"):
|
||||||
@ -165,12 +162,12 @@ def make_maps_for_xml(xml_roots, account_types, country_dir):
|
|||||||
if field.get("name")=="parent_id":
|
if field.get("name")=="parent_id":
|
||||||
parent_id = field.get("ref") or field.get("eval")
|
parent_id = field.get("ref") or field.get("eval")
|
||||||
data["parent_id"] = parent_id
|
data["parent_id"] = parent_id
|
||||||
|
|
||||||
if field.get("name")=="user_type":
|
if field.get("name")=="user_type":
|
||||||
value = field.get("ref")
|
value = field.get("ref")
|
||||||
if account_types.get(value, {}).get("report_type"):
|
if account_types.get(value, {}).get("report_type"):
|
||||||
data["report_type"] = account_types[value]["report_type"]
|
data["report_type"] = account_types[value]["report_type"]
|
||||||
|
|
||||||
if account_types.get(value, {}).get("account_type"):
|
if account_types.get(value, {}).get("account_type"):
|
||||||
data["account_type"] = account_types[value]["account_type"]
|
data["account_type"] = account_types[value]["account_type"]
|
||||||
if data["account_type"] not in all_account_types:
|
if data["account_type"] not in all_account_types:
|
||||||
@ -178,7 +175,7 @@ def make_maps_for_xml(xml_roots, account_types, country_dir):
|
|||||||
|
|
||||||
data["children"] = []
|
data["children"] = []
|
||||||
accounts[node.get("id")] = data
|
accounts[node.get("id")] = data
|
||||||
|
|
||||||
if node.get("model")=="account.chart.template":
|
if node.get("model")=="account.chart.template":
|
||||||
data = {}
|
data = {}
|
||||||
for field in node.findall("field"):
|
for field in node.findall("field"):
|
||||||
@ -202,7 +199,7 @@ def make_account_trees():
|
|||||||
for id in accounts.keys():
|
for id in accounts.keys():
|
||||||
if "children" in accounts[id] and not accounts[id].get("children"):
|
if "children" in accounts[id] and not accounts[id].get("children"):
|
||||||
del accounts[id]["children"]
|
del accounts[id]["children"]
|
||||||
|
|
||||||
def make_maps_for_csv(csv_content, account_types, country_dir):
|
def make_maps_for_csv(csv_content, account_types, country_dir):
|
||||||
for content in csv_content.get("account.account.template", []):
|
for content in csv_content.get("account.account.template", []):
|
||||||
for row in content[1:]:
|
for row in content[1:]:
|
||||||
@ -215,17 +212,17 @@ def make_maps_for_csv(csv_content, account_types, country_dir):
|
|||||||
user_type = data.get("user_type/id") or data.get("user_type:id")
|
user_type = data.get("user_type/id") or data.get("user_type:id")
|
||||||
if account_types.get(user_type, {}).get("report_type"):
|
if account_types.get(user_type, {}).get("report_type"):
|
||||||
account["report_type"] = account_types[user_type]["report_type"]
|
account["report_type"] = account_types[user_type]["report_type"]
|
||||||
|
|
||||||
if account_types.get(user_type, {}).get("account_type"):
|
if account_types.get(user_type, {}).get("account_type"):
|
||||||
account["account_type"] = account_types[user_type]["account_type"]
|
account["account_type"] = account_types[user_type]["account_type"]
|
||||||
if account["account_type"] not in all_account_types:
|
if account["account_type"] not in all_account_types:
|
||||||
all_account_types.append(account["account_type"])
|
all_account_types.append(account["account_type"])
|
||||||
|
|
||||||
accounts[data.get("id")] = account
|
accounts[data.get("id")] = account
|
||||||
if not account.get("parent_id") and data.get("chart_template_id:id"):
|
if not account.get("parent_id") and data.get("chart_template_id:id"):
|
||||||
chart_id = data.get("chart_template_id:id")
|
chart_id = data.get("chart_template_id:id")
|
||||||
charts.setdefault(chart_id, {}).update({"account_root_id": data.get("id")})
|
charts.setdefault(chart_id, {}).update({"account_root_id": data.get("id")})
|
||||||
|
|
||||||
for content in csv_content.get("account.chart.template", []):
|
for content in csv_content.get("account.chart.template", []):
|
||||||
for row in content[1:]:
|
for row in content[1:]:
|
||||||
if row:
|
if row:
|
||||||
@ -236,28 +233,28 @@ def make_maps_for_csv(csv_content, account_types, country_dir):
|
|||||||
"name": data.get("name"),
|
"name": data.get("name"),
|
||||||
"id": country_dir
|
"id": country_dir
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
def make_charts():
|
def make_charts():
|
||||||
"""write chart files in app/setup/doctype/company/charts"""
|
"""write chart files in app/setup/doctype/company/charts"""
|
||||||
for chart_id in charts:
|
for chart_id in charts:
|
||||||
src = charts[chart_id]
|
src = charts[chart_id]
|
||||||
if not src.get("name") or not src.get("account_root_id"):
|
if not src.get("name") or not src.get("account_root_id"):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if not src["account_root_id"] in accounts:
|
if not src["account_root_id"] in accounts:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
filename = src["id"][5:] + "_" + chart_id
|
filename = src["id"][5:] + "_" + chart_id
|
||||||
|
|
||||||
print "building " + filename
|
print "building " + filename
|
||||||
chart = {}
|
chart = {}
|
||||||
chart["name"] = src["name"]
|
chart["name"] = src["name"]
|
||||||
chart["root"] = accounts[src["account_root_id"]]
|
chart["root"] = accounts[src["account_root_id"]]
|
||||||
|
|
||||||
with open(os.path.join("erpnext", "accounts", "doctype", "chart_of_accounts",
|
with open(os.path.join("erpnext", "accounts", "doctype", "chart_of_accounts",
|
||||||
"charts", filename + ".json"), "w") as chartfile:
|
"charts", filename + ".json"), "w") as chartfile:
|
||||||
chartfile.write(json.dumps(chart, indent=1, sort_keys=True))
|
chartfile.write(json.dumps(chart, indent=1, sort_keys=True))
|
||||||
|
|
||||||
if __name__=="__main__":
|
if __name__=="__main__":
|
||||||
go()
|
go()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user