From 1c67b9f50b4d3dec5edb56840d870108f8b53471 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=A4fliger?= Date: Tue, 4 Nov 2025 07:22:21 +0100 Subject: [PATCH] no message --- Abrechnung/module.php | 87 ++++++++++++++++++++++++++----------------- 1 file changed, 53 insertions(+), 34 deletions(-) diff --git a/Abrechnung/module.php b/Abrechnung/module.php index 666502b..a63660a 100644 --- a/Abrechnung/module.php +++ b/Abrechnung/module.php @@ -3,25 +3,36 @@ declare(strict_types=1); class Abrechnung extends IPSModule { - public function Create() - { - parent::Create(); +public function Create() +{ + parent::Create(); - $this->RegisterPropertyString('Users', '[]'); - $this->RegisterPropertyString('PowerMeters', '[]'); - $this->RegisterPropertyString('WaterMeters', '[]'); - $this->RegisterPropertyString('Tariffs', '[]'); + $this->RegisterPropertyString('Users', '[]'); + $this->RegisterPropertyString('PowerMeters', '[]'); + $this->RegisterPropertyString('WaterMeters', '[]'); + $this->RegisterPropertyString('Tariffs', '[]'); - $this->RegisterVariableInteger('FromDate', 'Startdatum', '~UnixTimestamp', 1); - $this->RegisterVariableInteger('ToDate', 'Enddatum', '~UnixTimestamp', 2); - $this->RegisterVariableString('LastResult', 'Letzte Abrechnung', '', 3); - $this->EnableAction('FromDate'); - $this->EnableAction('ToDate'); + $this->RegisterVariableInteger('FromDate', 'Startdatum', '~UnixTimestamp', 1); + $this->RegisterVariableInteger('ToDate', 'Enddatum', '~UnixTimestamp', 2); + $this->RegisterVariableString('LastResult', 'Letzte Abrechnung', '', 3); + $this->EnableAction('FromDate'); + $this->EnableAction('ToDate'); - $this->RegisterScript('StartBilling', 'Abrechnung starten', "InstanceID . ", 'StartBilling', ''); ?>"); + $this->RegisterScript('StartBilling', 'Abrechnung starten', "InstanceID . ", 'StartBilling', ''); ?>"); - IPS_LogMessage('Abrechnung', 'Create() fertig ausgeführt'); + // 🧾 Media-Objekt für PDF-Ergebnis anlegen (falls nicht vorhanden) + $mediaID = @IPS_GetObjectIDByIdent('InvoicePDF', $this->InstanceID); + if ($mediaID === false) { + $mediaID = IPS_CreateMedia(3); // 3 = PDF + IPS_SetParent($mediaID, $this->InstanceID); + IPS_SetIdent($mediaID, 'InvoicePDF'); + IPS_SetName($mediaID, 'Letzte Rechnung'); + IPS_SetMediaCached($mediaID, false); + $path = IPS_GetKernelDir() . 'media/Abrechnung_Letzte.pdf'; + IPS_SetMediaFile($mediaID, $path, false); + IPS_LogMessage('Abrechnung', "Media-Datei erstellt: $path"); } +} public function ApplyChanges() { @@ -29,30 +40,38 @@ class Abrechnung extends IPSModule IPS_LogMessage('Abrechnung', 'ApplyChanges() ausgeführt'); } - public function RequestAction($Ident, $Value) - { - IPS_LogMessage('Abrechnung', "RequestAction aufgerufen: $Ident = $Value"); - switch ($Ident) { - case 'FromDate': - case 'ToDate': - SetValue($this->GetIDForIdent($Ident), $Value); - IPS_LogMessage('Abrechnung', "Wert für $Ident gesetzt: " . date('Y-m-d H:i', $Value)); - break; +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) { - SetValue($this->GetIDForIdent('LastResult'), basename($pdfPath)); - IPS_LogMessage('Abrechnung', "✅ Abrechnung erstellt: $pdfPath"); - echo "✅ Abrechnung erstellt: " . basename($pdfPath); + case 'StartBilling': + IPS_LogMessage('Abrechnung', 'Starte Abrechnung...'); + $pdfPath = $this->GenerateInvoices(); + + if ($pdfPath && file_exists($pdfPath)) { + // 🔗 PDF in Media-Objekt speichern + $mediaID = @IPS_GetObjectIDByIdent('InvoicePDF', $this->InstanceID); + if ($mediaID) { + IPS_SetMediaFile($mediaID, $pdfPath, false); + IPS_LogMessage('Abrechnung', "✅ PDF in Media-Variable aktualisiert: $pdfPath"); } else { - IPS_LogMessage('Abrechnung', '❌ Fehler bei der PDF-Erstellung'); - echo "❌ Fehler bei der PDF-Erstellung"; + IPS_LogMessage('Abrechnung', "⚠️ Keine Media-Variable gefunden, PDF bleibt im Dateisystem"); } - break; - } + + 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; } +} public function GenerateInvoices() {