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 .= "