no message

This commit is contained in:
dh
2025-11-05 08:07:21 +01:00
parent 1f80404765
commit 7c1bf5b253
+31 -60
View File
@@ -277,9 +277,8 @@ private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type)
return ['row' => '', 'value' => 0]; return ['row' => '', 'value' => 0];
} }
// 🧩 Einheitstyp tolerant abgleichen // --- Relevante Tarife nach Typ filtern ---
$filteredTariffs = []; $filteredTariffs = array_filter($tariffs, function ($t) use ($type) {
foreach ($tariffs as $t) {
$tariffType = strtolower(trim($t['unit_type'] ?? '')); $tariffType = strtolower(trim($t['unit_type'] ?? ''));
$typeNorm = strtolower(trim($type)); $typeNorm = strtolower(trim($type));
@@ -289,121 +288,93 @@ private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type)
'kaltwasser' => ['kaltwasser', 'wasser', 'kww'], 'kaltwasser' => ['kaltwasser', 'wasser', 'kww'],
'wärme' => ['wärme', 'heizung', 'heat'] 'wärme' => ['wärme', 'heizung', 'heat']
]; ];
return in_array($tariffType, $map[$typeNorm] ?? []) || $tariffType === $typeNorm;
if (in_array($tariffType, $map[$typeNorm] ?? []) || $tariffType == $typeNorm) { });
$filteredTariffs[] = $t;
}
}
IPS_LogMessage('Abrechnung', "🔍 Suche Tarife für Typ '$type': gefunden " . count($filteredTariffs) . " Treffer");
foreach ($filteredTariffs as $t) {
IPS_LogMessage('Abrechnung', " Tarif '{$t['unit_type']}' von " . json_encode($t['start']) . " bis " . json_encode($t['end']) . " | {$t['price']} Rp");
}
if (empty($filteredTariffs)) { if (empty($filteredTariffs)) {
IPS_LogMessage('Abrechnung', "⚠ Keine passenden Tarife für $type gefunden"); IPS_LogMessage('Abrechnung', "⚠ Keine passenden Tarife für $type gefunden");
return ['row' => '', 'value' => 0]; return ['row' => '', 'value' => 0];
} }
// 🔹 Datumsfelder sicher umwandeln // --- Tarife sicher in Zeiträume umwandeln ---
foreach ($filteredTariffs as &$t) { foreach ($filteredTariffs as &$t) {
if (is_array($t['start'])) { $t['start_ts'] = is_numeric($t['start']) ? intval($t['start']) : strtotime($t['start']);
$t['start_ts'] = mktime(0, 0, 0, $t['start']['month'], $t['start']['day'], $t['start']['year']); $t['end_ts'] = is_numeric($t['end']) ? intval($t['end']) : strtotime($t['end']);
} else { if (!$t['start_ts'] || !$t['end_ts']) {
$t['start_ts'] = strtotime($t['start'] . ' 00:00:00'); IPS_LogMessage('Abrechnung', "⚠ Ungültiger Tarifzeitraum: " . json_encode($t));
} continue;
if (is_array($t['end'])) {
$t['end_ts'] = mktime(23, 59, 59, $t['end']['month'], $t['end']['day'], $t['end']['year']);
} else {
$t['end_ts'] = strtotime($t['end'] . ' 23:59:59');
}
if ($t['start_ts'] === false || $t['end_ts'] === false) {
IPS_LogMessage('Abrechnung', "❌ Ungültiges Tarifdatum nach Umwandlung: " . json_encode($t));
} else {
IPS_LogMessage('Abrechnung', "✅ Tarifzeitraum erkannt: " . date('d.m.Y', $t['start_ts']) . " - " . date('d.m.Y', $t['end_ts']));
} }
} }
unset($t); unset($t);
// Nach Startzeit sortieren // Sortiere Tarife nach Startzeit
usort($filteredTariffs, fn($a, $b) => $a['start_ts'] <=> $b['start_ts']); usort($filteredTariffs, fn($a, $b) => $a['start_ts'] <=> $b['start_ts']);
$current = $from; $current = $from;
while ($current < $to) { while ($current < $to) {
$activeTariff = null; // Finde aktiven Tarif für diesen Zeitpunkt
$segmentStart = $current;
$segmentEnd = $to;
// --- passenden Tarif finden (einfach & robust) ---
$activeTariff = null; $activeTariff = null;
foreach ($filteredTariffs as $t) { foreach ($filteredTariffs as $t) {
if ($t['start_ts'] <= $segmentStart && $segmentStart <= $t['end_ts']) { if ($t['start_ts'] <= $current && $current <= $t['end_ts']) {
$activeTariff = $t; $activeTariff = $t;
break; break;
} }
} }
if ($activeTariff) { // Wenn kein aktiver Tarif, nimm nächsten oder rechne 0 Rp
// Tarif aktiv: Segment geht bis Tarifende oder Rechnungsende if (!$activeTariff) {
$segmentEnd = min($activeTariff['end_ts'], $to);
$tariffPrice = floatval($activeTariff['price']);
$tariffLabel = number_format($tariffPrice, 2);
} else {
// Kein Tarif aktiv: bis zum nächsten Tarifstart (oder Rechnungsende) mit 0 Rp
$nextStart = $to; $nextStart = $to;
foreach ($filteredTariffs as $t) { foreach ($filteredTariffs as $t) {
if ($t['start_ts'] > $segmentStart && $t['start_ts'] < $nextStart) { if ($t['start_ts'] > $current && $t['start_ts'] < $nextStart) {
$nextStart = $t['start_ts']; $nextStart = $t['start_ts'];
} }
} }
$segmentEnd = min($nextStart, $to); $segmentEnd = min($nextStart, $to);
$tariffPrice = 0.0; $tariffPrice = 0.0;
$tariffLabel = 'kein Tarif'; $tariffLabel = 'kein Tarif';
} else {
$segmentEnd = min($activeTariff['end_ts'], $to);
$tariffPrice = floatval($activeTariff['price']);
$tariffLabel = number_format($tariffPrice, 2);
} }
// --- Verbrauch für diesen Zeitraum ---
// Archivwerte lesen $startVal = $this->GetValueAt($varId, $current, true);
$startVal = $this->GetValueAt($varId, $segmentStart, true);
$endVal = $this->GetValueAt($varId, $segmentEnd, true); $endVal = $this->GetValueAt($varId, $segmentEnd, true);
if ($startVal === null || $endVal === null) { if ($startVal === null || $endVal === null) {
IPS_LogMessage('Abrechnung', "⚠ Keine Archivwerte für {$meter['name']} ($type) im Zeitraum " . date('d.m.Y H:i', $segmentStart) . " - " . date('d.m.Y H:i', $segmentEnd)); IPS_LogMessage('Abrechnung', "⚠ Keine Werte für {$meter['name']} im Zeitraum " . date('d.m.Y H:i', $current) . " - " . date('d.m.Y H:i', $segmentEnd));
$current = $segmentEnd + 1; $current = $segmentEnd + 1;
continue; continue;
} }
$diff = max(0, $endVal - $startVal); $diff = max(0, $endVal - $startVal);
$costCHF = ($tariffPrice / 100) * $diff; $costCHF = round(($tariffPrice / 100) * $diff, 2);
$totalCost += $costCHF;
IPS_LogMessage('Abrechnung', "{$meter['name']} ($type): " . date('d.m.Y H:i', $segmentStart) . " bis " . date('d.m.Y H:i', $segmentEnd) . " | Tarif {$tariffLabel} Rp | Verbrauch {$diff}");
// --- Ausgabezeile ---
$rows .= " $rows .= "
<tr> <tr>
<td>{$meter['name']}</td> <td>{$meter['name']}</td>
<td>$type</td> <td>$type</td>
<td>" . date('d.m.Y H:i', $segmentStart) . "</td> <td>" . date('d.m.Y H:i', $current) . "</td>
<td>" . date('d.m.Y H:i', $segmentEnd) . "</td> <td>" . date('d.m.Y H:i', $segmentEnd) . "</td>
<td>" . number_format($startVal, 2) . "</td> <td>" . number_format($startVal, 2) . "</td>
<td>" . number_format($endVal, 2) . "</td> <td>" . number_format($endVal, 2) . "</td>
<td>" . number_format($diff, 2) . "</td> <td>" . number_format($diff, 2) . "</td>
<td>" . number_format($tariffPrice, 2) . "</td> <td>" . $tariffLabel . "</td>
<td>" . number_format($costCHF, 2) . "</td> <td>" . number_format($costCHF, 2) . "</td>
</tr>"; </tr>";
$totalCost += $costCHF;
$current = $segmentEnd + 1; $current = $segmentEnd + 1;
} }
// Fallback, falls keine Zeile entstand // --- Falls nichts berechnet wurde (z. B. fehlende Tarife) ---
if ($rows == '') { if ($rows == '') {
$startVal = $this->GetValueAt($varId, $from, false); $startVal = $this->GetValueAt($varId, $from, false);
$endVal = $this->GetValueAt($varId, $to, true); $endVal = $this->GetValueAt($varId, $to, true);
$diff = max(0, $endVal - $startVal); $diff = max(0, $endVal - $startVal);
$rows = "
$rows .= "
<tr> <tr>
<td>{$meter['name']}</td> <td>{$meter['name']}</td>
<td>$type</td> <td>$type</td>