no message
This commit is contained in:
@@ -218,36 +218,42 @@ public function GenerateInvoices()
|
|||||||
private function GetValueAt(int $varId, int $timestamp)
|
private function GetValueAt(int $varId, int $timestamp)
|
||||||
{
|
{
|
||||||
$archiveID = @IPS_GetInstanceListByModuleID('{43192F0B-135B-4CE7-A0A7-1475603F3060}')[0];
|
$archiveID = @IPS_GetInstanceListByModuleID('{43192F0B-135B-4CE7-A0A7-1475603F3060}')[0];
|
||||||
if (!$archiveID || !IPS_VariableExists($varId)) return null;
|
if (!$archiveID || !IPS_VariableExists($varId)) {
|
||||||
|
IPS_LogMessage('Abrechnung', "❌ Variable $varId oder Archiv $archiveID nicht gefunden");
|
||||||
// 1. Werte **vor und nach** dem Timestamp holen
|
return null;
|
||||||
$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) {
|
// Letzter Wert <= Timestamp
|
||||||
$after = floatval($valuesAfter[0]['Value']);
|
$before = @AC_GetLoggedValues($archiveID, $varId, 0, $timestamp, 1);
|
||||||
} else {
|
$beforeVal = ($before && count($before) > 0) ? floatval($before[count($before)-1]['Value']) : null;
|
||||||
$after = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Priorität: exakter Wert >= Timestamp, sonst letzter Wert davor
|
// Erster Wert >= Timestamp
|
||||||
if ($after !== null) return $after;
|
$after = @AC_GetLoggedValues($archiveID, $varId, $timestamp, time(), 1);
|
||||||
if ($before !== null) return $before;
|
$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
|
// 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)
|
private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type)
|
||||||
{
|
{
|
||||||
$startVal = $this->GetValueAt($meter['var_consumption'], $from);
|
$startVal = $this->GetValueAt($meter['var_consumption'], $from);
|
||||||
$endVal = $this->GetValueAt($meter['var_consumption'], $to);
|
$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];
|
if ($startVal === null || $endVal === null) return ['row' => '', 'value' => 0];
|
||||||
|
|
||||||
$totalDiff = max(0, $endVal - $startVal);
|
$totalDiff = max(0, $endVal - $startVal);
|
||||||
@@ -257,46 +263,60 @@ private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type)
|
|||||||
$totalCost = 0.0;
|
$totalCost = 0.0;
|
||||||
|
|
||||||
// Alle Tarife dieses Typs prüfen
|
// Alle Tarife dieses Typs prüfen
|
||||||
foreach ($tariffs as $t) {
|
foreach ($tariffs as $t) {
|
||||||
if (strtolower($t['unit_type']) != strtolower($type)) continue;
|
if (strtolower($t['unit_type']) != strtolower($type)) continue;
|
||||||
|
|
||||||
$tariffStart = strtotime($t['start']);
|
$tariffStart = strtotime($t['start']);
|
||||||
$tariffEnd = strtotime($t['end']);
|
$tariffEnd = strtotime($t['end']);
|
||||||
|
|
||||||
// Zeitliche Überschneidung mit Abrechnungszeitraum?
|
// Überschneidung mit Abrechnungszeitraum prüfen
|
||||||
if ($tariffEnd < $from || $tariffStart > $to) continue;
|
if ($tariffEnd < $from || $tariffStart > $to) continue;
|
||||||
|
|
||||||
// Überschneidung bestimmen
|
// Überschneidung bestimmen
|
||||||
$sectionStart = max($from, $tariffStart);
|
$sectionStart = max($from, $tariffStart);
|
||||||
$sectionEnd = min($to, $tariffEnd);
|
$sectionEnd = min($to, $tariffEnd);
|
||||||
$sectionDuration = $sectionEnd - $sectionStart;
|
$sectionDuration = $sectionEnd - $sectionStart;
|
||||||
$totalDuration = $to - $from;
|
$totalDuration = $to - $from;
|
||||||
|
|
||||||
if ($sectionDuration <= 0) continue;
|
if ($sectionDuration <= 0) continue;
|
||||||
|
|
||||||
// Anteil des Gesamtverbrauchs nach Zeitanteil
|
// Verbrauch anteilig nach Dauer
|
||||||
$sectionFraction = $sectionDuration / $totalDuration;
|
$sectionFraction = $sectionDuration / $totalDuration;
|
||||||
$sectionDiff = $totalDiff * $sectionFraction;
|
$sectionDiff = $totalDiff * $sectionFraction;
|
||||||
|
|
||||||
$tariffRp = floatval($t['price']);
|
$tariffRp = floatval($t['price']);
|
||||||
$costCHF = ($tariffRp / 100) * $sectionDiff;
|
$costCHF = ($tariffRp / 100) * $sectionDiff;
|
||||||
|
|
||||||
$rows .= "
|
$rows .= "
|
||||||
<tr>
|
<tr>
|
||||||
<td>{$meter['name']}</td>
|
<td>{$meter['name']}</td>
|
||||||
<td>$type</td>
|
<td>$type</td>
|
||||||
<td>" . date('d.m.Y', $sectionStart) . "</td>
|
<td>" . date('d.m.Y H:i', $sectionStart) . "</td>
|
||||||
<td>" . date('d.m.Y', $sectionEnd) . "</td>
|
<td>" . date('d.m.Y H:i', $sectionEnd) . "</td>
|
||||||
<td>" . number_format($sectionDiff, 2) . "</td>
|
<td>" . number_format($sectionDiff, 2) . "</td>
|
||||||
<td>" . number_format($tariffRp, 2) . "</td>
|
<td>" . number_format($tariffRp, 2) . "</td>
|
||||||
<td>" . number_format($costCHF, 2) . "</td>
|
<td>" . number_format($costCHF, 2) . "</td>
|
||||||
</tr>";
|
</tr>";
|
||||||
|
|
||||||
$totalCost += $costCHF;
|
$totalCost += $costCHF;
|
||||||
}
|
|
||||||
|
|
||||||
return ['row' => $rows, 'value' => $totalCost];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user