2022-10-07 18:41:50 +00:00
|
|
|
import { t } from 'fyo';
|
|
|
|
import { DocValue } from 'fyo/core/types';
|
2022-04-24 06:48:44 +00:00
|
|
|
import { Doc } from 'fyo/model/doc';
|
2022-10-07 18:41:50 +00:00
|
|
|
import { FiltersMap, FormulaMap, ValidationMap } from 'fyo/model/types';
|
|
|
|
import { NotFoundError } from 'fyo/utils/errors';
|
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;
|
|
|
|
}
|
|
|
|
|
2022-10-07 18:41:50 +00:00
|
|
|
const party = await this.parentdoc?.loadAndGetLink('party');
|
|
|
|
if (!party) {
|
2022-05-03 13:13:47 +00:00
|
|
|
return ModelNameEnum.SalesInvoice;
|
|
|
|
}
|
|
|
|
|
2022-10-07 18:41:50 +00:00
|
|
|
if (party.role === PartyRoleEnum.Supplier) {
|
2022-05-03 13:13:47 +00:00
|
|
|
return ModelNameEnum.PurchaseInvoice;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ModelNameEnum.SalesInvoice;
|
|
|
|
},
|
|
|
|
},
|
2022-10-07 18:41:50 +00:00
|
|
|
referenceName: {
|
|
|
|
formula: async () => {
|
|
|
|
if (!this.referenceName || !this.referenceType) {
|
|
|
|
return this.referenceName;
|
|
|
|
}
|
|
|
|
|
|
|
|
const exists = await this.fyo.db.exists(
|
|
|
|
this.referenceType,
|
|
|
|
this.referenceName
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!exists) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.referenceName;
|
|
|
|
},
|
|
|
|
dependsOn: ['referenceType'],
|
|
|
|
},
|
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) => {
|
2022-10-24 08:37:19 +00:00
|
|
|
const zero =
|
|
|
|
'0.' +
|
|
|
|
'0'.repeat(doc.fyo.singles.SystemSettings?.internalPrecision ?? 11);
|
|
|
|
|
2022-05-03 13:13:47 +00:00
|
|
|
const baseFilters = {
|
2022-10-24 08:37:19 +00:00
|
|
|
outstandingAmount: ['!=', zero],
|
2022-05-03 13:13:47 +00:00
|
|
|
submitted: true,
|
|
|
|
cancelled: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
const party = doc?.parentdoc?.party as undefined | string;
|
2022-10-24 08:37:19 +00:00
|
|
|
if (!party) {
|
2022-05-03 13:13:47 +00:00
|
|
|
return baseFilters;
|
|
|
|
}
|
|
|
|
|
|
|
|
return { ...baseFilters, party };
|
|
|
|
},
|
2022-04-11 09:41:49 +00:00
|
|
|
};
|
2022-10-07 18:41:50 +00:00
|
|
|
|
|
|
|
validations: ValidationMap = {
|
|
|
|
referenceName: async (value: DocValue) => {
|
|
|
|
const exists = await this.fyo.db.exists(
|
|
|
|
this.referenceType!,
|
|
|
|
value as string
|
|
|
|
);
|
|
|
|
if (exists) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new NotFoundError(
|
|
|
|
t`${this.fyo.schemaMap[this.referenceType!]?.label!} ${
|
|
|
|
value as string
|
|
|
|
} does not exist`,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
},
|
|
|
|
};
|
2022-04-11 09:41:49 +00:00
|
|
|
}
|