no message
This commit is contained in:
+62
-78
@@ -1,6 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
include_once __DIR__ . '/libs/vendor/autoload.php'; // TCPDF Autoloader laden
|
||||||
|
|
||||||
class Abrechnung extends IPSModule
|
class Abrechnung extends IPSModule
|
||||||
{
|
{
|
||||||
public function Create()
|
public function Create()
|
||||||
@@ -13,7 +15,7 @@ class Abrechnung extends IPSModule
|
|||||||
$this->RegisterPropertyString('WaterMeters', '[]');
|
$this->RegisterPropertyString('WaterMeters', '[]');
|
||||||
$this->RegisterPropertyString('Tariffs', '[]');
|
$this->RegisterPropertyString('Tariffs', '[]');
|
||||||
|
|
||||||
// Variablen für Zeitraum + Status
|
// Variablen für Zeitraum und Status
|
||||||
$this->RegisterVariableInteger('FromDate', 'Startdatum', '~UnixTimestamp', 1);
|
$this->RegisterVariableInteger('FromDate', 'Startdatum', '~UnixTimestamp', 1);
|
||||||
$this->RegisterVariableInteger('ToDate', 'Enddatum', '~UnixTimestamp', 2);
|
$this->RegisterVariableInteger('ToDate', 'Enddatum', '~UnixTimestamp', 2);
|
||||||
$this->RegisterVariableString('LastResult', 'Letzte Abrechnung', '', 3);
|
$this->RegisterVariableString('LastResult', 'Letzte Abrechnung', '', 3);
|
||||||
@@ -23,21 +25,26 @@ class Abrechnung extends IPSModule
|
|||||||
// Abrechnungs-Button
|
// Abrechnungs-Button
|
||||||
$this->RegisterScript('StartBilling', 'Abrechnung starten', "<?php IPS_RequestAction(" . $this->InstanceID . ", 'StartBilling', ''); ?>");
|
$this->RegisterScript('StartBilling', 'Abrechnung starten', "<?php IPS_RequestAction(" . $this->InstanceID . ", 'StartBilling', ''); ?>");
|
||||||
|
|
||||||
// 📄 Media-Objekt für PDF anlegen (falls nicht vorhanden)
|
// 🧾 Media-Objekt (PDF) anlegen
|
||||||
$mediaID = @IPS_GetObjectIDByIdent('InvoicePDF', $this->InstanceID);
|
$this->RegisterMediaDocument('InvoicePDF', 'Letzte Rechnung', 'pdf');
|
||||||
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()
|
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)
|
public function RequestAction($Ident, $Value)
|
||||||
@@ -52,23 +59,15 @@ class Abrechnung extends IPSModule
|
|||||||
|
|
||||||
case 'StartBilling':
|
case 'StartBilling':
|
||||||
IPS_LogMessage('Abrechnung', 'Starte Abrechnung...');
|
IPS_LogMessage('Abrechnung', 'Starte Abrechnung...');
|
||||||
$pdfPath = $this->GenerateInvoices();
|
$pdfContent = $this->GenerateInvoices();
|
||||||
|
|
||||||
if ($pdfPath && file_exists($pdfPath)) {
|
if ($pdfContent && strlen($pdfContent) > 100) {
|
||||||
$mediaID = @IPS_GetObjectIDByIdent('InvoicePDF', $this->InstanceID);
|
$mediaID = $this->GetIDForIdent('InvoicePDF');
|
||||||
if ($mediaID) {
|
IPS_SetMediaContent($mediaID, base64_encode($pdfContent));
|
||||||
// 🧩 PDF-Inhalt direkt ins Media-Objekt laden
|
IPS_LogMessage('Abrechnung', '✅ PDF erfolgreich im Media-Objekt gespeichert');
|
||||||
$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));
|
SetValue($this->GetIDForIdent('LastResult'), 'Abrechnung ' . date('Y-m-d H:i'));
|
||||||
echo "✅ Abrechnung erstellt: " . basename($pdfPath);
|
echo "✅ Abrechnung erfolgreich erstellt.";
|
||||||
} else {
|
} else {
|
||||||
IPS_LogMessage('Abrechnung', '❌ Fehler bei der PDF-Erstellung');
|
IPS_LogMessage('Abrechnung', '❌ Fehler bei der PDF-Erstellung');
|
||||||
echo "❌ 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()
|
public function GenerateInvoices()
|
||||||
{
|
{
|
||||||
IPS_LogMessage('Abrechnung', 'GenerateInvoices() gestartet');
|
|
||||||
|
|
||||||
$from = GetValue($this->GetIDForIdent('FromDate'));
|
$from = GetValue($this->GetIDForIdent('FromDate'));
|
||||||
$to = GetValue($this->GetIDForIdent('ToDate'));
|
$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) {
|
if ($from >= $to) {
|
||||||
IPS_LogMessage('Abrechnung', '❌ Ungültiger Zeitraum');
|
IPS_LogMessage('Abrechnung', '❌ Ungültiger Zeitraum');
|
||||||
@@ -96,46 +92,40 @@ class Abrechnung extends IPSModule
|
|||||||
$water = json_decode($this->ReadPropertyString('WaterMeters'), true);
|
$water = json_decode($this->ReadPropertyString('WaterMeters'), true);
|
||||||
$tariffs = json_decode($this->ReadPropertyString('Tariffs'), 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
|
// TCPDF initialisieren (wie im SymconReport)
|
||||||
$lib = __DIR__ . '/libs/tcpdf/tcpdf.php';
|
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
|
||||||
if (!file_exists($lib)) {
|
$pdf->SetCreator(PDF_CREATOR);
|
||||||
IPS_LogMessage('Abrechnung', "❌ TCPDF nicht gefunden unter $lib");
|
$pdf->SetAuthor('IPSymcon Abrechnung');
|
||||||
return false;
|
|
||||||
}
|
|
||||||
require_once $lib;
|
|
||||||
|
|
||||||
$pdf = new TCPDF();
|
|
||||||
$pdf->SetCreator('IPSymcon Abrechnung');
|
|
||||||
$pdf->SetAuthor('Abrechnung Modul');
|
|
||||||
$pdf->SetTitle('Zählerabrechnung');
|
$pdf->SetTitle('Zählerabrechnung');
|
||||||
$pdf->SetMargins(15, 15, 15);
|
$pdf->SetSubject('Automatische Abrechnung');
|
||||||
$pdf->SetAutoPageBreak(true, 20);
|
|
||||||
|
|
||||||
|
$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) {
|
foreach ($users as $user) {
|
||||||
$pdf->AddPage();
|
$pdf->AddPage();
|
||||||
$pdf->SetFont('helvetica', 'B', 14);
|
|
||||||
$pdf->Cell(0, 10, 'Rechnung für ' . $user['name'], 0, 1);
|
$pdf->writeHTML("<h2>Rechnung für {$user['name']}</h2>", true, false, true, false, '');
|
||||||
$pdf->SetFont('helvetica', '', 10);
|
$pdf->writeHTML("<p>{$user['address']}<br>{$user['city']}</p>", true, false, true, false, '');
|
||||||
$pdf->Cell(0, 6, $user['address'] . ' - ' . $user['city'], 0, 1);
|
|
||||||
$pdf->Ln(5);
|
$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(
|
$userMeters = array_merge(
|
||||||
array_filter($power, fn($m) => $m['user_id'] == $user['id']),
|
array_filter($power, fn($m) => $m['user_id'] == $user['id']),
|
||||||
array_filter($water, fn($m) => $m['user_id'] == $user['id'])
|
array_filter($water, fn($m) => $m['user_id'] == $user['id'])
|
||||||
);
|
);
|
||||||
|
|
||||||
$total = 0.0;
|
$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) {
|
foreach ($userMeters as $m) {
|
||||||
$start = $this->GetValueAt($m['var_consumption'], $from);
|
$start = $this->GetValueAt($m['var_consumption'], $from);
|
||||||
$end = $this->GetValueAt($m['var_consumption'], $to);
|
$end = $this->GetValueAt($m['var_consumption'], $to);
|
||||||
@@ -146,27 +136,21 @@ class Abrechnung extends IPSModule
|
|||||||
$cost = $diff * $price;
|
$cost = $diff * $price;
|
||||||
$total += $cost;
|
$total += $cost;
|
||||||
|
|
||||||
$pdf->Cell(60, 7, $m['name'], 1);
|
$pdf->writeHTML("
|
||||||
$pdf->Cell(35, 7, number_format($start, 2), 1);
|
<tr>
|
||||||
$pdf->Cell(35, 7, number_format($end, 2), 1);
|
<td>{$m['name']}</td>
|
||||||
$pdf->Cell(25, 7, number_format($diff, 2), 1);
|
<td>$start</td>
|
||||||
$pdf->Cell(25, 7, number_format($cost, 2), 1);
|
<td>$end</td>
|
||||||
$pdf->Ln();
|
<td>$diff</td>
|
||||||
|
<td>" . number_format($cost, 2) . "</td>
|
||||||
|
</tr>", false, false, false, false, '');
|
||||||
}
|
}
|
||||||
|
|
||||||
$pdf->Ln(4);
|
$pdf->writeHTML("</table><br><h3>Gesamt: " . number_format($total, 2) . " CHF</h3>", true, false, true, false, '');
|
||||||
$pdf->SetFont('helvetica', 'B', 11);
|
|
||||||
$pdf->Cell(0, 8, 'Gesamtbetrag: ' . number_format($total, 2) . ' CHF', 0, 1, 'R');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 💾 PDF im temporären Ordner speichern
|
// Ausgabe als String (nicht auf Datei)
|
||||||
$dir = sys_get_temp_dir();
|
return $pdf->Output('Abrechnung.pdf', 'S'); // "S" = return as string
|
||||||
$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)
|
private function GetValueAt(int $varId, int $timestamp)
|
||||||
|
|||||||
Reference in New Issue
Block a user