2
0
mirror of https://github.com/frappe/books.git synced 2024-11-14 17:34:04 +00:00

feat: prevent coupon usage if maximum limit is exceeded

This commit is contained in:
AbleKSaju 2024-09-18 10:41:15 +05:30
parent 8697e7ebf9
commit 41a67cc864
2 changed files with 23 additions and 0 deletions

View File

@ -23,10 +23,18 @@ export class AppliedCouponCodes extends InvoiceItem {
'pricingRule',
'validFrom',
'validTo',
'maximumUse',
'used',
],
filters: { name: value as string },
});
if ((coupon[0]?.maximumUse as number) <= (coupon[0]?.used as number)) {
throw new ValidationError(
'Coupon code has been used maximum number of times'
);
}
const applicableCouponCodesNames = await getApplicableCouponCodesName(
value as string,
this.parentdoc as SalesInvoice

View File

@ -236,6 +236,10 @@ export abstract class Invoice extends Transactional {
await this._updateIsItemsReturned();
await this._createLoyaltyPointEntry();
if (this.schemaName === ModelNameEnum.SalesInvoice) {
this.updateUsedCountOfCoupons();
}
}
async afterCancel() {
@ -553,6 +557,17 @@ export abstract class Invoice extends Transactional {
return newReturnDoc;
}
updateUsedCountOfCoupons() {
this.coupons?.map(async (coupon) => {
const couponDoc = await this.fyo.doc.getDoc(
ModelNameEnum.CouponCode,
coupon.coupons
);
await couponDoc.setAndSync({ used: (couponDoc.used as number) + 1 });
});
}
async _updateIsItemsReturned() {
if (!this.isReturn || !this.returnAgainst || this.isQuote) {
return;