no message
This commit is contained in:
@@ -95,11 +95,10 @@ class Abrechnung extends IPSModule
|
||||
$pdf->SetAutoPageBreak(true, 20);
|
||||
$pdf->SetFont('dejavusans', '', 9);
|
||||
|
||||
foreach ($users as $user) {
|
||||
$pdf->AddPage();
|
||||
$html = $this->BuildUserInvoice($user, $power, $water, $tariffs, $from, $to);
|
||||
$pdf->writeHTML($html);
|
||||
}
|
||||
foreach ($users as $user) {
|
||||
IPS_LogMessage('Abrechnung', '→ Erstelle Seite für Benutzer: ' . $user['name']);
|
||||
$this->BuildUserInvoice($pdf, $user, $power, $water, $tariffs, $from, $to);
|
||||
}
|
||||
|
||||
return $pdf->Output('Abrechnung.pdf', 'S');
|
||||
}
|
||||
@@ -107,122 +106,104 @@ class Abrechnung extends IPSModule
|
||||
// ===========================================================
|
||||
// 🧱 Layout / PDF-Aufbau pro Benutzerseite
|
||||
// ===========================================================
|
||||
private function BuildUserInvoice($user, $power, $water, $tariffs, $from, $to)
|
||||
private function CalculatePowerCosts(array $powerMeters, array $tariffs, int $userId, int $from, int $to): array
|
||||
{
|
||||
$stromRows = '';
|
||||
$stromTotal = 0.0;
|
||||
$stromTariffsUsed = [];
|
||||
$html = "<table border='1' cellspacing='0' cellpadding='3' width='100%' style='font-size:8px;'>
|
||||
<tr style='background-color:#f0f0f0;'>
|
||||
<th>Zähler</th>
|
||||
<th>Typ</th>
|
||||
<th>Start</th>
|
||||
<th>Ende</th>
|
||||
<th>Zählerstand Start</th>
|
||||
<th>Zählerstand Ende</th>
|
||||
<th>Verbrauch</th>
|
||||
<th>Tarif (Rp)</th>
|
||||
<th>Kosten (CHF)</th>
|
||||
</tr>";
|
||||
$total = 0.0;
|
||||
|
||||
$nebenRows = '';
|
||||
$nebenTotal = 0.0;
|
||||
$nebenTariffsUsed = [];
|
||||
|
||||
// ---------- Stromkosten ----------
|
||||
foreach ($power as $m) {
|
||||
if ($m['user_id'] != $user['id']) continue;
|
||||
$res = $this->AddMeterToPDFRow($m, $tariffs, $from, $to, 'Strombezug');
|
||||
$stromRows .= $res['row'];
|
||||
$stromTotal += $res['value'];
|
||||
foreach ($powerMeters as $m) {
|
||||
if ($m['user_id'] != $userId) continue;
|
||||
$cost = $this->AddMeterToPDFRow($m, $tariffs, $from, $to, 'Strombezug');
|
||||
$html .= $cost['row'];
|
||||
$total += $cost['value'];
|
||||
}
|
||||
|
||||
// ---------- Nebenkosten ----------
|
||||
foreach ($water as $m) {
|
||||
if ($m['user_id'] != $user['id']) continue;
|
||||
$html .= "
|
||||
<tr style='background-color:#f9f9f9; font-weight:bold;'>
|
||||
<td colspan='8' align='right'>Total Stromkosten:</td>
|
||||
<td align='right'>" . number_format($total, 2) . "</td>
|
||||
</tr>
|
||||
</table><br>";
|
||||
|
||||
return ['html' => $html, 'sum' => $total];
|
||||
}
|
||||
|
||||
private function CalculatePowerCosts(array $powerMeters, array $tariffs, int $userId, int $from, int $to): array
|
||||
{
|
||||
$html = "<table border='1' cellspacing='0' cellpadding='3' width='100%' style='font-size:8px;'>
|
||||
<tr style='background-color:#f0f0f0;'>
|
||||
<th>Zähler</th>
|
||||
<th>Typ</th>
|
||||
<th>Start</th>
|
||||
<th>Ende</th>
|
||||
<th>Zählerstand Start</th>
|
||||
<th>Zählerstand Ende</th>
|
||||
<th>Verbrauch</th>
|
||||
<th>Tarif (Rp)</th>
|
||||
<th>Kosten (CHF)</th>
|
||||
</tr>";
|
||||
$total = 0.0;
|
||||
|
||||
foreach ($powerMeters as $m) {
|
||||
if ($m['user_id'] != $userId) continue;
|
||||
$cost = $this->AddMeterToPDFRow($m, $tariffs, $from, $to, 'Strombezug');
|
||||
$html .= $cost['row'];
|
||||
$total += $cost['value'];
|
||||
}
|
||||
|
||||
$html .= "
|
||||
<tr style='background-color:#f9f9f9; font-weight:bold;'>
|
||||
<td colspan='8' align='right'>Total Stromkosten:</td>
|
||||
<td align='right'>" . number_format($total, 2) . "</td>
|
||||
</tr>
|
||||
</table><br>";
|
||||
|
||||
return ['html' => $html, 'sum' => $total];
|
||||
}
|
||||
|
||||
private function CalculateAdditionalCosts(array $waterMeters, array $tariffs, int $userId, int $from, int $to): array
|
||||
{
|
||||
$html = "<table border='1' cellspacing='0' cellpadding='3' width='100%' style='font-size:8px;'>
|
||||
<tr style='background-color:#f0f0f0;'>
|
||||
<th>Zähler</th>
|
||||
<th>Typ</th>
|
||||
<th>Start</th>
|
||||
<th>Ende</th>
|
||||
<th>Zählerstand Start</th>
|
||||
<th>Zählerstand Ende</th>
|
||||
<th>Verbrauch</th>
|
||||
<th>Tarif (Rp)</th>
|
||||
<th>Kosten (CHF)</th>
|
||||
</tr>";
|
||||
$total = 0.0;
|
||||
|
||||
foreach ($waterMeters as $m) {
|
||||
if ($m['user_id'] != $userId) continue;
|
||||
$type = $m['meter_type'] ?? 'Warmwasser';
|
||||
$res = $this->AddMeterToPDFRow($m, $tariffs, $from, $to, $type);
|
||||
$nebenRows .= $res['row'];
|
||||
$nebenTotal += $res['value'];
|
||||
$cost = $this->AddMeterToPDFRow($m, $tariffs, $from, $to, $type);
|
||||
$html .= $cost['row'];
|
||||
$total += $cost['value'];
|
||||
}
|
||||
|
||||
// ---------- Alle Tarife gruppieren ----------
|
||||
$tariffGroups = [];
|
||||
foreach ($tariffs as $t) {
|
||||
$type = ucfirst(strtolower($t['unit_type'] ?? 'Unbekannt'));
|
||||
$sTs = $this->toUnixTs($t['start'], false);
|
||||
$eTs = $this->toUnixTs($t['end'], true);
|
||||
if ($sTs && $eTs) {
|
||||
$period = date('d.m.Y', $sTs) . ' – ' . date('d.m.Y', $eTs);
|
||||
$price = number_format(floatval($t['price']), 2);
|
||||
$tariffGroups[$type][] = "{$period}: {$price} Rp";
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- Gesamtsummen ----------
|
||||
$gesamtTotal = $stromTotal + $nebenTotal;
|
||||
|
||||
// ---------- Tabellenstil ----------
|
||||
$tableStyle = "border-collapse:collapse;width:100%;font-size:8px;";
|
||||
$cellStyle = "border:0.3px solid #bbb;padding:3px;vertical-align:middle;";
|
||||
|
||||
// ---------- Kopfbereich ----------
|
||||
$html = "
|
||||
<h2 style='text-align:center;'>Rechnung für {$user['name']}</h2>
|
||||
<p>{$user['address']}<br>{$user['city']}</p>
|
||||
<p><b>Abrechnungszeitraum:</b> " . date('d.m.Y', $from) . " – " . date('d.m.Y', $to) . "</p>";
|
||||
|
||||
// ---------- Stromkosten ----------
|
||||
$html .= "
|
||||
<h3 style='margin-top:10px;color:#004085;'>⚡ Stromkosten</h3>
|
||||
<table style='{$tableStyle}'>
|
||||
<tr style='background-color:#e0e0e0;text-align:center;'>
|
||||
<th style='{$cellStyle}'>Zähler</th>
|
||||
<th style='{$cellStyle}'>Typ</th>
|
||||
<th style='{$cellStyle}'>Startzeit</th>
|
||||
<th style='{$cellStyle}'>Endzeit</th>
|
||||
<th style='{$cellStyle}'>Start</th>
|
||||
<th style='{$cellStyle}'>Ende</th>
|
||||
<th style='{$cellStyle}'>Verbrauch</th>
|
||||
<th style='{$cellStyle}'>Tarif (Rp)</th>
|
||||
<th style='{$cellStyle}'>Kosten (CHF)</th>
|
||||
<tr style='background-color:#f9f9f9; font-weight:bold;'>
|
||||
<td colspan='8' align='right'>Total Nebenkosten:</td>
|
||||
<td align='right'>" . number_format($total, 2) . "</td>
|
||||
</tr>
|
||||
{$stromRows}
|
||||
<tr style='border-top:1px solid black;font-weight:bold;background-color:#f8f9fa;'>
|
||||
<td colspan='8' style='text-align:right;{$cellStyle}'>Total Stromkosten:</td>
|
||||
<td style='{$cellStyle}text-align:right;'>" . number_format($stromTotal, 2) . "</td>
|
||||
</tr>
|
||||
</table>";
|
||||
</table><br>";
|
||||
|
||||
// ---------- Nebenkosten ----------
|
||||
$html .= "
|
||||
<h3 style='margin-top:15px;color:#6c757d;'>💧 Nebenkosten (Wasser & Wärme)</h3>
|
||||
<table style='{$tableStyle}'>
|
||||
<tr style='background-color:#e0e0e0;text-align:center;'>
|
||||
<th style='{$cellStyle}'>Zähler</th>
|
||||
<th style='{$cellStyle}'>Typ</th>
|
||||
<th style='{$cellStyle}'>Startzeit</th>
|
||||
<th style='{$cellStyle}'>Endzeit</th>
|
||||
<th style='{$cellStyle}'>Start</th>
|
||||
<th style='{$cellStyle}'>Ende</th>
|
||||
<th style='{$cellStyle}'>Verbrauch</th>
|
||||
<th style='{$cellStyle}'>Tarif (Rp)</th>
|
||||
<th style='{$cellStyle}'>Kosten (CHF)</th>
|
||||
</tr>
|
||||
{$nebenRows}
|
||||
<tr style='border-top:1px solid black;font-weight:bold;background-color:#f8f9fa;'>
|
||||
<td colspan='8' style='text-align:right;{$cellStyle}'>Total Nebenkosten:</td>
|
||||
<td style='{$cellStyle}text-align:right;'>" . number_format($nebenTotal, 2) . "</td>
|
||||
</tr>
|
||||
</table>";
|
||||
|
||||
// ---------- Tarife (alle Typen) ----------
|
||||
if (!empty($tariffGroups)) {
|
||||
$html .= "<h4 style='margin-top:15px;'>📋 Angewendete Tarife</h4><table style='{$tableStyle}'>";
|
||||
foreach ($tariffGroups as $type => $entries) {
|
||||
$html .= "<tr><td colspan='2' style='{$cellStyle}background-color:#efefef;'><b>{$type}</b></td></tr>";
|
||||
foreach ($entries as $line) {
|
||||
$html .= "<tr><td colspan='2' style='{$cellStyle}border-top:none;'>{$line}</td></tr>";
|
||||
}
|
||||
}
|
||||
$html .= "</table>";
|
||||
}
|
||||
|
||||
// ---------- Gesamttotal ----------
|
||||
$html .= "
|
||||
<h3 style='text-align:right;margin-top:15px;background-color:#f1f1f1;padding:6px;border:0.5px solid #aaa;'>
|
||||
💰 <u>Gesamttotal:</u> " . number_format($gesamtTotal, 2) . " CHF
|
||||
</h3>";
|
||||
|
||||
return $html;
|
||||
return ['html' => $html, 'sum' => $total];
|
||||
}
|
||||
|
||||
|
||||
@@ -302,7 +283,7 @@ private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type)
|
||||
|
||||
date_default_timezone_set('Europe/Zurich');
|
||||
|
||||
// 🔹 Passende Tarife nach Typ filtern
|
||||
// 🔹 Filtere relevante Tarife nach Typ
|
||||
$filteredTariffs = array_filter($tariffs, function ($t) use ($type) {
|
||||
return strtolower(trim($t['unit_type'] ?? '')) === strtolower(trim($type));
|
||||
});
|
||||
@@ -312,7 +293,7 @@ private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type)
|
||||
return ['row' => '', 'value' => 0];
|
||||
}
|
||||
|
||||
// 🔹 Zeitstempel konvertieren und sortieren
|
||||
// 🔹 Normiere Zeiträume
|
||||
foreach ($filteredTariffs as &$t) {
|
||||
$t['start_ts'] = $this->toUnixTs($t['start'], false);
|
||||
$t['end_ts'] = $this->toUnixTs($t['end'], true);
|
||||
@@ -323,6 +304,7 @@ private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type)
|
||||
$currentStart = $from;
|
||||
|
||||
while ($currentStart < $to) {
|
||||
// 🔹 Aktiven Tarif suchen
|
||||
$activeTariff = null;
|
||||
foreach ($filteredTariffs as $t) {
|
||||
if (($t['start_ts'] ?? 0) <= $currentStart && $currentStart <= ($t['end_ts'] ?? 0)) {
|
||||
@@ -342,32 +324,35 @@ private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type)
|
||||
|
||||
$tariffEnd = intval($activeTariff['end_ts']);
|
||||
$tariffPrice = floatval($activeTariff['price']);
|
||||
$tariffLabel = number_format($tariffPrice, 2, ',', '');
|
||||
|
||||
$startValue = $this->GetValueAt($varId, $currentStart, true);
|
||||
$segmentEnd = ($tariffEnd < $to) ? $tariffEnd : $to;
|
||||
$endValue = $this->GetValueAt($varId, $segmentEnd, true);
|
||||
// 🔹 Werte aus Archiv lesen
|
||||
$startValue = $this->GetValueAt($varId, $currentStart, true);
|
||||
$segmentEnd = ($tariffEnd < $to) ? $tariffEnd : $to;
|
||||
$endValue = $this->GetValueAt($varId, $segmentEnd, true);
|
||||
|
||||
if ($startValue === null || $endValue === null) break;
|
||||
|
||||
$verbrauch = max(0, $endValue - $startValue);
|
||||
$kosten = round(($tariffPrice / 100) * $verbrauch, 2);
|
||||
$totalCost += $kosten;
|
||||
$verbrauch = max(0, $endValue - $startValue);
|
||||
$kosten = round(($tariffPrice / 100) * $verbrauch, 2);
|
||||
$totalCost += $kosten;
|
||||
|
||||
// 🔹 Kurze Datumsangaben
|
||||
$startDate = date('d.m.Y', $currentStart);
|
||||
$endDate = date('d.m.Y', $segmentEnd);
|
||||
|
||||
// 🧾 TCPDF-kompatible Tabellenzeile (sichtbare Linien)
|
||||
// 🔹 Tabellenzeile mit sichtbaren Linien
|
||||
$rows .= "
|
||||
<tr nobr='true'>
|
||||
<td align='left' border='1' style='padding:2px;'>{$meter['name']}</td>
|
||||
<td align='left' border='1' style='padding:2px;'>{$type}</td>
|
||||
<td align='center' border='1' style='padding:2px;'>{$startDate}</td>
|
||||
<td align='center' border='1' style='padding:2px;'>{$endDate}</td>
|
||||
<td align='right' border='1' style='padding:2px;'>" . number_format($startValue, 2) . "</td>
|
||||
<td align='right' border='1' style='padding:2px;'>" . number_format($endValue, 2) . "</td>
|
||||
<td align='right' border='1' style='padding:2px;'>" . number_format($verbrauch, 2) . "</td>
|
||||
<td align='right' border='1' style='padding:2px;'>" . number_format($tariffPrice, 2) . "</td>
|
||||
<td align='right' border='1' style='padding:2px;'>" . number_format($kosten, 2) . "</td>
|
||||
<tr>
|
||||
<td border='1' align='left' style='padding:2px;'>{$meter['name']}</td>
|
||||
<td border='1' align='left' style='padding:2px;'>{$type}</td>
|
||||
<td border='1' align='center' style='padding:2px;'>{$startDate}</td>
|
||||
<td border='1' align='center' style='padding:2px;'>{$endDate}</td>
|
||||
<td border='1' align='right' style='padding:2px;'>" . number_format($startValue, 2) . "</td>
|
||||
<td border='1' align='right' style='padding:2px;'>" . number_format($endValue, 2) . "</td>
|
||||
<td border='1' align='right' style='padding:2px;'>" . number_format($verbrauch, 2) . "</td>
|
||||
<td border='1' align='right' style='padding:2px;'>" . $tariffLabel . "</td>
|
||||
<td border='1' align='right' style='padding:2px;'>" . number_format($kosten, 2) . "</td>
|
||||
</tr>";
|
||||
|
||||
if ($tariffEnd < $to) {
|
||||
@@ -377,36 +362,26 @@ private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type)
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback (keine Segmente)
|
||||
// 🔹 Fallback – kein Segment gefunden
|
||||
if ($rows === '') {
|
||||
$startVal = $this->GetValueAt($varId, $from, false);
|
||||
$endVal = $this->GetValueAt($varId, $to, true);
|
||||
$verbrauch = max(0, $endVal - $startVal);
|
||||
$rows = "
|
||||
<tr nobr='true'>
|
||||
<td align='left' border='1'>{$meter['name']}</td>
|
||||
<td align='left' border='1'>{$type}</td>
|
||||
<td align='center' border='1'>" . date('d.m.Y', $from) . "</td>
|
||||
<td align='center' border='1'>" . date('d.m.Y', $to) . "</td>
|
||||
<td align='right' border='1'>" . number_format($startVal, 2) . "</td>
|
||||
<td align='right' border='1'>" . number_format($endVal, 2) . "</td>
|
||||
<td align='right' border='1'>" . number_format($verbrauch, 2) . "</td>
|
||||
<td align='right' border='1'>0.00</td>
|
||||
<td align='right' border='1'>0.00</td>
|
||||
<tr>
|
||||
<td border='1' align='left'>{$meter['name']}</td>
|
||||
<td border='1' align='left'>{$type}</td>
|
||||
<td border='1' align='center'>" . date('d.m.Y', $from) . "</td>
|
||||
<td border='1' align='center'>" . date('d.m.Y', $to) . "</td>
|
||||
<td border='1' align='right'>" . number_format($startVal, 2) . "</td>
|
||||
<td border='1' align='right'>" . number_format($endVal, 2) . "</td>
|
||||
<td border='1' align='right'>" . number_format($verbrauch, 2) . "</td>
|
||||
<td border='1' align='right'>0.00</td>
|
||||
<td border='1' align='right'>0.00</td>
|
||||
</tr>";
|
||||
}
|
||||
|
||||
// 🧾 Totalsumme in separater, deutlich sichtbarer Zeile
|
||||
$rows .= "
|
||||
<tr nobr='true' style='font-weight:bold; background-color:#f0f0f0;'>
|
||||
<td colspan='8' align='right' border='1' style='padding:4px;'>Total {$type}:</td>
|
||||
<td align='right' border='1' style='padding:4px;'>" . number_format($totalCost, 2) . "</td>
|
||||
</tr>";
|
||||
|
||||
// Komplett in eine sichtbare Tabelle einbetten (damit TCPDF Linien rendert)
|
||||
$table = "<table border='1' cellspacing='0' cellpadding='3' width='100%' style='font-size:8px;'>{$rows}</table>";
|
||||
|
||||
return ['row' => $table, 'value' => $totalCost];
|
||||
return ['row' => $rows, 'value' => $totalCost];
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user