no message
This commit is contained in:
+141
-103
@@ -139,125 +139,163 @@ class Abrechnung extends IPSModule
|
|||||||
|
|
||||||
// ====================== Stromkosten ======================
|
// ====================== Stromkosten ======================
|
||||||
|
|
||||||
private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $to)
|
private function CalculatePowerCosts($powerMeters, $tariffs, $userId, $from, $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>Typ</th><th>Start</th><th>Ende</th>
|
<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>
|
<th>Zähler Start</th><th>Zähler Ende</th><th>Verbrauch</th><th>Tarif (Rp)</th><th>Kosten (CHF)</th>
|
||||||
</tr>";
|
</tr>";
|
||||||
|
|
||||||
$total = 0.0;
|
$total = 0.0;
|
||||||
foreach ($powerMeters as $m) {
|
$usedTariffs = [];
|
||||||
if ($m['user_id'] != $userId) continue;
|
|
||||||
$cost = $this->AddMeterToPDFRow($m, $tariffs, $from, $to, 'Strombezug');
|
|
||||||
$html .= $cost['row'];
|
|
||||||
$total += $cost['value'];
|
|
||||||
}
|
|
||||||
|
|
||||||
$html .= "<tr style='background-color:#f9f9f9; font-weight:bold;'>
|
foreach ($powerMeters as $m) {
|
||||||
<td colspan='8' align='right'>Total Stromkosten:</td>
|
if ($m['user_id'] != $userId) continue;
|
||||||
<td align='right'>" . number_format($total, 2) . "</td>
|
$cost = $this->AddMeterToPDFRow($m, $tariffs, $from, $to, 'Strombezug');
|
||||||
</tr></table><br>";
|
$html .= $cost['row'];
|
||||||
|
$total += $cost['value'];
|
||||||
return ['html' => $html, 'sum' => $total];
|
$usedTariffs = array_merge($usedTariffs, $cost['tariffs']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$html .= "<tr style='background-color:#f9f9f9; font-weight:bold;'>
|
||||||
|
<td colspan='8' align='right'>Total Stromkosten:</td>
|
||||||
|
<td align='right'>" . number_format($total, 2) . "</td>
|
||||||
|
</tr></table><br>";
|
||||||
|
|
||||||
|
// 👇 Tarifliste anhängen
|
||||||
|
if (!empty($usedTariffs)) {
|
||||||
|
$html .= "<p><strong>Angewendete Stromtarife:</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/kWh</li>";
|
||||||
|
}
|
||||||
|
$html .= "</ul><br>";
|
||||||
|
}
|
||||||
|
|
||||||
|
return ['html' => $html, 'sum' => $total];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// ====================== Nebenkosten ======================
|
// ====================== Nebenkosten ======================
|
||||||
|
|
||||||
private function CalculateAdditionalCosts($waterMeters, $tariffs, $userId, $from, $to)
|
private function CalculateAdditionalCosts($waterMeters, $tariffs, $userId, $from, $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>Typ</th><th>Start</th><th>Ende</th>
|
<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>
|
<th>Zähler Start</th><th>Zähler Ende</th><th>Verbrauch</th><th>Tarif (Rp)</th><th>Kosten (CHF)</th>
|
||||||
</tr>";
|
</tr>";
|
||||||
|
|
||||||
$total = 0.0;
|
$total = 0.0;
|
||||||
foreach ($waterMeters as $m) {
|
$usedTariffs = [];
|
||||||
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'];
|
|
||||||
}
|
|
||||||
|
|
||||||
$html .= "<tr style='background-color:#f9f9f9; font-weight:bold;'>
|
foreach ($waterMeters as $m) {
|
||||||
<td colspan='8' align='right'>Total Nebenkosten:</td>
|
if ($m['user_id'] != $userId) continue;
|
||||||
<td align='right'>" . number_format($total, 2) . "</td>
|
$type = $m['meter_type'] ?? 'Warmwasser';
|
||||||
</tr></table><br>";
|
$cost = $this->AddMeterToPDFRow($m, $tariffs, $from, $to, $type);
|
||||||
|
$html .= $cost['row'];
|
||||||
return ['html' => $html, 'sum' => $total];
|
$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)
|
||||||
{
|
{
|
||||||
$rows = '';
|
$rows = '';
|
||||||
$totalCost = 0.0;
|
$totalCost = 0.0;
|
||||||
$varId = $meter['var_consumption'];
|
$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) =>
|
$filteredTariffs = array_filter($tariffs, fn($t) =>
|
||||||
strtolower(trim($t['unit_type'] ?? '')) === strtolower(trim($type))
|
strtolower(trim($t['unit_type'] ?? '')) === strtolower(trim($type))
|
||||||
);
|
);
|
||||||
|
|
||||||
foreach ($filteredTariffs as &$t) {
|
foreach ($filteredTariffs as &$t) {
|
||||||
$t['start_ts'] = $this->toUnixTs($t['start'], false);
|
$t['start_ts'] = $this->toUnixTs($t['start'], false);
|
||||||
$t['end_ts'] = $this->toUnixTs($t['end'], true);
|
$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 .= "<tr>
|
|
||||||
<td>{$meter['name']}</td>
|
|
||||||
<td>{$type}</td>
|
|
||||||
<td align='center'>" . date('d.m.Y', $currentStart) . "</td>
|
|
||||||
<td align='center'>" . date('d.m.Y', $segmentEnd) . "</td>
|
|
||||||
<td align='right'>" . number_format($startValue, 2) . "</td>
|
|
||||||
<td align='right'>" . number_format($endValue, 2) . "</td>
|
|
||||||
<td align='right'>" . number_format($verbrauch, 2) . "</td>
|
|
||||||
<td align='right'>" . number_format($tariffPrice, 2) . "</td>
|
|
||||||
<td align='right'>" . number_format($kosten, 2) . "</td>
|
|
||||||
</tr>";
|
|
||||||
|
|
||||||
if ($tariffEnd < $to) $currentStart = $tariffEnd + 1;
|
|
||||||
else break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($rows === '') return ['row' => '', 'value' => 0];
|
|
||||||
|
|
||||||
return ['row' => $rows, 'value' => $totalCost];
|
|
||||||
}
|
}
|
||||||
|
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 .= "<tr>
|
||||||
|
<td>{$meter['name']}</td>
|
||||||
|
<td>{$type}</td>
|
||||||
|
<td align='center'>" . date('d.m.Y', $currentStart) . "</td>
|
||||||
|
<td align='center'>" . date('d.m.Y', $segmentEnd) . "</td>
|
||||||
|
<td align='right'>" . number_format($startValue, 2) . "</td>
|
||||||
|
<td align='right'>" . number_format($endValue, 2) . "</td>
|
||||||
|
<td align='right'>" . number_format($verbrauch, 2) . "</td>
|
||||||
|
<td align='right'>" . number_format($tariffPrice, 2) . "</td>
|
||||||
|
<td align='right'>" . number_format($kosten, 2) . "</td>
|
||||||
|
</tr>";
|
||||||
|
|
||||||
|
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 ======================
|
// ====================== Hilfsfunktionen ======================
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user