no message

This commit is contained in:
2025-11-04 07:42:40 +01:00
parent 78dd81f961
commit 4dcc1e7099

View File

@@ -1,6 +1,8 @@
<?php
declare(strict_types=1);
include_once __DIR__ . '/libs/vendor/autoload.php'; // TCPDF Autoloader laden
class Abrechnung extends IPSModule
{
public function Create()
@@ -13,7 +15,7 @@ class Abrechnung extends IPSModule
$this->RegisterPropertyString('WaterMeters', '[]');
$this->RegisterPropertyString('Tariffs', '[]');
// Variablen für Zeitraum + Status
// Variablen für Zeitraum und Status
$this->RegisterVariableInteger('FromDate', 'Startdatum', '~UnixTimestamp', 1);
$this->RegisterVariableInteger('ToDate', 'Enddatum', '~UnixTimestamp', 2);
$this->RegisterVariableString('LastResult', 'Letzte Abrechnung', '', 3);
@@ -23,21 +25,26 @@ class Abrechnung extends IPSModule
// Abrechnungs-Button
$this->RegisterScript('StartBilling', 'Abrechnung starten', "<?php IPS_RequestAction(" . $this->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');
}
// 🧾 Media-Objekt (PDF) anlegen
$this->RegisterMediaDocument('InvoicePDF', 'Letzte Rechnung', 'pdf');
}
public function ApplyChanges()
private function RegisterMediaDocument($Ident, $Name, $Extension, $Position = 0)
{
parent::ApplyChanges();
$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)
@@ -52,23 +59,15 @@ class Abrechnung extends IPSModule
case 'StartBilling':
IPS_LogMessage('Abrechnung', 'Starte Abrechnung...');
$pdfPath = $this->GenerateInvoices();
$pdfContent = $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");
}
}
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'), basename($pdfPath));
echo "✅ Abrechnung erstellt: " . basename($pdfPath);
SetValue($this->GetIDForIdent('LastResult'), 'Abrechnung ' . date('Y-m-d H:i'));
echo "✅ Abrechnung erfolgreich erstellt.";
} else {
IPS_LogMessage('Abrechnung', '❌ Fehler bei der PDF-Erstellung');
echo "❌ Fehler bei der PDF-Erstellung";
@@ -77,14 +76,11 @@ class Abrechnung extends IPSModule
}
}
// 🧾 Hauptfunktion: PDF generieren
// 🧾 Hauptfunktion: PDF erzeugen (wie in PDFReportMulti)
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');
@@ -96,46 +92,40 @@ class Abrechnung extends IPSModule
$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));
IPS_LogMessage('Abrechnung', 'Starte PDF-Erstellung mit TCPDF...');
// 🔧 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');
// 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->SetMargins(15, 15, 15);
$pdf->SetAutoPageBreak(true, 20);
$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->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);
$pdf->writeHTML("<h2>Rechnung für {$user['name']}</h2>", true, false, true, false, '');
$pdf->writeHTML("<p>{$user['address']}<br>{$user['city']}</p>", true, false, true, false, '');
$pdf->writeHTML("<table border=\"1\" cellpadding=\"4\">
<tr style=\"background-color:#eee;\">
<th>Zähler</th><th>Start</th><th>Ende</th><th>Verbrauch</th><th>Kosten (CHF)</th>
</tr>", false, false, false, false, '');
$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);
@@ -146,27 +136,21 @@ class Abrechnung extends IPSModule
$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->writeHTML("
<tr>
<td>{$m['name']}</td>
<td>$start</td>
<td>$end</td>
<td>$diff</td>
<td>" . number_format($cost, 2) . "</td>
</tr>", false, false, false, false, '');
}
$pdf->Ln(4);
$pdf->SetFont('helvetica', 'B', 11);
$pdf->Cell(0, 8, 'Gesamtbetrag: ' . number_format($total, 2) . ' CHF', 0, 1, 'R');
$pdf->writeHTML("</table><br><h3>Gesamt: " . number_format($total, 2) . " CHF</h3>", true, false, true, false, '');
}
// 💾 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;
// Ausgabe als String (nicht auf Datei)
return $pdf->Output('Abrechnung.pdf', 'S'); // "S" = return as string
}
private function GetValueAt(int $varId, int $timestamp)