2
0
mirror of https://github.com/frappe/books.git synced 2024-12-23 11:29:03 +00:00
books/models/baseModels/PaymentFor/PaymentFor.ts
18alantom 401a86eeeb chore: bump pesa
- fix isPesa impl
2022-05-23 16:18:23 +05:30

78 lines
1.8 KiB
TypeScript

import { Doc } from 'fyo/model/doc';
import { FiltersMap, FormulaMap } from 'fyo/model/types';
import { ModelNameEnum } from 'models/types';
import { Money } from 'pesa';
import { PartyRoleEnum } from '../Party/types';
import { Payment } from '../Payment/Payment';
export class PaymentFor extends Doc {
parentdoc?: Payment | undefined;
referenceType?: ModelNameEnum.SalesInvoice | ModelNameEnum.PurchaseInvoice;
referenceName?: string;
amount?: Money;
formulas: FormulaMap = {
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;
},
},
amount: {
formula: async () => {
if (!this.referenceName) {
return this.fyo.pesa(0);
}
const outstandingAmount = (await this.fyo.getValue(
this.referenceType as string,
this.referenceName as string,
'outstandingAmount'
)) as Money;
if (outstandingAmount) {
return outstandingAmount;
}
return this.fyo.pesa(0);
},
dependsOn: ['referenceName'],
},
};
static filters: FiltersMap = {
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 };
},
};
}