feat: add buildCsv for cashbox export

This commit is contained in:
Bastian Wagner
2026-08-03 21:08:03 +02:00
parent cf7c3efb0f
commit cab04c5869
2 changed files with 72 additions and 1 deletions

View File

@@ -60,3 +60,41 @@ export function buildRows(team: Team, from: string, to: string): CashboxExportRo
return { ...row, runningTotal };
});
}
const TYPE_LABELS: Record<string, string> = {
payment: 'Zahlung',
credit: 'Guthaben',
expense: 'Ausgabe',
};
function formatGermanAmount(value: number): string {
return value.toFixed(2).replace('.', ',');
}
function escapeCsvField(value: string): string {
if (/[;"\n\r]/.test(value)) {
return `"${value.replace(/"/g, '""')}"`;
}
return value;
}
export function buildCsv(rows: CashboxExportRow[]): string {
const lines = ['Datum;Typ;Wer;Notiz;Betrag;Periodensaldo'];
if (rows.length === 0) {
lines.push('Keine Buchungen im gewählten Zeitraum');
} else {
for (const row of rows) {
lines.push(
[
row.date.slice(0, 10),
TYPE_LABELS[row.type] ?? row.type,
escapeCsvField(row.who),
escapeCsvField(row.note),
formatGermanAmount(row.amount),
formatGermanAmount(row.runningTotal),
].join(';'),
);
}
}
return lines.join('\r\n');
}