diff --git a/models/baseModels/Invoice/Invoice.ts b/models/baseModels/Invoice/Invoice.ts index 7452696ec..820f854f5 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 80f8ab828..141ecc9c8 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: {