2022-04-24 06:48:44 +00:00
|
|
|
import { Doc } from 'fyo/model/doc';
|
2022-07-13 17:48:20 +00:00
|
|
|
import {
|
2022-07-14 12:33:31 +00:00
|
|
|
ChangeArg,
|
2022-07-13 17:48:20 +00:00
|
|
|
FiltersMap,
|
|
|
|
ListsMap,
|
|
|
|
ReadOnlyMap,
|
2022-07-14 12:33:31 +00:00
|
|
|
ValidationMap
|
2022-07-13 17:48:20 +00:00
|
|
|
} from 'fyo/model/types';
|
2022-04-22 11:02:03 +00:00
|
|
|
import { validateEmail } from 'fyo/model/validationFunction';
|
2022-07-14 12:33:31 +00:00
|
|
|
import { createDiscountAccount } from 'src/setup/setupInstance';
|
2022-04-23 09:23:44 +00:00
|
|
|
import { getCountryInfo } from 'utils/misc';
|
2022-04-11 09:41:49 +00:00
|
|
|
|
|
|
|
export class AccountingSettings extends Doc {
|
2022-07-14 12:33:31 +00:00
|
|
|
enableDiscounting?: boolean;
|
2022-04-11 09:41:49 +00:00
|
|
|
static filters: FiltersMap = {
|
|
|
|
writeOffAccount: () => ({
|
|
|
|
isGroup: false,
|
|
|
|
rootType: 'Expense',
|
|
|
|
}),
|
|
|
|
roundOffAccount: () => ({
|
|
|
|
isGroup: false,
|
|
|
|
rootType: 'Expense',
|
|
|
|
}),
|
2022-07-14 12:33:31 +00:00
|
|
|
discountAccount: () => ({
|
|
|
|
isGroup: false,
|
|
|
|
rootType: 'Income',
|
|
|
|
}),
|
2022-04-11 09:41:49 +00:00
|
|
|
};
|
|
|
|
|
2022-04-22 11:02:03 +00:00
|
|
|
validations: ValidationMap = {
|
|
|
|
email: validateEmail,
|
|
|
|
};
|
|
|
|
|
2022-04-11 09:41:49 +00:00
|
|
|
static lists: ListsMap = {
|
2022-04-23 09:23:44 +00:00
|
|
|
country: () => Object.keys(getCountryInfo()),
|
2022-04-11 09:41:49 +00:00
|
|
|
};
|
2022-07-13 17:48:20 +00:00
|
|
|
|
|
|
|
readOnly: ReadOnlyMap = {
|
|
|
|
enableDiscounting: () => {
|
|
|
|
return !!this.enableDiscounting;
|
|
|
|
},
|
|
|
|
};
|
2022-07-14 12:33:31 +00:00
|
|
|
|
|
|
|
async change(ch: ChangeArg) {
|
|
|
|
const discountingEnabled =
|
|
|
|
ch.changed === 'enableDiscounting' && this.enableDiscounting;
|
|
|
|
const discountAccountNotSet = !this.discountAccount;
|
|
|
|
|
|
|
|
if (discountingEnabled && discountAccountNotSet) {
|
|
|
|
await createDiscountAccount(this.fyo);
|
|
|
|
}
|
|
|
|
}
|
2022-04-11 09:41:49 +00:00
|
|
|
}
|