diff --git a/Abrechnung/module.php b/Abrechnung/module.php index 832cbe3..916ed08 100644 --- a/Abrechnung/module.php +++ b/Abrechnung/module.php @@ -359,92 +359,62 @@ class Abrechnung extends IPSModule return ['html' => $html, 'sum' => $total]; } - private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type) - { - $rows = ''; - $totalCost = 0.0; - $usedTariffs = []; - $varId = $meter['var_consumption']; +private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type) +{ + $varId = $meter['var_consumption']; - if (!IPS_VariableExists($varId)) { - return ['row' => '', 'value' => 0, 'tariffs' => []]; - } - - $filteredTariffs = array_filter($tariffs, fn($t) => - strtolower(trim($t['unit_type'] ?? '')) === strtolower(trim($type)) - ); - - foreach ($filteredTariffs as &$t) { - $t['start_ts'] = $this->toUnixTs($t['start'], false); - $t['end_ts'] = $this->toUnixTs($t['end'], true); - } - unset($t); - usort($filteredTariffs, fn($a, $b) => ($a['start_ts'] ?? 0) <=> ($b['start_ts'] ?? 0)); - - $currentStart = $from; - while ($currentStart < $to) { - $activeTariff = null; - foreach ($filteredTariffs as $t) { - if (($t['start_ts'] ?? 0) <= $currentStart && $currentStart <= ($t['end_ts'] ?? 0)) { - $activeTariff = $t; - break; - } - } - - if (!$activeTariff) { - $activeTariff = ['start_ts' => $currentStart, 'end_ts' => $to, 'price' => 0.0]; - } - - $tariffEnd = (int)$activeTariff['end_ts']; - $tariffPrice = (float)$activeTariff['price']; - - $key = $activeTariff['start_ts'] . '-' . $activeTariff['end_ts'] . '-' . $tariffPrice; - if (!isset($usedTariffs[$key])) { - $usedTariffs[$key] = [ - 'start' => $activeTariff['start_ts'], - 'end' => $activeTariff['end_ts'], - 'price' => $tariffPrice - ]; - } - - $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; - - $rows .= " - {$meter['name']} - {$type} - " . date('d.m.Y', $currentStart) . " - " . date('d.m.Y', $segmentEnd) . " - " . number_format($startValue, 2) . " - " . number_format($endValue, 2) . " - " . number_format($verbrauch, 2) . " - " . number_format($tariffPrice, 2) . " - " . number_format($kosten, 2) . " - "; - - if ($tariffEnd < $to) { - $currentStart = $tariffEnd + 1; - } else { - break; - } - } - - if ($rows === '') { - return ['row' => '', 'value' => 0, 'tariffs' => []]; - } - - return ['row' => $rows, 'value' => $totalCost, 'tariffs' => array_values($usedTariffs)]; + if (!IPS_VariableExists($varId)) { + return ['row' => '', 'value' => 0, 'tariffs' => []]; } + // ---- 1) Start- und Endwerte direkt holen ---- + $startValue = $this->GetValueAt($varId, $from, false); // letzter Wert VOR dem Start + $endValue = $this->GetValueAt($varId, $to, true); // erster Wert NACH dem Ende + + if ($startValue === null || $endValue === null) { + return ['row' => '', 'value' => 0, 'tariffs' => []]; + } + + // ---- 2) Verbrauch simple Differenz ---- + $verbrauch = max(0, $endValue - $startValue); + + // ---- 3) passenden Tarif suchen ---- + $tariffPrice = 0.0; + foreach ($tariffs as $t) { + if (strtolower(trim($t['unit_type'] ?? '')) === strtolower(trim($type))) { + $tariffPrice = floatval($t['price']); + break; + } + } + + // ---- 4) Kosten (Rp → CHF) ---- + $kosten = round(($tariffPrice / 100) * $verbrauch, 2); + + // ---- 5) Tabellenzeile ---- + $row = " + {$meter['name']} + {$type} + " . date('d.m.Y', $from) . " + " . date('d.m.Y', $to) . " + " . number_format($startValue, 2) . " + " . number_format($endValue, 2) . " + " . number_format($verbrauch, 2) . " + " . number_format($tariffPrice, 2) . " + " . number_format($kosten, 2) . " + "; + + return [ + 'row' => $row, + 'value' => $kosten, + 'tariffs' => [[ + 'start' => $from, + 'end' => $to, + 'price' => $tariffPrice + ]] + ]; +} + + // ====================== Hilfsfunktionen ====================== private function GetValueAt($varId, $timestamp, $nearestAfter = true)