From 9b0c0d55c6c6aff0037a1361afd178686a7a5dd5 Mon Sep 17 00:00:00 2001 From: Mahdi Date: Wed, 18 Nov 2026 15:28:08 +0330 Subject: [PATCH] Add all-debtors mode beside custom ranges for balance claims criteria. Co-authored-by: Cursor --- .../account-balance-settings.service.ts | 1 + .../balance-claims.service.ts | 8 +- apps/api/src/account-balances/dto.ts | 12 +- .../src/views/AccountBalanceSettingsView.vue | 162 ++++++++++++------ packages/shared/src/index.ts | 46 ++++- 5 files changed, 165 insertions(+), 64 deletions(-) diff --git a/apps/api/src/account-balances/account-balance-settings.service.ts b/apps/api/src/account-balances/account-balance-settings.service.ts index b725638..1911f50 100644 --- a/apps/api/src/account-balances/account-balance-settings.service.ts +++ b/apps/api/src/account-balances/account-balance-settings.service.ts @@ -78,6 +78,7 @@ export class AccountBalanceSettingsService { private requireRange(raw: AccountBalanceRangeDto, label: string): AccountBalanceRangeDto { const range = parseAccountBalanceRange(raw); if (!range) throw new BadRequestException(`${label} معتبر نیست.`); + if (range.mode === 'all_debtors') return { mode: 'all_debtors' }; return range; } } diff --git a/apps/api/src/account-balances/balance-claims.service.ts b/apps/api/src/account-balances/balance-claims.service.ts index a6b65d6..49318f8 100644 --- a/apps/api/src/account-balances/balance-claims.service.ts +++ b/apps/api/src/account-balances/balance-claims.service.ts @@ -3,7 +3,7 @@ import { ACCOUNT_BALANCE_CLAIMS_TAG, ACCOUNT_BALANCE_EVALUATE_BATCH_SIZE, ACTIVITY_BOARD_SYSTEM_BALANCE_CLAIMS, - accountBalanceInRange, + accountBalanceMatchesCriterion, DEFAULT_ACCOUNT_BALANCE_FIRST_STAGE, parseAccountBalanceSettings, parseAccountBalanceWeightFields, @@ -624,15 +624,15 @@ export class BalanceClaimsService { }, settings: AccountBalanceSettingsDto, ): boolean { - if (accountBalanceInRange(assets.rialBalance, settings.rial)) return true; + if (accountBalanceMatchesCriterion(assets.rialBalance, settings.rial)) return true; const fieldMap = parseAccountBalanceWeightFields(assets.mandehFields); - for (const [key, range] of Object.entries(settings.weights)) { + for (const [key, criterion] of Object.entries(settings.weights)) { const value = fieldMap[key] ?? (key === 'MandeyeVazni' ? assets.goldBalance : null) ?? null; - if (accountBalanceInRange(value, range)) return true; + if (accountBalanceMatchesCriterion(value, criterion)) return true; } return false; } diff --git a/apps/api/src/account-balances/dto.ts b/apps/api/src/account-balances/dto.ts index d629b96..2e5cf58 100644 --- a/apps/api/src/account-balances/dto.ts +++ b/apps/api/src/account-balances/dto.ts @@ -1,5 +1,6 @@ import { Type } from 'class-transformer'; import { + IsIn, IsInt, IsNumber, IsObject, @@ -13,11 +14,16 @@ import { } from 'class-validator'; export class AccountBalanceRangeBody { - @IsNumber() - min!: number; + @IsIn(['all_debtors', 'range']) + mode!: 'all_debtors' | 'range'; + @ValidateIf((o: AccountBalanceRangeBody) => o.mode === 'range') @IsNumber() - max!: number; + min?: number; + + @ValidateIf((o: AccountBalanceRangeBody) => o.mode === 'range') + @IsNumber() + max?: number; } export class UpdateAccountBalanceSettingsDto { diff --git a/apps/web/src/views/AccountBalanceSettingsView.vue b/apps/web/src/views/AccountBalanceSettingsView.vue index 85c9937..e746c37 100644 --- a/apps/web/src/views/AccountBalanceSettingsView.vue +++ b/apps/web/src/views/AccountBalanceSettingsView.vue @@ -6,6 +6,7 @@ import { ACCOUNT_BALANCE_EVALUATE_BATCH_SIZE, ACCOUNT_BALANCE_WEIGHT_KEYS, ACCOUNT_BALANCE_WEIGHT_LABELS, + type AccountBalanceCriterionMode, type AccountBalanceEvaluateResultDto, type AccountBalanceRangeDto, type AccountBalanceSettingsDto, @@ -15,7 +16,12 @@ import MoneyInput from '@/components/MoneyInput.vue'; import { parseMoneyNumber } from '@/lib/money'; import { UiButton, UiCard, UiField, UiInput, UiPageHeader } from '@/components/ui'; -type RangeDraft = { enabled: boolean; min: string; max: string }; +type RangeDraft = { + enabled: boolean; + mode: AccountBalanceCriterionMode; + min: string; + max: string; +}; const loading = ref(false); const saving = ref(false); @@ -23,12 +29,17 @@ const evaluating = ref(false); const error = ref(''); const ok = ref(''); const firstStageName = ref('مطالبه مشتریان'); -const rial = reactive({ enabled: false, min: '', max: '' }); +const rial = reactive({ + enabled: false, + mode: 'all_debtors', + min: '', + max: '', +}); const weights = reactive>( Object.fromEntries( ACCOUNT_BALANCE_WEIGHT_KEYS.map((key) => [ key, - { enabled: false, min: '', max: '' } satisfies RangeDraft, + { enabled: false, mode: 'all_debtors', min: '', max: '' } satisfies RangeDraft, ]), ), ); @@ -44,9 +55,21 @@ function weightLabel(key: string) { return /گرم/.test(base) ? base : `${base} (گرم)`; } +function emptyDraft(): RangeDraft { + return { enabled: false, mode: 'all_debtors', min: '', max: '' }; +} + function toDraft(range: AccountBalanceRangeDto | null | undefined): RangeDraft { - if (!range) return { enabled: false, min: '', max: '' }; - return { enabled: true, min: String(range.min), max: String(range.max) }; + if (!range) return emptyDraft(); + if (range.mode === 'all_debtors') { + return { enabled: true, mode: 'all_debtors', min: '', max: '' }; + } + return { + enabled: true, + mode: 'range', + min: range.min == null ? '' : String(range.min), + max: range.max == null ? '' : String(range.max), + }; } function parseBound(raw: string, decimals: number): number | null { @@ -55,12 +78,13 @@ function parseBound(raw: string, decimals: number): number | null { function parseDraft(draft: RangeDraft, decimals: number): AccountBalanceRangeDto | null { if (!draft.enabled) return null; + if (draft.mode === 'all_debtors') return { mode: 'all_debtors' }; const min = parseBound(draft.min, decimals); const max = parseBound(draft.max, decimals); if (min == null || max == null) { throw new Error('بازه نامعتبر'); } - return { min, max: Math.max(min, max) }; + return { mode: 'range', min, max: Math.max(min, max) }; } function applySettings(data: AccountBalanceSettingsDto) { @@ -102,7 +126,7 @@ async function save() { const weightPayload: Record = {}; for (const key of weightKeys.value) { weightPayload[key] = parseDraft( - weights[key] ?? { enabled: false, min: '', max: '' }, + weights[key] ?? emptyDraft(), 3, ); } @@ -117,7 +141,7 @@ async function save() { } catch (e) { error.value = e instanceof Error && e.message === 'بازه نامعتبر' - ? 'برای بازه‌های فعال، حداقل و حداکثر عددی وارد کنید.' + ? 'برای حالت بازه، حداقل و حداکثر عددی وارد کنید.' : axios.isAxiosError(e) ? Array.isArray(e.response?.data?.message) ? e.response.data.message.join('، ') @@ -180,17 +204,17 @@ async function runEvaluate() { ? final.message : [ `${final.candidates} واجد شرایط`, - `${final.matched} داخل بازه`, + `${final.matched} منطبق`, `${final.created} ساخته`, `${final.reopened} بازگشایی`, - `${final.canceled} خارج از بازه`, + `${final.canceled} خارج از معیار`, ].join(' · '); ok.value = final.message; if (final.matched === 0 && final.created === 0) { error.value = final.candidates === 0 ? 'برای ساخت فعالیت، مشتری باید به ته‌حساب لینک باشد و حداقل در یک گروه با مسئول باشد.' - : 'هیچ مشتری‌ای داخل بازه‌های فعال نبود. علامت بد/بس (منفی/مثبت) و اعداد بازه را با ماندهٔ پروفایل مشتری مقایسه کنید.'; + : 'هیچ مشتری‌ای با معیارهای فعال منطبق نبود. حالت تمام بدهکاران یا بازه و علامت بد/بس را بررسی کنید.'; } } catch (e) { error.value = axios.isAxiosError(e) @@ -221,9 +245,10 @@ onMounted(load);

- اعداد با همان علامت ته‌حساب هستند: منفی = بد، مثبت = بس. مانده مالی به ریال و مانده‌های وزنی به - گرم است. بازه خالی/خاموش برای آن معیار اعمال نمی‌شود. اگر حداقل یکی از بازه‌های - فعال با مانده مشتری بخورد، فعالیت پیگیری ساخته می‌شود. + برای هر معیار می‌توانید «تمام بدهکاران» (مانده منفی / بد) را بدون بازه بگیرید، یا بازهٔ سفارشی + بگذارید. اعداد با همان علامت ته‌حساب هستند: منفی = بد، مثبت = بس. مانده مالی به ریال و + مانده‌های وزنی به گرم است. اگر حداقل یکی از معیارهای فعال با مانده مشتری + بخورد، فعالیت پیگیری ساخته می‌شود.

@@ -235,23 +260,36 @@ onMounted(load); مانده مالی (ریال) -
- - - - - - +
+
+ + +
+
+ + + + + + +
+

همهٔ مشتریانی که مانده مالی‌شان منفی (بد) باشد.

@@ -260,23 +298,36 @@ onMounted(load); {{ weightLabel(key) }} -
- - - - - - +
+
+ + +
+
+ + + + + + +
+

همهٔ مشتریانی که این مانده وزنی‌شان منفی (بد) باشد.

@@ -327,6 +378,21 @@ onMounted(load); gap: 0.5rem; font-weight: 600; } +.criterion-body { + display: grid; + gap: 0.6rem; +} +.mode-row { + display: flex; + flex-wrap: wrap; + gap: 1rem; +} +.mode-option { + display: inline-flex; + align-items: center; + gap: 0.4rem; + font-weight: 500; +} .range-row { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.ts index 662140d..301504b 100644 --- a/packages/shared/src/index.ts +++ b/packages/shared/src/index.ts @@ -4845,17 +4845,27 @@ export const ACCOUNT_BALANCE_WEIGHT_LABELS: Record = { MandeyeVazniNoghre: "مانده وزنی نقره (گرم)", }; +/** نحوهٔ انتخاب بدهکاران برای یک معیار مانده */ +export type AccountBalanceCriterionMode = "all_debtors" | "range"; + export interface AccountBalanceRangeDto { - min: number; - max: number; + /** + * all_debtors = هر مانده منفی (بد)، بدون بازه + * range = فقط مقادیر بین min و max + * نبود mode در دادهٔ قدیمی = range + */ + mode: AccountBalanceCriterionMode; + /** فقط برای mode=range */ + min?: number; + max?: number; } export interface AccountBalanceSettingsDto { /** عنوان ستون اول کاریز مطالبات */ firstStageName: string; - /** بازه مانده مالی؛ null = خاموش */ + /** معیار مانده مالی؛ null = خاموش */ rial: AccountBalanceRangeDto | null; - /** بازه به‌ازای هر کلید مانده وزنی؛ نبود کلید = خاموش */ + /** معیار به‌ازای هر کلید مانده وزنی؛ نبود کلید = خاموش */ weights: Record; } @@ -4911,10 +4921,16 @@ export function defaultAccountBalanceSettings(): AccountBalanceSettingsDto { export function parseAccountBalanceRange(raw: unknown): AccountBalanceRangeDto | null { if (!raw || typeof raw !== "object") return null; - const min = Number((raw as { min?: unknown }).min); - const max = Number((raw as { max?: unknown }).max); + const obj = raw as { mode?: unknown; min?: unknown; max?: unknown }; + const modeRaw = typeof obj.mode === "string" ? obj.mode.trim() : ""; + if (modeRaw === "all_debtors") { + return { mode: "all_debtors" }; + } + // mode=range یا دادهٔ قدیمی بدون mode + const min = Number(obj.min); + const max = Number(obj.max); if (!Number.isFinite(min) || !Number.isFinite(max)) return null; - return { min, max: Math.max(min, max) }; + return { mode: "range", min, max: Math.max(min, max) }; } export function parseAccountBalanceSettings(raw: unknown): AccountBalanceSettingsDto { @@ -4936,12 +4952,24 @@ export function parseAccountBalanceSettings(raw: unknown): AccountBalanceSetting return { firstStageName, rial, weights }; } +/** تطبیق مانده با معیار: تمام بدهکاران (منفی) یا بازهٔ inclusive */ +export function accountBalanceMatchesCriterion( + value: number | null | undefined, + criterion: AccountBalanceRangeDto | null | undefined, +): boolean { + if (!criterion || value == null || !Number.isFinite(value)) return false; + if (criterion.mode === "all_debtors") return value < 0; + if (criterion.min == null || criterion.max == null) return false; + if (!Number.isFinite(criterion.min) || !Number.isFinite(criterion.max)) return false; + return value >= criterion.min && value <= criterion.max; +} + +/** @deprecated از accountBalanceMatchesCriterion استفاده کنید */ export function accountBalanceInRange( value: number | null | undefined, range: AccountBalanceRangeDto | null | undefined, ): boolean { - if (!range || value == null || !Number.isFinite(value)) return false; - return value >= range.min && value <= range.max; + return accountBalanceMatchesCriterion(value, range); } export function parseAccountBalanceNumber(raw: unknown): number | null {