no message

This commit is contained in:
dh
2025-11-05 14:14:22 +01:00
parent f96a505cb9
commit f23fabd460
+38 -82
View File
@@ -295,17 +295,14 @@ private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type)
$totalCost = 0.0; $totalCost = 0.0;
$varId = $meter['var_consumption']; $varId = $meter['var_consumption'];
IPS_LogMessage('Abrechnung', "🔧 [AddMeterToPDFRow] Starte für '{$meter['name']}' (Typ: {$type}) von " . date('d.m.Y H:i', $from) . " bis " . date('d.m.Y H:i', $to));
if (!IPS_VariableExists($varId)) { if (!IPS_VariableExists($varId)) {
IPS_LogMessage('Abrechnung', "❌ Variable {$varId} für {$meter['name']} nicht gefunden"); IPS_LogMessage('Abrechnung', "❌ Variable {$varId} für {$meter['name']} nicht gefunden");
return ['row' => '', 'value' => 0]; return ['row' => '', 'value' => 0];
} }
// Zeitzone setzen, um strtotime()-Abweichungen zu vermeiden
date_default_timezone_set('Europe/Zurich'); date_default_timezone_set('Europe/Zurich');
// 1️⃣ Relevante Tarife nach Typ filtern // 🔹 Tarife nach Typ filtern
$filteredTariffs = array_filter($tariffs, function ($t) use ($type) { $filteredTariffs = array_filter($tariffs, function ($t) use ($type) {
return strtolower(trim($t['unit_type'] ?? '')) === strtolower(trim($type)); return strtolower(trim($t['unit_type'] ?? '')) === strtolower(trim($type));
}); });
@@ -315,24 +312,10 @@ private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type)
return ['row' => '', 'value' => 0]; return ['row' => '', 'value' => 0];
} }
// 2️⃣ Zeitstempel konvertieren, JSON-Objekte erkennen & sortieren // 🔹 Zeitstempel normieren und sortieren
foreach ($filteredTariffs as &$t) { foreach ($filteredTariffs as &$t) {
// JSON-Fix: Start/Ende evtl. verschachtelt $t['start_ts'] = $this->toUnixTs($t['start'], false);
if (is_string($t['start']) && str_starts_with(trim($t['start']), '{')) { $t['end_ts'] = $this->toUnixTs($t['end'], true);
$s = json_decode($t['start'], true);
if (is_array($s)) {
$t['start'] = sprintf('%04d-%02d-%02d 00:00:00', $s['year'], $s['month'], $s['day']);
}
}
if (is_string($t['end']) && str_starts_with(trim($t['end']), '{')) {
$e = json_decode($t['end'], true);
if (is_array($e)) {
$t['end'] = sprintf('%04d-%02d-%02d 23:59:59', $e['year'], $e['month'], $e['day']);
}
}
$t['start_ts'] = is_numeric($t['start']) ? intval($t['start']) : strtotime($t['start']);
$t['end_ts'] = is_numeric($t['end']) ? intval($t['end']) : strtotime($t['end']);
if (!$t['start_ts'] || !$t['end_ts']) { if (!$t['start_ts'] || !$t['end_ts']) {
IPS_LogMessage('Abrechnung', "⚠ Ungültiger Tarifzeitraum: " . json_encode($t)); IPS_LogMessage('Abrechnung', "⚠ Ungültiger Tarifzeitraum: " . json_encode($t));
@@ -346,37 +329,23 @@ private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type)
} }
} }
unset($t); unset($t);
usort($filteredTariffs, fn($a, $b) => $a['start_ts'] <=> $b['start_ts']); usort($filteredTariffs, fn($a, $b) => ($a['start_ts'] ?? 0) <=> ($b['start_ts'] ?? 0));
// 3️⃣ Abrechnungslogik
$currentStart = $from; $currentStart = $from;
$segmentIndex = 1; $segmentIndex = 1;
while ($currentStart < $to) { while ($currentStart < $to) {
IPS_LogMessage('Abrechnung', "➡️ Segment {$segmentIndex} Startzeit: " . date('d.m.Y H:i', $currentStart)); // 🔹 Aktiven Tarif finden
// 🔹 Aktiven Tarif bestimmen (FIX: <= auch am Ende erlaubt)
$activeTariff = null; $activeTariff = null;
foreach ($filteredTariffs as $t) { foreach ($filteredTariffs as $t) {
$startTs = intval($t['start_ts']); if (($t['start_ts'] ?? 0) <= $currentStart && $currentStart <= ($t['end_ts'] ?? 0)) {
$endTs = intval($t['end_ts']);
if ($startTs === 0 || $endTs === 0) continue;
if ($startTs <= $currentStart && $currentStart <= $endTs) {
$activeTariff = $t; $activeTariff = $t;
IPS_LogMessage('Abrechnung', sprintf(
" ✅ Tarif erkannt: %.2f Rp gültig %s → %s",
floatval($t['price']),
date('d.m.Y H:i', $startTs),
date('d.m.Y H:i', $endTs)
));
break; break;
} }
} }
// 🔹 Falls kein aktiver Tarif → 0 Rp
if (!$activeTariff) { if (!$activeTariff) {
IPS_LogMessage('Abrechnung', "⚠ Kein Tarif aktiv bei " . date('d.m.Y H:i', $currentStart) . " → 0 Rp"); IPS_LogMessage('Abrechnung', "⚠ Kein Tarif aktiv bei " . date('d.m.Y H:i', $currentStart));
$activeTariff = [ $activeTariff = [
'start_ts' => $currentStart, 'start_ts' => $currentStart,
'end_ts' => $to, 'end_ts' => $to,
@@ -389,45 +358,34 @@ private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type)
$tariffPrice = floatval($activeTariff['price']); $tariffPrice = floatval($activeTariff['price']);
$tariffLabel = number_format($tariffPrice, 2, ',', ''); $tariffLabel = number_format($tariffPrice, 2, ',', '');
// 🔹 Start- & Endwerte aus Archiv // 🔹 Werte lesen
$startValue = $this->GetValueAt($varId, $currentStart, true); $startValue = $this->GetValueAt($varId, $currentStart, true);
if ($startValue === null) {
IPS_LogMessage('Abrechnung', "⚠ Kein Startwert für " . date('d.m.Y H:i', $currentStart));
break;
}
$segmentEnd = ($tariffEnd < $to) ? $tariffEnd : $to; $segmentEnd = ($tariffEnd < $to) ? $tariffEnd : $to;
$endValue = $this->GetValueAt($varId, $segmentEnd, true); $endValue = $this->GetValueAt($varId, $segmentEnd, true);
if ($endValue === null) {
IPS_LogMessage('Abrechnung', "⚠ Kein Endwert für " . date('d.m.Y H:i', $segmentEnd)); if ($startValue === null || $endValue === null) {
IPS_LogMessage('Abrechnung', "⚠ Keine Werte für {$meter['name']} im Zeitraum " . date('d.m.Y H:i', $currentStart));
break; break;
} }
// 🔹 Verbrauch & Kosten
$verbrauch = max(0, $endValue - $startValue); $verbrauch = max(0, $endValue - $startValue);
$kosten = round(($tariffPrice / 100) * $verbrauch, 2); $kosten = round(($tariffPrice / 100) * $verbrauch, 2);
$totalCost += $kosten; $totalCost += $kosten;
IPS_LogMessage('Abrechnung', sprintf( // 🔹 Tabellenzeile formatiert
" 📊 Segment %d: %.3f → %.3f (Δ=%.3f) | Tarif=%.3f Rp | Kosten=%.2f CHF",
$segmentIndex, $startValue, $endValue, $verbrauch, $tariffPrice, $kosten
));
// 🔹 Tabellenzeile
$rows .= " $rows .= "
<tr> <tr style='border-bottom:0.3px solid #ccc;'>
<td>{$meter['name']}</td> <td style='padding:2px;'>{$meter['name']}</td>
<td>{$type}</td> <td style='padding:2px;'>{$type}</td>
<td>" . date('d.m.Y H:i', $currentStart) . "</td> <td style='padding:2px; text-align:center;'>" . date('d.m.Y H:i', $currentStart) . "</td>
<td>" . date('d.m.Y H:i', $segmentEnd) . "</td> <td style='padding:2px; text-align:center;'>" . date('d.m.Y H:i', $segmentEnd) . "</td>
<td>" . number_format($startValue, 2) . "</td> <td style='padding:2px; text-align:right;'>" . number_format($startValue, 2) . "</td>
<td>" . number_format($endValue, 2) . "</td> <td style='padding:2px; text-align:right;'>" . number_format($endValue, 2) . "</td>
<td>" . number_format($verbrauch, 2) . "</td> <td style='padding:2px; text-align:right;'>" . number_format($verbrauch, 2) . "</td>
<td>{$tariffLabel}</td> <td style='padding:2px; text-align:right;'>" . $tariffLabel . "</td>
<td>" . number_format($kosten, 2) . "</td> <td style='padding:2px; text-align:right;'>" . number_format($kosten, 2) . "</td>
</tr>"; </tr>";
// 🔹 Neuen Startpunkt setzen (FIX: +1 Sekunde nach Tarifende)
if ($tariffEnd < $to) { if ($tariffEnd < $to) {
$currentStart = $tariffEnd + 1; $currentStart = $tariffEnd + 1;
} else { } else {
@@ -437,29 +395,27 @@ private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type)
$segmentIndex++; $segmentIndex++;
} }
// Fallback, falls keine Segmente erzeugt wurden // Fallback – kein Segment berechnet
if ($rows === '') { if ($rows === '') {
IPS_LogMessage('Abrechnung', "⚠ Keine Segmente erzeugt, Fallback auf Gesamtzeitraum"); $startVal = $this->GetValueAt($varId, $from, false);
$startValue = $this->GetValueAt($varId, $from, false); $endVal = $this->GetValueAt($varId, $to, true);
$endValue = $this->GetValueAt($varId, $to, true); $verbrauch = max(0, $endVal - $startVal);
$verbrauch = max(0, $endValue - $startValue);
$rows = " $rows = "
<tr> <tr>
<td>{$meter['name']}</td> <td style='padding:2px;'>{$meter['name']}</td>
<td>{$type}</td> <td style='padding:2px;'>{$type}</td>
<td>" . date('d.m.Y H:i', $from) . "</td> <td style='padding:2px;'>" . date('d.m.Y H:i', $from) . "</td>
<td>" . date('d.m.Y H:i', $to) . "</td> <td style='padding:2px;'>" . date('d.m.Y H:i', $to) . "</td>
<td>" . number_format($startValue, 2) . "</td> <td style='padding:2px; text-align:right;'>" . number_format($startVal, 2) . "</td>
<td>" . number_format($endValue, 2) . "</td> <td style='padding:2px; text-align:right;'>" . number_format($endVal, 2) . "</td>
<td>" . number_format($verbrauch, 2) . "</td> <td style='padding:2px; text-align:right;'>" . number_format($verbrauch, 2) . "</td>
<td>0</td> <td style='padding:2px; text-align:right;'>0.00</td>
<td>0.00</td> <td style='padding:2px; text-align:right;'>0.00</td>
</tr>"; </tr>";
} }
IPS_LogMessage('Abrechnung', sprintf("✅ [AddMeterToPDFRow] Fertig: %.2f CHF total, %d Segmente", $totalCost, $segmentIndex - 1));
return ['row' => $rows, 'value' => $totalCost]; return ['row' => $rows, 'value' => $totalCost];
} }
} }