diff --git a/Abrechnung/module.php b/Abrechnung/module.php index 591341e..f015a5d 100644 --- a/Abrechnung/module.php +++ b/Abrechnung/module.php @@ -135,7 +135,7 @@ public function GenerateInvoices() $html = "

Rechnung für {$user['name']}

{$user['address']}
{$user['city']}

- +
@@ -241,71 +241,62 @@ private function GetValueAt(int $varId, int $timestamp, bool $nearestAfter = fal private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type) { - $startValTotal = $this->GetValueAt($meter['var_consumption'], $from, false); // letzter Wert ≤ Start - $endValTotal = $this->GetValueAt($meter['var_consumption'], $to, true); // erster Wert ≥ Ende - - 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 = ''; $totalCost = 0.0; - foreach ($tariffs as $t) { - if (strtolower($t['unit_type']) != strtolower($type)) continue; + $currentTime = $from; + $startValTotal = $this->GetValueAt($meter['var_consumption'], $from, false); - $tariffStart = strtotime($t['start']); - $tariffEnd = strtotime($t['end']); + IPS_LogMessage('Abrechnung', "Starte Abrechnung für Meter {$meter['name']} ($type) von " . date('d.m.Y H:i', $from) . " bis " . date('d.m.Y H:i', $to)); - // Überschneidung mit Abrechnungszeitraum - $sectionStart = max($from, $tariffStart); - $sectionEnd = min($to, $tariffEnd); - if ($sectionEnd <= $sectionStart) continue; + while ($currentTime < $to) { + // 1. Tarif zum aktuellen Zeitpunkt ermitteln + $activeTariff = null; + foreach ($tariffs as $t) { + if (strtolower($t['unit_type']) != strtolower($type)) continue; + $tStart = strtotime($t['start']); + $tEnd = strtotime($t['end']); + if ($currentTime >= $tStart && $currentTime < $tEnd) { + $activeTariff = $t; + break; + } + } - // Verbrauch anteilig - $sectionFraction = ($sectionEnd - $sectionStart) / ($to - $from); - $sectionDiff = $totalDiff * $sectionFraction; + if ($activeTariff === null) { + IPS_LogMessage('Abrechnung', "⚠ Kein aktiver Tarif gefunden für $type am " . date('d.m.Y H:i', $currentTime)); + break; // Kein Tarif → Stop + } - // Start-/Endwert für diese Tarifzeit berechnen - $sectionStartVal = $startValTotal + $totalDiff * (($sectionStart - $from) / ($to - $from)); - $sectionEndVal = $sectionStartVal + $sectionDiff; + // 2. Zeitraum für diesen Tarif + $periodStart = $currentTime; + $periodEnd = min($to, strtotime($activeTariff['end'])); - $tariffRp = floatval($t['price']); - $costCHF = ($tariffRp / 100) * $sectionDiff; + // 3. Zählerstände für diesen Zeitraum ermitteln + $startVal = ($periodStart == $from) ? $startValTotal : $this->GetValueAt($meter['var_consumption'], $periodStart, false); + $endVal = $this->GetValueAt($meter['var_consumption'], $periodEnd, true); + $diff = max(0, $endVal - $startVal); + $tariffRp = floatval($activeTariff['price']); + $costCHF = ($tariffRp / 100) * $diff; + + // 4. Zeile für PDF $rows .= " - - - - - + + + + + "; $totalCost += $costCHF; - } - // Fallback, falls kein Tarif passt - if ($rows === '') { - $rows .= " - - - - - - - - - - - "; + // 5. Zum nächsten Tarif springen + $currentTime = $periodEnd; } return ['row' => $rows, 'value' => $totalCost]; @@ -313,4 +304,5 @@ private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type) + } \ No newline at end of file
Zähler Typ
{$meter['name']} $type" . date('d.m.Y H:i', $sectionStart) . "" . date('d.m.Y H:i', $sectionEnd) . "" . number_format($sectionStartVal, 2) . "" . number_format($sectionEndVal, 2) . "" . number_format($sectionDiff, 2) . "" . date('d.m.Y H:i', $periodStart) . "" . date('d.m.Y H:i', $periodEnd) . "" . number_format($startVal, 2) . "" . number_format($endVal, 2) . "" . number_format($diff, 2) . " " . number_format($tariffRp, 2) . " " . number_format($costCHF, 2) . "
{$meter['name']}$type" . date('d.m.Y H:i', $from) . "" . date('d.m.Y H:i', $to) . "" . number_format($startValTotal, 2) . "" . number_format($endValTotal, 2) . "" . number_format($totalDiff, 2) . "00.00