2
0
mirror of https://github.com/frappe/books.git synced 2024-12-22 10:58:59 +00:00

feat: reset used coupon count after canceling or returning an invoice

This commit is contained in:
AbleKSaju 2024-09-30 14:57:09 +05:30
parent 5f86cc8ff2
commit 301a04a638
2 changed files with 26 additions and 3 deletions

View File

@ -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) {

View File

@ -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: {