import { categorize, roundEur } from "../lib/categorize"; import { resolveAssignedAndEffective, type SalaryShiftSettings } from "../lib/month"; import type { NormalizedTransaction } from "./types"; export type FintsStatementTransaction = { valueDate: Date; entryDate: Date; amount: number; transactionType: string; bankReference: string; bookingText?: string; purpose?: string; remoteName?: string; customerReference?: string; }; export function formatFinTsDate(date: Date): string { const y = date.getFullYear(); const m = String(date.getMonth() + 1).padStart(2, "0"); const d = String(date.getDate()).padStart(2, "0"); return `${y}-${m}-${d}`; } export function mapFinTsTransaction( tx: FintsStatementTransaction, ownNames: string[], salaryShift: SalaryShiftSettings, ): Omit & { categoryName: string; assignedMonth?: string; effectiveMonth?: string; externalRef?: string; } { const amount = roundEur(tx.amount); const rawText = [tx.purpose, tx.bookingText, tx.transactionType].filter(Boolean).join(" "); const counterparty = tx.remoteName || undefined; const description = counterparty ?? (rawText.slice(0, 80) || "Umsatz"); const vorgang = tx.transactionType || tx.bookingText; const bookingDate = formatFinTsDate(tx.entryDate); const valueDate = formatFinTsDate(tx.valueDate); const categoryName = categorize(rawText, amount, vorgang ?? "", ownNames); const { assignedMonth, effectiveMonth } = resolveAssignedAndEffective( bookingDate, amount, categoryName, salaryShift, ); return { bookingDate, valueDate, description, counterparty, amount, vorgang, isPending: false, rawText: rawText || undefined, externalRef: tx.bankReference || tx.customerReference || undefined, categoryName, assignedMonth, effectiveMonth, }; }