2022-04-19 05:59:36 +00:00
|
|
|
import { DocValue } from 'fyo/core/types';
|
2022-04-24 06:48:44 +00:00
|
|
|
import { Doc } from 'fyo/model/doc';
|
2022-04-19 05:59:36 +00:00
|
|
|
import { DefaultMap, FiltersMap, FormulaMap } from 'fyo/model/types';
|
2022-04-19 06:05:39 +00:00
|
|
|
import { getExchangeRate } from 'models/helpers';
|
2022-05-03 13:13:47 +00:00
|
|
|
import { Transactional } from 'models/Transactional/Transactional';
|
2022-05-02 10:15:16 +00:00
|
|
|
import { ModelNameEnum } from 'models/types';
|
2022-05-23 05:30:54 +00:00
|
|
|
import { Money } from 'pesa';
|
2022-05-02 10:15:16 +00:00
|
|
|
import { getIsNullOrUndef } from 'utils';
|
2022-04-14 08:01:33 +00:00
|
|
|
import { Party } from '../Party/Party';
|
|
|
|
import { Payment } from '../Payment/Payment';
|
|
|
|
import { Tax } from '../Tax/Tax';
|
2022-04-14 09:22:45 +00:00
|
|
|
import { TaxSummary } from '../TaxSummary/TaxSummary';
|
2022-04-14 08:01:33 +00:00
|
|
|
|
2022-05-03 13:13:47 +00:00
|
|
|
export abstract class Invoice extends Transactional {
|
2022-04-14 08:01:33 +00:00
|
|
|
_taxes: Record<string, Tax> = {};
|
2022-04-14 09:22:45 +00:00
|
|
|
taxes?: TaxSummary[];
|
2022-04-14 08:01:33 +00:00
|
|
|
|
2022-04-14 09:22:45 +00:00
|
|
|
party?: string;
|
|
|
|
account?: string;
|
|
|
|
currency?: string;
|
|
|
|
netTotal?: Money;
|
2022-05-17 12:12:57 +00:00
|
|
|
grandTotal?: Money;
|
2022-04-14 09:22:45 +00:00
|
|
|
baseGrandTotal?: Money;
|
2022-04-20 06:38:47 +00:00
|
|
|
outstandingAmount?: Money;
|
2022-04-14 09:22:45 +00:00
|
|
|
exchangeRate?: number;
|
|
|
|
|
2022-04-20 06:38:47 +00:00
|
|
|
submitted?: boolean;
|
|
|
|
cancelled?: boolean;
|
|
|
|
|
2022-04-14 09:22:45 +00:00
|
|
|
get isSales() {
|
|
|
|
return this.schemaName === 'SalesInvoice';
|
|
|
|
}
|
2022-04-14 08:01:33 +00:00
|
|
|
|
|
|
|
async afterSubmit() {
|
2022-05-03 13:13:47 +00:00
|
|
|
await super.afterSubmit();
|
2022-04-14 08:01:33 +00:00
|
|
|
|
|
|
|
// update outstanding amounts
|
2022-04-19 05:59:36 +00:00
|
|
|
await this.fyo.db.update(this.schemaName, {
|
2022-04-14 08:01:33 +00:00
|
|
|
name: this.name as string,
|
2022-04-14 09:22:45 +00:00
|
|
|
outstandingAmount: this.baseGrandTotal!,
|
2022-04-14 08:01:33 +00:00
|
|
|
});
|
|
|
|
|
2022-04-19 05:59:36 +00:00
|
|
|
const party = (await this.fyo.doc.getDoc('Party', this.party!)) as Party;
|
2022-04-14 08:01:33 +00:00
|
|
|
await party.updateOutstandingAmount();
|
|
|
|
}
|
|
|
|
|
2022-05-04 08:19:40 +00:00
|
|
|
async afterCancel() {
|
|
|
|
await super.afterCancel();
|
|
|
|
await this._cancelPayments();
|
|
|
|
await this._updatePartyOutStanding();
|
|
|
|
}
|
|
|
|
|
|
|
|
async _cancelPayments() {
|
2022-05-03 13:13:47 +00:00
|
|
|
const paymentIds = await this.getPaymentIds();
|
|
|
|
for (const paymentId of paymentIds) {
|
|
|
|
const paymentDoc = (await this.fyo.doc.getDoc(
|
2022-04-14 08:01:33 +00:00
|
|
|
'Payment',
|
2022-05-03 13:13:47 +00:00
|
|
|
paymentId
|
2022-04-14 08:01:33 +00:00
|
|
|
)) as Payment;
|
2022-05-03 13:13:47 +00:00
|
|
|
await paymentDoc.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-04 08:19:40 +00:00
|
|
|
async _updatePartyOutStanding() {
|
2022-05-03 13:13:47 +00:00
|
|
|
const partyDoc = (await this.fyo.doc.getDoc(
|
|
|
|
ModelNameEnum.Party,
|
|
|
|
this.party!
|
|
|
|
)) as Party;
|
|
|
|
|
|
|
|
await partyDoc.updateOutstandingAmount();
|
|
|
|
}
|
|
|
|
|
|
|
|
async afterDelete() {
|
|
|
|
await super.afterDelete();
|
|
|
|
const paymentIds = await this.getPaymentIds();
|
|
|
|
for (const name of paymentIds) {
|
2022-05-04 08:19:40 +00:00
|
|
|
const paymentDoc = await this.fyo.doc.getDoc(ModelNameEnum.Payment, name);
|
|
|
|
await paymentDoc.delete();
|
2022-05-03 13:13:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async getPaymentIds() {
|
|
|
|
const payments = (await this.fyo.db.getAll('PaymentFor', {
|
|
|
|
fields: ['parent'],
|
|
|
|
filters: { referenceType: this.schemaName, referenceName: this.name! },
|
|
|
|
orderBy: 'name',
|
|
|
|
})) as { parent: string }[];
|
2022-04-14 08:01:33 +00:00
|
|
|
|
2022-05-03 13:13:47 +00:00
|
|
|
if (payments.length != 0) {
|
|
|
|
return [...new Set(payments.map(({ parent }) => parent))];
|
2022-04-14 08:01:33 +00:00
|
|
|
}
|
2022-05-03 13:13:47 +00:00
|
|
|
|
|
|
|
return [];
|
2022-04-14 08:01:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async getExchangeRate() {
|
2022-05-02 10:15:16 +00:00
|
|
|
if (!this.currency) {
|
|
|
|
return 1.0;
|
|
|
|
}
|
2022-04-14 08:01:33 +00:00
|
|
|
|
2022-05-02 10:15:16 +00:00
|
|
|
const currency = await this.fyo.getValue(
|
|
|
|
ModelNameEnum.SystemSettings,
|
|
|
|
'currency'
|
2022-04-18 11:29:20 +00:00
|
|
|
);
|
2022-05-02 10:15:16 +00:00
|
|
|
if (this.currency === currency) {
|
2022-04-14 08:01:33 +00:00
|
|
|
return 1.0;
|
|
|
|
}
|
|
|
|
return await getExchangeRate({
|
2022-04-14 09:22:45 +00:00
|
|
|
fromCurrency: this.currency!,
|
2022-05-02 10:15:16 +00:00
|
|
|
toCurrency: currency as string,
|
2022-04-14 08:01:33 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async getTaxSummary() {
|
|
|
|
const taxes: Record<
|
|
|
|
string,
|
2022-04-14 09:22:45 +00:00
|
|
|
{
|
|
|
|
account: string;
|
|
|
|
rate: number;
|
|
|
|
amount: Money;
|
|
|
|
baseAmount: Money;
|
|
|
|
[key: string]: DocValue;
|
|
|
|
}
|
2022-04-14 08:01:33 +00:00
|
|
|
> = {};
|
|
|
|
|
|
|
|
for (const row of this.items as Doc[]) {
|
|
|
|
if (!row.tax) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const tax = await this.getTax(row.tax as string);
|
|
|
|
for (const d of tax.details as Doc[]) {
|
|
|
|
const account = d.account as string;
|
|
|
|
const rate = d.rate as number;
|
|
|
|
|
|
|
|
taxes[account] = taxes[account] || {
|
|
|
|
account,
|
|
|
|
rate,
|
2022-04-19 05:59:36 +00:00
|
|
|
amount: this.fyo.pesa(0),
|
|
|
|
baseAmount: this.fyo.pesa(0),
|
2022-04-14 08:01:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const amount = (row.amount as Money).mul(rate).div(100);
|
|
|
|
taxes[account].amount = taxes[account].amount.add(amount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object.keys(taxes)
|
|
|
|
.map((account) => {
|
|
|
|
const tax = taxes[account];
|
2022-04-14 09:22:45 +00:00
|
|
|
tax.baseAmount = tax.amount.mul(this.exchangeRate!);
|
2022-04-14 08:01:33 +00:00
|
|
|
return tax;
|
|
|
|
})
|
|
|
|
.filter((tax) => !tax.amount.isZero());
|
|
|
|
}
|
|
|
|
|
|
|
|
async getTax(tax: string) {
|
|
|
|
if (!this._taxes![tax]) {
|
2022-04-19 05:59:36 +00:00
|
|
|
this._taxes[tax] = await this.fyo.doc.getDoc('Tax', tax);
|
2022-04-14 08:01:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return this._taxes[tax];
|
|
|
|
}
|
|
|
|
|
|
|
|
async getGrandTotal() {
|
|
|
|
return ((this.taxes ?? []) as Doc[])
|
|
|
|
.map((doc) => doc.amount as Money)
|
2022-04-14 09:22:45 +00:00
|
|
|
.reduce((a, b) => a.add(b), this.netTotal!);
|
2022-04-14 08:01:33 +00:00
|
|
|
}
|
2022-04-14 09:22:45 +00:00
|
|
|
|
|
|
|
formulas: FormulaMap = {
|
2022-04-29 13:00:24 +00:00
|
|
|
account: {
|
2022-05-02 10:15:16 +00:00
|
|
|
formula: async () => {
|
|
|
|
return (await this.fyo.getValue(
|
|
|
|
'Party',
|
|
|
|
this.party!,
|
|
|
|
'defaultAccount'
|
|
|
|
)) as string;
|
|
|
|
},
|
2022-04-29 13:00:24 +00:00
|
|
|
dependsOn: ['party'],
|
|
|
|
},
|
|
|
|
currency: {
|
2022-05-02 10:15:16 +00:00
|
|
|
formula: async () => {
|
|
|
|
const currency = (await this.fyo.getValue(
|
|
|
|
'Party',
|
|
|
|
this.party!,
|
|
|
|
'currency'
|
|
|
|
)) as string;
|
|
|
|
|
|
|
|
if (!getIsNullOrUndef(currency)) {
|
|
|
|
return currency;
|
|
|
|
}
|
|
|
|
return this.fyo.singles.SystemSettings!.currency as string;
|
|
|
|
},
|
2022-04-29 13:00:24 +00:00
|
|
|
dependsOn: ['party'],
|
|
|
|
},
|
|
|
|
exchangeRate: { formula: async () => await this.getExchangeRate() },
|
|
|
|
netTotal: { formula: async () => this.getSum('items', 'amount', false) },
|
|
|
|
baseNetTotal: {
|
|
|
|
formula: async () => this.netTotal!.mul(this.exchangeRate!),
|
|
|
|
},
|
|
|
|
taxes: { formula: async () => await this.getTaxSummary() },
|
|
|
|
grandTotal: { formula: async () => await this.getGrandTotal() },
|
|
|
|
baseGrandTotal: {
|
|
|
|
formula: async () => (this.grandTotal as Money).mul(this.exchangeRate!),
|
|
|
|
},
|
|
|
|
outstandingAmount: {
|
|
|
|
formula: async () => {
|
|
|
|
if (this.submitted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.baseGrandTotal!;
|
|
|
|
},
|
2022-04-14 09:22:45 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-04-25 06:33:31 +00:00
|
|
|
static defaults: DefaultMap = {
|
2022-04-14 09:22:45 +00:00
|
|
|
date: () => new Date().toISOString().slice(0, 10),
|
|
|
|
};
|
|
|
|
|
|
|
|
static filters: FiltersMap = {
|
2022-05-02 10:15:16 +00:00
|
|
|
party: (doc: Doc) => ({
|
|
|
|
role: ['in', [doc.isSales ? 'Customer' : 'Supplier', 'Both']],
|
|
|
|
}),
|
2022-04-14 09:22:45 +00:00
|
|
|
account: (doc: Doc) => ({
|
|
|
|
isGroup: false,
|
|
|
|
accountType: doc.isSales ? 'Receivable' : 'Payable',
|
|
|
|
}),
|
|
|
|
numberSeries: (doc: Doc) => ({ referenceType: doc.schemaName }),
|
|
|
|
};
|
2022-05-18 16:55:24 +00:00
|
|
|
|
|
|
|
static createFilters: FiltersMap = {
|
|
|
|
party: (doc: Doc) => ({
|
|
|
|
role: doc.isSales ? 'Customer' : 'Supplier',
|
|
|
|
}),
|
|
|
|
};
|
2022-04-14 08:01:33 +00:00
|
|
|
}
|