fix: Chart Of Accounts import button not visible

(cherry picked from commit 3529622a0d28e9cdd6e602b5450ca79d13cfa0ed)
This commit is contained in:
Deepesh Garg 2021-10-02 20:35:11 +05:30 committed by mergify-bot
parent 197e97d9d6
commit 9244fe563a
2 changed files with 74 additions and 47 deletions

View File

@ -12,11 +12,6 @@ frappe.ui.form.on('Chart of Accounts Importer', {
frm.set_df_property('import_file_section', 'hidden', frm.doc.company ? 0 : 1); frm.set_df_property('import_file_section', 'hidden', frm.doc.company ? 0 : 1);
frm.set_df_property('chart_preview', 'hidden', frm.set_df_property('chart_preview', 'hidden',
$(frm.fields_dict['chart_tree'].wrapper).html()!="" ? 0 : 1); $(frm.fields_dict['chart_tree'].wrapper).html()!="" ? 0 : 1);
// Show import button when file is successfully attached
if (frm.page && frm.page.show_import_button) {
create_import_button(frm);
}
}, },
download_template: function(frm) { download_template: function(frm) {
@ -78,7 +73,12 @@ frappe.ui.form.on('Chart of Accounts Importer', {
frm.page.set_indicator(""); frm.page.set_indicator("");
$(frm.fields_dict['chart_tree'].wrapper).empty(); // empty wrapper on removing file $(frm.fields_dict['chart_tree'].wrapper).empty(); // empty wrapper on removing file
} else { } else {
generate_tree_preview(frm); frappe.run_serially([
() => validate_coa(frm),
() => generate_tree_preview(frm),
() => create_import_button(frm),
() => frm.set_df_property('chart_preview', 'hidden', 0),
]);
} }
}, },
@ -104,24 +104,26 @@ frappe.ui.form.on('Chart of Accounts Importer', {
}); });
var create_import_button = function(frm) { var create_import_button = function(frm) {
frm.page.set_primary_action(__("Import"), function () { if (frm.page.show_import_button) {
frappe.call({ frm.page.set_primary_action(__("Import"), function () {
method: "erpnext.accounts.doctype.chart_of_accounts_importer.chart_of_accounts_importer.import_coa", return frappe.call({
args: { method: "erpnext.accounts.doctype.chart_of_accounts_importer.chart_of_accounts_importer.import_coa",
file_name: frm.doc.import_file, args: {
company: frm.doc.company file_name: frm.doc.import_file,
}, company: frm.doc.company
freeze: true, },
freeze_message: __("Creating Accounts..."), freeze: true,
callback: function(r) { freeze_message: __("Creating Accounts..."),
if(!r.exc) { callback: function(r) {
clearInterval(frm.page["interval"]); if(!r.exc) {
frm.page.set_indicator(__('Import Successful'), 'blue'); clearInterval(frm.page["interval"]);
create_reset_button(frm); frm.page.set_indicator(__('Import Successful'), 'blue');
create_reset_button(frm);
}
} }
} });
}); }).addClass('btn btn-primary');
}).addClass('btn btn-primary'); }
}; };
var create_reset_button = function(frm) { var create_reset_button = function(frm) {
@ -132,13 +134,35 @@ var create_reset_button = function(frm) {
}).addClass('btn btn-primary'); }).addClass('btn btn-primary');
}; };
var validate_coa = function(frm) {
if (frm.doc.import_file) {
let parent = __('All Accounts');
return frappe.call({
'method': 'erpnext.accounts.doctype.chart_of_accounts_importer.chart_of_accounts_importer.get_coa',
'args': {
file_name: frm.doc.import_file,
parent: parent,
doctype: 'Chart of Accounts Importer',
file_type: frm.doc.file_type,
for_validate: 1
},
callback: function(r) {
if (r.message['show_import_button']) {
frm.page['show_import_button'] = Boolean(r.message['show_import_button']);
}
}
})
}
};
var generate_tree_preview = function(frm) { var generate_tree_preview = function(frm) {
if (frm.doc.import_file) { if (frm.doc.import_file) {
let parent = __('All Accounts'); let parent = __('All Accounts');
$(frm.fields_dict['chart_tree'].wrapper).empty(); // empty wrapper to load new data $(frm.fields_dict['chart_tree'].wrapper).empty(); // empty wrapper to load new data
// generate tree structure based on the csv data // generate tree structure based on the csv data
new frappe.ui.Tree({ return new frappe.ui.Tree({
parent: $(frm.fields_dict['chart_tree'].wrapper), parent: $(frm.fields_dict['chart_tree'].wrapper),
label: parent, label: parent,
expandable: true, expandable: true,

View File

@ -64,6 +64,7 @@ def import_coa(file_name, company):
else: else:
data = generate_data_from_excel(file_doc, extension) data = generate_data_from_excel(file_doc, extension)
frappe.local.flags.ignore_root_company_validation = True
forest = build_forest(data) forest = build_forest(data)
create_charts(company, custom_chart=forest) create_charts(company, custom_chart=forest)
@ -128,7 +129,7 @@ def generate_data_from_excel(file_doc, extension, as_dict=False):
return data return data
@frappe.whitelist() @frappe.whitelist()
def get_coa(doctype, parent, is_root=False, file_name=None): def get_coa(doctype, parent, is_root=False, file_name=None, for_validate=0):
''' called by tree view (to fetch node's children) ''' ''' called by tree view (to fetch node's children) '''
file_doc, extension = get_file(file_name) file_doc, extension = get_file(file_name)
@ -140,14 +141,20 @@ def get_coa(doctype, parent, is_root=False, file_name=None):
data = generate_data_from_excel(file_doc, extension) data = generate_data_from_excel(file_doc, extension)
validate_columns(data) validate_columns(data)
validate_accounts(data) validate_accounts(file_doc, extension)
forest = build_forest(data)
accounts = build_tree_from_json("", chart_data=forest) # returns alist of dict in a tree render-able form
# filter out to show data for the selected node only if not for_validate:
accounts = [d for d in accounts if d['parent_account']==parent] forest = build_forest(data)
accounts = build_tree_from_json("", chart_data=forest) # returns a list of dict in a tree render-able form
return accounts # filter out to show data for the selected node only
accounts = [d for d in accounts if d['parent_account']==parent]
return accounts
else:
return {
'show_import_button': 1
}
def build_forest(data): def build_forest(data):
''' '''
@ -279,7 +286,7 @@ def get_template(template_type):
def get_sample_template(writer): def get_sample_template(writer):
template = [ template = [
["Application Of Funds(Assets)", "", "", "", 1, "", "Asset"], ["Application Of Funds(Assets)", "", "", "", 1, "", "Asset"],
["Sources Of Funds(Liabilities)", "", "", "", 1, "", "Liability"], # ["Sources Of Funds(Liabilities)", "", "", "", 1, "", "Liability"],
["Equity", "", "", "", 1, "", "Equity"], ["Equity", "", "", "", 1, "", "Equity"],
["Expenses", "", "", "", 1, "", "Expense"], ["Expenses", "", "", "", 1, "", "Expense"],
["Income", "", "", "", 1, "", "Income"], ["Income", "", "", "", 1, "", "Income"],
@ -304,10 +311,7 @@ def get_sample_template(writer):
@frappe.whitelist() @frappe.whitelist()
def validate_accounts(file_name): def validate_accounts(file_doc, extension):
file_doc, extension = get_file(file_name)
if extension == 'csv': if extension == 'csv':
accounts = generate_data_from_csv(file_doc, as_dict=True) accounts = generate_data_from_csv(file_doc, as_dict=True)
else: else:
@ -326,8 +330,6 @@ def validate_accounts(file_name):
validate_root(accounts_dict) validate_root(accounts_dict)
validate_account_types(accounts_dict)
return [True, len(accounts)] return [True, len(accounts)]
def validate_root(accounts): def validate_root(accounts):
@ -340,9 +342,19 @@ def validate_root(accounts):
elif account.get("root_type") not in get_root_types() and account.get("account_name"): elif account.get("root_type") not in get_root_types() and account.get("account_name"):
error_messages.append(_("Root Type for {0} must be one of the Asset, Liability, Income, Expense and Equity").format(account.get("account_name"))) error_messages.append(_("Root Type for {0} must be one of the Asset, Liability, Income, Expense and Equity").format(account.get("account_name")))
validate_missing_roots(roots)
if error_messages: if error_messages:
frappe.throw("<br>".join(error_messages)) frappe.throw("<br>".join(error_messages))
def validate_missing_roots(roots):
root_types_added = set(d.get('root_type') for d in roots)
missing = list(set(get_root_types()) - root_types_added)
if missing:
frappe.throw(_("Please add Root Account for - {0}").format(' , '.join(missing)))
def get_root_types(): def get_root_types():
return ('Asset', 'Liability', 'Expense', 'Income', 'Equity') return ('Asset', 'Liability', 'Expense', 'Income', 'Equity')
@ -368,15 +380,6 @@ def get_mandatory_account_types():
{'account_type': 'Stock', 'root_type': 'Asset'} {'account_type': 'Stock', 'root_type': 'Asset'}
] ]
def validate_account_types(accounts):
account_types_for_ledger = ["Cost of Goods Sold", "Depreciation", "Fixed Asset", "Payable", "Receivable", "Stock Adjustment"]
account_types = [accounts[d]["account_type"] for d in accounts if not cint(accounts[d]['is_group']) == 1]
missing = list(set(account_types_for_ledger) - set(account_types))
if missing:
frappe.throw(_("Please identify/create Account (Ledger) for type - {0}").format(' , '.join(missing)))
def unset_existing_data(company): def unset_existing_data(company):
linked = frappe.db.sql('''select fieldname from tabDocField linked = frappe.db.sql('''select fieldname from tabDocField
where fieldtype="Link" and options="Account" and parent="Company"''', as_dict=True) where fieldtype="Link" and options="Account" and parent="Company"''', as_dict=True)