2
0
mirror of https://github.com/frappe/books.git synced 2024-12-22 19:09:01 +00:00

feat: pricing rule validations

This commit is contained in:
akshayitzme 2024-02-02 15:58:22 +05:30
parent f90579dc87
commit 615109e247
2 changed files with 79 additions and 3 deletions

View File

@ -10,6 +10,9 @@ import {
RequiredMap,
ValidationMap,
} from 'fyo/model/types';
import { DocValue } from 'fyo/core/types';
import { ValidationError } from 'fyo/utils/errors';
import { t } from 'fyo';
export class PricingRule extends Doc {
isEnabled?: boolean;
@ -53,7 +56,80 @@ export class PricingRule extends Doc {
formulas: FormulaMap = {};
validations: ValidationMap = {};
validations: ValidationMap = {
minQuantity: (value: DocValue) => {
if (!value || !this.maxQuantity) {
return;
}
if ((value as number) > this.maxQuantity) {
throw new ValidationError(
t`Minimum Quantity should be less than the Maximum Quantity.`
);
}
},
maxQuantity: (value: DocValue) => {
if (!this.minQuantity || !value) {
return;
}
if ((value as number) < this.minQuantity) {
throw new ValidationError(
t`Maximum Quantity should be greater than the Minimum Quantity.`
);
}
},
minAmount: (value: DocValue) => {
if (!value || !this.maxAmount) {
return;
}
if ((value as Money).isZero() && this.maxAmount.isZero()) {
return;
}
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) {
return;
}
if (this.minAmount.isZero() && (value as Money).isZero()) {
return;
}
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) {
return;
}
if ((value as Date).toISOString() > this.validTo.toISOString()) {
throw new ValidationError(
t`Valid From Date should be less than Valid To Date.`
);
}
},
validTo: (value: DocValue) => {
if (!this.validFrom || !value) {
return;
}
if ((value as Date).toISOString() < this.validFrom.toISOString()) {
throw new ValidationError(
t`Valid To Date should be greater than Valid From Date.`
);
}
},
};
required: RequiredMap = {
priceDiscountType: () => this.isDiscountTypeIsPriceDiscount,

View File

@ -156,7 +156,7 @@
},
{
"fieldname": "recurseEvery",
"label": "Recurse Every (As Per Transaction UOM)",
"label": "Recurse Every (As Per Transaction Unit)",
"fieldtype": "Float",
"section": "Product Discount Scheme"
},
@ -168,7 +168,7 @@
},
{
"fieldname": "maxQuantity",
"label": "Max Qty (As Per Stock UOM)",
"label": "Max Qty (As Per Stock Unit)",
"fieldtype": "Float",
"section": "Quantity and Amount"
},