no message
This commit is contained in:
@@ -104,7 +104,7 @@
|
|||||||
"caption": "Einheit",
|
"caption": "Einheit",
|
||||||
"name": "unit_type",
|
"name": "unit_type",
|
||||||
"width": "20%",
|
"width": "20%",
|
||||||
"add": "Strombezug",
|
"add": "Netztarif",
|
||||||
"edit": {
|
"edit": {
|
||||||
"type": "Select",
|
"type": "Select",
|
||||||
"options": [
|
"options": [
|
||||||
|
|||||||
@@ -141,6 +141,8 @@ class Abrechnung extends IPSModule
|
|||||||
|
|
||||||
private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to)
|
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 = "<table border='1' cellspacing='0' cellpadding='3' width='100%' style='font-size:8px;'>
|
$html = "<table border='1' cellspacing='0' cellpadding='3' width='100%' style='font-size:8px;'>
|
||||||
<tr style='background-color:#f0f0f0;'>
|
<tr style='background-color:#f0f0f0;'>
|
||||||
<th>Zähler</th>
|
<th>Zähler</th>
|
||||||
@@ -158,9 +160,8 @@ private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to
|
|||||||
|
|
||||||
// Initialisierung
|
// Initialisierung
|
||||||
$acc = [];
|
$acc = [];
|
||||||
|
|
||||||
// Benutzerzähler laden
|
|
||||||
$meters = [];
|
$meters = [];
|
||||||
|
|
||||||
foreach ($powerMeters as $m) {
|
foreach ($powerMeters as $m) {
|
||||||
if ($m['user_id'] != $userId) continue;
|
if ($m['user_id'] != $userId) continue;
|
||||||
$name = $m['name'];
|
$name = $m['name'];
|
||||||
@@ -179,6 +180,7 @@ private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (empty($meters)) {
|
if (empty($meters)) {
|
||||||
|
IPS_LogMessage('Abrechnung', "⚠️ Keine Stromzähler für Benutzer $userId gefunden.");
|
||||||
$html .= "<tr><td colspan='11' align='center'>Keine Stromzähler für diesen Benutzer</td></tr></table><br>";
|
$html .= "<tr><td colspan='11' align='center'>Keine Stromzähler für diesen Benutzer</td></tr></table><br>";
|
||||||
return ['html'=>$html, 'sum'=>0.0];
|
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) {
|
for ($ts = $from; $ts < $to; $ts += 900) {
|
||||||
$slotEnd = min($to, $ts + 900);
|
$slotEnd = min($to, $ts + 900);
|
||||||
|
|
||||||
// Tarife für diesen Zeitraum holen
|
// Tarife abrufen
|
||||||
$pGrid = $this->getTariffPriceAt($tariffs, ['Netztarif'], $ts);
|
$pGrid = $this->getTariffPriceAt($tariffs, ['Netztarif'], $ts);
|
||||||
$pSolar = $this->getTariffPriceAt($tariffs, ['Solartarif'], $ts);
|
$pSolar = $this->getTariffPriceAt($tariffs, ['Solartarif'], $ts);
|
||||||
$pFeed = $this->getTariffPriceAt($tariffs, ['Einspeisetarif'], $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;
|
$impTotal = 0.0;
|
||||||
$expTotal = 0.0;
|
$expTotal = 0.0;
|
||||||
$slot = [];
|
$slot = [];
|
||||||
@@ -211,6 +217,8 @@ private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to
|
|||||||
$slot[$name] = ['imp'=>$impDelta, 'exp'=>$expDelta];
|
$slot[$name] = ['imp'=>$impDelta, 'exp'=>$expDelta];
|
||||||
$impTotal += $impDelta;
|
$impTotal += $impDelta;
|
||||||
$expTotal += $expDelta;
|
$expTotal += $expDelta;
|
||||||
|
|
||||||
|
IPS_LogMessage('Abrechnung', sprintf(" → %s: imp=%.4f, exp=%.4f", $name, $impDelta, $expDelta));
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($impTotal <= 0 && $expTotal <= 0) continue;
|
if ($impTotal <= 0 && $expTotal <= 0) continue;
|
||||||
@@ -223,7 +231,6 @@ private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to
|
|||||||
$imp = $slot[$name]['imp'];
|
$imp = $slot[$name]['imp'];
|
||||||
$exp = $slot[$name]['exp'];
|
$exp = $slot[$name]['exp'];
|
||||||
|
|
||||||
// Mengen
|
|
||||||
$acc[$name]['imp'] += $imp;
|
$acc[$name]['imp'] += $imp;
|
||||||
$acc[$name]['exp'] += $exp;
|
$acc[$name]['exp'] += $exp;
|
||||||
|
|
||||||
@@ -232,10 +239,11 @@ private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to
|
|||||||
$acc[$name]['solareinspeisung']+= (1 - $ratio) * $exp;
|
$acc[$name]['solareinspeisung']+= (1 - $ratio) * $exp;
|
||||||
$acc[$name]['solarverkauf'] += $ratio * $exp;
|
$acc[$name]['solarverkauf'] += $ratio * $exp;
|
||||||
|
|
||||||
// Kosten / Erträge
|
|
||||||
if ($pSolar !== null) $acc[$name]['cost_solar'] += ($imp * $pSolar) / 100;
|
if ($pSolar !== null) $acc[$name]['cost_solar'] += ($imp * $pSolar) / 100;
|
||||||
if ($pFeed !== null) $acc[$name]['rev_feedin'] += ((1 - $ratio) * $exp * $pFeed) / 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 =====
|
// ===== 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 ($pSolar !== null) $acc[$name]['cost_solar'] += ($ratio * $imp * $pSolar) / 100;
|
||||||
if ($pFeed !== null) $acc[$name]['rev_feedin'] += ($exp * $pFeed) / 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;
|
$grand = 0.0;
|
||||||
foreach ($acc as $name => $v) {
|
foreach ($acc as $name => $v) {
|
||||||
$sum = $v['cost_solar'] + $v['cost_grid'] - $v['rev_feedin'];
|
$sum = $v['cost_solar'] + $v['cost_grid'] - $v['rev_feedin'];
|
||||||
@@ -286,52 +296,11 @@ private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to
|
|||||||
<td align='right'>" . number_format($grand, 2) . "</td>
|
<td align='right'>" . number_format($grand, 2) . "</td>
|
||||||
</tr></table><br>";
|
</tr></table><br>";
|
||||||
|
|
||||||
|
IPS_LogMessage('Abrechnung', sprintf("✅ Stromkosten fertig: %.2f CHF total", $grand));
|
||||||
|
|
||||||
return ['html'=>$html, 'sum'=>$grand];
|
return ['html'=>$html, 'sum'=>$grand];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ====================== Nebenkosten ======================
|
|
||||||
|
|
||||||
private function CalculateAdditionalCosts($waterMeters, $tariffs, $userId, $from, $to)
|
|
||||||
{
|
|
||||||
$html = "<table border='1' cellspacing='0' cellpadding='3' width='100%' style='font-size:8px;'>
|
|
||||||
<tr style='background-color:#f0f0f0;'>
|
|
||||||
<th>Zähler</th><th>Typ</th><th>Start</th><th>Ende</th>
|
|
||||||
<th>Zähler Start</th><th>Zähler Ende</th><th>Verbrauch</th><th>Tarif (Rp)</th><th>Kosten (CHF)</th>
|
|
||||||
</tr>";
|
|
||||||
|
|
||||||
$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 .= "<tr style='background-color:#f9f9f9; font-weight:bold;'>
|
|
||||||
<td colspan='8' align='right'>Total Nebenkosten:</td>
|
|
||||||
<td align='right'>" . number_format($total, 2) . "</td>
|
|
||||||
</tr></table><br>";
|
|
||||||
|
|
||||||
// 👇 Tarifliste anhängen
|
|
||||||
if (!empty($usedTariffs)) {
|
|
||||||
$html .= "<p><strong>Angewendete Nebenkostentarife:</strong></p><ul>";
|
|
||||||
foreach ($usedTariffs as $t) {
|
|
||||||
$html .= "<li>" . date('d.m.Y', $t['start']) . " – " . date('d.m.Y', $t['end']) . ": " .
|
|
||||||
number_format($t['price'], 2) . " Rp</li>";
|
|
||||||
}
|
|
||||||
$html .= "</ul><br>";
|
|
||||||
}
|
|
||||||
|
|
||||||
return ['html' => $html, 'sum' => $total];
|
|
||||||
}
|
|
||||||
|
|
||||||
// ====================== Kernberechnung ======================
|
// ====================== Kernberechnung ======================
|
||||||
|
|
||||||
private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type)
|
private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type)
|
||||||
|
|||||||
Reference in New Issue
Block a user