From 0b685261a09ed85c19e5c7e6a0738688a8e7256a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=A4fliger?= Date: Thu, 6 Nov 2025 15:07:53 +0100 Subject: [PATCH] no message --- Abrechnung/form.json | 2 +- Abrechnung/module.php | 69 ++++++++++++------------------------------- 2 files changed, 20 insertions(+), 51 deletions(-) diff --git a/Abrechnung/form.json b/Abrechnung/form.json index 97aa22f..d5fc2f5 100644 --- a/Abrechnung/form.json +++ b/Abrechnung/form.json @@ -104,7 +104,7 @@ "caption": "Einheit", "name": "unit_type", "width": "20%", - "add": "Strombezug", + "add": "Netztarif", "edit": { "type": "Select", "options": [ diff --git a/Abrechnung/module.php b/Abrechnung/module.php index eb56e63..e7aa366 100644 --- a/Abrechnung/module.php +++ b/Abrechnung/module.php @@ -141,6 +141,8 @@ class Abrechnung extends IPSModule private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to) { + IPS_LogMessage('Abrechnung', "⚡ Starte Stromkostenberechnung von " . date('d.m.Y H:i', $from) . " bis " . date('d.m.Y H:i', $to)); + $html = " @@ -158,9 +160,8 @@ private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to // Initialisierung $acc = []; - - // Benutzerzähler laden $meters = []; + foreach ($powerMeters as $m) { if ($m['user_id'] != $userId) continue; $name = $m['name']; @@ -179,6 +180,7 @@ private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to } if (empty($meters)) { + IPS_LogMessage('Abrechnung', "⚠️ Keine Stromzähler für Benutzer $userId gefunden."); $html .= "
Zähler
Keine Stromzähler für diesen Benutzer

"; return ['html'=>$html, 'sum'=>0.0]; } @@ -187,12 +189,16 @@ private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to for ($ts = $from; $ts < $to; $ts += 900) { $slotEnd = min($to, $ts + 900); - // Tarife für diesen Zeitraum holen + // Tarife abrufen $pGrid = $this->getTariffPriceAt($tariffs, ['Netztarif'], $ts); $pSolar = $this->getTariffPriceAt($tariffs, ['Solartarif'], $ts); $pFeed = $this->getTariffPriceAt($tariffs, ['Einspeisetarif'], $ts); - // Deltas + IPS_LogMessage('Abrechnung', sprintf("⏱ %s – %s | Tarife: Netz=%.3f Rp, Solar=%.3f Rp, Einspeise=%.3f Rp", + date('d.m.Y H:i', $ts), date('H:i', $slotEnd), + $pGrid ?? 0, $pSolar ?? 0, $pFeed ?? 0 + )); + $impTotal = 0.0; $expTotal = 0.0; $slot = []; @@ -211,6 +217,8 @@ private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to $slot[$name] = ['imp'=>$impDelta, 'exp'=>$expDelta]; $impTotal += $impDelta; $expTotal += $expDelta; + + IPS_LogMessage('Abrechnung', sprintf(" → %s: imp=%.4f, exp=%.4f", $name, $impDelta, $expDelta)); } if ($impTotal <= 0 && $expTotal <= 0) continue; @@ -223,7 +231,6 @@ private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to $imp = $slot[$name]['imp']; $exp = $slot[$name]['exp']; - // Mengen $acc[$name]['imp'] += $imp; $acc[$name]['exp'] += $exp; @@ -232,10 +239,11 @@ private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to $acc[$name]['solareinspeisung']+= (1 - $ratio) * $exp; $acc[$name]['solarverkauf'] += $ratio * $exp; - // Kosten / Erträge if ($pSolar !== null) $acc[$name]['cost_solar'] += ($imp * $pSolar) / 100; if ($pFeed !== null) $acc[$name]['rev_feedin'] += ((1 - $ratio) * $exp * $pFeed) / 100; } + + IPS_LogMessage('Abrechnung', sprintf(" ⚙️ Fall 1 (imp<=exp): ratio=%.3f", $ratio)); } // ===== Fall 2: import > export ===== @@ -257,10 +265,12 @@ private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to if ($pSolar !== null) $acc[$name]['cost_solar'] += ($ratio * $imp * $pSolar) / 100; if ($pFeed !== null) $acc[$name]['rev_feedin'] += ($exp * $pFeed) / 100; } + + IPS_LogMessage('Abrechnung', sprintf(" ⚙️ Fall 2 (imp>exp): ratio=%.3f", $ratio)); } } - // Ausgabe + // Ausgabe erzeugen $grand = 0.0; foreach ($acc as $name => $v) { $sum = $v['cost_solar'] + $v['cost_grid'] - $v['rev_feedin']; @@ -286,52 +296,11 @@ private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to " . number_format($grand, 2) . "
"; + IPS_LogMessage('Abrechnung', sprintf("✅ Stromkosten fertig: %.2f CHF total", $grand)); + return ['html'=>$html, 'sum'=>$grand]; } - - - - // ====================== Nebenkosten ====================== - -private function CalculateAdditionalCosts($waterMeters, $tariffs, $userId, $from, $to) -{ - $html = " - - - - "; - - $total = 0.0; - $usedTariffs = []; - - 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 .= " - - -
ZählerTypStartEndeZähler StartZähler EndeVerbrauchTarif (Rp)Kosten (CHF)
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)