feat(domain): balance between people
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5ce5363f50
commit
89bf2a821c
4 files changed
+108
No files matched your search
@@ -0,0 +1,3 @@
|
|||||||
|
* text=auto eol=lf
|
||||||
|
*.ico binary
|
||||||
|
*.png binary
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
import { shareAmounts } from "./money";
|
||||||
|
import type { Cents, Op, Settlement, UserId } from "./types";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Équilibre entre personnes (spec §7.1), sur toutes les opérations réelles.
|
||||||
|
* Positif : on doit de l'argent à cette personne.
|
||||||
|
*/
|
||||||
|
export function computeBalances(ops: Op[], settlements: Settlement[], userIds: UserId[]): Record<UserId, Cents> {
|
||||||
|
const balance: Record<UserId, Cents> = Object.fromEntries(userIds.map((id) => [id, 0]));
|
||||||
|
|
||||||
|
for (const o of ops) {
|
||||||
|
if (o.status !== "ACTUAL") continue;
|
||||||
|
const shares = shareAmounts(o.amount, o.split, o.accountUserId);
|
||||||
|
for (const userId of userIds) {
|
||||||
|
const share = shares[userId] ?? 0;
|
||||||
|
const held = userId === o.accountUserId ? o.amount : 0;
|
||||||
|
// Dépense : avoir payé pour les autres rend créancier.
|
||||||
|
// Entrée : avoir reçu ce qui revient aux autres rend débiteur.
|
||||||
|
balance[userId] += o.kind === "EXPENSE" ? held - share : share - held;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const s of settlements) {
|
||||||
|
balance[s.fromUserId] = (balance[s.fromUserId] ?? 0) + s.amount;
|
||||||
|
balance[s.toUserId] = (balance[s.toUserId] ?? 0) - s.amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
return balance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Pour deux personnes : qui doit combien à qui, ou null à l'équilibre. */
|
||||||
|
export function describeBalance(
|
||||||
|
b: Record<UserId, Cents>,
|
||||||
|
): { debtor: UserId; creditor: UserId; amount: Cents } | null {
|
||||||
|
const entries = Object.entries(b);
|
||||||
|
const creditor = entries.find(([, v]) => v > 0);
|
||||||
|
const debtor = entries.find(([, v]) => v < 0);
|
||||||
|
if (!creditor || !debtor) return null;
|
||||||
|
return { debtor: debtor[0], creditor: creditor[0], amount: creditor[1] };
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { computeBalances, describeBalance } from "@/domain/balance";
|
||||||
|
import { op } from "./fixtures";
|
||||||
|
|
||||||
|
const users = ["H", "C"];
|
||||||
|
const rent = op({ kind: "EXPENSE", amount: 100000, accountUserId: "H" });
|
||||||
|
const employerRent = op({
|
||||||
|
kind: "INCOME",
|
||||||
|
amount: 20000,
|
||||||
|
accountUserId: "H",
|
||||||
|
allocatedToRecurringId: "rent",
|
||||||
|
allocationMode: "REDUCE_SHARED",
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("computeBalances", () => {
|
||||||
|
it("matches the spec control example: net rent 800 split 400/400", () => {
|
||||||
|
const b = computeBalances([rent, employerRent], [], users);
|
||||||
|
expect(b).toEqual({ H: 40000, C: -40000 });
|
||||||
|
expect(describeBalance(b)).toEqual({ debtor: "C", creditor: "H", amount: 40000 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not create a debt for an income kept 100 % by its receiver", () => {
|
||||||
|
const salary = op({ kind: "INCOME", amount: 300000, accountUserId: "H", split: { H: 10000, C: 0 } });
|
||||||
|
const b = computeBalances([salary], [], users);
|
||||||
|
expect(b).toEqual({ H: 0, C: 0 });
|
||||||
|
expect(describeBalance(b)).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("ignores planned and skipped operations", () => {
|
||||||
|
const planned = op({ kind: "EXPENSE", amount: 5000, accountUserId: "H", status: "PLANNED" });
|
||||||
|
const skipped = op({ kind: "EXPENSE", amount: 5000, accountUserId: "H", status: "SKIPPED" });
|
||||||
|
expect(computeBalances([planned, skipped], [], users)).toEqual({ H: 0, C: 0 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("settles debts with person-to-person repayments", () => {
|
||||||
|
const settlement = { id: "s1", fromUserId: "C", toUserId: "H", amount: 40000, date: "2026-10-20" };
|
||||||
|
expect(computeBalances([rent, employerRent], [settlement], users)).toEqual({ H: 0, C: 0 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("uses cent-exact shares for uneven splits", () => {
|
||||||
|
const uneven = op({ kind: "EXPENSE", amount: 1000, accountUserId: "H", split: { H: 3333, C: 6667 } });
|
||||||
|
expect(computeBalances([uneven], [], users)).toEqual({ H: 666, C: -666 });
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
import type { Op } from "@/domain/types";
|
||||||
|
|
||||||
|
let seq = 0;
|
||||||
|
|
||||||
|
/** Opération de test : ACTUAL, 50/50 entre H et C, sans affectation, sauf surcharge. */
|
||||||
|
export function op(partial: Partial<Op> & Pick<Op, "kind" | "amount" | "accountUserId">): Op {
|
||||||
|
seq += 1;
|
||||||
|
return {
|
||||||
|
id: `op${seq}`,
|
||||||
|
date: "2026-10-15",
|
||||||
|
label: "test",
|
||||||
|
categoryId: "cat",
|
||||||
|
status: "ACTUAL",
|
||||||
|
split: { H: 5000, C: 5000 },
|
||||||
|
recurringId: null,
|
||||||
|
allocatedToRecurringId: null,
|
||||||
|
allocatedToOperationId: null,
|
||||||
|
allocationMode: null,
|
||||||
|
...partial,
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in new issue
Block a user