no message
This commit is contained in:
@@ -359,92 +359,62 @@ class Abrechnung extends IPSModule
|
|||||||
return ['html' => $html, 'sum' => $total];
|
return ['html' => $html, 'sum' => $total];
|
||||||
}
|
}
|
||||||
|
|
||||||
private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type)
|
private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type)
|
||||||
{
|
{
|
||||||
$rows = '';
|
$varId = $meter['var_consumption'];
|
||||||
$totalCost = 0.0;
|
|
||||||
$usedTariffs = [];
|
|
||||||
$varId = $meter['var_consumption'];
|
|
||||||
|
|
||||||
if (!IPS_VariableExists($varId)) {
|
if (!IPS_VariableExists($varId)) {
|
||||||
return ['row' => '', 'value' => 0, 'tariffs' => []];
|
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);
|
|
||||||
|
|
||||||
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)];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---- 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) {
|
||||||
|
return ['row' => '', 'value' => 0, 'tariffs' => []];
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- 2) Verbrauch simple Differenz ----
|
||||||
|
$verbrauch = max(0, $endValue - $startValue);
|
||||||
|
|
||||||
|
// ---- 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', $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>
|
||||||
|
<td align='right'>" . number_format($tariffPrice, 2) . "</td>
|
||||||
|
<td align='right'>" . number_format($kosten, 2) . "</td>
|
||||||
|
</tr>";
|
||||||
|
|
||||||
|
return [
|
||||||
|
'row' => $row,
|
||||||
|
'value' => $kosten,
|
||||||
|
'tariffs' => [[
|
||||||
|
'start' => $from,
|
||||||
|
'end' => $to,
|
||||||
|
'price' => $tariffPrice
|
||||||
|
]]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// ====================== Hilfsfunktionen ======================
|
// ====================== Hilfsfunktionen ======================
|
||||||
|
|
||||||
private function GetValueAt($varId, $timestamp, $nearestAfter = true)
|
private function GetValueAt($varId, $timestamp, $nearestAfter = true)
|
||||||
|
|||||||
Reference in New Issue
Block a user