no message

This commit is contained in:
dh
2025-11-04 07:10:59 +01:00
parent 78c229e25b
commit 5ae542061c
+125 -75
View File
@@ -7,57 +7,63 @@ class Abrechnung extends IPSModule
{ {
parent::Create(); parent::Create();
// Eigenschaften registrieren
$this->RegisterPropertyString('Users', '[]'); $this->RegisterPropertyString('Users', '[]');
$this->RegisterPropertyString('PowerMeters', '[]'); $this->RegisterPropertyString('PowerMeters', '[]');
$this->RegisterPropertyString('WaterMeters', '[]'); $this->RegisterPropertyString('WaterMeters', '[]');
$this->RegisterPropertyString('Tariffs', '[]'); $this->RegisterPropertyString('Tariffs', '[]');
// Variablen für UI in der Visualisierung
$this->RegisterVariableInteger('FromDate', 'Startdatum', '~UnixTimestamp', 1); $this->RegisterVariableInteger('FromDate', 'Startdatum', '~UnixTimestamp', 1);
$this->RegisterVariableInteger('ToDate', 'Enddatum', '~UnixTimestamp', 2); $this->RegisterVariableInteger('ToDate', 'Enddatum', '~UnixTimestamp', 2);
$this->RegisterVariableString('LastResult', 'Letzte Abrechnung', '', 3); $this->RegisterVariableString('LastResult', 'Letzte Abrechnung', '', 3);
$this->EnableAction('FromDate'); $this->EnableAction('FromDate');
$this->EnableAction('ToDate'); $this->EnableAction('ToDate');
// Skript-Button für Abrechnung
$this->RegisterScript('StartBilling', 'Abrechnung starten', "<?php IPS_RequestAction(" . $this->InstanceID . ", 'StartBilling', ''); ?>"); $this->RegisterScript('StartBilling', 'Abrechnung starten', "<?php IPS_RequestAction(" . $this->InstanceID . ", 'StartBilling', ''); ?>");
IPS_LogMessage('Abrechnung', 'Create() fertig ausgeführt');
} }
public function ApplyChanges() public function ApplyChanges()
{ {
parent::ApplyChanges(); parent::ApplyChanges();
IPS_LogMessage('Abrechnung', 'ApplyChanges() ausgeführt');
} }
// UI-Eingaben (Visualisierung)
public function RequestAction($Ident, $Value) public function RequestAction($Ident, $Value)
{ {
IPS_LogMessage('Abrechnung', "RequestAction aufgerufen: $Ident = $Value");
switch ($Ident) { switch ($Ident) {
case 'FromDate': case 'FromDate':
SetValue($this->GetIDForIdent('FromDate'), $Value);
break;
case 'ToDate': case 'ToDate':
SetValue($this->GetIDForIdent('ToDate'), $Value); SetValue($this->GetIDForIdent($Ident), $Value);
IPS_LogMessage('Abrechnung', "Wert für $Ident gesetzt: " . date('Y-m-d H:i', $Value));
break; break;
case 'StartBilling': case 'StartBilling':
IPS_LogMessage('Abrechnung', 'Starte Abrechnung...');
$pdfPath = $this->GenerateInvoices(); $pdfPath = $this->GenerateInvoices();
if ($pdfPath) { if ($pdfPath) {
SetValue($this->GetIDForIdent('LastResult'), basename($pdfPath)); SetValue($this->GetIDForIdent('LastResult'), basename($pdfPath));
IPS_LogMessage('Abrechnung', "✅ Abrechnung erstellt: $pdfPath");
echo "✅ Abrechnung erstellt: " . basename($pdfPath); echo "✅ Abrechnung erstellt: " . basename($pdfPath);
} else { } else {
IPS_LogMessage('Abrechnung', '❌ Fehler bei der PDF-Erstellung');
echo "❌ Fehler bei der PDF-Erstellung"; echo "❌ Fehler bei der PDF-Erstellung";
} }
break; break;
} }
} }
// Hauptfunktion: PDF generieren
public function GenerateInvoices() public function GenerateInvoices()
{ {
IPS_LogMessage('Abrechnung', 'GenerateInvoices() gestartet');
$from = GetValue($this->GetIDForIdent('FromDate')); $from = GetValue($this->GetIDForIdent('FromDate'));
$to = GetValue($this->GetIDForIdent('ToDate')); $to = GetValue($this->GetIDForIdent('ToDate'));
IPS_LogMessage('Abrechnung', "Zeitraum: " . date('Y-m-d H:i', $from) . " bis " . date('Y-m-d H:i', $to));
if ($from >= $to) { if ($from >= $to) {
IPS_LogMessage('Abrechnung', 'Ungültiger Zeitraum'); IPS_LogMessage('Abrechnung', '❌ Ungültiger Zeitraum');
return false; return false;
} }
@@ -65,101 +71,145 @@ class Abrechnung extends IPSModule
$power = json_decode($this->ReadPropertyString('PowerMeters'), true); $power = json_decode($this->ReadPropertyString('PowerMeters'), true);
$water = json_decode($this->ReadPropertyString('WaterMeters'), true); $water = json_decode($this->ReadPropertyString('WaterMeters'), true);
$tariffs = json_decode($this->ReadPropertyString('Tariffs'), true); $tariffs = json_decode($this->ReadPropertyString('Tariffs'), true);
IPS_LogMessage('Abrechnung', 'Daten geladen: Benutzer=' . count($users) . ', Power=' . count($power) . ', Water=' . count($water) . ', Tarife=' . count($tariffs));
// TCPDF laden // TCPDF laden
if (!class_exists('TCPDF')) { $lib = __DIR__ . '/libs/tcpdf/tcpdf.php';
$lib = IPS_GetKernelDir() . 'libs/tcpdf/tcpdf.php'; IPS_LogMessage('Abrechnung', "Suche TCPDF unter: $lib");
if (!file_exists($lib)) { if (!file_exists($lib)) {
IPS_LogMessage('Abrechnung', 'TCPDF fehlt in /libs/tcpdf/'); IPS_LogMessage('Abrechnung', '❌ TCPDF nicht gefunden!');
return false; return false;
} }
try {
require_once $lib; require_once $lib;
IPS_LogMessage('Abrechnung', '✅ TCPDF erfolgreich eingebunden');
} catch (Throwable $e) {
IPS_LogMessage('Abrechnung', '❌ Fehler beim Laden von TCPDF: ' . $e->getMessage());
return false;
} }
$pdf = new TCPDF(); try {
$pdf->SetCreator('IPSymcon Abrechnung'); $pdf = new TCPDF();
$pdf->SetAuthor('Abrechnung Modul'); $pdf->SetCreator('IPSymcon Abrechnung');
$pdf->SetTitle('Zählerabrechnung'); $pdf->SetAuthor('Abrechnung Modul');
$pdf->SetMargins(15, 15, 15); $pdf->SetTitle('Zählerabrechnung');
$pdf->SetAutoPageBreak(true, 20); $pdf->SetMargins(15, 15, 15);
$pdf->SetAutoPageBreak(true, 20);
foreach ($users as $user) { foreach ($users as $user) {
$pdf->AddPage(); IPS_LogMessage('Abrechnung', 'Erstelle Seite für Benutzer: ' . $user['name']);
$pdf->SetFont('helvetica', 'B', 14);
$pdf->Cell(0, 10, 'Rechnung für ' . $user['name'], 0, 1);
$pdf->SetFont('helvetica', '', 10);
$pdf->Cell(0, 6, $user['address'] . ' - ' . $user['city'], 0, 1);
$pdf->Ln(5);
// Alle Zähler des Benutzers $pdf->AddPage();
$userMeters = array_merge( $pdf->SetFont('helvetica', 'B', 14);
array_filter($power, fn($m) => $m['user_id'] == $user['id']), $pdf->Cell(0, 10, 'Rechnung für ' . $user['name'], 0, 1);
array_filter($water, fn($m) => $m['user_id'] == $user['id']) $pdf->SetFont('helvetica', '', 10);
); $pdf->Cell(0, 6, $user['address'] . ' - ' . $user['city'], 0, 1);
$pdf->Ln(5);
$total = 0.0; $userMeters = array_merge(
$pdf->SetFont('helvetica', 'B', 11); array_filter($power, fn($m) => $m['user_id'] == $user['id']),
$pdf->Cell(60, 8, 'Zähler', 1); array_filter($water, fn($m) => $m['user_id'] == $user['id'])
$pdf->Cell(35, 8, 'Start', 1); );
$pdf->Cell(35, 8, 'Ende', 1); IPS_LogMessage('Abrechnung', 'Zähler gefunden: ' . count($userMeters));
$pdf->Cell(25, 8, 'Verbrauch', 1);
$pdf->Cell(25, 8, 'Kosten', 1);
$pdf->Ln();
$pdf->SetFont('helvetica', '', 9); $total = 0.0;
foreach ($userMeters as $m) { $pdf->SetFont('helvetica', 'B', 11);
$start = $this->GetValueAt($m['var_consumption'], $from); $pdf->Cell(60, 8, 'Zähler', 1);
$end = $this->GetValueAt($m['var_consumption'], $to); $pdf->Cell(35, 8, 'Start', 1);
if ($start === null || $end === null) continue; $pdf->Cell(35, 8, 'Ende', 1);
$pdf->Cell(25, 8, 'Verbrauch', 1);
$diff = max(0, $end - $start); $pdf->Cell(25, 8, 'Kosten', 1);
$price = $this->GetTariffForMeter($tariffs, $from, $to, $m);
$cost = $diff * $price;
$total += $cost;
$pdf->Cell(60, 7, $m['name'], 1);
$pdf->Cell(35, 7, number_format($start, 2), 1);
$pdf->Cell(35, 7, number_format($end, 2), 1);
$pdf->Cell(25, 7, number_format($diff, 2), 1);
$pdf->Cell(25, 7, number_format($cost, 2), 1);
$pdf->Ln(); $pdf->Ln();
$pdf->SetFont('helvetica', '', 9);
foreach ($userMeters as $m) {
$start = $this->GetValueAt($m['var_consumption'], $from);
$end = $this->GetValueAt($m['var_consumption'], $to);
IPS_LogMessage('Abrechnung', "Zähler {$m['name']} Start=$start Ende=$end");
if ($start === null || $end === null) {
IPS_LogMessage('Abrechnung', "⚠️ Keine Werte für {$m['name']}");
continue;
}
$diff = max(0, $end - $start);
$price = $this->GetTariffForMeter($tariffs, $from, $to, $m);
$cost = $diff * $price;
$total += $cost;
IPS_LogMessage('Abrechnung', "{$m['name']} Verbrauch=$diff, Preis=$price, Kosten=$cost");
$pdf->Cell(60, 7, $m['name'], 1);
$pdf->Cell(35, 7, number_format($start, 2), 1);
$pdf->Cell(35, 7, number_format($end, 2), 1);
$pdf->Cell(25, 7, number_format($diff, 2), 1);
$pdf->Cell(25, 7, number_format($cost, 2), 1);
$pdf->Ln();
}
$pdf->Ln(4);
$pdf->SetFont('helvetica', 'B', 11);
$pdf->Cell(0, 8, 'Gesamtbetrag: ' . number_format($total, 2) . ' CHF', 0, 1, 'R');
} }
$pdf->Ln(4); $dir = IPS_GetKernelDir() . 'media/Abrechnung';
$pdf->SetFont('helvetica', 'B', 11); if (!is_dir($dir)) {
$pdf->Cell(0, 8, 'Gesamtbetrag: ' . number_format($total, 2) . ' CHF', 0, 1, 'R'); mkdir($dir, 0777, true);
IPS_LogMessage('Abrechnung', "Verzeichnis erstellt: $dir");
}
$filename = 'Abrechnung_' . date('Ymd_His', $from) . '.pdf';
$path = $dir . '/' . $filename;
$pdf->Output($path, 'F');
IPS_LogMessage('Abrechnung', "✅ PDF erfolgreich gespeichert: $path");
return $path;
} catch (Throwable $e) {
IPS_LogMessage('Abrechnung', '❌ Fehler in GenerateInvoices(): ' . $e->getMessage());
return false;
} }
$dir = IPS_GetKernelDir() . 'media/Abrechnung';
if (!is_dir($dir)) mkdir($dir, 0777, true);
$filename = 'Abrechnung_' . date('Ymd_His', $from) . '.pdf';
$path = $dir . '/' . $filename;
$pdf->Output($path, 'F');
return $path;
} }
private function GetValueAt(int $varId, int $timestamp) private function GetValueAt(int $varId, int $timestamp)
{ {
if (!AC_GetLoggedValues(0, 0, 0, 0)) return GetValue($varId); // Fallback IPS_LogMessage('Abrechnung', "GetValueAt() für VarID=$varId, Timestamp=$timestamp");
$archiveID = @IPS_GetInstanceListByModuleID('{43192F0B-135B-4CE7-A0A7-1475603F3060}')[0]; try {
if (!$archiveID) return GetValue($varId); $archiveID = @IPS_GetInstanceListByModuleID('{43192F0B-135B-4CE7-A0A7-1475603F3060}')[0];
$values = AC_GetLoggedValues($archiveID, $varId, $timestamp - 3600, $timestamp + 3600, 1); if (!$archiveID) {
if (count($values) > 0) return floatval($values[0]['Value']); IPS_LogMessage('Abrechnung', '⚠️ Kein Archivmodul gefunden, nutze aktuellen Wert');
return GetValue($varId); return GetValue($varId);
}
$values = AC_GetLoggedValues($archiveID, $varId, $timestamp - 3600, $timestamp + 3600, 1);
if (count($values) > 0) {
IPS_LogMessage('Abrechnung', 'Archivwert gefunden: ' . $values[0]['Value']);
return floatval($values[0]['Value']);
}
IPS_LogMessage('Abrechnung', '⚠️ Keine archivierten Werte, nehme aktuellen Wert');
return GetValue($varId);
} catch (Throwable $e) {
IPS_LogMessage('Abrechnung', '❌ Fehler in GetValueAt(): ' . $e->getMessage());
return null;
}
} }
private function GetTariffForMeter(array $tariffs, int $from, int $to, array $meter): float private function GetTariffForMeter(array $tariffs, int $from, int $to, array $meter): float
{ {
$type = $meter['meter_type'] ?? 'Wärme'; $type = $meter['meter_type'] ?? 'Wärme';
IPS_LogMessage('Abrechnung', "GetTariffForMeter() für Typ=$type");
$tariff = 0.0; $tariff = 0.0;
foreach ($tariffs as $t) { foreach ($tariffs as $t) {
$start = strtotime($t['start']); $start = strtotime($t['start']);
$end = strtotime($t['end']); $end = strtotime($t['end']);
if ($from >= $start && $to <= $end && $t['unit_type'] == $type) { if ($from >= $start && $to <= $end && $t['unit_type'] == $type) {
$tariff = floatval($t['price']) / 100.0; // Rp → CHF $tariff = floatval($t['price']) / 100.0;
IPS_LogMessage('Abrechnung', "Passender Tarif gefunden: {$tariff} CHF/$type");
} }
} }
if ($tariff == 0.0)
IPS_LogMessage('Abrechnung', "⚠️ Kein passender Tarif für $type gefunden");
return $tariff; return $tariff;
} }
} }