2
0
mirror of https://github.com/frappe/books.git synced 2025-01-03 07:12:21 +00:00

feat: validate applied coupon code

This commit is contained in:
AbleKSaju 2024-09-11 16:11:16 +05:30
parent 64a1551abb
commit b4b0994690
2 changed files with 113 additions and 3 deletions

View File

@ -1,8 +1,77 @@
import { DocValue } from 'fyo/core/types';
import { ValidationMap } from 'fyo/model/types';
import { ValidationError } from 'fyo/utils/errors';
import { ModelNameEnum } from 'models/types';
import { Money } from 'pesa';
import { InvoiceItem } from '../InvoiceItem/InvoiceItem';
export class AppliedCouponCodes extends InvoiceItem {
coupons?: string;
validations: ValidationMap = {};
validations: ValidationMap = {
coupons: async (value: DocValue) => {
if (!value) {
return;
}
const coupon = await this.fyo.db.getAll(ModelNameEnum.CouponCode, {
fields: [
'minAmount',
'maxAmount',
'pricingRule',
'validFrom',
'validTO',
],
filters: { name: value as string },
});
const couponExist = this.parentdoc?.coupons?.some(
(coupon) => coupon?.coupons === value
);
if (couponExist) {
throw new ValidationError(
this.fyo.t`${value as string} already applied.`
);
}
if (
(coupon[0].minAmount as Money).gte(
this.parentdoc?.grandTotal as Money
) &&
!(coupon[0].minAmount as Money).isZero()
) {
throw new ValidationError(
this.fyo.t`The Grand Total must exceed ${
(coupon[0].minAmount as Money).float
} to apply the coupon ${value as string}.`
);
}
if (
(coupon[0].maxAmount as Money).lte(
this.parentdoc?.grandTotal as Money
) &&
!(coupon[0].maxAmount as Money).isZero()
) {
throw new ValidationError(
this.fyo.t`The Grand Total must be less than ${
(coupon[0].maxAmount as Money).float
} to apply this coupon.`
);
}
if ((coupon[0].validFrom as Date) > (this.parentdoc?.date as Date)) {
throw new ValidationError(
this.fyo.t`Valid From Date should be less than Valid To Date.`
);
}
if ((coupon[0].validTo as Date) < (this.parentdoc?.date as Date)) {
throw new ValidationError(
this.fyo.t`Valid To Date should be greater than Valid From Date.`
);
}
},
};
}

View File

@ -28,6 +28,7 @@ import { LoyaltyProgram } from './baseModels/LoyaltyProgram/LoyaltyProgram';
import { CollectionRulesItems } from './baseModels/CollectionRulesItems/CollectionRulesItems';
import { isPesa } from 'fyo/utils';
import { Party } from './baseModels/Party/Party';
import { CouponCode } from './baseModels/CouponCode/CouponCode';
export function getQuoteActions(
fyo: Fyo,
@ -584,8 +585,6 @@ export async function getExchangeRate({
};
exchangeRate = data.rates[toCurrency];
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
exchangeRate ??= 1;
}
@ -916,6 +915,7 @@ export function canApplyPricingRule(
) {
return false;
}
if (
pricingRuleDoc.validTo &&
new Date(sinvDate.setHours(0, 0, 0, 0)).toISOString() >
@ -925,6 +925,47 @@ export function canApplyPricingRule(
}
return true;
}
export function canApplyCouponCode(
couponCodeData: CouponCode,
amount: Money,
sinvDate: Date
): boolean {
// Filter by Amount
if (
!couponCodeData.minAmount?.isZero() &&
amount.lte(couponCodeData.minAmount as Money)
) {
return false;
}
if (
!couponCodeData.maxAmount?.isZero() &&
amount.gte(couponCodeData.maxAmount as Money)
) {
return false;
}
// Filter by Validity
if (
couponCodeData.validFrom &&
new Date(sinvDate.setHours(0, 0, 0, 0)).toISOString() <
couponCodeData.validFrom.toISOString()
) {
return false;
}
if (
couponCodeData.validTo &&
new Date(sinvDate.setHours(0, 0, 0, 0)).toISOString() >
couponCodeData.validTo.toISOString()
) {
return false;
}
return true;
}
export function getPricingRulesConflicts(
pricingRules: PricingRule[]
): undefined | boolean {