feat(domain): money parsing/formatting and splits

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
YugonitoandClaude Opus 5.5 committed 2026-10-27 17:57:50 +01:00
1 parent 8d41c2fffe
commit 5ce5363f50
3 files changed
+189

No files matched your search

+71
View File
@@ -0,0 +1,71 @@
import type { Cents, Split, UserId } from "./types";
const euro = new Intl.NumberFormat("fr-FR", { style: "currency", currency: "EUR" });
const MINUS = "−";
/** "86,40", "1 234,56", "86.40" → centimes. null si vide, nul, négatif ou invalide. */
export function parseEuro(input: string): Cents | null {
const s = input.replace(/[\s  ]/g, "").replace(",", ".");
if (!/^\d+(\.\d{1,2})?$/.test(s)) return null;
const [units, decimals = ""] = s.split(".");
const cents = Number(units) * 100 + Number(decimals.padEnd(2, "0"));
return cents > 0 ? cents : null;
}
export function formatEuro(c: Cents, opts: { sign?: boolean } = {}): string {
const text = euro.format(Math.abs(c) / 100);
if (c < 0) return MINUS + text;
return opts.sign && c > 0 ? "+" + text : text;
}
/** Decimal Prisma (ou chaîne/nombre) → centimes, sans passer par un float. */
export function centsFromDecimal(d: { toString(): string } | string | number): Cents {
const s = d.toString();
const negative = s.startsWith("-");
const [units, decimals = ""] = s.replace("-", "").split(".");
const cents = Number(units) * 100 + Number(decimals.padEnd(2, "0").slice(0, 2));
return negative ? -cents : cents;
}
export function decimalFromCents(c: Cents): string {
const abs = Math.abs(c);
return `${c < 0 ? "-" : ""}${Math.floor(abs / 100)}.${String(abs % 100).padStart(2, "0")}`;
}
export function validateSplit(s: Split): string | null {
const values = Object.values(s);
if (values.some((v) => v < 0 || !Number.isInteger(v))) return "Les pourcentages doivent être positifs.";
if (values.reduce((a, b) => a + b, 0) !== 10000) return "La répartition doit totaliser 100 %.";
return null;
}
/** Part de chacun en centimes ; le résidu d'arrondi va au titulaire du compte. */
export function shareAmounts(amount: Cents, split: Split, accountUserId: UserId): Record<UserId, Cents> {
const shares: Record<UserId, Cents> = {};
let total = 0;
for (const [userId, bp] of Object.entries(split)) {
shares[userId] = Math.floor((amount * bp) / 10000);
total += shares[userId];
}
shares[accountUserId] = (shares[accountUserId] ?? 0) + amount - total;
return shares;
}
/** "33,33" → 3333. null si invalide ou hors [0, 100]. */
export function percentToBp(input: string): number | null {
const s = input.replace(/[\s%]/g, "").replace(",", ".");
if (!/^\d+(\.\d{1,2})?$/.test(s)) return null;
const [units, decimals = ""] = s.split(".");
const bp = Number(units) * 100 + Number(decimals.padEnd(2, "0"));
return bp <= 10000 ? bp : null;
}
export function bpToPercentLabel(bp: number): string {
const units = Math.floor(bp / 100);
const rest = bp % 100;
return rest === 0 ? String(units) : `${units},${String(rest).padStart(2, "0").replace(/0$/, "")}`;
}
export function fullSplit(userId: UserId, userIds: UserId[]): Split {
return Object.fromEntries(userIds.map((id) => [id, id === userId ? 10000 : 0]));
}
+33
View File
@@ -0,0 +1,33 @@
export type Cents = number;
export type UserId = string;
/** Répartition en points de base : 100 % = 10000. */
export type Split = Record<UserId, number>;
export type OpKind = "INCOME" | "EXPENSE";
export type OpStatus = "PLANNED" | "ACTUAL" | "SKIPPED";
export type AllocationMode = "REDUCE_SHARED" | "BENEFICIARY_ONLY" | "CUSTOM";
export type Frequency = "MONTHLY" | "QUARTERLY" | "YEARLY";
export interface Op {
id: string;
kind: OpKind;
amount: Cents;
date: string;
label: string;
categoryId: string;
accountUserId: UserId;
status: OpStatus;
split: Split;
recurringId: string | null;
allocatedToRecurringId: string | null;
allocatedToOperationId: string | null;
allocationMode: AllocationMode | null;
}
export interface Settlement {
id: string;
fromUserId: UserId;
toUserId: UserId;
amount: Cents;
date: string;
}