2
0
mirror of https://github.com/frappe/books.git synced 2025-01-24 07:38:25 +00:00

feat: allow regional model updates

This commit is contained in:
18alantom 2021-12-01 18:38:00 +05:30
parent 209e8bf7ec
commit 3f69d7fcc5
6 changed files with 88 additions and 44 deletions

View File

@ -4,6 +4,7 @@ import { _ } from 'frappejs/utils';
export default { export default {
name: 'Party', name: 'Party',
label: 'Party', label: 'Party',
regional: 1,
keywordFields: ['name'], keywordFields: ['name'],
fields: [ fields: [
{ {
@ -99,22 +100,7 @@ export default {
} }
}, },
}, },
{
fieldname: 'gstin',
label: 'GSTIN No.',
fieldtype: 'Data',
hidden: (form) => {
return form.gstType === 'Registered Regular' ? 0 : 1;
},
},
], ],
quickEditFields: [ quickEditFields: ['email', 'phone', 'address', 'defaultAccount', 'currency'],
'email',
'phone',
'address',
'defaultAccount',
'currency',
'gstin',
],
}; };

View File

@ -1,21 +1,39 @@
import party from './Party'; import { cloneDeep } from 'lodash';
import PartyOriginal from './Party';
party.fields.splice(3, 0, { export default function getAugmentedParty({ country }) {
//insert at 3rd position const Party = cloneDeep(PartyOriginal);
if (!country) {
return Party;
}
if (country === 'India') {
Party.fields.splice(
3,
0,
{
fieldname: 'gstin', fieldname: 'gstin',
label: 'GSTIN No.', label: 'GSTIN No.',
fieldtype: 'Data', fieldtype: 'Data',
hidden: form => { hidden: (form) => {
return form.gstType === 'Registered Regular' ? 0 : 1; return form.gstType === 'Registered Regular' ? 0 : 1;
} },
}); },
party.fields.splice(4, 0, { {
fieldname: 'gstType', fieldname: 'gstType',
label: 'GST Registration Type', label: 'GST Registration Type',
fieldtype: 'Select', fieldtype: 'Select',
options: ['Unregistered', 'Registered Regular', 'Consumer'] options: ['Unregistered', 'Registered Regular', 'Consumer'],
}
);
Party.quickEditFields.push('gstin');
} else {
Party.fields.splice(3, 0, {
fieldname: 'taxId',
label: 'Tax ID',
fieldtype: 'Data',
}); });
party.fields.join(); Party.quickEditFields.push('taxId');
const newParty = party; }
return Party;
export default newParty; }

View File

@ -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);
}
}

View File

@ -3,6 +3,8 @@ import { ipcRenderer } from 'electron';
import { _ } from 'frappejs'; import { _ } from 'frappejs';
import SQLiteDatabase from 'frappejs/backends/sqlite'; import SQLiteDatabase from 'frappejs/backends/sqlite';
import fs from 'fs'; import fs from 'fs';
import models from '../models';
import regionalModelUpdates from '../models/regionalModelUpdates';
import postStart from '../server/postStart'; import postStart from '../server/postStart';
import { IPC_ACTIONS } from './messages'; import { IPC_ACTIONS } from './messages';
import migrate from './migrate'; import migrate from './migrate';
@ -36,6 +38,18 @@ export async function createNewDatabase() {
return filePath; 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) { export async function connectToLocalDatabase(filePath) {
if (!filePath) { if (!filePath) {
return false; return false;
@ -52,6 +66,12 @@ export async function connectToLocalDatabase(filePath) {
return false; return false;
} }
try {
await runRegionalModelUpdates();
} catch (error) {
console.error('regional model updates failed', error);
}
await migrate(); await migrate();
await postStart(); await postStart();
@ -75,7 +95,7 @@ export async function connectToLocalDatabase(filePath) {
export function purgeCache(purgeAll = false) { export function purgeCache(purgeAll = false) {
const filterFunction = purgeAll const filterFunction = purgeAll
? (d) => true ? () => true
: (d) => frappe.docs[d][d] instanceof frappe.BaseMeta; : (d) => frappe.docs[d][d] instanceof frappe.BaseMeta;
Object.keys(frappe.docs) Object.keys(frappe.docs)
@ -84,4 +104,9 @@ export function purgeCache(purgeAll = false) {
frappe.removeFromCache(d, d); frappe.removeFromCache(d, d);
delete frappe[d]; delete frappe[d];
}); });
if (purgeAll) {
delete frappe.db;
frappe.initializeAndRegister(models, true);
}
} }

View File

@ -1,8 +1,8 @@
import config from '@/config';
import frappe from 'frappejs'; import frappe from 'frappejs';
import countryList from '~/fixtures/countryInfo.json'; import countryList from '~/fixtures/countryInfo.json';
import generateTaxes from '../../../models/doctype/Tax/RegionalChanges'; import generateTaxes from '../../../models/doctype/Tax/RegionalEntries';
import { getCountryCOA } from '../../../accounting/importCOA'; import regionalModelUpdates from '../../../models/regionalModelUpdates';
import config from '@/config';
export default async function setupCompany(setupWizardValues) { export default async function setupCompany(setupWizardValues) {
const { const {
@ -100,13 +100,9 @@ async function setupChartOfAccounts(bankName, country) {
async function setupRegionalChanges(country) { async function setupRegionalChanges(country) {
await generateTaxes(country); await generateTaxes(country);
if (country === 'India') { await regionalModelUpdates({ country });
frappe.models.Party = (
await import('../../../models/doctype/Party/RegionalChanges')
).default;
await frappe.db.migrate(); await frappe.db.migrate();
} }
}
function updateCompanyNameInConfig() { function updateCompanyNameInConfig() {
let filePath = frappe.db.dbPath; let filePath = frappe.db.dbPath;