RegisterPropertyString('Users', '[]'); $this->RegisterPropertyString('PowerMeters', '[]'); $this->RegisterPropertyString('WaterMeters', '[]'); $this->RegisterPropertyString('Tariffs', '[]'); // Variablen für UI in der Visualisierung $this->RegisterVariableInteger('FromDate', 'Startdatum', '~UnixTimestamp', 1); $this->RegisterVariableInteger('ToDate', 'Enddatum', '~UnixTimestamp', 2); $this->RegisterVariableString('LastResult', 'Letzte Abrechnung', '', 3); $this->EnableAction('FromDate'); $this->EnableAction('ToDate'); // Skript-Button für Abrechnung $this->RegisterScript('StartBilling', 'Abrechnung starten', "InstanceID . ", 'StartBilling', ''); ?>"); } public function ApplyChanges() { parent::ApplyChanges(); } // UI-Eingaben (Visualisierung) public function RequestAction($Ident, $Value) { switch ($Ident) { case 'FromDate': SetValue($this->GetIDForIdent('FromDate'), $Value); break; case 'ToDate': SetValue($this->GetIDForIdent('ToDate'), $Value); break; case 'StartBilling': $pdfPath = $this->GenerateInvoices(); if ($pdfPath) { SetValue($this->GetIDForIdent('LastResult'), basename($pdfPath)); echo "✅ Abrechnung erstellt: " . basename($pdfPath); } else { echo "❌ Fehler bei der PDF-Erstellung"; } break; } } // Hauptfunktion: PDF generieren 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); // TCPDF laden if (!class_exists('TCPDF')) { $lib = IPS_GetKernelDir() . 'libs/tcpdf/tcpdf.php'; if (!file_exists($lib)) { IPS_LogMessage('Abrechnung', 'TCPDF fehlt in /libs/tcpdf/'); return false; } require_once $lib; } $pdf = new TCPDF(); $pdf->SetCreator('IPSymcon Abrechnung'); $pdf->SetAuthor('Abrechnung Modul'); $pdf->SetTitle('Zählerabrechnung'); $pdf->SetMargins(15, 15, 15); $pdf->SetAutoPageBreak(true, 20); 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); // Alle Zähler des Benutzers $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); if ($start === null || $end === null) continue; $diff = max(0, $end - $start); $price = $this->GetTariffForMeter($tariffs, $from, $to, $m); $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->Ln(4); $pdf->SetFont('helvetica', 'B', 11); $pdf->Cell(0, 8, 'Gesamtbetrag: ' . number_format($total, 2) . ' CHF', 0, 1, 'R'); } $dir = IPS_GetKernelDir() . 'media/Abrechnung'; if (!is_dir($dir)) mkdir($dir, 0777, true); $filename = 'Abrechnung_' . date('Ymd_His', $from) . '.pdf'; $path = $dir . '/' . $filename; $pdf->Output($path, 'F'); return $path; } private function GetValueAt(int $varId, int $timestamp) { if (!AC_GetLoggedValues(0, 0, 0, 0)) return GetValue($varId); // Fallback $archiveID = @IPS_GetInstanceListByModuleID('{43192F0B-135B-4CE7-A0A7-1475603F3060}')[0]; if (!$archiveID) return GetValue($varId); $values = AC_GetLoggedValues($archiveID, $varId, $timestamp - 3600, $timestamp + 3600, 1); if (count($values) > 0) return floatval($values[0]['Value']); return GetValue($varId); } private function GetTariffForMeter(array $tariffs, int $from, int $to, array $meter): float { $type = $meter['meter_type'] ?? 'Wärme'; $tariff = 0.0; foreach ($tariffs as $t) { $start = strtotime($t['start']); $end = strtotime($t['end']); if ($from >= $start && $to <= $end && $t['unit_type'] == $type) { $tariff = floatval($t['price']) / 100.0; // Rp → CHF } } return $tariff; } }