no message

This commit is contained in:
2025-12-12 08:14:12 +01:00
parent b4de095a20
commit 558fd9aa4e
+50 -51
View File
@@ -281,90 +281,90 @@ class Abrechnung extends IPSModule
$pSolar = $this->getTariffPriceAt($tariffs, ['Solartarif'], $ts); $pSolar = $this->getTariffPriceAt($tariffs, ['Solartarif'], $ts);
$pFeed = $this->getTariffPriceAt($tariffs, ['Einspeisetarif'], $ts); $pFeed = $this->getTariffPriceAt($tariffs, ['Einspeisetarif'], $ts);
foreach ($metersByUser as $userId => $meters) { // ==========================================================
// 1) GLOBAL DELTAS EINMAL BERECHNEN
// ==========================================================
$globalImpTotal = 0.0;
$globalExpTotal = 0.0;
$globalSlot = [];
$impTotal = 0.0; foreach ($powerMeters as $m) {
$expTotal = 0.0;
$slot = [];
// Deltas je Zähler des Users
foreach ($meters as $name => $mm) {
$name = $m['name'];
$impDelta = 0.0; $impDelta = 0.0;
$expDelta = 0.0; $expDelta = 0.0;
// IMPORT // IMPORT
if (!empty($mm['importVar'])) { if (!empty($m['var_consumption']) && IPS_VariableExists((int)$m['var_consumption'])) {
$varId = (int)$mm['importVar']; $impDelta = $this->getDeltaFromArchive((int)$m['var_consumption'], $ts, $slotEnd);
if (IPS_VariableExists($varId)) {
$impDelta = $this->getDeltaFromArchive($varId, $ts, $slotEnd);
}
} }
// EXPORT // EXPORT
if (!empty($mm['exportVar'])) { if (!empty($m['var_feed']) && IPS_VariableExists((int)$m['var_feed'])) {
$varId = (int)$mm['exportVar']; $expDelta = $this->getDeltaFromArchive((int)$m['var_feed'], $ts, $slotEnd);
if (IPS_VariableExists($varId)) {
$expDelta = $this->getDeltaFromArchive($varId, $ts, $slotEnd);
}
} }
// Nur hinzufügen, wenn Werte vorhanden sind
if ($impDelta > 0.0 || $expDelta > 0.0) { if ($impDelta > 0.0 || $expDelta > 0.0) {
$globalSlot[$name] = [
$slot[$name] = [
'imp' => $impDelta, 'imp' => $impDelta,
'exp' => $expDelta 'exp' => $expDelta,
'user_id' => (int)$m['user_id']
]; ];
$impTotal += $impDelta; $globalImpTotal += $impDelta;
$expTotal += $expDelta; $globalExpTotal += $expDelta;
}
} }
} // Wenn keine Imp/Exp vorhanden → nächster Slot
if ($globalImpTotal == 0.0 && $globalExpTotal == 0.0) {
// Logging der Eingangswerte
// Fall 1: Weder Import noch Export -> nichts zu berechnen
if ($impTotal == 0.0 && $expTotal == 0.0) {
continue; continue;
} }
// Verhältnis PV / Netz pro User // ==========================================================
if ($impTotal == 0.0 && $expTotal > 0.0) { // 2) Verhältnis PV / Netz
// Kein Import, aber Export → PV deckt alles // ==========================================================
if ($globalImpTotal == 0.0 && $globalExpTotal > 0.0) {
// nur Export → PV deckt alles
$ratio = 0.0; $ratio = 0.0;
$pvCoversAll = true; $pvCoversAll = true;
} elseif ($expTotal == 0.0 && $impTotal > 0.0) { } elseif ($globalExpTotal == 0.0 && $globalImpTotal > 0.0) {
// Kein Export, aber Import → Netz deckt alles // nur Import → Netz deckt alles
$ratio = 0.0; $ratio = 0.0;
$pvCoversAll = false; $pvCoversAll = false;
} elseif ($impTotal <= $expTotal) { } elseif ($globalImpTotal <= $globalExpTotal) {
// PV produziert genug oder mehr // PV produziert genug
$ratio = $expTotal > 0 ? ($impTotal / $expTotal) : 0.0; $ratio = $globalExpTotal ? ($globalImpTotal / $globalExpTotal) : 0.0;
$pvCoversAll = true; $pvCoversAll = true;
} else { } else {
// Import ist größer als Export → Netz deckt den Rest // Netzbezug notwendig
$ratio = $impTotal > 0 ? ($expTotal / $impTotal) : 0.0; $ratio = $globalImpTotal ? ($globalExpTotal / $globalImpTotal) : 0.0;
$pvCoversAll = false; $pvCoversAll = false;
} }
// ==========================================================
// 3) PRO USER VERTEILEN
// ==========================================================
foreach ($globalSlot as $name => $v) {
$userId = $v['user_id'];
// Falls User nicht im Cache ist → ignorieren
if (!isset($this->powerCostCache[$userId][$name])) {
continue;
}
// Werte pro Zähler verteilen
foreach ($slot as $name => $v) {
$imp = $v['imp']; $imp = $v['imp'];
$exp = $v['exp']; $exp = $v['exp'];
// Totale speichern
$this->powerCostCache[$userId][$name]['imp'] += $imp; $this->powerCostCache[$userId][$name]['imp'] += $imp;
$this->powerCostCache[$userId][$name]['exp'] += $exp; $this->powerCostCache[$userId][$name]['exp'] += $exp;
// PV deckt alles
if ($pvCoversAll) { if ($pvCoversAll) {
// PV deckt gesamten Verbrauch
$this->powerCostCache[$userId][$name]['solar_bezug'] += $imp; $this->powerCostCache[$userId][$name]['solar_bezug'] += $imp;
$this->powerCostCache[$userId][$name]['solareinspeisung'] += (1 - $ratio) * $exp; $this->powerCostCache[$userId][$name]['solareinspeisung'] += (1 - $ratio) * $exp;
$this->powerCostCache[$userId][$name]['solarverkauf'] += $ratio * $exp; $this->powerCostCache[$userId][$name]['solarverkauf'] += $ratio * $exp;
@@ -374,11 +374,11 @@ foreach ($meters as $name => $mm) {
} }
if ($pFeed !== null) { if ($pFeed !== null) {
$this->powerCostCache[$userId][$name]['rev_feedin'] += ((1 - $ratio) * $exp * $pFeed) / 100.0; $this->powerCostCache[$userId][$name]['rev_feedin'] += ((1 - $ratio) * $exp * $pFeed) / 100.0;
$this->powerCostCache[$userId][$name]['rev_zev'] += (($ratio) * $exp * $pSolar) / 100.0; $this->powerCostCache[$userId][$name]['rev_zev'] += ($ratio * $exp * $pSolar) / 100.0;
} }
// Netz deckt einen Teil
} else { } else {
// Teil Netzbezug
$this->powerCostCache[$userId][$name]['solar_bezug'] += $ratio * $imp; $this->powerCostCache[$userId][$name]['solar_bezug'] += $ratio * $imp;
$this->powerCostCache[$userId][$name]['netz_bezug'] += (1 - $ratio) * $imp; $this->powerCostCache[$userId][$name]['netz_bezug'] += (1 - $ratio) * $imp;
$this->powerCostCache[$userId][$name]['solarverkauf'] += $exp; $this->powerCostCache[$userId][$name]['solarverkauf'] += $exp;
@@ -391,13 +391,12 @@ foreach ($meters as $name => $mm) {
} }
if ($pFeed !== null) { if ($pFeed !== null) {
$this->powerCostCache[$userId][$name]['rev_zev'] += ($exp * $pSolar) / 100.0; $this->powerCostCache[$userId][$name]['rev_zev'] += ($exp * $pSolar) / 100.0;
}
}
}
}
} }
}
}
}
}
}
private function GetCalculatedPowerCosts($userId) private function GetCalculatedPowerCosts($userId)
{ {