2
0
mirror of https://github.com/frappe/books.git synced 2024-11-15 01:44:04 +00:00
books/models/baseModels/TaxSummary/TaxSummary.ts
Mildred Ki'Lya 2bffcda8ff feat: #755 Accounting for taxes on payments
When defining taxes, it is possible to define an additional payment
account that will be used during payments to move taxes from the
original tax account to this new payment tax account. This allows to
account for taxes only when payment is received.

Now payments can reference tax summary objects that will reference the
two accounts to move funds between when the payment is committed. Reuse
some of the Invoice code to generate these tax summary objects.
2023-12-22 10:07:47 +01:00

50 lines
1.3 KiB
TypeScript

import { Fyo } from 'fyo';
import { DocValueMap } from 'fyo/core/types';
import { Doc } from 'fyo/model/doc';
import { CurrenciesMap } from 'fyo/model/types';
import { DEFAULT_CURRENCY } from 'fyo/utils/consts';
import { Money } from 'pesa';
import { FieldTypeEnum, Schema } from 'schemas/types';
import { Invoice } from '../Invoice/Invoice';
export class TaxSummary extends Doc {
account?: string;
from_account?: string;
rate?: number;
amount?: Money;
parentdoc?: Invoice;
get exchangeRate() {
return this.parentdoc?.exchangeRate ?? 1;
}
get currency() {
return this.parentdoc?.currency ?? DEFAULT_CURRENCY;
}
constructor(schema: Schema, data: DocValueMap, fyo: Fyo) {
super(schema, data, fyo);
this._setGetCurrencies();
}
getCurrencies: CurrenciesMap = {};
_getCurrency() {
if (this.exchangeRate === 1) {
return this.fyo.singles.SystemSettings?.currency ?? DEFAULT_CURRENCY;
}
return this.currency;
}
_setGetCurrencies() {
const currencyFields = this.schema.fields.filter(
({ fieldtype }) => fieldtype === FieldTypeEnum.Currency
);
const getCurrency = this._getCurrency.bind(this);
for (const { fieldname } of currencyFields) {
this.getCurrencies[fieldname] ??= getCurrency;
}
}
}