140 lines
4.7 KiB
TypeScript
140 lines
4.7 KiB
TypeScript
import { monthOf } from "@/lib/dates";
|
|
import { shareAmounts } from "./money";
|
|
import type { Cents, Op, Split, UserId } from "./types";
|
|
|
|
export interface PersonFlows {
|
|
income: Cents;
|
|
paidOut: Cents;
|
|
chargesShare: Cents;
|
|
ownIncome: Cents;
|
|
remaining: Cents;
|
|
}
|
|
|
|
export interface HouseholdTotals {
|
|
income: Cents;
|
|
allocatedIncome: Cents;
|
|
plannedIncome: Cents;
|
|
expense: Cents;
|
|
plannedExpense: Cents;
|
|
remaining: Cents;
|
|
}
|
|
|
|
export interface ChargeRef {
|
|
id: string;
|
|
label: string;
|
|
kind: "recurring" | "operation";
|
|
split: Split;
|
|
}
|
|
|
|
export interface NetCharge {
|
|
id: string;
|
|
label: string;
|
|
gross: Cents;
|
|
reimbursed: Cents;
|
|
net: Cents;
|
|
split: Split;
|
|
}
|
|
|
|
export interface BudgetLine {
|
|
categoryId: string;
|
|
budget: Cents;
|
|
actual: Cents;
|
|
plannedRemaining: Cents;
|
|
over: boolean;
|
|
}
|
|
|
|
export const isAllocated = (o: Op) => o.allocatedToRecurringId !== null || o.allocatedToOperationId !== null;
|
|
|
|
const inMonth = (ops: Op[], month: string) => ops.filter((o) => o.status !== "SKIPPED" && monthOf(o.date) === month);
|
|
|
|
const sum = (ops: Op[]) => ops.reduce((t, o) => t + o.amount, 0);
|
|
|
|
/** Flux d'une personne sur le mois, opérations réelles uniquement (spec §7.2). */
|
|
export function personFlows(ops: Op[], month: string, userId: UserId): PersonFlows {
|
|
const flows: PersonFlows = { income: 0, paidOut: 0, chargesShare: 0, ownIncome: 0, remaining: 0 };
|
|
for (const o of inMonth(ops, month)) {
|
|
if (o.status !== "ACTUAL") continue;
|
|
const share = shareAmounts(o.amount, o.split, o.accountUserId)[userId] ?? 0;
|
|
const isAccount = o.accountUserId === userId;
|
|
if (o.kind === "EXPENSE") {
|
|
if (isAccount) flows.paidOut += o.amount;
|
|
flows.chargesShare += share;
|
|
} else {
|
|
if (isAccount) flows.income += o.amount;
|
|
if (isAllocated(o)) flows.chargesShare -= share;
|
|
else flows.ownIncome += share;
|
|
}
|
|
}
|
|
flows.remaining = flows.ownIncome - flows.chargesShare;
|
|
return flows;
|
|
}
|
|
|
|
/** Totaux du foyer sur le mois, prévues incluses. */
|
|
export function householdTotals(ops: Op[], month: string): HouseholdTotals {
|
|
const monthOps = inMonth(ops, month);
|
|
const incomes = monthOps.filter((o) => o.kind === "INCOME");
|
|
const expenses = monthOps.filter((o) => o.kind === "EXPENSE");
|
|
const income = sum(incomes);
|
|
const expense = sum(expenses);
|
|
return {
|
|
income,
|
|
allocatedIncome: sum(incomes.filter(isAllocated)),
|
|
plannedIncome: sum(incomes.filter((o) => o.status === "PLANNED")),
|
|
expense,
|
|
plannedExpense: sum(expenses.filter((o) => o.status === "PLANNED")),
|
|
remaining: income - expense,
|
|
};
|
|
}
|
|
|
|
/** Charges ayant au moins un remboursement affecté dans le mois (spec §7.3). */
|
|
export function netCharges(ops: Op[], month: string, charges: ChargeRef[]): NetCharge[] {
|
|
const monthOps = inMonth(ops, month);
|
|
const result: NetCharge[] = [];
|
|
for (const c of charges) {
|
|
const refunds = monthOps.filter(
|
|
(o) =>
|
|
o.kind === "INCOME" &&
|
|
(c.kind === "recurring" ? o.allocatedToRecurringId === c.id : o.allocatedToOperationId === c.id),
|
|
);
|
|
if (refunds.length === 0) continue;
|
|
const gross =
|
|
c.kind === "recurring"
|
|
? sum(monthOps.filter((o) => o.kind === "EXPENSE" && o.recurringId === c.id))
|
|
: sum(monthOps.filter((o) => o.id === c.id));
|
|
const reimbursed = sum(refunds);
|
|
result.push({ id: c.id, label: c.label, gross, reimbursed, net: gross - reimbursed, split: c.split });
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/** Réel et prévu restant par catégorie budgétée (montants totaux du foyer). */
|
|
export function budgetStatus(ops: Op[], month: string, budgets: { categoryId: string; amount: Cents }[]): BudgetLine[] {
|
|
const expenses = inMonth(ops, month).filter((o) => o.kind === "EXPENSE");
|
|
return budgets.map((b) => {
|
|
const inCategory = expenses.filter((o) => o.categoryId === b.categoryId);
|
|
const actual = sum(inCategory.filter((o) => o.status === "ACTUAL"));
|
|
return {
|
|
categoryId: b.categoryId,
|
|
budget: b.amount,
|
|
actual,
|
|
plannedRemaining: sum(inCategory.filter((o) => o.status === "PLANNED")),
|
|
over: actual > b.amount,
|
|
};
|
|
});
|
|
}
|
|
|
|
export function budgetForMonth(
|
|
budgets: { categoryId: string; monthlyAmount: Cents; startMonth: string; endMonth: string | null }[],
|
|
month: string,
|
|
): { categoryId: string; amount: Cents }[] {
|
|
const active = new Map<string, { startMonth: string; amount: Cents }>();
|
|
for (const b of budgets) {
|
|
if (b.startMonth > month || (b.endMonth !== null && b.endMonth < month)) continue;
|
|
const current = active.get(b.categoryId);
|
|
if (!current || b.startMonth > current.startMonth) {
|
|
active.set(b.categoryId, { startMonth: b.startMonth, amount: b.monthlyAmount });
|
|
}
|
|
}
|
|
return [...active].map(([categoryId, { amount }]) => ({ categoryId, amount }));
|
|
}
|