diff --git a/Abrechnung/module.php b/Abrechnung/module.php
index 7b3c922..8b8d989 100644
--- a/Abrechnung/module.php
+++ b/Abrechnung/module.php
@@ -129,19 +129,7 @@ private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to
IPS_LogMessage("ABR", "====================== START CalculatePowerCosts ======================");
IPS_LogMessage("ABR", "UserId: $userId | From: $from (" . date("d.m.Y H:i", $from) . ") | To: $to (" . date("d.m.Y H:i", $to) . ")");
- // ---- Debug: Tarife ----
- IPS_LogMessage("ABR", "Geladene Tarife:");
- foreach ($tariffs as $t) {
- IPS_LogMessage(
- "ABR",
- " Tarif: type=" . ($t['unit_type'] ?? '?') .
- " price=" . ($t['price'] ?? '?') .
- " start=" . json_encode($t['start']) .
- " end=" . json_encode($t['end'])
- );
- }
-
- // ---- HTML Tabellenkopf ----
+ // Tabellenkopf
$html = "
| Zähler |
@@ -157,15 +145,11 @@ private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to
Summe (CHF) |
";
- $acc = [];
$meters = [];
+ $acc = [];
- // ---- Stromzähler suchen ----
- IPS_LogMessage("ABR", "Suche nach Stromzählern für userId=$userId");
-
+ // === Stromzähler laden ===
foreach ($powerMeters as $m) {
- IPS_LogMessage("ABR", " Prüfe Zähler: {$m['name']} (user_id={$m['user_id']})");
-
if ($m['user_id'] != $userId) continue;
$name = $m['name'];
@@ -174,8 +158,6 @@ private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to
'exp' => $m['var_export'] ?? null
];
- IPS_LogMessage("ABR", " → Zugeordnet: $name | ImportVar=" . $meters[$name]['imp'] . " ExportVar=" . $meters[$name]['exp']);
-
$acc[$name] = [
'imp' => 0.0,
'exp' => 0.0,
@@ -190,52 +172,38 @@ private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to
}
if (empty($meters)) {
- IPS_LogMessage("ABR", "❌ Keine Stromzähler für Benutzer gefunden!");
- $html .= "| Keine Stromzähler für diesen Benutzer |
";
- return ['html' => $html, 'sum' => 0.0];
+ $html .= "| Keine Stromzähler für diesen Benutzer |
";
+ return ['html' => $html, 'sum' => 0];
}
- IPS_LogMessage("ABR", "Gefundene Stromzähler: " . implode(", ", array_keys($meters)));
-
- // ====================================================================================
- // 🔥 HAUPTSCHLEIFE: 15-Minuten-Abrechnung
- // ====================================================================================
+ // === 15-Minuten-Raster ===
for ($ts = $from; $ts < $to; $ts += 900) {
$slotEnd = min($to, $ts + 900);
- IPS_LogMessage("ABR", "----- INTERVALL " . date("d.m.Y H:i", $ts) . " → " . date("d.m.Y H:i", $slotEnd) . " -----");
-
- // Tarife zu Zeitpunkt ermitteln
+ // Tarife bestimmen
$pGrid = $this->getTariffPriceAt($tariffs, ['Netztarif'], $ts);
$pSolar = $this->getTariffPriceAt($tariffs, ['Solartarif'], $ts);
$pFeed = $this->getTariffPriceAt($tariffs, ['Einspeisetarif'], $ts);
- IPS_LogMessage("ABR", " Tarife: Grid=" . var_export($pGrid, true) .
- " Solar=" . var_export($pSolar, true) .
- " FeedIn=" . var_export($pFeed, true));
-
$impTotal = 0.0;
$expTotal = 0.0;
- $slot = [];
+ $slot = [];
- // ---- Zähler auslesen ----
+ // === Für jeden Zähler Import/Export lesen ===
foreach ($meters as $name => $mm) {
- IPS_LogMessage("ABR", " → Zähler: $name");
+ // Start-/Endwert Import
+ $impStart = $this->GetValueAt((int)$mm['imp'], $ts, false);
+ $impEnd = $this->GetValueAt((int)$mm['imp'], $slotEnd, true);
- $impDelta = 0.0;
- $expDelta = 0.0;
+ // Start-/Endwert Export
+ $expStart = $this->GetValueAt((int)$mm['exp'], $ts, false);
+ $expEnd = $this->GetValueAt((int)$mm['exp'], $slotEnd, true);
- if (!empty($mm['imp'])) {
- $impDelta = $this->saferReadDelta((int)$mm['imp'], $ts, $slotEnd);
- IPS_LogMessage("ABR", " Import-Delta: $impDelta");
- }
-
- if (!empty($mm['exp'])) {
- $expDelta = $this->saferReadDelta((int)$mm['exp'], $ts, $slotEnd);
- IPS_LogMessage("ABR", " Export-Delta: $expDelta");
- }
+ // Delta berechnen
+ $impDelta = ($impStart !== null && $impEnd !== null) ? max(0, $impEnd - $impStart) : 0;
+ $expDelta = ($expStart !== null && $expEnd !== null) ? max(0, $expEnd - $expStart) : 0;
if ($impDelta > 0 || $expDelta > 0) {
$slot[$name] = ['imp' => $impDelta, 'exp' => $expDelta];
@@ -244,36 +212,29 @@ private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to
}
}
- IPS_LogMessage("ABR", " SUMME Intervall: impTotal=$impTotal | expTotal=$expTotal");
-
if ($impTotal == 0 && $expTotal == 0) {
- IPS_LogMessage("ABR", " → Übersprungen (keine Werte)");
continue;
}
- // Verhältnis bestimmen PV vs Netz
+ // === Verhältnis PV/Netz bestimmen ===
if ($impTotal <= $expTotal && $expTotal > 0) {
- $ratio = $impTotal / $expTotal;
- IPS_LogMessage("ABR", " Fall Solar deckt alles → ratio=$ratio");
+ $ratio = $impTotal / $expTotal; // PV deckt alles
} else {
$ratio = ($impTotal > 0) ? ($expTotal / $impTotal) : 0;
- IPS_LogMessage("ABR", " Fall Teil-Netzbezug → ratio=$ratio");
}
- // ----- Werte auf einzelne Zähler herunterbrechen -----
+ // === Zuweisung auf einzelne Zähler ===
foreach ($slot as $name => $v) {
$imp = $v['imp'];
$exp = $v['exp'];
- IPS_LogMessage("ABR", " → Verrechne $name: imp=$imp exp=$exp ratio=$ratio");
-
- // Summen Import/Export speichern
+ // Summen Import/Export
$acc[$name]['imp'] += $imp;
$acc[$name]['exp'] += $exp;
if ($impTotal <= $expTotal) {
- // PV deckt alles
+ // PV deckt Verbrauch
$acc[$name]['solar_bezug'] += $imp;
$acc[$name]['solareinspeisung'] += (1 - $ratio) * $exp;
$acc[$name]['solarverkauf'] += $ratio * $exp;
@@ -282,7 +243,7 @@ private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to
if ($pFeed !== null) $acc[$name]['rev_feedin'] += ((1 - $ratio) * $exp * $pFeed) / 100;
} else {
- // Netzbezug
+ // Teil Netzbezug
$acc[$name]['solar_bezug'] += $ratio * $imp;
$acc[$name]['netz_bezug'] += (1 - $ratio) * $imp;
$acc[$name]['solarverkauf'] += $exp;
@@ -294,17 +255,9 @@ private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to
}
}
- // ====================================================================================
- // 📊 Abschlusstabelle
- // ====================================================================================
- IPS_LogMessage("ABR", "==== END 15-Minuten Analyse ====");
-
+ // === Tabelle schließen ===
$sum = 0.0;
-
foreach ($acc as $name => $a) {
-
- IPS_LogMessage("ABR", "FINAL $name: " . json_encode($a));
-
$subtotal = $a['cost_grid'] + $a['cost_solar'] - $a['rev_feedin'];
$sum += $subtotal;
@@ -323,17 +276,13 @@ private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to
";
}
- IPS_LogMessage("ABR", "====================== END CalculatePowerCosts ======================");
-
- $html .= "
- | Total Stromkosten: |
- " . number_format($sum, 2) . " |
-
";
+ $html .= "
";
return ['html' => $html, 'sum' => $sum];
}
+
private function CalculateAdditionalCosts($waterMeters, $tariffs, $userId, $from, $to)
{
$html = "