RegisterPropertyString('Users', '[]'); $this->RegisterPropertyString('PowerMeters', '[]'); $this->RegisterPropertyString('WaterMeters', '[]'); $this->RegisterPropertyString('Tariffs', '[]'); // Variablen für Zeitraum + Status $this->RegisterVariableInteger('FromDate', 'Startdatum', '~UnixTimestamp', 1); $this->RegisterVariableInteger('ToDate', 'Enddatum', '~UnixTimestamp', 2); $this->RegisterVariableString('LastResult', 'Letzte Abrechnung', '', 3); $this->EnableAction('FromDate'); $this->EnableAction('ToDate'); // Abrechnungs-Button $this->RegisterScript('StartBilling', 'Abrechnung starten', "InstanceID . ", 'StartBilling', ''); ?>"); // 📄 Media-Objekt für PDF anlegen (falls nicht vorhanden) $mediaID = @IPS_GetObjectIDByIdent('InvoicePDF', $this->InstanceID); if ($mediaID === false) { $mediaID = IPS_CreateMedia(3); // 3 = Dokument (PDF) IPS_SetParent($mediaID, $this->InstanceID); IPS_SetIdent($mediaID, 'InvoicePDF'); IPS_SetName($mediaID, 'Letzte Rechnung'); IPS_SetMediaCached($mediaID, false); IPS_LogMessage('Abrechnung', 'Media-Objekt "Letzte Rechnung" angelegt'); } } public function ApplyChanges() { parent::ApplyChanges(); } public function RequestAction($Ident, $Value) { IPS_LogMessage('Abrechnung', "RequestAction: $Ident"); switch ($Ident) { case 'FromDate': case 'ToDate': SetValue($this->GetIDForIdent($Ident), $Value); break; case 'StartBilling': IPS_LogMessage('Abrechnung', 'Starte Abrechnung...'); $pdfPath = $this->GenerateInvoices(); if ($pdfPath && file_exists($pdfPath)) { $mediaID = @IPS_GetObjectIDByIdent('InvoicePDF', $this->InstanceID); if ($mediaID) { // 🧩 PDF-Inhalt direkt ins Media-Objekt laden $content = file_get_contents($pdfPath); if ($content !== false && strlen($content) > 100) { IPS_SetMediaContent($mediaID, base64_encode($content)); IPS_LogMessage('Abrechnung', "✅ PDF erfolgreich in Media-Variable gespeichert ($pdfPath)"); } else { IPS_LogMessage('Abrechnung', "⚠️ PDF-Datei leer oder fehlerhaft: $pdfPath"); } } SetValue($this->GetIDForIdent('LastResult'), basename($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'); return false; } $users = json_decode($this->ReadPropertyString('Users'), true); $power = json_decode($this->ReadPropertyString('PowerMeters'), true); $water = json_decode($this->ReadPropertyString('WaterMeters'), true); $tariffs = json_decode($this->ReadPropertyString('Tariffs'), true); IPS_LogMessage('Abrechnung', "Benutzer=" . count($users) . ", Power=" . count($power) . ", Water=" . count($water) . ", Tarife=" . count($tariffs)); // 🔧 TCPDF einbinden $lib = __DIR__ . '/libs/tcpdf/tcpdf.php'; if (!file_exists($lib)) { IPS_LogMessage('Abrechnung', "❌ TCPDF nicht gefunden unter $lib"); return false; } require_once $lib; $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); $userMeters = array_merge( array_filter($power, fn($m) => $m['user_id'] == $user['id']), array_filter($water, fn($m) => $m['user_id'] == $user['id']) ); $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); 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); $pdf->Ln(); } $pdf->Ln(4); $pdf->SetFont('helvetica', 'B', 11); $pdf->Cell(0, 8, 'Gesamtbetrag: ' . number_format($total, 2) . ' CHF', 0, 1, 'R'); } // 💾 PDF im temporären Ordner speichern $dir = sys_get_temp_dir(); $filename = 'Abrechnung_' . date('Ymd_His') . '.pdf'; $path = $dir . '/' . $filename; $pdf->Output($path, 'F'); IPS_LogMessage('Abrechnung', "PDF gespeichert unter: $path"); return $path; } private function GetValueAt(int $varId, int $timestamp) { $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); } private function GetTariffForMeter(array $tariffs, int $from, int $to, array $meter): float { $type = $meter['meter_type'] ?? 'Wärme'; $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; } } return $tariff; } }