diff --git a/Abrechnung/module.php b/Abrechnung/module.php index cb2105e..3a85ea9 100644 --- a/Abrechnung/module.php +++ b/Abrechnung/module.php @@ -139,125 +139,163 @@ class Abrechnung extends IPSModule // ====================== Stromkosten ====================== - private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to) - { - $html = " - - - - "; +private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to) +{ + $html = "
ZählerTypStartEndeZähler StartZähler EndeVerbrauchTarif (Rp)Kosten (CHF)
+ + + + "; - $total = 0.0; - foreach ($powerMeters as $m) { - if ($m['user_id'] != $userId) continue; - $cost = $this->AddMeterToPDFRow($m, $tariffs, $from, $to, 'Strombezug'); - $html .= $cost['row']; - $total += $cost['value']; - } + $total = 0.0; + $usedTariffs = []; - $html .= " - - -
ZählerTypStartEndeZähler StartZähler EndeVerbrauchTarif (Rp)Kosten (CHF)
Total Stromkosten:" . number_format($total, 2) . "

"; - - return ['html' => $html, 'sum' => $total]; + foreach ($powerMeters as $m) { + if ($m['user_id'] != $userId) continue; + $cost = $this->AddMeterToPDFRow($m, $tariffs, $from, $to, 'Strombezug'); + $html .= $cost['row']; + $total += $cost['value']; + $usedTariffs = array_merge($usedTariffs, $cost['tariffs']); } + $html .= " + Total Stromkosten: + " . number_format($total, 2) . " +
"; + + // 👇 Tarifliste anhängen + if (!empty($usedTariffs)) { + $html .= "

Angewendete Stromtarife:


"; + } + + return ['html' => $html, 'sum' => $total]; +} + + // ====================== Nebenkosten ====================== - private function CalculateAdditionalCosts($waterMeters, $tariffs, $userId, $from, $to) - { - $html = " - - - - "; +private function CalculateAdditionalCosts($waterMeters, $tariffs, $userId, $from, $to) +{ + $html = "
ZählerTypStartEndeZähler StartZähler EndeVerbrauchTarif (Rp)Kosten (CHF)
+ + + + "; - $total = 0.0; - foreach ($waterMeters as $m) { - if ($m['user_id'] != $userId) continue; - $type = $m['meter_type'] ?? 'Warmwasser'; - $cost = $this->AddMeterToPDFRow($m, $tariffs, $from, $to, $type); - $html .= $cost['row']; - $total += $cost['value']; - } + $total = 0.0; + $usedTariffs = []; - $html .= " - - -
ZählerTypStartEndeZähler StartZähler EndeVerbrauchTarif (Rp)Kosten (CHF)
Total Nebenkosten:" . number_format($total, 2) . "

"; - - return ['html' => $html, 'sum' => $total]; + foreach ($waterMeters as $m) { + if ($m['user_id'] != $userId) continue; + $type = $m['meter_type'] ?? 'Warmwasser'; + $cost = $this->AddMeterToPDFRow($m, $tariffs, $from, $to, $type); + $html .= $cost['row']; + $total += $cost['value']; + $usedTariffs = array_merge($usedTariffs, $cost['tariffs']); } + $html .= " + Total Nebenkosten: + " . number_format($total, 2) . " +
"; + + // 👇 Tarifliste anhängen + if (!empty($usedTariffs)) { + $html .= "

Angewendete Nebenkostentarife:


"; + } + + return ['html' => $html, 'sum' => $total]; +} + // ====================== Kernberechnung ====================== - private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type) - { - $rows = ''; - $totalCost = 0.0; - $varId = $meter['var_consumption']; +private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type) +{ + $rows = ''; + $totalCost = 0.0; + $usedTariffs = []; // 👈 neu: Liste der verwendeten Tarife + $varId = $meter['var_consumption']; - if (!IPS_VariableExists($varId)) return ['row' => '', 'value' => 0]; + if (!IPS_VariableExists($varId)) return ['row' => '', 'value' => 0, 'tariffs' => []]; - $filteredTariffs = array_filter($tariffs, fn($t) => - strtolower(trim($t['unit_type'] ?? '')) === strtolower(trim($type)) - ); + $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 = intval($activeTariff['end_ts']); - $tariffPrice = floatval($activeTariff['price']); - - $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]; - - return ['row' => $rows, 'value' => $totalCost]; + 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 = intval($activeTariff['end_ts']); + $tariffPrice = floatval($activeTariff['price']); + + // 👇 Tarif in Liste aufnehmen (nur wenn noch nicht drin) + $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)]; +} // ====================== Hilfsfunktionen ======================