no message

This commit is contained in:
2025-11-04 15:06:45 +01:00
parent 121269ea0d
commit a094dc925b

View File

@@ -215,114 +215,88 @@ public function GenerateInvoices()
return 0.0;
}
private function GetValueAt(int $varId, int $timestamp)
{
$archiveID = @IPS_GetInstanceListByModuleID('{43192F0B-135B-4CE7-A0A7-1475603F3060}')[0];
if (!$archiveID || !IPS_VariableExists($varId)) return null;
private function GetValueAt(int $varId, int $timestamp)
{
$archiveID = @IPS_GetInstanceListByModuleID('{43192F0B-135B-4CE7-A0A7-1475603F3060}')[0];
if (!$archiveID || !IPS_VariableExists($varId)) return null;
$values = @AC_GetLoggedValues($archiveID, $varId, $timestamp - 3600, $timestamp + 3600, 1);
if ($values && count($values) > 0) {
return floatval($values[0]['Value']);
}
return floatval(GetValue($varId));
// 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 ($valuesAfter && count($valuesAfter) > 0) {
$after = floatval($valuesAfter[0]['Value']);
} else {
$after = null;
}
// Priorität: exakter Wert >= Timestamp, sonst letzter Wert davor
if ($after !== null) return $after;
if ($before !== null) return $before;
// Fallback auf aktuellen Wert
return floatval(GetValue($varId));
}
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) {
IPS_LogMessage('Abrechnung', "❌ Kein Wert für {$meter['name']} ($type)");
return ['row' => '', 'value' => 0];
}
if ($startVal === null || $endVal === null) return ['row' => '', 'value' => 0];
$totalDiff = max(0, $endVal - $startVal);
if ($totalDiff <= 0) {
IPS_LogMessage('Abrechnung', "⚠️ Kein Verbrauch für {$meter['name']} ($type): Start=$startVal, Ende=$endVal");
return ['row' => '', 'value' => 0];
}
IPS_LogMessage('Abrechnung', "📊 {$meter['name']} ($type): Verbrauch gesamt=$totalDiff");
if ($totalDiff <= 0) return ['row' => '', 'value' => 0];
$rows = '';
$totalCost = 0.0;
$foundTariff = false;
foreach ($tariffs as $t) {
if (!isset($t['start'], $t['end'], $t['price'], $t['unit_type'])) continue;
if (strtolower($t['unit_type']) != strtolower($type)) continue;
// 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']);
if ($tariffEnd < $from || $tariffStart > $to) continue;
$tariffStart = strtotime($t['start']);
$tariffEnd = strtotime($t['end']);
$foundTariff = true;
// Zeitliche Überschneidung mit Abrechnungszeitraum?
if ($tariffEnd < $from || $tariffStart > $to) continue;
$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;
$sectionFraction = $sectionDuration / $totalDuration;
$sectionDiff = $totalDiff * $sectionFraction;
// Anteil des Gesamtverbrauchs nach Zeitanteil
$sectionFraction = $sectionDuration / $totalDuration;
$sectionDiff = $totalDiff * $sectionFraction;
$tariffRp = floatval($t['price']);
$costCHF = ($tariffRp / 100) * $sectionDiff;
$tariffRp = floatval($t['price']);
$costCHF = ($tariffRp / 100) * $sectionDiff;
IPS_LogMessage('Abrechnung', "→ Tarif {$t['price']} Rp vom " . date('d.m.Y', $tariffStart) . " bis " . date('d.m.Y', $tariffEnd) .
" deckt " . date('d.m.Y', $sectionStart) . "" . date('d.m.Y', $sectionEnd) . " ab, Anteil=" . round($sectionFraction*100,1) . "%");
$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', $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>";
$totalCost += $costCHF;
}
if (!$foundTariff) {
IPS_LogMessage('Abrechnung', "⚠️ Kein Tarif für $type im Zeitraum gefunden");
}
return ['row' => $rows, 'value' => $totalCost];
}
private function GetValueAt(int $varId, int $timestamp)
{
$archiveID = @IPS_GetInstanceListByModuleID('{43192F0B-135B-4CE7-A0A7-1475603F3060}')[0];
if (!$archiveID || !IPS_VariableExists($varId)) {
IPS_LogMessage('Abrechnung', "⚠️ Archiv oder Variable fehlt für ID $varId");
return null;
$totalCost += $costCHF;
}
// Letzter Wert vor oder gleich Timestamp
$before = @AC_GetLoggedValues($archiveID, $varId, $timestamp - 86400 * 60, $timestamp, 1);
if ($before && count($before) > 0) {
$val = floatval($before[0]['Value']);
IPS_LogMessage('Abrechnung', "📉 Wert vor $timestamp: $val");
return $val;
}
// Erster Wert nach Timestamp
$after = @AC_GetLoggedValues($archiveID, $varId, $timestamp, $timestamp + 86400 * 60, 1);
if ($after && count($after) > 0) {
$val = floatval($after[0]['Value']);
IPS_LogMessage('Abrechnung', "📈 Wert nach $timestamp: $val");
return $val;
}
// Fallback auf aktuellen Wert
$val = floatval(GetValue($varId));
IPS_LogMessage('Abrechnung', "⚙️ Fallback Wert (aktuell): $val");
return $val;
return ['row' => $rows, 'value' => $totalCost];
}
}