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:
parent
8697e7ebf9
commit
41a67cc864
@ -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
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user