diff --git a/Abrechnung/module.php b/Abrechnung/module.php index bf1e829..5678267 100644 --- a/Abrechnung/module.php +++ b/Abrechnung/module.php @@ -227,28 +227,59 @@ public function GenerateInvoices() return floatval(GetValue($varId)); } - private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type) - { - $start = $this->GetValueAt($meter['var_consumption'], $from); - $end = $this->GetValueAt($meter['var_consumption'], $to); - if ($start === null || $end === null) return ['row' => '', 'value' => 0]; +private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type) +{ + $startVal = $this->GetValueAt($meter['var_consumption'], $from); + $endVal = $this->GetValueAt($meter['var_consumption'], $to); + if ($startVal === null || $endVal === null) return ['row' => '', 'value' => 0]; - $diff = max(0, $end - $start); - $tariffRp = $this->GetTariff($tariffs, $type, $from, $to); - $cost = ($tariffRp / 100) * $diff; + $totalDiff = max(0, $endVal - $startVal); + if ($totalDiff <= 0) return ['row' => '', 'value' => 0]; - $row = " - - {$meter['name']} - $type - $start - $end - $diff - $tariffRp - " . number_format($cost, 2) . " - "; + $rows = ''; + $totalCost = 0.0; - return ['row' => $row, 'value' => $cost]; + // Alle Tarife dieses Typs prüfen + foreach ($tariffs as $t) { + if (strtolower($t['unit_type']) != strtolower($type)) continue; + + $tariffStart = strtotime($t['start']); + $tariffEnd = strtotime($t['end']); + + // Zeitliche Überschneidung mit Abrechnungszeitraum? + if ($tariffEnd < $from || $tariffStart > $to) continue; + + // Überschneidung bestimmen + $sectionStart = max($from, $tariffStart); + $sectionEnd = min($to, $tariffEnd); + $sectionDuration = $sectionEnd - $sectionStart; + $totalDuration = $to - $from; + + if ($sectionDuration <= 0) continue; + + // Anteil des Gesamtverbrauchs nach Zeitanteil + $sectionFraction = $sectionDuration / $totalDuration; + $sectionDiff = $totalDiff * $sectionFraction; + + $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) . " + "; + + $totalCost += $costCHF; + } + + return ['row' => $rows, 'value' => $totalCost]; } + } \ No newline at end of file