From 3f69d7fcc5d9da75715ad4f792623e10e1ec92bc Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Wed, 1 Dec 2021 18:38:00 +0530 Subject: [PATCH] feat: allow regional model updates --- models/doctype/Party/Party.js | 18 +------ models/doctype/Party/RegionalChanges.js | 54 ++++++++++++------- ...{RegionalChanges.js => RegionalEntries.js} | 0 models/regionalModelUpdates.js | 19 +++++++ src/initialization.js | 27 +++++++++- src/pages/SetupWizard/setupCompany.js | 14 ++--- 6 files changed, 88 insertions(+), 44 deletions(-) rename models/doctype/Tax/{RegionalChanges.js => RegionalEntries.js} (100%) create mode 100644 models/regionalModelUpdates.js diff --git a/models/doctype/Party/Party.js b/models/doctype/Party/Party.js index 3a4daabf..f7c2c663 100644 --- a/models/doctype/Party/Party.js +++ b/models/doctype/Party/Party.js @@ -4,6 +4,7 @@ import { _ } from 'frappejs/utils'; export default { name: 'Party', label: 'Party', + regional: 1, keywordFields: ['name'], fields: [ { @@ -99,22 +100,7 @@ export default { } }, }, - { - fieldname: 'gstin', - label: 'GSTIN No.', - fieldtype: 'Data', - hidden: (form) => { - return form.gstType === 'Registered Regular' ? 0 : 1; - }, - }, ], - quickEditFields: [ - 'email', - 'phone', - 'address', - 'defaultAccount', - 'currency', - 'gstin', - ], + quickEditFields: ['email', 'phone', 'address', 'defaultAccount', 'currency'], }; diff --git a/models/doctype/Party/RegionalChanges.js b/models/doctype/Party/RegionalChanges.js index 9b9c5df1..6b11673e 100644 --- a/models/doctype/Party/RegionalChanges.js +++ b/models/doctype/Party/RegionalChanges.js @@ -1,21 +1,39 @@ -import party from './Party'; +import { cloneDeep } from 'lodash'; +import PartyOriginal from './Party'; -party.fields.splice(3, 0, { - //insert at 3rd position - fieldname: 'gstin', - label: 'GSTIN No.', - fieldtype: 'Data', - hidden: form => { - return form.gstType === 'Registered Regular' ? 0 : 1; +export default function getAugmentedParty({ country }) { + const Party = cloneDeep(PartyOriginal); + if (!country) { + return Party; } -}); -party.fields.splice(4, 0, { - fieldname: 'gstType', - label: 'GST Registration Type', - fieldtype: 'Select', - options: ['Unregistered', 'Registered Regular', 'Consumer'] -}); -party.fields.join(); -const newParty = party; -export default newParty; + if (country === 'India') { + Party.fields.splice( + 3, + 0, + { + fieldname: 'gstin', + label: 'GSTIN No.', + fieldtype: 'Data', + hidden: (form) => { + return form.gstType === 'Registered Regular' ? 0 : 1; + }, + }, + { + fieldname: 'gstType', + label: 'GST Registration Type', + fieldtype: 'Select', + options: ['Unregistered', 'Registered Regular', 'Consumer'], + } + ); + Party.quickEditFields.push('gstin'); + } else { + Party.fields.splice(3, 0, { + fieldname: 'taxId', + label: 'Tax ID', + fieldtype: 'Data', + }); + Party.quickEditFields.push('taxId'); + } + return Party; +} diff --git a/models/doctype/Tax/RegionalChanges.js b/models/doctype/Tax/RegionalEntries.js similarity index 100% rename from models/doctype/Tax/RegionalChanges.js rename to models/doctype/Tax/RegionalEntries.js diff --git a/models/regionalModelUpdates.js b/models/regionalModelUpdates.js new file mode 100644 index 00000000..f74cd749 --- /dev/null +++ b/models/regionalModelUpdates.js @@ -0,0 +1,19 @@ +import frappe from 'frappejs'; + +async function setAugmentedModel(model, regionalInfo) { + const getAugmentedModel = ( + await import('./doctype/' + model + '/RegionalChanges') + ).default; + frappe.models[model] = getAugmentedModel(regionalInfo); + frappe.models[model].augmented = 1; +} + +export default async function regionalModelUpdates(regionalInfo) { + for (let model in frappe.models) { + const { regional, basedOn, augmented } = frappe.models[model]; + if (!regional || basedOn || augmented) { + continue; + } + await setAugmentedModel(model, regionalInfo); + } +} diff --git a/src/initialization.js b/src/initialization.js index 9cb8769c..96845fc9 100644 --- a/src/initialization.js +++ b/src/initialization.js @@ -3,6 +3,8 @@ import { ipcRenderer } from 'electron'; import { _ } from 'frappejs'; import SQLiteDatabase from 'frappejs/backends/sqlite'; import fs from 'fs'; +import models from '../models'; +import regionalModelUpdates from '../models/regionalModelUpdates'; import postStart from '../server/postStart'; import { IPC_ACTIONS } from './messages'; import migrate from './migrate'; @@ -36,6 +38,18 @@ export async function createNewDatabase() { return filePath; } +async function runRegionalModelUpdates() { + if (!(await frappe.db.knex.schema.hasTable('SingleValue'))) { + return; + } + + const { country, setupComplete } = await frappe.db.getSingle( + 'AccountingSettings' + ); + if (!parseInt(setupComplete)) return; + await regionalModelUpdates({ country }); +} + export async function connectToLocalDatabase(filePath) { if (!filePath) { return false; @@ -52,6 +66,12 @@ export async function connectToLocalDatabase(filePath) { return false; } + try { + await runRegionalModelUpdates(); + } catch (error) { + console.error('regional model updates failed', error); + } + await migrate(); await postStart(); @@ -75,7 +95,7 @@ export async function connectToLocalDatabase(filePath) { export function purgeCache(purgeAll = false) { const filterFunction = purgeAll - ? (d) => true + ? () => true : (d) => frappe.docs[d][d] instanceof frappe.BaseMeta; Object.keys(frappe.docs) @@ -84,4 +104,9 @@ export function purgeCache(purgeAll = false) { frappe.removeFromCache(d, d); delete frappe[d]; }); + + if (purgeAll) { + delete frappe.db; + frappe.initializeAndRegister(models, true); + } } diff --git a/src/pages/SetupWizard/setupCompany.js b/src/pages/SetupWizard/setupCompany.js index 125666eb..fee6deed 100644 --- a/src/pages/SetupWizard/setupCompany.js +++ b/src/pages/SetupWizard/setupCompany.js @@ -1,8 +1,8 @@ +import config from '@/config'; import frappe from 'frappejs'; import countryList from '~/fixtures/countryInfo.json'; -import generateTaxes from '../../../models/doctype/Tax/RegionalChanges'; -import { getCountryCOA } from '../../../accounting/importCOA'; -import config from '@/config'; +import generateTaxes from '../../../models/doctype/Tax/RegionalEntries'; +import regionalModelUpdates from '../../../models/regionalModelUpdates'; export default async function setupCompany(setupWizardValues) { const { @@ -100,12 +100,8 @@ async function setupChartOfAccounts(bankName, country) { async function setupRegionalChanges(country) { await generateTaxes(country); - if (country === 'India') { - frappe.models.Party = ( - await import('../../../models/doctype/Party/RegionalChanges') - ).default; - await frappe.db.migrate(); - } + await regionalModelUpdates({ country }); + await frappe.db.migrate(); } function updateCompanyNameInConfig() {