no message

This commit is contained in:
2025-12-05 10:42:51 +01:00
parent 82e4d4c523
commit 1b6dbbaa2e

View File

@@ -359,71 +359,43 @@ class Abrechnung extends IPSModule
return ['html' => $html, 'sum' => $total];
}
private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type)
{
$rows = '';
$totalCost = 0.0;
$usedTariffs = [];
private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type)
{
$varId = $meter['var_consumption'];
if (!IPS_VariableExists($varId)) {
return ['row' => '', 'value' => 0, 'tariffs' => []];
}
$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 = (int)$activeTariff['end_ts'];
$tariffPrice = (float)$activeTariff['price'];
$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);
// ---- 1) Start- und Endwerte direkt holen ----
$startValue = $this->GetValueAt($varId, $from, false); // letzter Wert VOR dem Start
$endValue = $this->GetValueAt($varId, $to, true); // erster Wert NACH dem Ende
if ($startValue === null || $endValue === null) {
break;
return ['row' => '', 'value' => 0, 'tariffs' => []];
}
// ---- 2) Verbrauch simple Differenz ----
$verbrauch = max(0, $endValue - $startValue);
$kosten = round(($tariffPrice / 100) * $verbrauch, 2);
$totalCost += $kosten;
$rows .= "<tr>
// ---- 3) passenden Tarif suchen ----
$tariffPrice = 0.0;
foreach ($tariffs as $t) {
if (strtolower(trim($t['unit_type'] ?? '')) === strtolower(trim($type))) {
$tariffPrice = floatval($t['price']);
break;
}
}
// ---- 4) Kosten (Rp → CHF) ----
$kosten = round(($tariffPrice / 100) * $verbrauch, 2);
// ---- 5) Tabellenzeile ----
$row = "<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='center'>" . date('d.m.Y', $from) . "</td>
<td align='center'>" . date('d.m.Y', $to) . "</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>
@@ -431,19 +403,17 @@ class Abrechnung extends IPSModule
<td align='right'>" . number_format($kosten, 2) . "</td>
</tr>";
if ($tariffEnd < $to) {
$currentStart = $tariffEnd + 1;
} else {
break;
}
}
return [
'row' => $row,
'value' => $kosten,
'tariffs' => [[
'start' => $from,
'end' => $to,
'price' => $tariffPrice
]]
];
}
if ($rows === '') {
return ['row' => '', 'value' => 0, 'tariffs' => []];
}
return ['row' => $rows, 'value' => $totalCost, 'tariffs' => array_values($usedTariffs)];
}
// ====================== Hilfsfunktionen ======================