diff --git a/Abrechnung/module.php b/Abrechnung/module.php index 58206a6..6b50596 100644 --- a/Abrechnung/module.php +++ b/Abrechnung/module.php @@ -1,7 +1,7 @@ RegisterPropertyString('WaterMeters', '[]'); $this->RegisterPropertyString('Tariffs', '[]'); - // Variablen für Zeitraum und Status + // Variablen $this->RegisterVariableInteger('FromDate', 'Startdatum', '~UnixTimestamp', 1); $this->RegisterVariableInteger('ToDate', 'Enddatum', '~UnixTimestamp', 2); $this->RegisterVariableString('LastResult', 'Letzte Abrechnung', '', 3); @@ -25,25 +25,27 @@ class Abrechnung extends IPSModule // Abrechnungs-Button $this->RegisterScript('StartBilling', 'Abrechnung starten', "InstanceID . ", 'StartBilling', ''); ?>"); - // 🧾 Media-Objekt (PDF) anlegen + // 🧾 Media-Objekt für PDF-Ergebnis $this->RegisterMediaDocument('InvoicePDF', 'Letzte Rechnung', 'pdf'); } + public function ApplyChanges() + { + parent::ApplyChanges(); + IPS_LogMessage('Abrechnung', 'Modul geladen'); + } + 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 */); + $mid = IPS_CreateMedia(5); // 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); + IPS_LogMessage('Abrechnung', 'Media-Datei erstellt: media/' . $mid . '.' . $Extension); } } @@ -58,25 +60,23 @@ class Abrechnung extends IPSModule break; case 'StartBilling': - IPS_LogMessage('Abrechnung', 'Starte Abrechnung...'); $pdfContent = $this->GenerateInvoices(); - - if ($pdfContent && strlen($pdfContent) > 100) { + if ($pdfContent) { $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."; + SetValue($this->GetIDForIdent('LastResult'), 'Abrechnung vom ' . date('d.m.Y H:i')); + IPS_LogMessage('Abrechnung', '✅ Abrechnung erfolgreich gespeichert'); + echo "✅ PDF erfolgreich erstellt."; } else { - IPS_LogMessage('Abrechnung', '❌ Fehler bei der PDF-Erstellung'); + IPS_LogMessage('Abrechnung', '❌ Fehler bei PDF-Erstellung'); echo "❌ Fehler bei der PDF-Erstellung"; } break; } } - // 🧾 Hauptfunktion: PDF erzeugen (wie in PDFReportMulti) + // ====================== PDF-Logik ====================== + public function GenerateInvoices() { $from = GetValue($this->GetIDForIdent('FromDate')); @@ -87,93 +87,111 @@ class Abrechnung extends IPSModule return false; } - $users = json_decode($this->ReadPropertyString('Users'), true); - $power = json_decode($this->ReadPropertyString('PowerMeters'), true); - $water = json_decode($this->ReadPropertyString('WaterMeters'), true); + $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 (wie im SymconReport) - $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) . " | -
{$user['address']}
{$user['city']}
| Zähler | +Typ | +Start | +Ende | +Verbrauch | +Tarif (Rp) | +Kosten (CHF) | +
|---|