added invoices and status in cockpit, created info button for contract status types
This commit is contained in:
@@ -124,14 +124,14 @@ export async function getContractById(id: number, decryptPassword = false) {
|
||||
contractCategory: true,
|
||||
previousContract: {
|
||||
include: {
|
||||
energyDetails: { include: { meter: { include: { readings: true } } } },
|
||||
energyDetails: { include: { meter: { include: { readings: true } }, invoices: true } },
|
||||
internetDetails: { include: { phoneNumbers: true } },
|
||||
mobileDetails: { include: { simCards: true } },
|
||||
tvDetails: true,
|
||||
carInsuranceDetails: true,
|
||||
},
|
||||
},
|
||||
energyDetails: { include: { meter: { include: { readings: true } } } },
|
||||
energyDetails: { include: { meter: { include: { readings: true } }, invoices: true } },
|
||||
internetDetails: { include: { phoneNumbers: true } },
|
||||
mobileDetails: { include: { simCards: true } },
|
||||
tvDetails: true,
|
||||
|
||||
@@ -49,6 +49,7 @@ export interface CockpitSummary {
|
||||
contractEnding: number;
|
||||
missingCredentials: number;
|
||||
missingData: number;
|
||||
missingInvoices: number;
|
||||
openTasks: number;
|
||||
pendingContracts: number;
|
||||
};
|
||||
@@ -142,11 +143,11 @@ export async function getCockpitData(): Promise<CockpitResult> {
|
||||
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: {
|
||||
@@ -182,6 +183,7 @@ export async function getCockpitData(): Promise<CockpitResult> {
|
||||
energyDetails: {
|
||||
include: {
|
||||
meter: true,
|
||||
invoices: true,
|
||||
},
|
||||
},
|
||||
internetDetails: {
|
||||
@@ -224,6 +226,7 @@ export async function getCockpitData(): Promise<CockpitResult> {
|
||||
contractEnding: 0,
|
||||
missingCredentials: 0,
|
||||
missingData: 0,
|
||||
missingInvoices: 0,
|
||||
openTasks: 0,
|
||||
pendingContracts: 0,
|
||||
},
|
||||
@@ -426,6 +429,75 @@ export async function getCockpitData(): Promise<CockpitResult> {
|
||||
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);
|
||||
|
||||
@@ -0,0 +1,157 @@
|
||||
import { PrismaClient, InvoiceType } from '@prisma/client';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
export interface CreateInvoiceData {
|
||||
invoiceDate: Date;
|
||||
invoiceType: InvoiceType;
|
||||
documentPath?: string;
|
||||
notes?: string;
|
||||
}
|
||||
|
||||
export interface UpdateInvoiceData {
|
||||
invoiceDate?: Date;
|
||||
invoiceType?: InvoiceType;
|
||||
documentPath?: string;
|
||||
notes?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alle Rechnungen für ein EnergyContractDetails abrufen
|
||||
*/
|
||||
export async function getInvoices(energyContractDetailsId: number) {
|
||||
return prisma.invoice.findMany({
|
||||
where: { energyContractDetailsId },
|
||||
orderBy: { invoiceDate: 'desc' },
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Einzelne Rechnung abrufen
|
||||
*/
|
||||
export async function getInvoice(energyContractDetailsId: number, invoiceId: number) {
|
||||
return prisma.invoice.findFirst({
|
||||
where: { id: invoiceId, energyContractDetailsId },
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Neue Rechnung hinzufügen
|
||||
*/
|
||||
export async function addInvoice(energyContractDetailsId: number, data: CreateInvoiceData) {
|
||||
// Validierung: documentPath ist Pflicht, außer bei NOT_AVAILABLE
|
||||
if (data.invoiceType !== 'NOT_AVAILABLE' && !data.documentPath) {
|
||||
throw new Error('Dokument ist Pflicht (außer bei Typ "Nicht verfügbar")');
|
||||
}
|
||||
|
||||
// Prüfen ob EnergyContractDetails existiert
|
||||
const energyDetails = await prisma.energyContractDetails.findUnique({
|
||||
where: { id: energyContractDetailsId },
|
||||
});
|
||||
|
||||
if (!energyDetails) {
|
||||
throw new Error('Energievertrag nicht gefunden');
|
||||
}
|
||||
|
||||
return prisma.invoice.create({
|
||||
data: {
|
||||
energyContractDetailsId,
|
||||
invoiceDate: data.invoiceDate,
|
||||
invoiceType: data.invoiceType,
|
||||
documentPath: data.documentPath,
|
||||
notes: data.notes,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Rechnung aktualisieren
|
||||
*/
|
||||
export async function updateInvoice(
|
||||
energyContractDetailsId: number,
|
||||
invoiceId: number,
|
||||
data: UpdateInvoiceData
|
||||
) {
|
||||
// Prüfen ob Rechnung existiert und zum EnergyContractDetails gehört
|
||||
const invoice = await prisma.invoice.findFirst({
|
||||
where: { id: invoiceId, energyContractDetailsId },
|
||||
});
|
||||
|
||||
if (!invoice) {
|
||||
throw new Error('Rechnung nicht gefunden');
|
||||
}
|
||||
|
||||
// Validierung bei Typ-Änderung
|
||||
const newType = data.invoiceType ?? invoice.invoiceType;
|
||||
const newPath = data.documentPath !== undefined ? data.documentPath : invoice.documentPath;
|
||||
|
||||
if (newType !== 'NOT_AVAILABLE' && !newPath) {
|
||||
throw new Error('Dokument ist Pflicht (außer bei Typ "Nicht verfügbar")');
|
||||
}
|
||||
|
||||
return prisma.invoice.update({
|
||||
where: { id: invoiceId },
|
||||
data: {
|
||||
invoiceDate: data.invoiceDate,
|
||||
invoiceType: data.invoiceType,
|
||||
documentPath: data.documentPath,
|
||||
notes: data.notes,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Rechnung löschen
|
||||
*/
|
||||
export async function deleteInvoice(energyContractDetailsId: number, invoiceId: number) {
|
||||
const invoice = await prisma.invoice.findFirst({
|
||||
where: { id: invoiceId, energyContractDetailsId },
|
||||
});
|
||||
|
||||
if (!invoice) {
|
||||
throw new Error('Rechnung nicht gefunden');
|
||||
}
|
||||
|
||||
// Datei löschen falls vorhanden
|
||||
if (invoice.documentPath) {
|
||||
const filePath = path.join(process.cwd(), invoice.documentPath);
|
||||
if (fs.existsSync(filePath)) {
|
||||
fs.unlinkSync(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
return prisma.invoice.delete({ where: { id: invoiceId } });
|
||||
}
|
||||
|
||||
/**
|
||||
* Rechnung direkt erstellen (für E-Mail-Integration)
|
||||
* Erstellt eine Rechnung mit bereits vorhandenem Dokument
|
||||
*/
|
||||
export async function createInvoiceWithDocument(
|
||||
energyContractDetailsId: number,
|
||||
invoiceDate: Date,
|
||||
invoiceType: InvoiceType,
|
||||
documentPath: string,
|
||||
notes?: string
|
||||
) {
|
||||
// Prüfen ob EnergyContractDetails existiert
|
||||
const energyDetails = await prisma.energyContractDetails.findUnique({
|
||||
where: { id: energyContractDetailsId },
|
||||
});
|
||||
|
||||
if (!energyDetails) {
|
||||
throw new Error('Energievertrag nicht gefunden');
|
||||
}
|
||||
|
||||
return prisma.invoice.create({
|
||||
data: {
|
||||
energyContractDetailsId,
|
||||
invoiceDate,
|
||||
invoiceType,
|
||||
documentPath,
|
||||
notes,
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user