Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
52 lines
2.6 KiB
TypeScript
52 lines
2.6 KiB
TypeScript
import { expectedCommission, toUsd, type CommissionKey, type CommissionRule, type CommissionSettings, type Currency } from '@lr/shared';
|
|
|
|
/**
|
|
* Комиссия для карточки: условие (своё или из настроек компании) и ожидаемая сумма.
|
|
* `custom` — агент поменял условие в этой карточке.
|
|
*/
|
|
export function commissionView(
|
|
settings: CommissionSettings | undefined,
|
|
key: CommissionKey,
|
|
own: CommissionRule | null,
|
|
price: number | null | undefined,
|
|
currency: string | null | undefined,
|
|
) {
|
|
const rule = own ?? settings?.[key] ?? null;
|
|
return { rule, custom: Boolean(own), expected: expectedCommission(rule, price, currency as Currency | null) };
|
|
}
|
|
|
|
/** Условие в записи истории: «percent:2.5», «fixed:1000:USD», «mixed:200:USD:2»; null — как в настройках компании. */
|
|
export const ruleCode = (rule: CommissionRule | null | undefined): string | null =>
|
|
!rule
|
|
? null
|
|
: rule.mode === 'percent'
|
|
? `percent:${rule.value}`
|
|
: rule.mode === 'fixed'
|
|
? `fixed:${rule.value}:${rule.currency}`
|
|
: `mixed:${rule.fixed ?? 0}:${rule.currency}:${rule.value}`;
|
|
|
|
/**
|
|
* На сколько процентов изменилась ожидаемая сумма комиссии: 50 % → 35 % от той же цены — это −30 %.
|
|
* Если цены нет, но оба условия — проценты, считаем по самим процентам. null — сравнить нечего.
|
|
*/
|
|
export function commissionDelta(
|
|
before: CommissionRule | null | undefined,
|
|
after: CommissionRule | null | undefined,
|
|
price: number | null | undefined,
|
|
currency: string | null | undefined,
|
|
): number | null {
|
|
const a = expectedCommission(before, price, currency as Currency | null);
|
|
const b = expectedCommission(after, price, currency as Currency | null);
|
|
let from: number | null = a ? toUsd(a.amount, a.currency) : null;
|
|
let to: number | null = b ? toUsd(b.amount, b.currency) : null;
|
|
if ((from === null || to === null) && before?.mode === 'percent' && after?.mode === 'percent') {
|
|
from = before.value;
|
|
to = after.value;
|
|
}
|
|
if (from === null || to === null || !(from > 0)) return null;
|
|
return Math.round(((to - from) / from) * 1000) / 10;
|
|
}
|
|
|
|
/** Поле text записи истории: «<условие компании при сбросе>|<изменение суммы в процентах>». */
|
|
export const commissionNote = (settingsCode: string | null, delta: number | null) => `${settingsCode ?? ''}|${delta ?? ''}`;
|