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

fix: remove unused coupons

This commit is contained in:
AbleKSaju 2024-11-27 17:51:35 +05:30
parent 97a639e35e
commit 4a381c3a86
2 changed files with 36 additions and 65 deletions

View File

@ -18,9 +18,9 @@ import {
createLoyaltyPointEntry,
filterPricingRules,
getAddedLPWithGrandTotal,
getApplicableCouponCodesName,
getExchangeRate,
getNumberSeries,
removeUnusedCoupons,
getPricingRulesConflicts,
removeLoyaltyPoint,
roundFreeItemQty,
@ -41,7 +41,7 @@ import { TaxSummary } from '../TaxSummary/TaxSummary';
import { ReturnDocItem } from 'models/inventory/types';
import { AccountFieldEnum, PaymentTypeEnum } from '../Payment/types';
import { PricingRule } from '../PricingRule/PricingRule';
import { ApplicableCouponCodes, ApplicablePricingRules } from './types';
import { ApplicablePricingRules } from './types';
import { PricingRuleDetail } from '../PricingRuleDetail/PricingRuleDetail';
import { LoyaltyProgram } from '../LoyaltyProgram/LoyaltyProgram';
import { AppliedCouponCodes } from '../AppliedCouponCodes/AppliedCouponCodes';
@ -758,7 +758,8 @@ export abstract class Invoice extends Transactional {
}
const pricingRule = await this.getPricingRule();
if (!pricingRule) {
if (!pricingRule || !pricingRule.length) {
return false;
}
@ -1074,30 +1075,7 @@ export abstract class Invoice extends Transactional {
this.clearFreeItems();
}
if (!this.coupons?.length) {
return;
}
const applicableCouponCodes = await Promise.all(
this.coupons?.map(async (coupon) => {
return await getApplicableCouponCodesName(
coupon.coupons as string,
this as SalesInvoice
);
})
);
const flattedApplicableCouponCodes = applicableCouponCodes?.flat();
const couponCodeDoc = (await this.fyo.doc.getDoc(
ModelNameEnum.CouponCode,
this.coupons[0].coupons
)) as CouponCode;
couponCodeDoc.removeUnusedCoupons(
flattedApplicableCouponCodes as ApplicableCouponCodes[],
this as SalesInvoice
);
await removeUnusedCoupons(this as SalesInvoice);
}
async beforeCancel(): Promise<void> {

View File

@ -12,7 +12,10 @@ import {
import { Fyo, t } from 'fyo';
import { InvoiceStatus, ModelNameEnum } from './types';
import { ApplicablePricingRules } from './baseModels/Invoice/types';
import {
ApplicableCouponCodes,
ApplicablePricingRules,
} from './baseModels/Invoice/types';
import { AppliedCouponCodes } from './baseModels/AppliedCouponCodes/AppliedCouponCodes';
import { CollectionRulesItems } from './baseModels/CollectionRulesItems/CollectionRulesItems';
import { CouponCode } from './baseModels/CouponCode/CouponCode';
@ -764,19 +767,6 @@ export function getLoyaltyProgramTier(
return loyaltyProgramTier;
}
export async function updatePricingRuleItem(doc: SalesInvoice) {
const pricingRule = (await getPricingRule(doc)) as ApplicablePricingRules[];
let appliedPricingRuleCount = doc?.pricingRuleDetail?.length;
if (appliedPricingRuleCount !== pricingRule?.length) {
appliedPricingRuleCount = pricingRule?.length;
await doc?.appendPricingRuleDetail(pricingRule);
await doc?.applyProductDiscount();
}
}
export async function removeLoyaltyPoint(doc: Doc) {
if (!doc.loyaltyProgram) {
return;
@ -1058,6 +1048,33 @@ export function canApplyCouponCode(
return true;
}
export async function removeUnusedCoupons(sinvDoc: SalesInvoice) {
if (!sinvDoc.coupons?.length) {
return;
}
const applicableCouponCodes = await Promise.all(
sinvDoc.coupons?.map(async (coupon) => {
return await getApplicableCouponCodesName(
coupon.coupons as string,
sinvDoc
);
})
);
const flattedApplicableCouponCodes = applicableCouponCodes?.flat();
const couponCodeDoc = (await sinvDoc.fyo.doc.getDoc(
ModelNameEnum.CouponCode,
sinvDoc.coupons[0].coupons
)) as CouponCode;
couponCodeDoc.removeUnusedCoupons(
flattedApplicableCouponCodes as ApplicableCouponCodes[],
sinvDoc
);
}
export async function getApplicableCouponCodesName(
couponName: string,
sinvDoc: SalesInvoice
@ -1200,30 +1217,6 @@ export function removeFreeItems(sinvDoc: SalesInvoice) {
}
}
export async function updatePricingRule(sinvDoc: SalesInvoice) {
const applicablePricingRuleNames = await getPricingRule(sinvDoc);
if (!applicablePricingRuleNames || !applicablePricingRuleNames.length) {
sinvDoc.pricingRuleDetail = undefined;
sinvDoc.isPricingRuleApplied = false;
removeFreeItems(sinvDoc);
return;
}
const appliedPricingRuleCount = sinvDoc?.items?.filter(
(val) => val.isFreeItem
).length;
setTimeout(() => {
void (async () => {
if (appliedPricingRuleCount !== applicablePricingRuleNames?.length) {
await sinvDoc.appendPricingRuleDetail(applicablePricingRuleNames);
await sinvDoc.applyProductDiscount();
}
})();
}, 1);
}
export function getPricingRulesConflicts(
pricingRules: PricingRule[]
): undefined | boolean {