diff --git a/Abrechnung/module.php b/Abrechnung/module.php index 5acbbf7..4b511ad 100644 --- a/Abrechnung/module.php +++ b/Abrechnung/module.php @@ -267,7 +267,6 @@ private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type) $tariffType = strtolower(trim($t['unit_type'] ?? '')); $typeNorm = strtolower(trim($type)); - // Mapping-Tabelle für Synonyme $map = [ 'strombezug' => ['strom', 'strombezug', 'stromzähler', 'stromverbrauch'], 'warmwasser' => ['warmwasser', 'wasser', 'hww'], @@ -280,10 +279,9 @@ private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type) } } - // 🪵 Logging zur Kontrolle 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 {$t['start']} bis {$t['end']} | {$t['price']} Rp"); + IPS_LogMessage('Abrechnung', " Tarif '{$t['unit_type']}' von " . json_encode($t['start']) . " bis " . json_encode($t['end']) . " | {$t['price']} Rp"); } if (empty($filteredTariffs)) { @@ -291,10 +289,30 @@ private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type) return ['row' => '', 'value' => 0]; } - // Nach Startdatum sortieren - usort($filteredTariffs, function ($a, $b) { - return strtotime($a['start']) <=> strtotime($b['start']); - }); + // 🔹 Datumsfelder sicher umwandeln + foreach ($filteredTariffs as &$t) { + if (is_array($t['start'])) { + $t['start_ts'] = mktime(0, 0, 0, $t['start']['month'], $t['start']['day'], $t['start']['year']); + } else { + $t['start_ts'] = strtotime($t['start'] . ' 00:00:00'); + } + + 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); + + // Nach Startzeit sortieren + usort($filteredTariffs, fn($a, $b) => $a['start_ts'] <=> $b['start_ts']); $current = $from; @@ -303,38 +321,27 @@ private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type) $segmentStart = $current; $segmentEnd = $to; - // Passenden Tarif für aktuellen Zeitpunkt finden + // passenden Tarif finden foreach ($filteredTariffs as $t) { - $tariffStart = is_array($t['start']) ? IPS_MakeTimeStamp($t['start']) : strtotime($t['start'] . ' 00:00:00'); - $tariffEnd = is_array($t['end']) ? IPS_MakeTimeStamp($t['end']) + 86399 : strtotime($t['end'] . ' 23:59:59'); - - - if ($tariffStart === false || $tariffEnd === false) { - IPS_LogMessage('Abrechnung', "❌ Ungültiges Tarifdatum: " . json_encode($t)); - continue; - } - - // Tarif gilt, wenn er sich mit dem Zeitraum überschneidet - if ($tariffEnd > $segmentStart && $tariffStart < $to) { - if ($tariffStart > $segmentStart) { - $segmentEnd = min($tariffStart, $to); + if ($t['end_ts'] > $segmentStart && $t['start_ts'] < $to) { + if ($t['start_ts'] > $segmentStart) { + $segmentEnd = min($t['start_ts'], $to); $activeTariff = null; break; } - $segmentEnd = min($tariffEnd, $to); + $segmentEnd = min($t['end_ts'], $to); $activeTariff = $t; break; } } - // Kein aktiver Tarif -> Nulltarif bis zum nächsten Start + // Kein aktiver Tarif gefunden if ($activeTariff === null) { $nextStart = $to; foreach ($filteredTariffs as $t) { - $ts = strtotime($t['start'] . ' 00:00:00'); - if ($ts > $segmentStart && $ts < $nextStart) { - $nextStart = $ts; + if ($t['start_ts'] > $segmentStart && $t['start_ts'] < $nextStart) { + $nextStart = $t['start_ts']; } } $segmentEnd = min($nextStart, $to); @@ -345,12 +352,12 @@ private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type) $tariffLabel = number_format($tariffPrice, 2); } - // 🔹 Archivwerte lesen + // Archivwerte lesen $startVal = $this->GetValueAt($varId, $segmentStart, false); $endVal = $this->GetValueAt($varId, $segmentEnd, true); if ($startVal === null || $endVal === null) { - IPS_LogMessage('Abrechnung', "⚠ Keine Archivwerte für {$meter['name']} ($type) von " . date('d.m.Y H:i', $segmentStart) . " bis " . date('d.m.Y H:i', $segmentEnd)); + 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)); $current = $segmentEnd + 1; continue; } @@ -358,10 +365,8 @@ private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type) $diff = max(0, $endVal - $startVal); $costCHF = ($tariffPrice / 100) * $diff; - // 🪵 Logausgabe pro Abschnitt 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}"); - // 🔸 Zeile fürs PDF $rows .= " {$meter['name']} @@ -379,7 +384,7 @@ private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type) $current = $segmentEnd + 1; } - // Falls keine Zeile zustande kam + // Fallback, falls keine Zeile entstand if ($rows == '') { $startVal = $this->GetValueAt($varId, $from, false); $endVal = $this->GetValueAt($varId, $to, true); @@ -404,4 +409,5 @@ private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type) + } \ No newline at end of file