2022-04-24 06:48:44 +00:00
|
|
|
import { Doc } from 'fyo/model/doc';
|
2022-04-19 05:59:36 +00:00
|
|
|
import { FiltersMap, FormulaMap } from 'fyo/model/types';
|
2022-05-03 13:13:47 +00:00
|
|
|
import { ModelNameEnum } from 'models/types';
|
2022-05-23 05:30:54 +00:00
|
|
|
import { Money } from 'pesa';
|
2022-05-03 13:13:47 +00:00
|
|
|
import { PartyRoleEnum } from '../Party/types';
|
|
|
|
import { Payment } from '../Payment/Payment';
|
2022-04-11 09:41:49 +00:00
|
|
|
|
|
|
|
export class PaymentFor extends Doc {
|
2022-05-03 13:13:47 +00:00
|
|
|
parentdoc?: Payment | undefined;
|
|
|
|
referenceType?: ModelNameEnum.SalesInvoice | ModelNameEnum.PurchaseInvoice;
|
|
|
|
referenceName?: string;
|
|
|
|
amount?: Money;
|
|
|
|
|
2022-04-11 09:41:49 +00:00
|
|
|
formulas: FormulaMap = {
|
2022-05-03 13:13:47 +00:00
|
|
|
referenceType: {
|
|
|
|
formula: async () => {
|
|
|
|
if (this.referenceType) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const party = this.parentdoc!.party;
|
|
|
|
if (party === undefined) {
|
|
|
|
return ModelNameEnum.SalesInvoice;
|
|
|
|
}
|
|
|
|
|
|
|
|
const role = await this.fyo.getValue(
|
|
|
|
ModelNameEnum.Party,
|
|
|
|
party,
|
|
|
|
'role'
|
|
|
|
);
|
|
|
|
|
|
|
|
if (role === PartyRoleEnum.Supplier) {
|
|
|
|
return ModelNameEnum.PurchaseInvoice;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ModelNameEnum.SalesInvoice;
|
|
|
|
},
|
|
|
|
},
|
2022-04-29 13:00:24 +00:00
|
|
|
amount: {
|
|
|
|
formula: async () => {
|
|
|
|
if (!this.referenceName) {
|
|
|
|
return this.fyo.pesa(0);
|
|
|
|
}
|
2022-04-11 09:41:49 +00:00
|
|
|
|
2022-05-02 10:15:16 +00:00
|
|
|
const outstandingAmount = (await this.fyo.getValue(
|
2022-04-29 13:00:24 +00:00
|
|
|
this.referenceType as string,
|
|
|
|
this.referenceName as string,
|
|
|
|
'outstandingAmount'
|
2022-05-02 10:15:16 +00:00
|
|
|
)) as Money;
|
2022-04-11 09:41:49 +00:00
|
|
|
|
2022-04-29 13:00:24 +00:00
|
|
|
if (outstandingAmount) {
|
|
|
|
return outstandingAmount;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.fyo.pesa(0);
|
|
|
|
},
|
|
|
|
dependsOn: ['referenceName'],
|
2022-04-11 09:41:49 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static filters: FiltersMap = {
|
2022-05-03 13:13:47 +00:00
|
|
|
referenceName: (doc) => {
|
|
|
|
const baseFilters = {
|
|
|
|
outstandingAmount: ['>', 0],
|
|
|
|
submitted: true,
|
|
|
|
cancelled: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
const party = doc?.parentdoc?.party as undefined | string;
|
|
|
|
if (party === undefined) {
|
|
|
|
return baseFilters;
|
|
|
|
}
|
|
|
|
|
|
|
|
return { ...baseFilters, party };
|
|
|
|
},
|
2022-04-11 09:41:49 +00:00
|
|
|
};
|
|
|
|
}
|