From 1e44cc9faee8f5aee7fea78ba275fead6baff817 Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Thu, 26 Dec 2019 23:37:26 +0530 Subject: [PATCH] fix: Move importCOA file to accounting --- .../Account => accounting}/importCOA.js | 10 +-- models/doctype/account/importCOA.js | 74 ------------------- server/registerServerMethods.js | 2 +- 3 files changed, 4 insertions(+), 82 deletions(-) rename {models/doctype/Account => accounting}/importCOA.js (83%) delete mode 100644 models/doctype/account/importCOA.js diff --git a/models/doctype/Account/importCOA.js b/accounting/importCOA.js similarity index 83% rename from models/doctype/Account/importCOA.js rename to accounting/importCOA.js index 2b4284ed..04d10417 100644 --- a/models/doctype/Account/importCOA.js +++ b/accounting/importCOA.js @@ -1,8 +1,6 @@ const frappe = require('frappejs'); -const path = require('path'); -const fs = require('fs'); -const countries = require('../../../fixtures/countryInfo.json'); -const standardCOA = require('../../../fixtures/verified/standardCOA.json'); +const countries = require('../fixtures/countryInfo.json'); +const standardCOA = require('../fixtures/verified/standardCOA.json'); const accountFields = [ 'accountType', 'rootType', @@ -59,9 +57,7 @@ async function getCountryCOA() { const conCode = countries[doc.country].code; try { - const countryCoa = require('../../../fixtures/verified/' + - conCode + - '.json'); + const countryCoa = require('../fixtures/verified/' + conCode + '.json'); return countryCoa.tree; } catch (e) { return standardCOA; diff --git a/models/doctype/account/importCOA.js b/models/doctype/account/importCOA.js deleted file mode 100644 index 2b4284ed..00000000 --- a/models/doctype/account/importCOA.js +++ /dev/null @@ -1,74 +0,0 @@ -const frappe = require('frappejs'); -const path = require('path'); -const fs = require('fs'); -const countries = require('../../../fixtures/countryInfo.json'); -const standardCOA = require('../../../fixtures/verified/standardCOA.json'); -const accountFields = [ - 'accountType', - 'rootType', - 'isGroup', - 'account_type', - 'root_type', - 'is_group' -]; - -async function importAccounts(children, parent, rootType, rootAccount) { - for (let accountName in children) { - const child = children[accountName]; - - if (rootAccount) { - rootType = child.rootType || child.root_type; - } - - if (!accountFields.includes(accountName)) { - let isGroup = identifyIsGroup(child); - const doc = frappe.newDoc({ - doctype: 'Account', - name: accountName, - parentAccount: parent, - isGroup, - rootType, - balance: 0, - accountType: child.accountType || child.account_type - }); - - await doc.insert(); - - await importAccounts(child, accountName, rootType); - } - } -} - -function identifyIsGroup(child) { - if (child.isGroup || child.is_group) { - return child.isGroup || child.is_group; - } - - const keys = Object.keys(child); - const children = keys.filter(key => !accountFields.includes(key)); - - if (children.length) { - return 1; - } - - return 0; -} - -async function getCountryCOA() { - const doc = await frappe.getDoc('AccountingSettings'); - const conCode = countries[doc.country].code; - - try { - const countryCoa = require('../../../fixtures/verified/' + - conCode + - '.json'); - return countryCoa.tree; - } catch (e) { - return standardCOA; - } -} - -module.exports = async function importCharts() { - const chart = await getCountryCOA(); - await importAccounts(chart, '', '', true); -}; diff --git a/server/registerServerMethods.js b/server/registerServerMethods.js index 72d84dc2..79ce185c 100644 --- a/server/registerServerMethods.js +++ b/server/registerServerMethods.js @@ -7,7 +7,7 @@ module.exports = function registerServerMethods() { frappe.registerMethod({ method: 'import-coa', async handler() { - const importCOA = require('../models/doctype/Account/importCOA'); + const importCOA = require('../accounting/importCOA'); await importCOA(); } });