From 301a04a63851f2ea8081967b41c560bbd796e90b Mon Sep 17 00:00:00 2001 From: AbleKSaju <126228406+AbleKSaju@users.noreply.github.com> Date: Mon, 30 Sep 2024 14:57:09 +0530 Subject: [PATCH] feat: reset used coupon count after canceling or returning an invoice --- models/baseModels/Invoice/Invoice.ts | 25 ++++++++++++++++++++++--- models/helpers.ts | 4 ++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/models/baseModels/Invoice/Invoice.ts b/models/baseModels/Invoice/Invoice.ts index 7452696e..820f854f 100644 --- a/models/baseModels/Invoice/Invoice.ts +++ b/models/baseModels/Invoice/Invoice.ts @@ -195,6 +195,9 @@ export abstract class Invoice extends Transactional { if (this.isReturn) { await this._removeLoyaltyPointEntry(); + await this.reduceUsedCountOfCoupons(); + + return; } if (this.isQuote) { @@ -248,12 +251,14 @@ export abstract class Invoice extends Transactional { await this._updatePartyOutStanding(); await this._updateIsItemsReturned(); await this._removeLoyaltyPointEntry(); + await this._reduceUsedCountOfCoupon(); + } + + async _reduceUsedCountOfCoupon() { + await this.reduceUsedCountOfCoupons(); } async _removeLoyaltyPointEntry() { - if (!this.loyaltyProgram) { - return; - } await removeLoyaltyPoint(this); } @@ -567,6 +572,20 @@ export abstract class Invoice extends Transactional { await couponDoc.setAndSync({ used: (couponDoc.used as number) + 1 }); }); } + async reduceUsedCountOfCoupons() { + if (!this.coupons?.length) { + return; + } + + 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) { diff --git a/models/helpers.ts b/models/helpers.ts index 80f8ab82..141ecc9c 100644 --- a/models/helpers.ts +++ b/models/helpers.ts @@ -762,6 +762,10 @@ export function getLoyaltyProgramTier( } export async function removeLoyaltyPoint(doc: Doc) { + if (!doc.loyaltyProgram) { + return; + } + const data = (await doc.fyo.db.getAll(ModelNameEnum.LoyaltyPointEntry, { fields: ['name', 'loyaltyPoints', 'expiryDate'], filters: {