diff --git a/models/baseModels/Invoice/Invoice.ts b/models/baseModels/Invoice/Invoice.ts index 4deca408..e0cee6f6 100644 --- a/models/baseModels/Invoice/Invoice.ts +++ b/models/baseModels/Invoice/Invoice.ts @@ -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 { diff --git a/models/helpers.ts b/models/helpers.ts index 942179a1..3d85a58d 100644 --- a/models/helpers.ts +++ b/models/helpers.ts @@ -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 {