no message
This commit is contained in:
@@ -139,125 +139,163 @@ class Abrechnung extends IPSModule
|
||||
|
||||
// ====================== Stromkosten ======================
|
||||
|
||||
private function CalculatePowerCosts($powerMeters, $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>";
|
||||
private function CalculatePowerCosts($powerMeters, $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;
|
||||
foreach ($powerMeters as $m) {
|
||||
if ($m['user_id'] != $userId) continue;
|
||||
$cost = $this->AddMeterToPDFRow($m, $tariffs, $from, $to, 'Strombezug');
|
||||
$html .= $cost['row'];
|
||||
$total += $cost['value'];
|
||||
}
|
||||
$total = 0.0;
|
||||
$usedTariffs = [];
|
||||
|
||||
$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>";
|
||||
|
||||
return ['html' => $html, 'sum' => $total];
|
||||
foreach ($powerMeters as $m) {
|
||||
if ($m['user_id'] != $userId) continue;
|
||||
$cost = $this->AddMeterToPDFRow($m, $tariffs, $from, $to, 'Strombezug');
|
||||
$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 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 ======================
|
||||
|
||||
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>";
|
||||
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;
|
||||
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'];
|
||||
}
|
||||
$total = 0.0;
|
||||
$usedTariffs = [];
|
||||
|
||||
$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>";
|
||||
|
||||
return ['html' => $html, 'sum' => $total];
|
||||
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 ======================
|
||||
|
||||
private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type)
|
||||
{
|
||||
$rows = '';
|
||||
$totalCost = 0.0;
|
||||
$varId = $meter['var_consumption'];
|
||||
private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type)
|
||||
{
|
||||
$rows = '';
|
||||
$totalCost = 0.0;
|
||||
$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) =>
|
||||
strtolower(trim($t['unit_type'] ?? '')) === strtolower(trim($type))
|
||||
);
|
||||
$filteredTariffs = array_filter($tariffs, fn($t) =>
|
||||
strtolower(trim($t['unit_type'] ?? '')) === strtolower(trim($type))
|
||||
);
|
||||
|
||||
foreach ($filteredTariffs as &$t) {
|
||||
$t['start_ts'] = $this->toUnixTs($t['start'], false);
|
||||
$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];
|
||||
foreach ($filteredTariffs as &$t) {
|
||||
$t['start_ts'] = $this->toUnixTs($t['start'], false);
|
||||
$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']);
|
||||
|
||||
// 👇 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 ======================
|
||||
|
||||
|
||||
Reference in New Issue
Block a user