diff --git a/Abrechnung/module.php b/Abrechnung/module.php index 5678267..8cbb939 100644 --- a/Abrechnung/module.php +++ b/Abrechnung/module.php @@ -231,55 +231,98 @@ private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type) { $startVal = $this->GetValueAt($meter['var_consumption'], $from); $endVal = $this->GetValueAt($meter['var_consumption'], $to); - if ($startVal === null || $endVal === null) return ['row' => '', 'value' => 0]; + if ($startVal === null || $endVal === null) { + IPS_LogMessage('Abrechnung', "❌ Kein Wert für {$meter['name']} ($type)"); + return ['row' => '', 'value' => 0]; + } $totalDiff = max(0, $endVal - $startVal); - if ($totalDiff <= 0) return ['row' => '', 'value' => 0]; + if ($totalDiff <= 0) { + IPS_LogMessage('Abrechnung', "⚠️ Kein Verbrauch für {$meter['name']} ($type): Start=$startVal, Ende=$endVal"); + return ['row' => '', 'value' => 0]; + } + + IPS_LogMessage('Abrechnung', "📊 {$meter['name']} ($type): Verbrauch gesamt=$totalDiff"); $rows = ''; $totalCost = 0.0; + $foundTariff = false; - // Alle Tarife dieses Typs prüfen - foreach ($tariffs as $t) { - if (strtolower($t['unit_type']) != strtolower($type)) continue; + foreach ($tariffs as $t) { + if (!isset($t['start'], $t['end'], $t['price'], $t['unit_type'])) continue; + if (strtolower($t['unit_type']) != strtolower($type)) continue; - $tariffStart = strtotime($t['start']); - $tariffEnd = strtotime($t['end']); + $tariffStart = strtotime($t['start']); + $tariffEnd = strtotime($t['end']); + if ($tariffEnd < $from || $tariffStart > $to) continue; - // Zeitliche Überschneidung mit Abrechnungszeitraum? - if ($tariffEnd < $from || $tariffStart > $to) continue; + $foundTariff = true; - // Überschneidung bestimmen - $sectionStart = max($from, $tariffStart); - $sectionEnd = min($to, $tariffEnd); - $sectionDuration = $sectionEnd - $sectionStart; - $totalDuration = $to - $from; + $sectionStart = max($from, $tariffStart); + $sectionEnd = min($to, $tariffEnd); + $sectionDuration = $sectionEnd - $sectionStart; + $totalDuration = $to - $from; - if ($sectionDuration <= 0) continue; + if ($sectionDuration <= 0) continue; - // Anteil des Gesamtverbrauchs nach Zeitanteil - $sectionFraction = $sectionDuration / $totalDuration; - $sectionDiff = $totalDiff * $sectionFraction; + $sectionFraction = $sectionDuration / $totalDuration; + $sectionDiff = $totalDiff * $sectionFraction; - $tariffRp = floatval($t['price']); - $costCHF = ($tariffRp / 100) * $sectionDiff; + $tariffRp = floatval($t['price']); + $costCHF = ($tariffRp / 100) * $sectionDiff; - $rows .= " -