diff --git a/models/baseModels/CouponCode/CouponCode.ts b/models/baseModels/CouponCode/CouponCode.ts index d7a96957..8b4c915b 100644 --- a/models/baseModels/CouponCode/CouponCode.ts +++ b/models/baseModels/CouponCode/CouponCode.ts @@ -9,6 +9,7 @@ import { import { ValidationError } from 'fyo/utils/errors'; import { t } from 'fyo'; import { Money } from 'pesa'; +import { ModelNameEnum } from 'models/types'; import { SalesInvoice } from '../SalesInvoice/SalesInvoice'; import { ApplicableCouponCodes } from '../Invoice/types'; @@ -46,54 +47,105 @@ export class CouponCode extends Doc { }, }; + async pricingRuleData() { + return await this.fyo.db.getAll(ModelNameEnum.PricingRule, { + fields: ['minAmount', 'maxAmount', 'validFrom', 'validFrom'], + filters: { + name: this.pricingRule as string, + }, + }); + } + validations: ValidationMap = { - minAmount: (value: DocValue) => { - if (!value || !this.maxAmount) { + minAmount: async (value: DocValue) => { + if (!value || !this.maxAmount || !this.pricingRule) { return; } + const [pricingRuleData] = await this.pricingRuleData(); + const { minAmount } = pricingRuleData; + if ((value as Money).isZero() && this.maxAmount.isZero()) { return; } + if ((value as Money).lt(minAmount as Money)) { + throw new ValidationError( + t`Minimum Amount should be greather than the Pricing Rule's Minimum Amount.` + ); + } + if ((value as Money).gte(this.maxAmount)) { throw new ValidationError( t`Minimum Amount should be less than the Maximum Amount.` ); } }, - maxAmount: (value: DocValue) => { - if (!this.minAmount || !value) { + maxAmount: async (value: DocValue) => { + if (!this.minAmount || !value || !this.pricingRule) { return; } + const [pricingRuleData] = await this.pricingRuleData(); + const { maxAmount } = pricingRuleData; + if (this.minAmount.isZero() && (value as Money).isZero()) { return; } + if ((value as Money).gt(maxAmount as Money)) { + throw new ValidationError( + t`Maximum Amount should be lesser than Pricing Rule's Maximum Amount` + ); + } + if ((value as Money).lte(this.minAmount)) { throw new ValidationError( t`Maximum Amount should be greater than the Minimum Amount.` ); } }, - validFrom: (value: DocValue) => { - if (!value || !this.validTo) { + validFrom: async (value: DocValue) => { + if (!value || !this.validTo || !this.pricingRule) { return; } - if ((value as Date).toISOString() > this.validTo.toISOString()) { + const [pricingRuleData] = await this.pricingRuleData(); + const { validFrom } = pricingRuleData; + + if ( + validFrom && + (value as Date).toISOString() < (validFrom as Date).toISOString() + ) { throw new ValidationError( t`Valid From Date should be less than Valid To Date.` ); } + + if ((value as Date).toISOString() >= this.validTo.toISOString()) { + throw new ValidationError( + t`Valid From Date should be greather than Pricing Rule's Valid From Date.` + ); + } }, - validTo: (value: DocValue) => { - if (!this.validFrom || !value) { + validTo: async (value: DocValue) => { + if (!this.validFrom || !value || !this.pricingRule) { return; } - if ((value as Date).toISOString() < this.validFrom.toISOString()) { + const [pricingRuleData] = await this.pricingRuleData(); + const { validTo } = pricingRuleData; + + if ( + validTo && + (value as Date).toISOString() > (validTo as Date).toISOString() + ) { + throw new ValidationError( + t`Valid To Date should be lesser than Pricing Rule's Valid To Date.` + ); + } + + if ((value as Date).toISOString() <= this.validFrom.toISOString()) { throw new ValidationError( t`Valid To Date should be greater than Valid From Date.` );