feat: add buildCsv for cashbox export
This commit is contained in:
@@ -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');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user