diff --git a/backend/src/utils/liquidityForecast.ts b/backend/src/utils/liquidityForecast.ts index a010db6..a32934c 100644 --- a/backend/src/utils/liquidityForecast.ts +++ b/backend/src/utils/liquidityForecast.ts @@ -302,15 +302,28 @@ export const generateLiquidityForecast = async (server: FastifyInstance, tenantI .where(and(eq(statementallocations.tenant, tenantId), eq(statementallocations.archived, false))), ]); + const activeAccounts = accounts.filter((account) => !account.archived); + const activeStatements = statements.filter((statement) => !statement.archived); + const activeDocuments = documents.filter((document) => !document.archived); + const activeIncomingInvoices = incomingInvoices.filter((invoice) => !invoice.archived); + const activeDocumentIds = new Set(activeDocuments.map((document) => document.id)); + const activeIncomingInvoiceIds = new Set(activeIncomingInvoices.map((invoice) => invoice.id)); + const activeAllocations = allocations.filter((allocation) => { + if (allocation.archived) return false; + if (allocation.createddocument && !activeDocumentIds.has(allocation.createddocument)) return false; + if (allocation.incominginvoice && !activeIncomingInvoiceIds.has(allocation.incominginvoice)) return false; + return true; + }); + const startingBalance = roundMoney( - accounts + activeAccounts .filter((account) => !account.expired) .reduce((sum, account) => sum + Number(account.balance || 0), 0) ); const allocationByDocument = new Map(); const allocationByIncomingInvoice = new Map(); - allocations.forEach((allocation) => { + activeAllocations.forEach((allocation) => { if (allocation.createddocument) { allocationByDocument.set( allocation.createddocument, @@ -326,14 +339,14 @@ export const generateLiquidityForecast = async (server: FastifyInstance, tenantI } }); - const cancelledDocumentIds = findCancellationDocumentIds(documents); + const cancelledDocumentIds = findCancellationDocumentIds(activeDocuments); const openEvents: ForecastEvent[] = []; - documents + activeDocuments .filter((document) => ["invoices", "advanceInvoices"].includes(document.type)) .filter((document) => document.state === "Gebucht" && !cancelledDocumentIds.has(document.id)) .forEach((document) => { - const total = getCreatedDocumentGrossAmount(document, documents); + const total = getCreatedDocumentGrossAmount(document, activeDocuments); const openAmount = roundMoney(total - (allocationByDocument.get(document.id) || 0)); if (openAmount <= 0.01) return; @@ -351,7 +364,7 @@ export const generateLiquidityForecast = async (server: FastifyInstance, tenantI }); }); - incomingInvoices + activeIncomingInvoices .filter((invoice) => invoice.state === "Gebucht" || invoice.state === "Vorbereitet") .forEach((invoice) => { const signedAmount = getIncomingInvoiceSignedAmount(invoice); @@ -372,8 +385,8 @@ export const generateLiquidityForecast = async (server: FastifyInstance, tenantI }); }); - const heuristicRecurring = detectRecurringHeuristically(statements); - const aiRecurring = await detectRecurringWithAi(server, statements); + const heuristicRecurring = detectRecurringHeuristically(activeStatements); + const aiRecurring = await detectRecurringWithAi(server, activeStatements); const recurring = mergeRecurringCandidates(heuristicRecurring, aiRecurring); const events = [ ...openEvents, @@ -422,7 +435,7 @@ export const generateLiquidityForecast = async (server: FastifyInstance, tenantI lowestBalanceDate: lowestPoint?.date || today.format("YYYY-MM-DD"), totalIncome, totalExpense, - accounts: accounts.map((account) => ({ + accounts: activeAccounts.map((account) => ({ id: account.id, name: account.name, iban: account.iban,