no message
This commit is contained in:
+62
-48
@@ -135,7 +135,7 @@ public function GenerateInvoices()
|
|||||||
$html = "
|
$html = "
|
||||||
<h2>Rechnung für {$user['name']}</h2>
|
<h2>Rechnung für {$user['name']}</h2>
|
||||||
<p>{$user['address']}<br>{$user['city']}</p>
|
<p>{$user['address']}<br>{$user['city']}</p>
|
||||||
<table border='1' cellpadding='3' style='font-size:8px;'>
|
<table border='1' cellpadding='5'>
|
||||||
<tr style='background-color:#e0e0e0;'>
|
<tr style='background-color:#e0e0e0;'>
|
||||||
<th>Zähler</th>
|
<th>Zähler</th>
|
||||||
<th>Typ</th>
|
<th>Typ</th>
|
||||||
@@ -215,7 +215,7 @@ public function GenerateInvoices()
|
|||||||
return 0.0;
|
return 0.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function GetValueAt(int $varId, int $timestamp, bool $nearestAfter = false)
|
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)) {
|
if (!$archiveID || !IPS_VariableExists($varId)) {
|
||||||
@@ -223,14 +223,22 @@ private function GetValueAt(int $varId, int $timestamp, bool $nearestAfter = fal
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($nearestAfter) {
|
// Letzter Wert <= Timestamp
|
||||||
// Endwert: erster Wert >= Timestamp
|
$before = @AC_GetLoggedValues($archiveID, $varId, 0, $timestamp, 1);
|
||||||
$values = @AC_GetLoggedValues($archiveID, $varId, $timestamp, $timestamp + 86400, 1);
|
$beforeVal = ($before && count($before) > 0) ? floatval($before[count($before)-1]['Value']) : null;
|
||||||
if ($values && count($values) > 0) return floatval($values[0]['Value']);
|
|
||||||
} else {
|
// Erster Wert >= Timestamp
|
||||||
// Startwert: letzter Wert <= Timestamp
|
$after = @AC_GetLoggedValues($archiveID, $varId, $timestamp, time(), 1);
|
||||||
$values = @AC_GetLoggedValues($archiveID, $varId, 0, $timestamp, 1);
|
$afterVal = ($after && count($after) > 0) ? floatval($after[0]['Value']) : null;
|
||||||
if ($values && count($values) > 0) return floatval($values[count($values)-1]['Value']);
|
|
||||||
|
// 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
|
||||||
@@ -241,68 +249,74 @@ private function GetValueAt(int $varId, int $timestamp, bool $nearestAfter = fal
|
|||||||
|
|
||||||
private function AddMeterToPDFRow($meter, $tariffs, $from, $to, $type)
|
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);
|
||||||
|
if ($totalDiff <= 0) return ['row' => '', 'value' => 0];
|
||||||
|
|
||||||
$rows = '';
|
$rows = '';
|
||||||
$totalCost = 0.0;
|
$totalCost = 0.0;
|
||||||
|
|
||||||
$currentTime = $from;
|
// Alle Tarife dieses Typs prüfen
|
||||||
$startValTotal = $this->GetValueAt($meter['var_consumption'], $from, false);
|
foreach ($tariffs as $t) {
|
||||||
|
if (strtolower($t['unit_type']) != strtolower($type)) continue;
|
||||||
|
|
||||||
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));
|
$tariffStart = strtotime($t['start']);
|
||||||
|
$tariffEnd = strtotime($t['end']);
|
||||||
|
|
||||||
while ($currentTime < $to) {
|
// Überschneidung mit Abrechnungszeitraum prüfen
|
||||||
// 1. Tarif zum aktuellen Zeitpunkt ermitteln
|
if ($tariffEnd < $from || $tariffStart > $to) continue;
|
||||||
$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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($activeTariff === null) {
|
// Überschneidung bestimmen
|
||||||
IPS_LogMessage('Abrechnung', "⚠ Kein aktiver Tarif gefunden für $type am " . date('d.m.Y H:i', $currentTime));
|
$sectionStart = max($from, $tariffStart);
|
||||||
break; // Kein Tarif → Stop
|
$sectionEnd = min($to, $tariffEnd);
|
||||||
}
|
$sectionDuration = $sectionEnd - $sectionStart;
|
||||||
|
$totalDuration = $to - $from;
|
||||||
|
|
||||||
// 2. Zeitraum für diesen Tarif
|
if ($sectionDuration <= 0) continue;
|
||||||
$periodStart = $currentTime;
|
|
||||||
$periodEnd = min($to, strtotime($activeTariff['end']));
|
|
||||||
|
|
||||||
// 3. Zählerstände für diesen Zeitraum ermitteln
|
// Verbrauch anteilig nach Dauer
|
||||||
$startVal = ($periodStart == $from) ? $startValTotal : $this->GetValueAt($meter['var_consumption'], $periodStart, false);
|
$sectionFraction = $sectionDuration / $totalDuration;
|
||||||
$endVal = $this->GetValueAt($meter['var_consumption'], $periodEnd, true);
|
$sectionDiff = $totalDiff * $sectionFraction;
|
||||||
|
|
||||||
$diff = max(0, $endVal - $startVal);
|
$tariffRp = floatval($t['price']);
|
||||||
$tariffRp = floatval($activeTariff['price']);
|
$costCHF = ($tariffRp / 100) * $sectionDiff;
|
||||||
$costCHF = ($tariffRp / 100) * $diff;
|
|
||||||
|
|
||||||
// 4. Zeile für PDF
|
|
||||||
$rows .= "
|
$rows .= "
|
||||||
<tr>
|
<tr>
|
||||||
<td>{$meter['name']}</td>
|
<td>{$meter['name']}</td>
|
||||||
<td>$type</td>
|
<td>$type</td>
|
||||||
<td>" . date('d.m.Y H:i', $periodStart) . "</td>
|
<td>" . date('d.m.Y H:i', $sectionStart) . "</td>
|
||||||
<td>" . date('d.m.Y H:i', $periodEnd) . "</td>
|
<td>" . date('d.m.Y H:i', $sectionEnd) . "</td>
|
||||||
<td>" . number_format($startVal, 2) . "</td>
|
<td>" . number_format($sectionDiff, 2) . "</td>
|
||||||
<td>" . number_format($endVal, 2) . "</td>
|
|
||||||
<td>" . number_format($diff, 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;
|
||||||
|
}
|
||||||
|
|
||||||
// 5. Zum nächsten Tarif springen
|
// Fallback, falls kein Tarif passt
|
||||||
$currentTime = $periodEnd;
|
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];
|
return ['row' => $rows, 'value' => $totalCost];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user