Archivierte Belege aus Liquiditätsprognose ausschließen

Sichert die Liquiditätsprognose zusätzlich gegen archivierte Ausgangs- und Eingangsbelege sowie deren Zuordnungen ab.
This commit is contained in:
2026-04-23 15:56:46 +02:00
parent 75148b2718
commit cb71e9d294

View File

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