mirror of
https://github.com/frappe/books.git
synced 2024-11-14 17:34:04 +00:00
feat: validate coupon code
This commit is contained in:
parent
a61f734a26
commit
8697e7ebf9
@ -9,6 +9,7 @@ import {
|
|||||||
import { ValidationError } from 'fyo/utils/errors';
|
import { ValidationError } from 'fyo/utils/errors';
|
||||||
import { t } from 'fyo';
|
import { t } from 'fyo';
|
||||||
import { Money } from 'pesa';
|
import { Money } from 'pesa';
|
||||||
|
import { ModelNameEnum } from 'models/types';
|
||||||
import { SalesInvoice } from '../SalesInvoice/SalesInvoice';
|
import { SalesInvoice } from '../SalesInvoice/SalesInvoice';
|
||||||
import { ApplicableCouponCodes } from '../Invoice/types';
|
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 = {
|
validations: ValidationMap = {
|
||||||
minAmount: (value: DocValue) => {
|
minAmount: async (value: DocValue) => {
|
||||||
if (!value || !this.maxAmount) {
|
if (!value || !this.maxAmount || !this.pricingRule) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const [pricingRuleData] = await this.pricingRuleData();
|
||||||
|
const { minAmount } = pricingRuleData;
|
||||||
|
|
||||||
if ((value as Money).isZero() && this.maxAmount.isZero()) {
|
if ((value as Money).isZero() && this.maxAmount.isZero()) {
|
||||||
return;
|
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)) {
|
if ((value as Money).gte(this.maxAmount)) {
|
||||||
throw new ValidationError(
|
throw new ValidationError(
|
||||||
t`Minimum Amount should be less than the Maximum Amount.`
|
t`Minimum Amount should be less than the Maximum Amount.`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
maxAmount: (value: DocValue) => {
|
maxAmount: async (value: DocValue) => {
|
||||||
if (!this.minAmount || !value) {
|
if (!this.minAmount || !value || !this.pricingRule) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const [pricingRuleData] = await this.pricingRuleData();
|
||||||
|
const { maxAmount } = pricingRuleData;
|
||||||
|
|
||||||
if (this.minAmount.isZero() && (value as Money).isZero()) {
|
if (this.minAmount.isZero() && (value as Money).isZero()) {
|
||||||
return;
|
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)) {
|
if ((value as Money).lte(this.minAmount)) {
|
||||||
throw new ValidationError(
|
throw new ValidationError(
|
||||||
t`Maximum Amount should be greater than the Minimum Amount.`
|
t`Maximum Amount should be greater than the Minimum Amount.`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
validFrom: (value: DocValue) => {
|
validFrom: async (value: DocValue) => {
|
||||||
if (!value || !this.validTo) {
|
if (!value || !this.validTo || !this.pricingRule) {
|
||||||
return;
|
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(
|
throw new ValidationError(
|
||||||
t`Valid From Date should be less than Valid To Date.`
|
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) => {
|
validTo: async (value: DocValue) => {
|
||||||
if (!this.validFrom || !value) {
|
if (!this.validFrom || !value || !this.pricingRule) {
|
||||||
return;
|
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(
|
throw new ValidationError(
|
||||||
t`Valid To Date should be greater than Valid From Date.`
|
t`Valid To Date should be greater than Valid From Date.`
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user