KI-AGENT: Kassenbucheintrag vereinfacht anlegen

This commit is contained in:
2026-05-28 16:43:03 +02:00
parent f2055d59eb
commit 2a5071b15a
2 changed files with 31 additions and 208 deletions

View File

@@ -260,7 +260,7 @@ export default async function bankingRoutes(server: FastifyInstance) {
}
if (!Number.isFinite(cashbookId)) return reply.code(400).send({ error: "Ungültige Kasse." })
if (!body.date || !dayjs(body.date).isValid()) return reply.code(400).send({ error: "Bitte ein gültiges Buchungsdatum angeben." })
const bookingDate = body.date && dayjs(body.date).isValid() ? dayjs(body.date) : dayjs()
if (!Number.isFinite(Number(body.amount)) || Number(body.amount) <= 0) return reply.code(400).send({ error: "Der Betrag muss größer als 0 sein." })
if (body.direction !== "income" && body.direction !== "expense") return reply.code(400).send({ error: "Bitte Einnahme oder Ausgabe auswählen." })
@@ -273,8 +273,11 @@ export default async function bankingRoutes(server: FastifyInstance) {
if (!cashbook[0]) return reply.code(404).send({ error: "Kasse nicht gefunden." })
const counterPayload = buildCashbookCounterPayload(String(body.counterType || ""), body.counterId)
if (!counterPayload) return reply.code(400).send({ error: "Bitte ein Gegenkonto auswählen." })
const hasCounterInput = Boolean(body.counterType || body.counterId)
const counterPayload = hasCounterInput
? buildCashbookCounterPayload(String(body.counterType || ""), body.counterId)
: null
if (hasCounterInput && !counterPayload) return reply.code(400).send({ error: "Bitte ein gültiges Gegenkonto auswählen." })
const signedAmount = body.direction === "income"
? Math.abs(Number(body.amount))
@@ -284,8 +287,8 @@ export default async function bankingRoutes(server: FastifyInstance) {
const created = await server.db.transaction(async (tx) => {
const insertedStatements = await tx.insert(bankstatements).values({
account: cashbookId,
date: dayjs(body.date).format("YYYY-MM-DD"),
valueDate: dayjs(body.date).format("YYYY-MM-DD"),
date: bookingDate.format("YYYY-MM-DD"),
valueDate: bookingDate.format("YYYY-MM-DD"),
amount: signedAmount,
tenant: req.user.tenant_id,
text: description,
@@ -296,18 +299,20 @@ export default async function bankingRoutes(server: FastifyInstance) {
}).returning()
const statement = insertedStatements[0]
const insertedAllocations = await tx.insert(statementallocations).values({
bankstatement: statement.id,
amount: signedAmount,
tenant: req.user.tenant_id,
description,
datevTaxKey: body.datevTaxKey ? String(body.datevTaxKey).trim() : null,
...counterPayload,
}).returning()
const insertedAllocations = counterPayload
? await tx.insert(statementallocations).values({
bankstatement: statement.id,
amount: signedAmount,
tenant: req.user.tenant_id,
description,
datevTaxKey: body.datevTaxKey ? String(body.datevTaxKey).trim() : null,
...counterPayload,
}).returning()
: []
return {
statement,
allocation: insertedAllocations[0],
allocation: insertedAllocations[0] || null,
}
})