diff --git a/Abrechnung/module.php b/Abrechnung/module.php index 74b2a96..7d91974 100644 --- a/Abrechnung/module.php +++ b/Abrechnung/module.php @@ -302,7 +302,7 @@ private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type) date_default_timezone_set('Europe/Zurich'); - // 🔹 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,30 +312,18 @@ private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type) return ['row' => '', 'value' => 0]; } - // 🔹 Zeitstempel normieren 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); - - if (!$t['start_ts'] || !$t['end_ts']) { - IPS_LogMessage('Abrechnung', "⚠ Ungültiger Tarifzeitraum: " . json_encode($t)); - } else { - IPS_LogMessage('Abrechnung', sprintf( - " 🕓 Tarif gültig von %s bis %s @ %.3f Rp", - date('d.m.Y H:i', $t['start_ts']), - date('d.m.Y H:i', $t['end_ts']), - floatval($t['price']) - )); - } } unset($t); usort($filteredTariffs, fn($a, $b) => ($a['start_ts'] ?? 0) <=> ($b['start_ts'] ?? 0)); $currentStart = $from; - $segmentIndex = 1; while ($currentStart < $to) { - // 🔹 Aktiven Tarif finden + // 🔹 Aktiven Tarif suchen $activeTariff = null; foreach ($filteredTariffs as $t) { if (($t['start_ts'] ?? 0) <= $currentStart && $currentStart <= ($t['end_ts'] ?? 0)) { @@ -345,7 +333,6 @@ private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type) } if (!$activeTariff) { - IPS_LogMessage('Abrechnung', "⚠ Kein Tarif aktiv bei " . date('d.m.Y H:i', $currentStart)); $activeTariff = [ 'start_ts' => $currentStart, 'end_ts' => $to, @@ -358,32 +345,33 @@ private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type) $tariffPrice = floatval($activeTariff['price']); $tariffLabel = number_format($tariffPrice, 2, ',', ''); - // 🔹 Werte lesen + // 🔹 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) { - IPS_LogMessage('Abrechnung', "⚠ Keine Werte für {$meter['name']} im Zeitraum " . date('d.m.Y H:i', $currentStart)); - break; - } + if ($startValue === null || $endValue === null) break; $verbrauch = max(0, $endValue - $startValue); $kosten = round(($tariffPrice / 100) * $verbrauch, 2); $totalCost += $kosten; - // 🔹 Tabellenzeile formatiert + // 🔹 Kurze Datumsangaben + $startDate = date('d.m.Y', $currentStart); + $endDate = date('d.m.Y', $segmentEnd); + + // 🔹 Tabellenzeile mit sichtbaren Linien $rows .= " - - {$meter['name']} - {$type} - " . date('d.m.Y H:i', $currentStart) . " - " . date('d.m.Y H:i', $segmentEnd) . " - " . number_format($startValue, 2) . " - " . number_format($endValue, 2) . " - " . number_format($verbrauch, 2) . " - " . $tariffLabel . " - " . number_format($kosten, 2) . " + + {$meter['name']} + {$type} + {$startDate} + {$endDate} + " . number_format($startValue, 2) . " + " . number_format($endValue, 2) . " + " . number_format($verbrauch, 2) . " + " . $tariffLabel . " + " . number_format($kosten, 2) . " "; if ($tariffEnd < $to) { @@ -391,26 +379,24 @@ private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type) } else { break; } - - $segmentIndex++; } - // Fallback – kein Segment berechnet + // 🔹 Fallback – kein Segment gefunden if ($rows === '') { $startVal = $this->GetValueAt($varId, $from, false); $endVal = $this->GetValueAt($varId, $to, true); $verbrauch = max(0, $endVal - $startVal); $rows = " - {$meter['name']} - {$type} - " . date('d.m.Y H:i', $from) . " - " . date('d.m.Y H:i', $to) . " - " . number_format($startVal, 2) . " - " . number_format($endVal, 2) . " - " . number_format($verbrauch, 2) . " - 0.00 - 0.00 + {$meter['name']} + {$type} + " . date('d.m.Y', $from) . " + " . date('d.m.Y', $to) . " + " . number_format($startVal, 2) . " + " . number_format($endVal, 2) . " + " . number_format($verbrauch, 2) . " + 0.00 + 0.00 "; }