no message

This commit is contained in:
2025-11-04 15:18:35 +01:00
parent a094dc925b
commit 43277a9e36

View File

@@ -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 .= "
<tr>
<td>{$meter['name']}</td>
<td>$type</td>
<td>" . date('d.m.Y', $sectionStart) . "</td>
<td>" . date('d.m.Y', $sectionEnd) . "</td>
<td>" . number_format($sectionDiff, 2) . "</td>
<td>" . number_format($tariffRp, 2) . "</td>
<td>" . number_format($costCHF, 2) . "</td>
</tr>";
$rows .= "
<tr>
<td>{$meter['name']}</td>
<td>$type</td>
<td>" . date('d.m.Y H:i', $sectionStart) . "</td>
<td>" . date('d.m.Y H:i', $sectionEnd) . "</td>
<td>" . number_format($sectionDiff, 2) . "</td>
<td>" . number_format($tariffRp, 2) . "</td>
<td>" . number_format($costCHF, 2) . "</td>
</tr>";
$totalCost += $costCHF;
}
return ['row' => $rows, 'value' => $totalCost];
$totalCost += $costCHF;
}
// Fallback, falls kein Tarif passt
if ($rows === '') {
$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($totalDiff, 2) . "</td>
<td>0</td>
<td>0.00</td>
</tr>";
}
return ['row' => $rows, 'value' => $totalCost];
}
}