2
0
mirror of https://github.com/frappe/books.git synced 2025-01-03 07:12:21 +00:00

feat: loyalty program validations

This commit is contained in:
AbleKSaju 2024-08-23 09:39:09 +05:30
parent 39e89bd8a0
commit 4b830618dd
2 changed files with 55 additions and 4 deletions

View File

@ -20,6 +20,7 @@ export class Party extends Doc {
party?: string;
fromLead?: string;
defaultAccount?: string;
loyaltyPoints?: number;
outstandingAmount?: Money;
async updateOutstandingAmount() {
/**

View File

@ -1,11 +1,18 @@
import { Fyo } from 'fyo';
import { Action, ListViewSettings } from 'fyo/model/types';
import { Fyo, t } from 'fyo';
import { Action, ListViewSettings, ValidationMap } from 'fyo/model/types';
import { LedgerPosting } from 'models/Transactional/LedgerPosting';
import { ModelNameEnum } from 'models/types';
import { getAddedLPWithGrandTotal, getInvoiceActions, getTransactionStatusColumn } from '../../helpers';
import {
getAddedLPWithGrandTotal,
getInvoiceActions,
getTransactionStatusColumn,
} from '../../helpers';
import { Invoice } from '../Invoice/Invoice';
import { SalesInvoiceItem } from '../SalesInvoiceItem/SalesInvoiceItem';
import { LoyaltyProgram } from '../LoyaltyProgram/LoyaltyProgram';
import { DocValue } from 'fyo/core/types';
import { Party } from '../Party/Party';
import { ValidationError } from 'fyo/utils/errors';
export class SalesInvoice extends Invoice {
items?: SalesInvoiceItem[];
@ -47,7 +54,6 @@ export class SalesInvoice extends Invoice {
await posting.credit(this.account!, totalAmount);
}
if (this.taxes) {
for (const tax of this.taxes) {
if (this.isReturn) {
@ -73,6 +79,50 @@ export class SalesInvoice extends Invoice {
return posting;
}
validations: ValidationMap = {
loyaltyPoints: async (value: DocValue) => {
if (!this.redeemLoyaltyPoints) {
return;
}
const partyDoc = (await this.fyo.doc.getDoc(
ModelNameEnum.Party,
this.party
)) as Party;
if ((value as number) <= 0) {
throw new ValidationError(t`Points must be greather than 0`);
}
if ((value as number) > (partyDoc?.loyaltyPoints || 0)) {
throw new ValidationError(
t`${this.party as string} only has ${
partyDoc.loyaltyPoints as number
} points`
);
}
const loyaltyProgramDoc = (await this.fyo.doc.getDoc(
ModelNameEnum.LoyaltyProgram,
this.loyaltyProgram
)) as LoyaltyProgram;
if (!this?.grandTotal) {
return;
}
const loyaltyPoint =
((value as number) || 0) *
((loyaltyProgramDoc?.conversionFactor as number) || 0);
if (this.grandTotal?.lt(loyaltyPoint)) {
throw new ValidationError(
t`no need ${value as number} points to purchase this item`
);
}
},
};
static getListViewSettings(): ListViewSettings {
return {
columns: [