added invoices and status in cockpit, created info button for contract status types
This commit is contained in:
+66
-2
@@ -105,11 +105,11 @@ async function getCockpitData() {
|
||||
const criticalDays = parseInt(settings.deadlineCriticalDays) || 14;
|
||||
const warningDays = parseInt(settings.deadlineWarningDays) || 42;
|
||||
const okDays = parseInt(settings.deadlineOkDays) || 90;
|
||||
// Lade alle aktiven/pending Verträge mit allen relevanten Daten
|
||||
// Lade alle relevanten Verträge (inkl. CANCELLED/DEACTIVATED für Schlussrechnung-Check)
|
||||
const contracts = await prisma.contract.findMany({
|
||||
where: {
|
||||
status: {
|
||||
in: ['ACTIVE', 'PENDING', 'DRAFT'],
|
||||
in: ['ACTIVE', 'PENDING', 'DRAFT', 'CANCELLED', 'DEACTIVATED', 'EXPIRED'],
|
||||
},
|
||||
},
|
||||
include: {
|
||||
@@ -145,6 +145,7 @@ async function getCockpitData() {
|
||||
energyDetails: {
|
||||
include: {
|
||||
meter: true,
|
||||
invoices: true,
|
||||
},
|
||||
},
|
||||
internetDetails: {
|
||||
@@ -186,6 +187,7 @@ async function getCockpitData() {
|
||||
contractEnding: 0,
|
||||
missingCredentials: 0,
|
||||
missingData: 0,
|
||||
missingInvoices: 0,
|
||||
openTasks: 0,
|
||||
pendingContracts: 0,
|
||||
},
|
||||
@@ -367,6 +369,68 @@ async function getCockpitData() {
|
||||
});
|
||||
summary.byCategory.pendingContracts++;
|
||||
}
|
||||
// 13. ENERGIE-RECHNUNGEN (nur für ELECTRICITY und GAS)
|
||||
if (['ELECTRICITY', 'GAS'].includes(contract.type) && contract.energyDetails) {
|
||||
const invoices = contract.energyDetails.invoices || [];
|
||||
const now = new Date();
|
||||
now.setHours(0, 0, 0, 0);
|
||||
// 13a. SCHLUSSRECHNUNG FEHLT (nur wenn Vertrag gekündigt/deaktiviert ist)
|
||||
// "Beendet" = CANCELLED oder DEACTIVATED (nicht nur Laufzeit abgelaufen!)
|
||||
const isContractTerminated = contract.status === 'CANCELLED' || contract.status === 'DEACTIVATED';
|
||||
if (isContractTerminated) {
|
||||
const hasFinalInvoice = invoices.some(inv => inv.invoiceType === 'FINAL');
|
||||
const hasNotAvailable = invoices.some(inv => inv.invoiceType === 'NOT_AVAILABLE');
|
||||
if (!hasFinalInvoice && !hasNotAvailable) {
|
||||
issues.push({
|
||||
type: 'missing_final_invoice',
|
||||
label: 'Schlussrechnung fehlt',
|
||||
urgency: 'warning',
|
||||
details: 'Vertrag gekündigt/deaktiviert, aber keine Schlussrechnung vorhanden',
|
||||
});
|
||||
summary.byCategory.missingInvoices++;
|
||||
}
|
||||
}
|
||||
// 13b. ZWISCHENRECHNUNG FEHLT/ÜBERFÄLLIG (wenn Vertrag > 12 Monate läuft)
|
||||
// Für alle Status außer DRAFT und nicht gekündigt/deaktiviert
|
||||
// Auch EXPIRED zählt hier, da der Vertrag ohne Kündigung weiterläuft!
|
||||
if (contract.startDate && contract.status !== 'DRAFT' && !isContractTerminated) {
|
||||
const startDate = new Date(contract.startDate);
|
||||
startDate.setHours(0, 0, 0, 0);
|
||||
const daysSinceStart = Math.floor((now.getTime() - startDate.getTime()) / (1000 * 60 * 60 * 24));
|
||||
if (daysSinceStart > 365) {
|
||||
// Vertrag läuft > 12 Monate
|
||||
if (invoices.length === 0) {
|
||||
// Keine Rechnungen vorhanden
|
||||
issues.push({
|
||||
type: 'missing_interim_invoice',
|
||||
label: 'Zwischenrechnung fehlt',
|
||||
urgency: 'warning',
|
||||
details: 'Vertrag läuft über 12 Monate ohne Rechnung',
|
||||
});
|
||||
summary.byCategory.missingInvoices++;
|
||||
}
|
||||
else {
|
||||
// Prüfen ob letzte Rechnung > 12 Monate alt
|
||||
const latestInvoice = invoices
|
||||
.filter(inv => inv.invoiceType !== 'NOT_AVAILABLE')
|
||||
.sort((a, b) => new Date(b.invoiceDate).getTime() - new Date(a.invoiceDate).getTime())[0];
|
||||
if (latestInvoice) {
|
||||
const invoiceDate = new Date(latestInvoice.invoiceDate);
|
||||
const daysSinceInvoice = Math.floor((now.getTime() - invoiceDate.getTime()) / (1000 * 60 * 60 * 24));
|
||||
if (daysSinceInvoice > 365) {
|
||||
issues.push({
|
||||
type: 'overdue_interim_invoice',
|
||||
label: 'Zwischenrechnung überfällig',
|
||||
urgency: 'warning',
|
||||
details: `Letzte Rechnung vor ${Math.floor(daysSinceInvoice / 30)} Monaten`,
|
||||
});
|
||||
summary.byCategory.missingInvoices++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Nur Verträge mit Issues hinzufügen
|
||||
if (issues.length > 0) {
|
||||
const highestUrgency = getHighestUrgency(issues);
|
||||
|
||||
Reference in New Issue
Block a user