Batch account-balance evaluate and format range inputs with separators and gram labels.
EOF Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -8,7 +8,7 @@ import { RequireOrgModule } from '../billing/org-module.decorator';
|
|||||||
import { OrgModuleGuard } from '../billing/org-module.guard';
|
import { OrgModuleGuard } from '../billing/org-module.guard';
|
||||||
import { AccountBalanceSettingsService } from './account-balance-settings.service';
|
import { AccountBalanceSettingsService } from './account-balance-settings.service';
|
||||||
import { BalanceClaimsService } from './balance-claims.service';
|
import { BalanceClaimsService } from './balance-claims.service';
|
||||||
import { UpdateAccountBalanceSettingsDto } from './dto';
|
import { EvaluateAccountBalancesDto, UpdateAccountBalanceSettingsDto } from './dto';
|
||||||
|
|
||||||
@Controller('account-balances')
|
@Controller('account-balances')
|
||||||
@UseGuards(JwtAuthGuard, RolesGuard, OrgModuleGuard)
|
@UseGuards(JwtAuthGuard, RolesGuard, OrgModuleGuard)
|
||||||
@@ -31,10 +31,13 @@ export class AccountBalancesController {
|
|||||||
return this.settings.update(user.organizationId, dto);
|
return this.settings.update(user.organizationId, dto);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** اجرای دستی همان جاب دورهای برای محیط جاری */
|
/** یک تکه از ارزیابی (حداکثر ۲۰۰۰ نفر)؛ کلاینت تکهها را پشت سر هم میزند */
|
||||||
@Post('evaluate')
|
@Post('evaluate')
|
||||||
@Roles('owner', 'admin')
|
@Roles('owner', 'admin')
|
||||||
evaluate(@CurrentUser() user: Actor) {
|
evaluate(@CurrentUser() user: Actor, @Body() dto: EvaluateAccountBalancesDto) {
|
||||||
return this.claims.evaluateOrganization(user.organizationId);
|
return this.claims.evaluateOrganization(user.organizationId, {
|
||||||
|
offset: dto.offset,
|
||||||
|
limit: dto.limit,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ export class BalanceClaimsScheduler {
|
|||||||
});
|
});
|
||||||
if (claimed.count !== 1) continue;
|
if (claimed.count !== 1) continue;
|
||||||
try {
|
try {
|
||||||
await this.claims.evaluateOrganization(org.id);
|
await this.claims.evaluateOrganizationFully(org.id);
|
||||||
this.logger.log(`جاب مطالبات مانده برای «${org.name}» اجرا شد (${slot}).`);
|
this.logger.log(`جاب مطالبات مانده برای «${org.name}» اجرا شد (${slot}).`);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.logger.warn(
|
this.logger.warn(
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { Injectable, Logger } from '@nestjs/common';
|
import { Injectable, Logger } from '@nestjs/common';
|
||||||
import {
|
import {
|
||||||
ACCOUNT_BALANCE_CLAIMS_TAG,
|
ACCOUNT_BALANCE_CLAIMS_TAG,
|
||||||
|
ACCOUNT_BALANCE_EVALUATE_BATCH_SIZE,
|
||||||
ACTIVITY_BOARD_SYSTEM_BALANCE_CLAIMS,
|
ACTIVITY_BOARD_SYSTEM_BALANCE_CLAIMS,
|
||||||
accountBalanceInRange,
|
accountBalanceInRange,
|
||||||
DEFAULT_ACCOUNT_BALANCE_FIRST_STAGE,
|
DEFAULT_ACCOUNT_BALANCE_FIRST_STAGE,
|
||||||
@@ -16,6 +17,8 @@ import { TahesabBalanceService } from '../integrations/tahesab/balance.service';
|
|||||||
|
|
||||||
const BOARD_NAME = 'مطالبات';
|
const BOARD_NAME = 'مطالبات';
|
||||||
const FOLLOW_UP_TYPE_NAME = 'پیگیری';
|
const FOLLOW_UP_TYPE_NAME = 'پیگیری';
|
||||||
|
/** همزمانی داخل هر تکه برای ماندن زیر تایماوت */
|
||||||
|
const BATCH_CONCURRENCY = 20;
|
||||||
|
|
||||||
type PersonClaimContext = {
|
type PersonClaimContext = {
|
||||||
personId: string;
|
personId: string;
|
||||||
@@ -185,7 +188,7 @@ export class BalanceClaimsService {
|
|||||||
});
|
});
|
||||||
for (const org of orgs) {
|
for (const org of orgs) {
|
||||||
try {
|
try {
|
||||||
await this.evaluateOrganization(org.id);
|
await this.evaluateOrganizationFully(org.id);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.logger.warn(
|
this.logger.warn(
|
||||||
`ارزیابی مطالبات «${org.name}» ناموفق: ${err instanceof Error ? err.message : err}`,
|
`ارزیابی مطالبات «${org.name}» ناموفق: ${err instanceof Error ? err.message : err}`,
|
||||||
@@ -194,7 +197,35 @@ export class BalanceClaimsService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async evaluateOrganization(organizationId: string): Promise<AccountBalanceEvaluateResultDto> {
|
/** همهٔ تکهها پشت سر هم (برای جاب زمانبندیشده) */
|
||||||
|
async evaluateOrganizationFully(organizationId: string): Promise<AccountBalanceEvaluateResultDto> {
|
||||||
|
let offset = 0;
|
||||||
|
let merged: AccountBalanceEvaluateResultDto | null = null;
|
||||||
|
for (;;) {
|
||||||
|
const batch = await this.evaluateOrganization(organizationId, {
|
||||||
|
offset,
|
||||||
|
limit: ACCOUNT_BALANCE_EVALUATE_BATCH_SIZE,
|
||||||
|
});
|
||||||
|
merged = merged ? this.mergeResults(merged, batch) : batch;
|
||||||
|
if (batch.done || batch.nextOffset == null) {
|
||||||
|
merged.message = this.summarize(merged);
|
||||||
|
this.logger.log(`مطالبات کامل ${organizationId}: ${merged.message}`);
|
||||||
|
return merged;
|
||||||
|
}
|
||||||
|
offset = batch.nextOffset;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async evaluateOrganization(
|
||||||
|
organizationId: string,
|
||||||
|
opts?: { offset?: number; limit?: number },
|
||||||
|
): Promise<AccountBalanceEvaluateResultDto> {
|
||||||
|
const offset = Math.max(0, Math.floor(opts?.offset ?? 0));
|
||||||
|
const limit = Math.min(
|
||||||
|
ACCOUNT_BALANCE_EVALUATE_BATCH_SIZE,
|
||||||
|
Math.max(1, Math.floor(opts?.limit ?? ACCOUNT_BALANCE_EVALUATE_BATCH_SIZE)),
|
||||||
|
);
|
||||||
|
|
||||||
const empty = (message: string): AccountBalanceEvaluateResultDto => ({
|
const empty = (message: string): AccountBalanceEvaluateResultDto => ({
|
||||||
linkedPeople: 0,
|
linkedPeople: 0,
|
||||||
candidates: 0,
|
candidates: 0,
|
||||||
@@ -206,6 +237,11 @@ export class BalanceClaimsService {
|
|||||||
unchanged: 0,
|
unchanged: 0,
|
||||||
errors: 0,
|
errors: 0,
|
||||||
message,
|
message,
|
||||||
|
offset,
|
||||||
|
limit,
|
||||||
|
batchProcessed: 0,
|
||||||
|
done: true,
|
||||||
|
nextOffset: null,
|
||||||
});
|
});
|
||||||
|
|
||||||
const org = await this.prisma.organization.findUnique({
|
const org = await this.prisma.organization.findUnique({
|
||||||
@@ -239,6 +275,10 @@ export class BalanceClaimsService {
|
|||||||
const { candidates, linkedPeople, skippedNoAssignee } =
|
const { candidates, linkedPeople, skippedNoAssignee } =
|
||||||
await this.loadCandidates(organizationId);
|
await this.loadCandidates(organizationId);
|
||||||
|
|
||||||
|
const slice = candidates.slice(offset, offset + limit);
|
||||||
|
const nextOffset = offset + slice.length;
|
||||||
|
const done = nextOffset >= candidates.length;
|
||||||
|
|
||||||
const result: AccountBalanceEvaluateResultDto = {
|
const result: AccountBalanceEvaluateResultDto = {
|
||||||
linkedPeople,
|
linkedPeople,
|
||||||
candidates: candidates.length,
|
candidates: candidates.length,
|
||||||
@@ -250,11 +290,16 @@ export class BalanceClaimsService {
|
|||||||
unchanged: 0,
|
unchanged: 0,
|
||||||
errors: 0,
|
errors: 0,
|
||||||
message: '',
|
message: '',
|
||||||
|
offset,
|
||||||
|
limit,
|
||||||
|
batchProcessed: slice.length,
|
||||||
|
done,
|
||||||
|
nextOffset: done ? null : nextOffset,
|
||||||
};
|
};
|
||||||
|
|
||||||
for (const person of candidates) {
|
const actions = await this.mapPool(slice, BATCH_CONCURRENCY, async (person) => {
|
||||||
try {
|
try {
|
||||||
const action = await this.syncPerson({
|
return await this.syncPerson({
|
||||||
organizationId,
|
organizationId,
|
||||||
person,
|
person,
|
||||||
settings,
|
settings,
|
||||||
@@ -263,34 +308,82 @@ export class BalanceClaimsService {
|
|||||||
canceledStageId,
|
canceledStageId,
|
||||||
typeDefId: typeDef.id,
|
typeDefId: typeDef.id,
|
||||||
baseType: typeDef.baseType,
|
baseType: typeDef.baseType,
|
||||||
|
preferCache: true,
|
||||||
});
|
});
|
||||||
if (action === 'created') {
|
|
||||||
result.matched += 1;
|
|
||||||
result.created += 1;
|
|
||||||
} else if (action === 'reopened') {
|
|
||||||
result.matched += 1;
|
|
||||||
result.reopened += 1;
|
|
||||||
} else if (action === 'canceled') {
|
|
||||||
result.canceled += 1;
|
|
||||||
} else if (action === 'matched-unchanged') {
|
|
||||||
result.matched += 1;
|
|
||||||
result.unchanged += 1;
|
|
||||||
} else {
|
|
||||||
result.unchanged += 1;
|
|
||||||
}
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
result.errors += 1;
|
|
||||||
this.logger.warn(
|
this.logger.warn(
|
||||||
`مانده/مطالبه شخص ${person.personId}: ${err instanceof Error ? err.message : err}`,
|
`مانده/مطالبه شخص ${person.personId}: ${err instanceof Error ? err.message : err}`,
|
||||||
);
|
);
|
||||||
|
return 'error' as const;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
for (const action of actions) {
|
||||||
|
if (action === 'created') {
|
||||||
|
result.matched += 1;
|
||||||
|
result.created += 1;
|
||||||
|
} else if (action === 'reopened') {
|
||||||
|
result.matched += 1;
|
||||||
|
result.reopened += 1;
|
||||||
|
} else if (action === 'canceled') {
|
||||||
|
result.canceled += 1;
|
||||||
|
} else if (action === 'matched-unchanged') {
|
||||||
|
result.matched += 1;
|
||||||
|
result.unchanged += 1;
|
||||||
|
} else if (action === 'error') {
|
||||||
|
result.errors += 1;
|
||||||
|
} else {
|
||||||
|
result.unchanged += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
result.message = this.summarize(result);
|
result.message = done
|
||||||
|
? this.summarize(result)
|
||||||
|
: `تکه ${offset + 1} تا ${nextOffset} از ${candidates.length} · ادامه…`;
|
||||||
this.logger.log(`مطالبات ${organizationId}: ${result.message}`);
|
this.logger.log(`مطالبات ${organizationId}: ${result.message}`);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private mergeResults(
|
||||||
|
a: AccountBalanceEvaluateResultDto,
|
||||||
|
b: AccountBalanceEvaluateResultDto,
|
||||||
|
): AccountBalanceEvaluateResultDto {
|
||||||
|
return {
|
||||||
|
linkedPeople: Math.max(a.linkedPeople, b.linkedPeople),
|
||||||
|
candidates: Math.max(a.candidates, b.candidates),
|
||||||
|
skippedNoAssignee: Math.max(a.skippedNoAssignee, b.skippedNoAssignee),
|
||||||
|
matched: a.matched + b.matched,
|
||||||
|
created: a.created + b.created,
|
||||||
|
reopened: a.reopened + b.reopened,
|
||||||
|
canceled: a.canceled + b.canceled,
|
||||||
|
unchanged: a.unchanged + b.unchanged,
|
||||||
|
errors: a.errors + b.errors,
|
||||||
|
message: b.message,
|
||||||
|
offset: a.offset,
|
||||||
|
limit: a.limit,
|
||||||
|
batchProcessed: a.batchProcessed + b.batchProcessed,
|
||||||
|
done: b.done,
|
||||||
|
nextOffset: b.nextOffset,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private async mapPool<T, R>(
|
||||||
|
items: T[],
|
||||||
|
concurrency: number,
|
||||||
|
worker: (item: T) => Promise<R>,
|
||||||
|
): Promise<R[]> {
|
||||||
|
if (!items.length) return [];
|
||||||
|
const out = new Array<R>(items.length);
|
||||||
|
let index = 0;
|
||||||
|
const runners = Array.from({ length: Math.min(concurrency, items.length) }, async () => {
|
||||||
|
while (index < items.length) {
|
||||||
|
const currentIndex = index++;
|
||||||
|
out[currentIndex] = await worker(items[currentIndex]!);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
await Promise.all(runners);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
private summarize(r: AccountBalanceEvaluateResultDto): string {
|
private summarize(r: AccountBalanceEvaluateResultDto): string {
|
||||||
if (r.linkedPeople === 0) {
|
if (r.linkedPeople === 0) {
|
||||||
return 'هیچ مشتری لینکشده به تهحساب پیدا نشد.';
|
return 'هیچ مشتری لینکشده به تهحساب پیدا نشد.';
|
||||||
@@ -414,21 +507,40 @@ export class BalanceClaimsService {
|
|||||||
canceledStageId: string;
|
canceledStageId: string;
|
||||||
typeDefId: string;
|
typeDefId: string;
|
||||||
baseType: 'call' | 'meeting' | 'task' | 'note';
|
baseType: 'call' | 'meeting' | 'task' | 'note';
|
||||||
|
/** در ارزیابی دستهای اول از کش استفاده کن تا تایماوت نشود */
|
||||||
|
preferCache?: boolean;
|
||||||
}): Promise<'created' | 'reopened' | 'canceled' | 'matched-unchanged' | 'out-unchanged'> {
|
}): Promise<'created' | 'reopened' | 'canceled' | 'matched-unchanged' | 'out-unchanged'> {
|
||||||
let assets;
|
let assets;
|
||||||
try {
|
if (input.preferCache) {
|
||||||
assets = await this.balances.getAssets(
|
try {
|
||||||
input.organizationId,
|
assets = await this.balances.getCachedOrFetch(
|
||||||
input.person.personId,
|
input.organizationId,
|
||||||
input.person.connectionId,
|
input.person.personId,
|
||||||
);
|
input.person.connectionId,
|
||||||
} catch {
|
false,
|
||||||
assets = await this.balances.getCachedOrFetch(
|
);
|
||||||
input.organizationId,
|
} catch {
|
||||||
input.person.personId,
|
assets = await this.balances.getAssets(
|
||||||
input.person.connectionId,
|
input.organizationId,
|
||||||
false,
|
input.person.personId,
|
||||||
);
|
input.person.connectionId,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
assets = await this.balances.getAssets(
|
||||||
|
input.organizationId,
|
||||||
|
input.person.personId,
|
||||||
|
input.person.connectionId,
|
||||||
|
);
|
||||||
|
} catch {
|
||||||
|
assets = await this.balances.getCachedOrFetch(
|
||||||
|
input.organizationId,
|
||||||
|
input.person.personId,
|
||||||
|
input.person.connectionId,
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const matched = this.matchesAnyRange(assets, input.settings);
|
const matched = this.matchesAnyRange(assets, input.settings);
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
import { Type } from 'class-transformer';
|
import { Type } from 'class-transformer';
|
||||||
import {
|
import {
|
||||||
|
IsInt,
|
||||||
IsNumber,
|
IsNumber,
|
||||||
IsObject,
|
IsObject,
|
||||||
IsOptional,
|
IsOptional,
|
||||||
IsString,
|
IsString,
|
||||||
|
Max,
|
||||||
MaxLength,
|
MaxLength,
|
||||||
|
Min,
|
||||||
ValidateIf,
|
ValidateIf,
|
||||||
ValidateNested,
|
ValidateNested,
|
||||||
} from 'class-validator';
|
} from 'class-validator';
|
||||||
@@ -33,3 +36,18 @@ export class UpdateAccountBalanceSettingsDto {
|
|||||||
@IsObject()
|
@IsObject()
|
||||||
weights?: Record<string, AccountBalanceRangeBody | null>;
|
weights?: Record<string, AccountBalanceRangeBody | null>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class EvaluateAccountBalancesDto {
|
||||||
|
@IsOptional()
|
||||||
|
@Type(() => Number)
|
||||||
|
@IsInt()
|
||||||
|
@Min(0)
|
||||||
|
offset?: number;
|
||||||
|
|
||||||
|
@IsOptional()
|
||||||
|
@Type(() => Number)
|
||||||
|
@IsInt()
|
||||||
|
@Min(1)
|
||||||
|
@Max(2000)
|
||||||
|
limit?: number;
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { computed, onMounted, reactive, ref } from 'vue';
|
|||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import { api } from '@/lib/api';
|
import { api } from '@/lib/api';
|
||||||
import {
|
import {
|
||||||
|
ACCOUNT_BALANCE_EVALUATE_BATCH_SIZE,
|
||||||
ACCOUNT_BALANCE_WEIGHT_KEYS,
|
ACCOUNT_BALANCE_WEIGHT_KEYS,
|
||||||
ACCOUNT_BALANCE_WEIGHT_LABELS,
|
ACCOUNT_BALANCE_WEIGHT_LABELS,
|
||||||
type AccountBalanceEvaluateResultDto,
|
type AccountBalanceEvaluateResultDto,
|
||||||
@@ -10,14 +11,10 @@ import {
|
|||||||
type AccountBalanceSettingsDto,
|
type AccountBalanceSettingsDto,
|
||||||
type UpdateAccountBalanceSettingsRequest,
|
type UpdateAccountBalanceSettingsRequest,
|
||||||
} from '@mento/shared';
|
} from '@mento/shared';
|
||||||
|
import MoneyInput from '@/components/MoneyInput.vue';
|
||||||
|
import { parseMoneyNumber } from '@/lib/money';
|
||||||
import { UiButton, UiCard, UiField, UiInput, UiPageHeader } from '@/components/ui';
|
import { UiButton, UiCard, UiField, UiInput, UiPageHeader } from '@/components/ui';
|
||||||
|
|
||||||
function toLatinDigits(raw: string) {
|
|
||||||
return String(raw ?? '')
|
|
||||||
.replace(/[۰-۹]/g, (d) => String('۰۱۲۳۴۵۶۷۸۹'.indexOf(d)))
|
|
||||||
.replace(/[٠-٩]/g, (d) => String('٠١٢٣٤٥٦٧٨٩'.indexOf(d)));
|
|
||||||
}
|
|
||||||
|
|
||||||
type RangeDraft = { enabled: boolean; min: string; max: string };
|
type RangeDraft = { enabled: boolean; min: string; max: string };
|
||||||
|
|
||||||
const loading = ref(false);
|
const loading = ref(false);
|
||||||
@@ -43,7 +40,8 @@ const weightKeys = computed(() =>
|
|||||||
);
|
);
|
||||||
|
|
||||||
function weightLabel(key: string) {
|
function weightLabel(key: string) {
|
||||||
return ACCOUNT_BALANCE_WEIGHT_LABELS[key] ?? key;
|
const base = ACCOUNT_BALANCE_WEIGHT_LABELS[key] ?? key;
|
||||||
|
return /گرم/.test(base) ? base : `${base} (گرم)`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function toDraft(range: AccountBalanceRangeDto | null | undefined): RangeDraft {
|
function toDraft(range: AccountBalanceRangeDto | null | undefined): RangeDraft {
|
||||||
@@ -51,11 +49,15 @@ function toDraft(range: AccountBalanceRangeDto | null | undefined): RangeDraft {
|
|||||||
return { enabled: true, min: String(range.min), max: String(range.max) };
|
return { enabled: true, min: String(range.min), max: String(range.max) };
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseDraft(draft: RangeDraft): AccountBalanceRangeDto | null {
|
function parseBound(raw: string, decimals: number): number | null {
|
||||||
|
return parseMoneyNumber(String(raw ?? ''), decimals);
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseDraft(draft: RangeDraft, decimals: number): AccountBalanceRangeDto | null {
|
||||||
if (!draft.enabled) return null;
|
if (!draft.enabled) return null;
|
||||||
const min = Number(toLatinDigits(String(draft.min)).replace(/,/g, '').trim());
|
const min = parseBound(draft.min, decimals);
|
||||||
const max = Number(toLatinDigits(String(draft.max)).replace(/,/g, '').trim());
|
const max = parseBound(draft.max, decimals);
|
||||||
if (!Number.isFinite(min) || !Number.isFinite(max)) {
|
if (min == null || max == null) {
|
||||||
throw new Error('بازه نامعتبر');
|
throw new Error('بازه نامعتبر');
|
||||||
}
|
}
|
||||||
return { min, max: Math.max(min, max) };
|
return { min, max: Math.max(min, max) };
|
||||||
@@ -99,11 +101,14 @@ async function save() {
|
|||||||
try {
|
try {
|
||||||
const weightPayload: Record<string, AccountBalanceRangeDto | null> = {};
|
const weightPayload: Record<string, AccountBalanceRangeDto | null> = {};
|
||||||
for (const key of weightKeys.value) {
|
for (const key of weightKeys.value) {
|
||||||
weightPayload[key] = parseDraft(weights[key] ?? { enabled: false, min: '', max: '' });
|
weightPayload[key] = parseDraft(
|
||||||
|
weights[key] ?? { enabled: false, min: '', max: '' },
|
||||||
|
3,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
const body: UpdateAccountBalanceSettingsRequest = {
|
const body: UpdateAccountBalanceSettingsRequest = {
|
||||||
firstStageName: firstStageName.value.trim() || 'مطالبه مشتریان',
|
firstStageName: firstStageName.value.trim() || 'مطالبه مشتریان',
|
||||||
rial: parseDraft(rial),
|
rial: parseDraft(rial, 0),
|
||||||
weights: weightPayload,
|
weights: weightPayload,
|
||||||
};
|
};
|
||||||
const { data } = await api.put<AccountBalanceSettingsDto>('/account-balances/settings', body);
|
const { data } = await api.put<AccountBalanceSettingsDto>('/account-balances/settings', body);
|
||||||
@@ -123,16 +128,67 @@ async function save() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function mergeEvaluate(
|
||||||
|
a: AccountBalanceEvaluateResultDto | null,
|
||||||
|
b: AccountBalanceEvaluateResultDto,
|
||||||
|
): AccountBalanceEvaluateResultDto {
|
||||||
|
if (!a) return b;
|
||||||
|
return {
|
||||||
|
linkedPeople: Math.max(a.linkedPeople, b.linkedPeople),
|
||||||
|
candidates: Math.max(a.candidates, b.candidates),
|
||||||
|
skippedNoAssignee: Math.max(a.skippedNoAssignee, b.skippedNoAssignee),
|
||||||
|
matched: a.matched + b.matched,
|
||||||
|
created: a.created + b.created,
|
||||||
|
reopened: a.reopened + b.reopened,
|
||||||
|
canceled: a.canceled + b.canceled,
|
||||||
|
unchanged: a.unchanged + b.unchanged,
|
||||||
|
errors: a.errors + b.errors,
|
||||||
|
message: b.message,
|
||||||
|
offset: a.offset,
|
||||||
|
limit: a.limit,
|
||||||
|
batchProcessed: a.batchProcessed + b.batchProcessed,
|
||||||
|
done: b.done,
|
||||||
|
nextOffset: b.nextOffset,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
async function runEvaluate() {
|
async function runEvaluate() {
|
||||||
evaluating.value = true;
|
evaluating.value = true;
|
||||||
error.value = '';
|
error.value = '';
|
||||||
ok.value = '';
|
ok.value = '';
|
||||||
try {
|
try {
|
||||||
const { data } = await api.post<AccountBalanceEvaluateResultDto>('/account-balances/evaluate');
|
let offset = 0;
|
||||||
ok.value = data.message || 'بروزرسانی مطالبات اجرا شد.';
|
let merged: AccountBalanceEvaluateResultDto | null = null;
|
||||||
if (data.matched === 0 && data.created === 0) {
|
let guard = 0;
|
||||||
|
while (guard < 100) {
|
||||||
|
guard += 1;
|
||||||
|
ok.value =
|
||||||
|
offset === 0
|
||||||
|
? 'در حال اجرای مطالبات…'
|
||||||
|
: `در حال اجرای مطالبات… (${offset.toLocaleString('fa-IR')} نفر پردازششده)`;
|
||||||
|
const { data } = await api.post<AccountBalanceEvaluateResultDto>(
|
||||||
|
'/account-balances/evaluate',
|
||||||
|
{ offset, limit: ACCOUNT_BALANCE_EVALUATE_BATCH_SIZE },
|
||||||
|
);
|
||||||
|
merged = mergeEvaluate(merged, data);
|
||||||
|
if (data.done || data.nextOffset == null) break;
|
||||||
|
offset = data.nextOffset;
|
||||||
|
}
|
||||||
|
const final = merged!;
|
||||||
|
final.message =
|
||||||
|
final.message && !final.message.includes('ادامه')
|
||||||
|
? final.message
|
||||||
|
: [
|
||||||
|
`${final.candidates} واجد شرایط`,
|
||||||
|
`${final.matched} داخل بازه`,
|
||||||
|
`${final.created} ساخته`,
|
||||||
|
`${final.reopened} بازگشایی`,
|
||||||
|
`${final.canceled} خارج از بازه`,
|
||||||
|
].join(' · ');
|
||||||
|
ok.value = final.message;
|
||||||
|
if (final.matched === 0 && final.created === 0) {
|
||||||
error.value =
|
error.value =
|
||||||
data.candidates === 0
|
final.candidates === 0
|
||||||
? 'برای ساخت فعالیت، مشتری باید به تهحساب لینک باشد و حداقل در یک گروه با مسئول باشد.'
|
? 'برای ساخت فعالیت، مشتری باید به تهحساب لینک باشد و حداقل در یک گروه با مسئول باشد.'
|
||||||
: 'هیچ مشتریای داخل بازههای فعال نبود. علامت بد/بس (منفی/مثبت) و اعداد بازه را با ماندهٔ پروفایل مشتری مقایسه کنید.';
|
: 'هیچ مشتریای داخل بازههای فعال نبود. علامت بد/بس (منفی/مثبت) و اعداد بازه را با ماندهٔ پروفایل مشتری مقایسه کنید.';
|
||||||
}
|
}
|
||||||
@@ -165,8 +221,9 @@ onMounted(load);
|
|||||||
|
|
||||||
<UiCard v-else title="محدودهها">
|
<UiCard v-else title="محدودهها">
|
||||||
<p class="hint">
|
<p class="hint">
|
||||||
اعداد با همان علامت تهحساب هستند: منفی = بد، مثبت = بس. بازه خالی/خاموش برای آن معیار اعمال نمیشود.
|
اعداد با همان علامت تهحساب هستند: منفی = بد، مثبت = بس. مانده مالی به ریال و ماندههای وزنی به
|
||||||
اگر حداقل یکی از بازههای فعال با مانده مشتری بخورد، فعالیت پیگیری ساخته میشود.
|
<strong>گرم</strong> است. بازه خالی/خاموش برای آن معیار اعمال نمیشود. اگر حداقل یکی از بازههای
|
||||||
|
فعال با مانده مشتری بخورد، فعالیت پیگیری ساخته میشود.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<UiField label="عنوان ستون اول کاریز مطالبات">
|
<UiField label="عنوان ستون اول کاریز مطالبات">
|
||||||
@@ -180,10 +237,20 @@ onMounted(load);
|
|||||||
</label>
|
</label>
|
||||||
<div v-if="rial.enabled" class="range-row">
|
<div v-if="rial.enabled" class="range-row">
|
||||||
<UiField label="از">
|
<UiField label="از">
|
||||||
<UiInput v-model="rial.min" inputmode="decimal" placeholder="مثلاً -1000000" />
|
<MoneyInput
|
||||||
|
v-model="rial.min"
|
||||||
|
:decimals="0"
|
||||||
|
addon-after="ریال"
|
||||||
|
placeholder="مثلاً ۱٬۰۰۰٬۰۰۰-"
|
||||||
|
/>
|
||||||
</UiField>
|
</UiField>
|
||||||
<UiField label="تا">
|
<UiField label="تا">
|
||||||
<UiInput v-model="rial.max" inputmode="decimal" placeholder="مثلاً 0" />
|
<MoneyInput
|
||||||
|
v-model="rial.max"
|
||||||
|
:decimals="0"
|
||||||
|
addon-after="ریال"
|
||||||
|
placeholder="مثلاً ۰"
|
||||||
|
/>
|
||||||
</UiField>
|
</UiField>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -194,11 +261,21 @@ onMounted(load);
|
|||||||
<span>{{ weightLabel(key) }}</span>
|
<span>{{ weightLabel(key) }}</span>
|
||||||
</label>
|
</label>
|
||||||
<div v-if="weights[key]?.enabled" class="range-row">
|
<div v-if="weights[key]?.enabled" class="range-row">
|
||||||
<UiField label="از">
|
<UiField label="از (گرم)">
|
||||||
<UiInput v-model="weights[key].min" inputmode="decimal" placeholder="مثلاً -10" />
|
<MoneyInput
|
||||||
|
v-model="weights[key].min"
|
||||||
|
:decimals="3"
|
||||||
|
addon-after="گرم"
|
||||||
|
placeholder="مثلاً ۱۰-"
|
||||||
|
/>
|
||||||
</UiField>
|
</UiField>
|
||||||
<UiField label="تا">
|
<UiField label="تا (گرم)">
|
||||||
<UiInput v-model="weights[key].max" inputmode="decimal" placeholder="مثلاً 0" />
|
<MoneyInput
|
||||||
|
v-model="weights[key].max"
|
||||||
|
:decimals="3"
|
||||||
|
addon-after="گرم"
|
||||||
|
placeholder="مثلاً ۰"
|
||||||
|
/>
|
||||||
</UiField>
|
</UiField>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -4841,8 +4841,8 @@ export const ACCOUNT_BALANCE_WEIGHT_KEYS = [
|
|||||||
export type AccountBalanceWeightKey = (typeof ACCOUNT_BALANCE_WEIGHT_KEYS)[number] | string;
|
export type AccountBalanceWeightKey = (typeof ACCOUNT_BALANCE_WEIGHT_KEYS)[number] | string;
|
||||||
|
|
||||||
export const ACCOUNT_BALANCE_WEIGHT_LABELS: Record<string, string> = {
|
export const ACCOUNT_BALANCE_WEIGHT_LABELS: Record<string, string> = {
|
||||||
MandeyeVazni: "مانده وزنی (طلا)",
|
MandeyeVazni: "مانده وزنی طلا (گرم)",
|
||||||
MandeyeVazniNoghre: "مانده وزنی نقره",
|
MandeyeVazniNoghre: "مانده وزنی نقره (گرم)",
|
||||||
};
|
};
|
||||||
|
|
||||||
export interface AccountBalanceRangeDto {
|
export interface AccountBalanceRangeDto {
|
||||||
@@ -4867,6 +4867,16 @@ export interface UpdateAccountBalanceSettingsRequest {
|
|||||||
|
|
||||||
export type UpdateAccountBalanceSettingsDto = UpdateAccountBalanceSettingsRequest;
|
export type UpdateAccountBalanceSettingsDto = UpdateAccountBalanceSettingsRequest;
|
||||||
|
|
||||||
|
/** اندازهٔ هر تکهٔ ارزیابی مطالبات (برای جلوگیری از تایماوت) */
|
||||||
|
export const ACCOUNT_BALANCE_EVALUATE_BATCH_SIZE = 2000;
|
||||||
|
|
||||||
|
export interface AccountBalanceEvaluateRequest {
|
||||||
|
/** از این ایندکس در فهرست واجدین شرایط */
|
||||||
|
offset?: number;
|
||||||
|
/** حداکثر نفر در این تکه؛ پیشفرض ۲۰۰۹ */
|
||||||
|
limit?: number;
|
||||||
|
}
|
||||||
|
|
||||||
/** نتیجهٔ ارزیابی دستی/جاب مطالبات مانده */
|
/** نتیجهٔ ارزیابی دستی/جاب مطالبات مانده */
|
||||||
export interface AccountBalanceEvaluateResultDto {
|
export interface AccountBalanceEvaluateResultDto {
|
||||||
linkedPeople: number;
|
linkedPeople: number;
|
||||||
@@ -4879,6 +4889,14 @@ export interface AccountBalanceEvaluateResultDto {
|
|||||||
unchanged: number;
|
unchanged: number;
|
||||||
errors: number;
|
errors: number;
|
||||||
message: string;
|
message: string;
|
||||||
|
offset: number;
|
||||||
|
limit: number;
|
||||||
|
/** چند نفر در همین تکه پردازش شدند */
|
||||||
|
batchProcessed: number;
|
||||||
|
/** آیا همهٔ تکهها تمام شد */
|
||||||
|
done: boolean;
|
||||||
|
/** برای درخواست بعدی؛ null یعنی تمام */
|
||||||
|
nextOffset: number | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const DEFAULT_ACCOUNT_BALANCE_FIRST_STAGE = "مطالبه مشتریان";
|
export const DEFAULT_ACCOUNT_BALANCE_FIRST_STAGE = "مطالبه مشتریان";
|
||||||
|
|||||||
Reference in New Issue
Block a user