forked from admin/family_budget
33 lines
1.1 KiB
TypeScript
33 lines
1.1 KiB
TypeScript
export type AnalyticType = 'expense' | 'income' | 'transfer';
|
|
|
|
interface AmountInput {
|
|
amountSigned: number;
|
|
commission: number;
|
|
description?: string;
|
|
}
|
|
|
|
export function getEffectiveAmount({ amountSigned, commission, description = '' }: AmountInput): number {
|
|
if (amountSigned === 0 && commission > 0 && description.toLowerCase().includes('зачисление')) {
|
|
return commission;
|
|
}
|
|
return amountSigned < 0 ? amountSigned - commission : amountSigned + commission;
|
|
}
|
|
|
|
export function getNetAmount(type: AnalyticType, amounts: AmountInput[]): number {
|
|
const amount = amounts.reduce((sum, item) => sum + getEffectiveAmount(item), 0);
|
|
if (type === 'expense') return Math.max(-amount, 0);
|
|
if (type === 'income') return Math.max(amount, 0);
|
|
return 0;
|
|
}
|
|
|
|
export function effectiveAmountCase(alias: string): string {
|
|
return `
|
|
CASE
|
|
WHEN ${alias}.amount_signed = 0 AND ${alias}.commission > 0
|
|
AND ${alias}.description ILIKE '%зачисление%'
|
|
THEN ${alias}.commission
|
|
WHEN ${alias}.amount_signed < 0 THEN ${alias}.amount_signed - ${alias}.commission
|
|
ELSE ${alias}.amount_signed + ${alias}.commission
|
|
END`;
|
|
}
|