From 5ae542061cc0473a5c59f9595c9dd5558bbe701d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=A4fliger?= Date: Tue, 4 Nov 2025 07:10:59 +0100 Subject: [PATCH] no message --- Abrechnung/module.php | 200 ++++++++++++++++++++++++++---------------- 1 file changed, 125 insertions(+), 75 deletions(-) diff --git a/Abrechnung/module.php b/Abrechnung/module.php index b33dea1..666502b 100644 --- a/Abrechnung/module.php +++ b/Abrechnung/module.php @@ -7,57 +7,63 @@ class Abrechnung extends IPSModule { parent::Create(); - // Eigenschaften registrieren $this->RegisterPropertyString('Users', '[]'); $this->RegisterPropertyString('PowerMeters', '[]'); $this->RegisterPropertyString('WaterMeters', '[]'); $this->RegisterPropertyString('Tariffs', '[]'); - // Variablen für UI in der Visualisierung $this->RegisterVariableInteger('FromDate', 'Startdatum', '~UnixTimestamp', 1); $this->RegisterVariableInteger('ToDate', 'Enddatum', '~UnixTimestamp', 2); $this->RegisterVariableString('LastResult', 'Letzte Abrechnung', '', 3); $this->EnableAction('FromDate'); $this->EnableAction('ToDate'); - // Skript-Button für Abrechnung $this->RegisterScript('StartBilling', 'Abrechnung starten', "InstanceID . ", 'StartBilling', ''); ?>"); + + IPS_LogMessage('Abrechnung', 'Create() fertig ausgeführt'); } public function ApplyChanges() { parent::ApplyChanges(); + IPS_LogMessage('Abrechnung', 'ApplyChanges() ausgeführt'); } - // UI-Eingaben (Visualisierung) public function RequestAction($Ident, $Value) { + IPS_LogMessage('Abrechnung', "RequestAction aufgerufen: $Ident = $Value"); switch ($Ident) { case 'FromDate': - SetValue($this->GetIDForIdent('FromDate'), $Value); - break; 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; + case 'StartBilling': + IPS_LogMessage('Abrechnung', 'Starte Abrechnung...'); $pdfPath = $this->GenerateInvoices(); if ($pdfPath) { SetValue($this->GetIDForIdent('LastResult'), basename($pdfPath)); + IPS_LogMessage('Abrechnung', "✅ Abrechnung erstellt: $pdfPath"); echo "✅ Abrechnung erstellt: " . basename($pdfPath); } else { + IPS_LogMessage('Abrechnung', '❌ Fehler bei der PDF-Erstellung'); echo "❌ Fehler bei der PDF-Erstellung"; } break; } } - // Hauptfunktion: PDF generieren public function GenerateInvoices() { + IPS_LogMessage('Abrechnung', 'GenerateInvoices() gestartet'); + $from = GetValue($this->GetIDForIdent('FromDate')); $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) { - IPS_LogMessage('Abrechnung', 'Ungültiger Zeitraum'); + IPS_LogMessage('Abrechnung', '❌ Ungültiger Zeitraum'); return false; } @@ -65,101 +71,145 @@ class Abrechnung extends IPSModule $power = json_decode($this->ReadPropertyString('PowerMeters'), true); $water = json_decode($this->ReadPropertyString('WaterMeters'), 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 - if (!class_exists('TCPDF')) { - $lib = IPS_GetKernelDir() . 'libs/tcpdf/tcpdf.php'; - if (!file_exists($lib)) { - IPS_LogMessage('Abrechnung', 'TCPDF fehlt in /libs/tcpdf/'); - return false; - } + $lib = __DIR__ . '/libs/tcpdf/tcpdf.php'; + IPS_LogMessage('Abrechnung', "Suche TCPDF unter: $lib"); + if (!file_exists($lib)) { + IPS_LogMessage('Abrechnung', '❌ TCPDF nicht gefunden!'); + return false; + } + + try { 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(); - $pdf->SetCreator('IPSymcon Abrechnung'); - $pdf->SetAuthor('Abrechnung Modul'); - $pdf->SetTitle('Zählerabrechnung'); - $pdf->SetMargins(15, 15, 15); - $pdf->SetAutoPageBreak(true, 20); + try { + $pdf = new TCPDF(); + $pdf->SetCreator('IPSymcon Abrechnung'); + $pdf->SetAuthor('Abrechnung Modul'); + $pdf->SetTitle('Zählerabrechnung'); + $pdf->SetMargins(15, 15, 15); + $pdf->SetAutoPageBreak(true, 20); - foreach ($users as $user) { - $pdf->AddPage(); - $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); + foreach ($users as $user) { + IPS_LogMessage('Abrechnung', 'Erstelle Seite für Benutzer: ' . $user['name']); - // Alle Zähler des Benutzers - $userMeters = array_merge( - array_filter($power, fn($m) => $m['user_id'] == $user['id']), - array_filter($water, fn($m) => $m['user_id'] == $user['id']) - ); + $pdf->AddPage(); + $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); - $total = 0.0; - $pdf->SetFont('helvetica', 'B', 11); - $pdf->Cell(60, 8, 'Zähler', 1); - $pdf->Cell(35, 8, 'Start', 1); - $pdf->Cell(35, 8, 'Ende', 1); - $pdf->Cell(25, 8, 'Verbrauch', 1); - $pdf->Cell(25, 8, 'Kosten', 1); - $pdf->Ln(); + $userMeters = array_merge( + array_filter($power, fn($m) => $m['user_id'] == $user['id']), + array_filter($water, fn($m) => $m['user_id'] == $user['id']) + ); + IPS_LogMessage('Abrechnung', 'Zähler gefunden: ' . count($userMeters)); - $pdf->SetFont('helvetica', '', 9); - foreach ($userMeters as $m) { - $start = $this->GetValueAt($m['var_consumption'], $from); - $end = $this->GetValueAt($m['var_consumption'], $to); - if ($start === null || $end === null) continue; - - $diff = max(0, $end - $start); - $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); + $total = 0.0; + $pdf->SetFont('helvetica', 'B', 11); + $pdf->Cell(60, 8, 'Zähler', 1); + $pdf->Cell(35, 8, 'Start', 1); + $pdf->Cell(35, 8, 'Ende', 1); + $pdf->Cell(25, 8, 'Verbrauch', 1); + $pdf->Cell(25, 8, 'Kosten', 1); $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); - $pdf->SetFont('helvetica', 'B', 11); - $pdf->Cell(0, 8, 'Gesamtbetrag: ' . number_format($total, 2) . ' CHF', 0, 1, 'R'); + $dir = IPS_GetKernelDir() . 'media/Abrechnung'; + if (!is_dir($dir)) { + 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) { - if (!AC_GetLoggedValues(0, 0, 0, 0)) return GetValue($varId); // Fallback - $archiveID = @IPS_GetInstanceListByModuleID('{43192F0B-135B-4CE7-A0A7-1475603F3060}')[0]; - if (!$archiveID) return GetValue($varId); - $values = AC_GetLoggedValues($archiveID, $varId, $timestamp - 3600, $timestamp + 3600, 1); - if (count($values) > 0) return floatval($values[0]['Value']); - return GetValue($varId); + IPS_LogMessage('Abrechnung', "GetValueAt() für VarID=$varId, Timestamp=$timestamp"); + try { + $archiveID = @IPS_GetInstanceListByModuleID('{43192F0B-135B-4CE7-A0A7-1475603F3060}')[0]; + if (!$archiveID) { + IPS_LogMessage('Abrechnung', '⚠️ Kein Archivmodul gefunden, nutze aktuellen Wert'); + 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 { $type = $meter['meter_type'] ?? 'Wärme'; + IPS_LogMessage('Abrechnung', "GetTariffForMeter() für Typ=$type"); $tariff = 0.0; + foreach ($tariffs as $t) { $start = strtotime($t['start']); $end = strtotime($t['end']); 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; } }