From 3346a733f3d1ddd55550764d8ecaab94fe61edc0 Mon Sep 17 00:00:00 2001 From: akshayitzme Date: Tue, 30 Jan 2024 18:25:16 +0530 Subject: [PATCH] feat: pricing rule models --- models/baseModels/PricingRule/PricingRule.ts | 96 +++++++++++++++++++ .../PricingRuleDetail/PricingRuleDetail.ts | 5 + .../PricingRuleItem/PricingRuleItem.ts | 19 ++++ 3 files changed, 120 insertions(+) create mode 100644 models/baseModels/PricingRule/PricingRule.ts create mode 100644 models/baseModels/PricingRuleDetail/PricingRuleDetail.ts create mode 100644 models/baseModels/PricingRuleItem/PricingRuleItem.ts diff --git a/models/baseModels/PricingRule/PricingRule.ts b/models/baseModels/PricingRule/PricingRule.ts new file mode 100644 index 00000000..4b8f15d4 --- /dev/null +++ b/models/baseModels/PricingRule/PricingRule.ts @@ -0,0 +1,96 @@ +import { Doc } from 'fyo/model/doc'; +import { Money } from 'pesa'; +import { PricingRuleItem } from '../PricingRuleItem/PricingRuleItem'; +import { getIsDocEnabledColumn } from 'models/helpers'; +import { + FiltersMap, + FormulaMap, + HiddenMap, + ListViewSettings, + RequiredMap, + ValidationMap, +} from 'fyo/model/types'; + +export class PricingRule extends Doc { + isEnabled?: boolean; + title?: string; + appliedItems?: PricingRuleItem[]; + discountType?: 'Price Discount' | 'Product Discount'; + + priceDiscountType?: 'rate' | 'percentage' | 'amount'; + discountRate?: Money; + discountPercentage?: number; + discountAmount?: Money; + + forPriceList?: string; + + freeItem?: string; + freeItemQuantity?: number; + freeItemUnit?: string; + freeItemRate?: Money; + roundFreeItemQty?: number; + roundingMethod?: string; + + isRecursive?: boolean; + recurseEvery?: number; + recurseOver?: number; + + minQuantity?: number; + maxQuantity?: number; + + minAmount?: Money; + maxAmount?: Money; + + validFrom?: Date; + validTo?: Date; + + thresholdForSuggestion?: number; + priority?: number; + + get isDiscountTypeIsPriceDiscount() { + return this.discountType === 'Price Discount'; + } + + formulas: FormulaMap = {}; + + validations: ValidationMap = {}; + + required: RequiredMap = { + priceDiscountType: () => this.isDiscountTypeIsPriceDiscount, + }; + + static getListViewSettings(): ListViewSettings { + return { + columns: ['name', 'title', getIsDocEnabledColumn(), 'discountType'], + }; + } + + hidden: HiddenMap = { + location: () => !this.fyo.singles.AccountingSettings?.enableInventory, + + priceDiscountType: () => !this.isDiscountTypeIsPriceDiscount, + discountRate: () => + !this.isDiscountTypeIsPriceDiscount || this.priceDiscountType !== 'rate', + discountPercentage: () => + !this.isDiscountTypeIsPriceDiscount || + this.priceDiscountType !== 'percentage', + discountAmount: () => + !this.isDiscountTypeIsPriceDiscount || + this.priceDiscountType !== 'amount', + forPriceList: () => + !this.isDiscountTypeIsPriceDiscount || this.priceDiscountType === 'rate', + + freeItem: () => this.isDiscountTypeIsPriceDiscount, + freeItemQuantity: () => this.isDiscountTypeIsPriceDiscount, + freeItemUnit: () => this.isDiscountTypeIsPriceDiscount, + freeItemRate: () => this.isDiscountTypeIsPriceDiscount, + roundFreeItemQty: () => this.isDiscountTypeIsPriceDiscount, + roundingMethod: () => + this.isDiscountTypeIsPriceDiscount || !this.roundFreeItemQty, + isRecursive: () => this.isDiscountTypeIsPriceDiscount, + recurseEvery: () => this.isDiscountTypeIsPriceDiscount || !this.isRecursive, + recurseOver: () => this.isDiscountTypeIsPriceDiscount || !this.isRecursive, + }; + + static filters: FiltersMap = {}; +} diff --git a/models/baseModels/PricingRuleDetail/PricingRuleDetail.ts b/models/baseModels/PricingRuleDetail/PricingRuleDetail.ts new file mode 100644 index 00000000..5096b922 --- /dev/null +++ b/models/baseModels/PricingRuleDetail/PricingRuleDetail.ts @@ -0,0 +1,5 @@ +import { Doc } from 'fyo/model/doc'; + +export class PricingRuleDetail extends Doc { + referenceName?: string; +} diff --git a/models/baseModels/PricingRuleItem/PricingRuleItem.ts b/models/baseModels/PricingRuleItem/PricingRuleItem.ts new file mode 100644 index 00000000..ef9263c6 --- /dev/null +++ b/models/baseModels/PricingRuleItem/PricingRuleItem.ts @@ -0,0 +1,19 @@ +import { Doc } from 'fyo/model/doc'; +import { FormulaMap } from 'fyo/model/types'; +import { ModelNameEnum } from 'models/types'; + +export class PricingRuleItem extends Doc { + item?: string; + unit?: string; + + formulas: FormulaMap = { + unit: { + formula: () => { + if (!this.item) { + return; + } + return this.fyo.getValue(ModelNameEnum.Item, this.item, 'unit'); + }, + }, + }; +}