RegisterPropertyString('Users', '[]'); $this->RegisterPropertyString('PowerMeters', '[]'); $this->RegisterPropertyString('WaterMeters', '[]'); $this->RegisterPropertyString('Tariffs', '[]'); // Variablen für Zeitraum und 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 das PDF $this->RegisterMediaDocument('InvoicePDF', 'Letzte Rechnung', 'pdf'); } public function ApplyChanges() { parent::ApplyChanges(); } private function RegisterMediaDocument($Ident, $Name, $Extension, $Position = 0) { $this->RegisterMedia(5, $Ident, $Name, $Extension, $Position); } private function RegisterMedia($Type, $Ident, $Name, $Extension, $Position) { $mid = @IPS_GetObjectIDByIdent($Ident, $this->InstanceID); if ($mid === false) { $mid = IPS_CreateMedia(5 /* Document */); IPS_SetParent($mid, $this->InstanceID); IPS_SetIdent($mid, $Ident); IPS_SetName($mid, $Name); IPS_SetPosition($mid, $Position); IPS_SetMediaFile($mid, 'media/' . $mid . '.' . $Extension, false); } } 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...'); $pdfContent = $this->GenerateInvoices(); if ($pdfContent && strlen($pdfContent) > 100) { $mediaID = $this->GetIDForIdent('InvoicePDF'); IPS_SetMediaContent($mediaID, base64_encode($pdfContent)); IPS_LogMessage('Abrechnung', '✅ PDF erfolgreich im Media-Objekt gespeichert'); SetValue($this->GetIDForIdent('LastResult'), 'Abrechnung ' . date('Y-m-d H:i')); echo "✅ Abrechnung erfolgreich erstellt."; } else { IPS_LogMessage('Abrechnung', '❌ Fehler bei der PDF-Erstellung oder leeres PDF'); echo "❌ Fehler bei der PDF-Erstellung"; } break; } } // 🧾 Hauptfunktion: PDF erzeugen (wie SymconReport) public function GenerateInvoices() { $from = GetValue($this->GetIDForIdent('FromDate')); $to = GetValue($this->GetIDForIdent('ToDate')); 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', 'Starte PDF-Erstellung mit TCPDF...'); // TCPDF initialisieren $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); $pdf->SetCreator(PDF_CREATOR); $pdf->SetAuthor('IPSymcon Abrechnung'); $pdf->SetTitle('Zählerabrechnung'); $pdf->SetSubject('Automatische Abrechnung'); $pdf->setPrintHeader(false); $pdf->setFooterFont([PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA]); $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP - 15, PDF_MARGIN_RIGHT); $pdf->SetAutoPageBreak(true, PDF_MARGIN_BOTTOM); $pdf->SetFont('dejavusans', '', 10); // Jede Seite = Benutzer foreach ($users as $user) { $pdf->AddPage(); $pdf->writeHTML("
{$user['address']}
{$user['city']}
| Zähler | Start | Ende | Verbrauch | Kosten (CHF) |
|---|---|---|---|---|
| {$m['name']} | $start | $end | $diff | " . number_format($cost, 2) . " |