no message

This commit is contained in:
2025-11-05 07:29:04 +01:00
parent 97e8e949fb
commit abd6016491

View File

@@ -243,92 +243,112 @@ private function GetValueAt(int $varId, int $timestamp, bool $nearestAfter = fal
private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type) private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type)
{ {
$startValTotal = $this->GetValueAt($meter['var_consumption'], $from, false);
$endValTotal = $this->GetValueAt($meter['var_consumption'], $to, true);
IPS_LogMessage('Abrechnung', "Meter {$meter['name']} ($type) von " . date('d.m.Y H:i', $from) . " bis " . date('d.m.Y H:i', $to) . ": start=$startValTotal, end=$endValTotal");
if ($startValTotal === null || $endValTotal === null) {
return ['row' => '', 'value' => 0];
}
$totalDiff = max(0, $endValTotal - $startValTotal);
if ($totalDiff <= 0) {
return ['row' => '', 'value' => 0];
}
$rows = ''; $rows = '';
$totalCost = 0.0; $totalCost = 0.0;
// Filtere Tarife nur für den relevanten Typ (z. B. Strom / Wasser) $varId = $meter['var_consumption'];
$tariffs = array_filter($tariffs, function($t) use ($type) { if (!IPS_VariableExists($varId)) {
IPS_LogMessage('Abrechnung', "❌ Variable {$varId} für {$meter['name']} nicht gefunden");
return ['row' => '', 'value' => 0];
}
// Filtere Tarife für diesen Typ (z. B. "Strombezug" oder "Warmwasser")
$tariffs = array_filter($tariffs, function ($t) use ($type) {
return strtolower($t['unit_type']) == strtolower($type); return strtolower($t['unit_type']) == strtolower($type);
}); });
if (empty($tariffs)) {
IPS_LogMessage('Abrechnung', "⚠ Keine passenden Tarife für $type gefunden");
return ['row' => '', 'value' => 0];
}
// Sortiere Tarife chronologisch // Sortiere Tarife chronologisch
usort($tariffs, function($a, $b) { usort($tariffs, function ($a, $b) {
return strtotime($a['start']) <=> strtotime($b['start']); return strtotime($a['start']) <=> strtotime($b['start']);
}); });
$currentStart = $from; $current = $from;
$currentStartVal = $startValTotal; while ($current < $to) {
// passenden Tarif für aktuellen Zeitpunkt finden
while ($currentStart < $to) {
// passenden Tarif zum aktuellen Zeitpunkt finden
$activeTariff = null; $activeTariff = null;
foreach ($tariffs as $t) { foreach ($tariffs as $t) {
$tariffStart = strtotime($t['start']); $tariffStart = strtotime($t['start']);
$tariffEnd = strtotime($t['end']); $tariffEnd = strtotime($t['end']);
if ($currentStart >= $tariffStart && $currentStart < $tariffEnd) { if ($current >= $tariffStart && $current < $tariffEnd) {
$activeTariff = $t; $activeTariff = $t;
break; break;
} }
} }
// wenn kein aktiver Tarif gefunden → Nulltarif bis nächster Start oder Rechnungsende
if ($activeTariff === null) { if ($activeTariff === null) {
// kein Tarif gefunden nehme Nulltarif bis zum nächsten bekannten Start oder Ende
$nextTariffStart = $to; $nextTariffStart = $to;
foreach ($tariffs as $t) { foreach ($tariffs as $t) {
$ts = strtotime($t['start']); $ts = strtotime($t['start']);
if ($ts > $currentStart && $ts < $nextTariffStart) { if ($ts > $current && $ts < $nextTariffStart) {
$nextTariffStart = $ts; $nextTariffStart = $ts;
} }
} }
$segmentStart = $current;
$segmentEnd = min($nextTariffStart, $to); $segmentEnd = min($nextTariffStart, $to);
$segmentTariff = 0.0; $segmentTariff = 0.0;
} else { } else {
$segmentStart = $current;
$segmentEnd = min(strtotime($activeTariff['end']), $to); $segmentEnd = min(strtotime($activeTariff['end']), $to);
$segmentTariff = floatval($activeTariff['price']); $segmentTariff = floatval($activeTariff['price']);
} }
// Verbrauchsanteil über Zeitverhältnis // Zählerstände direkt aus Archiv
$segmentFraction = ($segmentEnd - $currentStart) / ($to - $from); $startVal = $this->GetValueAt($varId, $segmentStart, false);
$segmentDiff = $totalDiff * $segmentFraction; $endVal = $this->GetValueAt($varId, $segmentEnd, true);
$segmentStartVal = $currentStartVal; if ($startVal === null || $endVal === null) {
$segmentEndVal = $segmentStartVal + $segmentDiff; IPS_LogMessage('Abrechnung', "⚠ Keine Werte im Archiv gefunden für {$meter['name']} ($type)");
$current = $segmentEnd;
continue;
}
$segmentCostCHF = ($segmentTariff / 100) * $segmentDiff; $diff = max(0, $endVal - $startVal);
$costCHF = ($segmentTariff / 100) * $diff;
$rows .= " $rows .= "
<tr> <tr>
<td>{$meter['name']}</td> <td>{$meter['name']}</td>
<td>$type</td> <td>$type</td>
<td>" . date('d.m.Y H:i', $currentStart) . "</td> <td>" . date('d.m.Y H:i', $segmentStart) . "</td>
<td>" . date('d.m.Y H:i', $segmentEnd) . "</td> <td>" . date('d.m.Y H:i', $segmentEnd) . "</td>
<td>" . number_format($segmentStartVal, 2) . "</td> <td>" . number_format($startVal, 2) . "</td>
<td>" . number_format($segmentEndVal, 2) . "</td> <td>" . number_format($endVal, 2) . "</td>
<td>" . number_format($segmentDiff, 2) . "</td> <td>" . number_format($diff, 2) . "</td>
<td>" . number_format($segmentTariff, 2) . "</td> <td>" . number_format($segmentTariff, 2) . "</td>
<td>" . number_format($segmentCostCHF, 2) . "</td> <td>" . number_format($costCHF, 2) . "</td>
</tr>"; </tr>";
$totalCost += $segmentCostCHF; $totalCost += $costCHF;
$currentStart = $segmentEnd; $current = $segmentEnd;
$currentStartVal = $segmentEndVal; }
if ($rows == '') {
// Falls keine Tarifzeile ermittelt wurde
$startVal = $this->GetValueAt($varId, $from, false);
$endVal = $this->GetValueAt($varId, $to, true);
$diff = max(0, $endVal - $startVal);
$rows .= "
<tr>
<td>{$meter['name']}</td>
<td>$type</td>
<td>" . date('d.m.Y H:i', $from) . "</td>
<td>" . date('d.m.Y H:i', $to) . "</td>
<td>" . number_format($startVal, 2) . "</td>
<td>" . number_format($endVal, 2) . "</td>
<td>" . number_format($diff, 2) . "</td>
<td>0</td>
<td>0.00</td>
</tr>";
} }
return ['row' => $rows, 'value' => $totalCost]; return ['row' => $rows, 'value' => $totalCost];
} }
} }