From 43277a9e364ffd5d5274bd13b95209f1f255d8a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=A4fliger?= Date: Tue, 4 Nov 2025 15:18:35 +0100 Subject: [PATCH] no message --- Abrechnung/module.php | 120 ++++++++++++++++++++++++------------------ 1 file changed, 70 insertions(+), 50 deletions(-) diff --git a/Abrechnung/module.php b/Abrechnung/module.php index e3c696d..c179424 100644 --- a/Abrechnung/module.php +++ b/Abrechnung/module.php @@ -218,36 +218,42 @@ public function GenerateInvoices() private function GetValueAt(int $varId, int $timestamp) { $archiveID = @IPS_GetInstanceListByModuleID('{43192F0B-135B-4CE7-A0A7-1475603F3060}')[0]; - if (!$archiveID || !IPS_VariableExists($varId)) return null; - - // 1. Werte **vor und nach** dem Timestamp holen - $valuesBefore = @AC_GetLoggedValues($archiveID, $varId, $timestamp - 86400, $timestamp, 1); // letzter Wert <= Timestamp - $valuesAfter = @AC_GetLoggedValues($archiveID, $varId, $timestamp, $timestamp + 86400, 1); // erster Wert >= Timestamp - - if ($valuesBefore && count($valuesBefore) > 0) { - $before = floatval($valuesBefore[count($valuesBefore)-1]['Value']); - } else { - $before = null; + if (!$archiveID || !IPS_VariableExists($varId)) { + IPS_LogMessage('Abrechnung', "❌ Variable $varId oder Archiv $archiveID nicht gefunden"); + return null; } - if ($valuesAfter && count($valuesAfter) > 0) { - $after = floatval($valuesAfter[0]['Value']); - } else { - $after = null; - } + // Letzter Wert <= Timestamp + $before = @AC_GetLoggedValues($archiveID, $varId, 0, $timestamp, 1); + $beforeVal = ($before && count($before) > 0) ? floatval($before[count($before)-1]['Value']) : null; - // Priorität: exakter Wert >= Timestamp, sonst letzter Wert davor - if ($after !== null) return $after; - if ($before !== null) return $before; + // Erster Wert >= Timestamp + $after = @AC_GetLoggedValues($archiveID, $varId, $timestamp, time(), 1); + $afterVal = ($after && count($after) > 0) ? floatval($after[0]['Value']) : null; + + // Wert auswählen: Priorität nach Timestamp + if ($afterVal !== null) { + IPS_LogMessage('Abrechnung', "Variable $varId: Wert nach Timestamp gefunden = $afterVal"); + return $afterVal; + } + if ($beforeVal !== null) { + IPS_LogMessage('Abrechnung', "Variable $varId: Wert vor Timestamp gefunden = $beforeVal"); + return $beforeVal; + } // Fallback auf aktuellen Wert - return floatval(GetValue($varId)); + $current = floatval(GetValue($varId)); + IPS_LogMessage('Abrechnung', "⚠ Kein Archivwert für $varId gefunden, nehme aktuellen Wert: $current"); + return $current; } private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type) { $startVal = $this->GetValueAt($meter['var_consumption'], $from); $endVal = $this->GetValueAt($meter['var_consumption'], $to); + + IPS_LogMessage('Abrechnung', "Meter {$meter['name']} ($type) von " . date('d.m.Y H:i', $from) . " bis " . date('d.m.Y H:i', $to) . ": start=$startVal, end=$endVal"); + if ($startVal === null || $endVal === null) return ['row' => '', 'value' => 0]; $totalDiff = max(0, $endVal - $startVal); @@ -257,46 +263,60 @@ private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type) $totalCost = 0.0; // Alle Tarife dieses Typs prüfen - foreach ($tariffs as $t) { - if (strtolower($t['unit_type']) != strtolower($type)) continue; + foreach ($tariffs as $t) { + if (strtolower($t['unit_type']) != strtolower($type)) continue; - $tariffStart = strtotime($t['start']); - $tariffEnd = strtotime($t['end']); + $tariffStart = strtotime($t['start']); + $tariffEnd = strtotime($t['end']); - // Zeitliche Überschneidung mit Abrechnungszeitraum? - if ($tariffEnd < $from || $tariffStart > $to) continue; + // Überschneidung mit Abrechnungszeitraum prüfen + if ($tariffEnd < $from || $tariffStart > $to) continue; - // Überschneidung bestimmen - $sectionStart = max($from, $tariffStart); - $sectionEnd = min($to, $tariffEnd); - $sectionDuration = $sectionEnd - $sectionStart; - $totalDuration = $to - $from; + // Überschneidung bestimmen + $sectionStart = max($from, $tariffStart); + $sectionEnd = min($to, $tariffEnd); + $sectionDuration = $sectionEnd - $sectionStart; + $totalDuration = $to - $from; - if ($sectionDuration <= 0) continue; + if ($sectionDuration <= 0) continue; - // Anteil des Gesamtverbrauchs nach Zeitanteil - $sectionFraction = $sectionDuration / $totalDuration; - $sectionDiff = $totalDiff * $sectionFraction; + // Verbrauch anteilig nach Dauer + $sectionFraction = $sectionDuration / $totalDuration; + $sectionDiff = $totalDiff * $sectionFraction; - $tariffRp = floatval($t['price']); - $costCHF = ($tariffRp / 100) * $sectionDiff; + $tariffRp = floatval($t['price']); + $costCHF = ($tariffRp / 100) * $sectionDiff; - $rows .= " - - {$meter['name']} - $type - " . date('d.m.Y', $sectionStart) . " - " . date('d.m.Y', $sectionEnd) . " - " . number_format($sectionDiff, 2) . " - " . number_format($tariffRp, 2) . " - " . number_format($costCHF, 2) . " - "; + $rows .= " + + {$meter['name']} + $type + " . date('d.m.Y H:i', $sectionStart) . " + " . date('d.m.Y H:i', $sectionEnd) . " + " . number_format($sectionDiff, 2) . " + " . number_format($tariffRp, 2) . " + " . number_format($costCHF, 2) . " + "; - $totalCost += $costCHF; - } - - return ['row' => $rows, 'value' => $totalCost]; + $totalCost += $costCHF; } + // Fallback, falls kein Tarif passt + if ($rows === '') { + $rows .= " + + {$meter['name']} + $type + " . date('d.m.Y H:i', $from) . " + " . date('d.m.Y H:i', $to) . " + " . number_format($totalDiff, 2) . " + 0 + 0.00 + "; + } + + return ['row' => $rows, 'value' => $totalCost]; +} + } \ No newline at end of file