2
0
mirror of https://github.com/frappe/books.git synced 2024-12-23 11:29:03 +00:00
books/models/baseModels/SetupWizard/SetupWizard.ts

167 lines
5.2 KiB
TypeScript
Raw Normal View History

import { t } from 'fyo';
import { Doc } from 'fyo/model/doc';
import { FormulaMap, ListsMap, ValidationMap } from 'fyo/model/types';
2022-04-22 11:02:03 +00:00
import { validateEmail } from 'fyo/model/validationFunction';
2022-10-24 07:35:13 +00:00
import { DateTime } from 'luxon';
import { getCountryInfo, getFiscalYear } from 'utils/misc';
2023-04-10 11:39:07 +00:00
function getCurrencyList(): { countryCode: string; name: string }[] {
const result: { countryCode: string; name: string }[] = [];
const countryInfo = getCountryInfo();
for (const info of Object.values(countryInfo)) {
const { currency, code } = info ?? {};
if (typeof currency !== 'string' || typeof code !== 'string') {
continue;
2023-04-10 00:50:14 +00:00
}
2023-04-10 11:39:07 +00:00
result.push({ name: currency, countryCode: code });
}
2023-04-10 00:50:14 +00:00
return result;
}
export function getCOAList() {
return [
{ name: t`Standard Chart of Accounts`, countryCode: '' },
{ countryCode: 'ae', name: 'U.A.E - Chart of Accounts' },
{
countryCode: 'ca',
name: 'Canada - Plan comptable pour les provinces francophones',
},
{ countryCode: 'gt', name: 'Guatemala - Cuentas' },
{ countryCode: 'hu', name: 'Hungary - Chart of Accounts' },
{ countryCode: 'id', name: 'Indonesia - Chart of Accounts' },
{ countryCode: 'in', name: 'India - Chart of Accounts' },
{ countryCode: 'mx', name: 'Mexico - Plan de Cuentas' },
{ countryCode: 'ni', name: 'Nicaragua - Catalogo de Cuentas' },
{ countryCode: 'nl', name: 'Netherlands - Grootboekschema' },
{ countryCode: 'sg', name: 'Singapore - Chart of Accounts' },
{ countryCode: 'fr', name: 'France - Plan Comptable General' },
2022-10-24 07:35:13 +00:00
/*
{ countryCode: 'th', name: 'Thailand - Chart of Accounts' },
{ countryCode: 'us', name: 'United States - Chart of Accounts' },
{ countryCode: 've', name: 'Venezuela - Plan de Cuentas' },
{ countryCode: 'za', name: 'South Africa - Chart of Accounts' },
{ countryCode: 'de', name: 'Germany - Kontenplan' },
{ countryCode: 'it', name: 'Italy - Piano dei Conti' },
{ countryCode: 'es', name: 'Spain - Plan de Cuentas' },
{ countryCode: 'pt', name: 'Portugal - Plan de Contas' },
{ countryCode: 'pl', name: 'Poland - Rejestr Kont' },
{ countryCode: 'ro', name: 'Romania - Contabilitate' },
{ countryCode: 'ru', name: 'Russia - Chart of Accounts' },
{ countryCode: 'se', name: 'Sweden - Kontoplan' },
{ countryCode: 'ch', name: 'Switzerland - Kontenplan' },
2022-08-28 15:15:51 +00:00
{ countryCode: 'tr', name: 'Turkey - Chart of Accounts' },*/
];
}
export class SetupWizard extends Doc {
2022-12-14 06:57:40 +00:00
fiscalYearEnd?: Date;
fiscalYearStart?: Date;
2022-10-24 07:35:13 +00:00
formulas: FormulaMap = {
fiscalYearStart: {
formula: (fieldname?: string) => {
2022-12-14 06:57:40 +00:00
if (
fieldname === 'fiscalYearEnd' &&
this.fiscalYearEnd &&
!this.fiscalYearStart
) {
return DateTime.fromJSDate(this.fiscalYearEnd)
2022-10-24 07:35:13 +00:00
.minus({ years: 1 })
.plus({ days: 1 })
2022-12-14 06:57:40 +00:00
.toJSDate();
2022-10-24 07:35:13 +00:00
}
if (!this.country) {
return;
}
const countryInfo = getCountryInfo();
const fyStart =
countryInfo[this.country as string]?.fiscal_year_start ?? '';
return getFiscalYear(fyStart, true);
},
2022-10-24 07:35:13 +00:00
dependsOn: ['country', 'fiscalYearEnd'],
},
fiscalYearEnd: {
formula: (fieldname?: string) => {
2022-12-14 06:57:40 +00:00
if (
fieldname === 'fiscalYearStart' &&
this.fiscalYearStart &&
!this.fiscalYearEnd
) {
return DateTime.fromJSDate(this.fiscalYearStart)
2022-10-24 07:35:13 +00:00
.plus({ years: 1 })
.minus({ days: 1 })
2022-12-14 06:57:40 +00:00
.toJSDate();
2022-10-24 07:35:13 +00:00
}
if (!this.country) {
return;
}
const countryInfo = getCountryInfo();
const fyEnd =
countryInfo[this.country as string]?.fiscal_year_end ?? '';
return getFiscalYear(fyEnd, false);
},
2022-10-24 07:35:13 +00:00
dependsOn: ['country', 'fiscalYearStart'],
},
currency: {
formula: () => {
2023-04-10 11:39:07 +00:00
const country = this.get('country');
if (typeof country !== 'string') {
return;
}
2023-04-10 11:39:07 +00:00
const countryInfo = getCountryInfo();
2023-04-10 11:39:07 +00:00
const { code } = countryInfo[country] ?? {};
if (!code) {
2023-04-10 00:50:14 +00:00
return;
}
2023-04-10 11:39:07 +00:00
const currencyList = getCurrencyList();
const currency = currencyList.find(
({ countryCode }) => countryCode === code
);
2023-04-10 00:50:14 +00:00
if (currency === undefined) {
2023-04-10 11:39:07 +00:00
return currencyList[0].name;
2023-04-10 00:50:14 +00:00
}
2023-04-10 11:39:07 +00:00
2023-04-10 00:50:14 +00:00
return currency.name;
},
dependsOn: ['country'],
},
chartOfAccounts: {
formula: () => {
const country = this.get('country') as string | undefined;
if (country === undefined) {
return;
}
const countryInfo = getCountryInfo();
const code = countryInfo[country]?.code;
if (!code) {
return;
}
const coaList = getCOAList();
const coa = coaList.find(({ countryCode }) => countryCode === code);
return coa?.name ?? coaList[0].name;
},
dependsOn: ['country'],
},
};
2022-04-22 11:02:03 +00:00
validations: ValidationMap = {
email: validateEmail,
};
static lists: ListsMap = {
country: () => Object.keys(getCountryInfo()),
2023-04-10 00:50:14 +00:00
currency: () => getCurrencyList().map(({ name }) => name),
chartOfAccounts: () => getCOAList().map(({ name }) => name),
};
}