no message

This commit is contained in:
2026-06-28 12:30:01 +02:00
parent 4f1026f39c
commit d478aeca38
+73 -15
View File
@@ -91,11 +91,50 @@ class Abrechnung extends IPSModule
{ {
parent::ApplyChanges(); parent::ApplyChanges();
$this->RegisterTariffVariableProfiles(); $this->RegisterTariffVariableProfiles();
$this->RegisterTariffWebhook();
$this->EnsureVisuTariffObjects(); $this->EnsureVisuTariffObjects();
$this->InitializeVisuTariffDefaults(); $this->InitializeVisuTariffDefaults();
$this->UpdateTariffOverview(); $this->UpdateTariffOverview();
} }
private function RegisterTariffWebhook()
{
$webhook = $this->GetTariffWebhookPath();
$ids = @IPS_GetInstanceListByModuleID('{015A6EB8-D6E5-4B93-B496-0D3F77AE9FE1}');
if (empty($ids)) {
return;
}
$hookID = $ids[0];
$hooks = json_decode(IPS_GetProperty($hookID, 'Hooks'), true);
$hooks = is_array($hooks) ? $hooks : [];
$found = false;
foreach ($hooks as $index => $hook) {
if (($hook['Hook'] ?? '') !== $webhook) {
continue;
}
$hooks[$index]['TargetID'] = $this->InstanceID;
$found = true;
break;
}
if (!$found) {
$hooks[] = [
'Hook' => $webhook,
'TargetID' => $this->InstanceID
];
}
IPS_SetProperty($hookID, 'Hooks', json_encode($hooks));
IPS_ApplyChanges($hookID);
}
private function GetTariffWebhookPath()
{
return '/hook/belevo_abrechnung_tariffs_' . $this->InstanceID;
}
private function EnsureVisuTariffObjects() private function EnsureVisuTariffObjects()
{ {
$this->RegisterVariableString('VisuTariffOverview', 'Tarife bearbeiten', '~HTMLBox', 10); $this->RegisterVariableString('VisuTariffOverview', 'Tarife bearbeiten', '~HTMLBox', 10);
@@ -347,12 +386,11 @@ class Abrechnung extends IPSModule
$typeOptions .= '<option value="' . $this->h($type) . '">' . $this->h($type) . '</option>'; $typeOptions .= '<option value="' . $this->h($type) . '">' . $this->h($type) . '</option>';
} }
$instanceID = (int)$this->InstanceID; $webhookPath = $this->GetTariffWebhookPath();
$payloadID = (int)$this->GetIDForIdent('VisuTariffPayload');
$status = $this->h(@GetValue($this->GetIDForIdent('VisuTariffStatus'))); $status = $this->h(@GetValue($this->GetIDForIdent('VisuTariffStatus')));
$html = " $html = "
<div class='belevo-tariffs' data-instance='" . $instanceID . "'> <div class='belevo-tariffs'>
<style> <style>
.belevo-tariffs{font-family:Arial,Helvetica,sans-serif;font-size:13px;color:#202124;max-width:980px} .belevo-tariffs{font-family:Arial,Helvetica,sans-serif;font-size:13px;color:#202124;max-width:980px}
.belevo-tariffs table{width:100%;border-collapse:collapse;background:#fff} .belevo-tariffs table{width:100%;border-collapse:collapse;background:#fff}
@@ -366,6 +404,8 @@ class Abrechnung extends IPSModule
.belevo-tariffs button.danger{color:#9b1c1c;border-color:#d7a6a6;background:#fff5f5} .belevo-tariffs button.danger{color:#9b1c1c;border-color:#d7a6a6;background:#fff5f5}
.belevo-tariffs .status{margin-left:8px;color:#4b5563} .belevo-tariffs .status{margin-left:8px;color:#4b5563}
</style> </style>
<form method='post' action='" . $this->h($webhookPath) . "' target='belevo_tariff_save_frame'>
<input type='hidden' name='payload' value=''>
<table> <table>
<thead> <thead>
<tr> <tr>
@@ -380,17 +420,25 @@ class Abrechnung extends IPSModule
</table> </table>
<div class='actions'> <div class='actions'>
<button type='button' data-action='add'>Zeile hinzufügen</button> <button type='button' data-action='add'>Zeile hinzufügen</button>
<button type='button' data-action='save' class='primary'>Tarife speichern</button> <button type='submit' data-action='save' class='primary'>Tarife speichern</button>
<span class='status'>" . $status . "</span> <span class='status'>" . $status . "</span>
</div> </div>
</form>
<iframe name='belevo_tariff_save_frame' style='display:none;width:0;height:0;border:0'></iframe>
<template data-row-template>" . $this->BuildTariffEditorRow('Netztarif', date('Y-01-01'), date('Y-12-31'), '0.000') . "</template> <template data-row-template>" . $this->BuildTariffEditorRow('Netztarif', date('Y-01-01'), date('Y-12-31'), '0.000') . "</template>
<script> <script>
(function(){ (function(){
var root=document.currentScript.closest('.belevo-tariffs'); var root=document.currentScript.closest('.belevo-tariffs');
if(!root||root.dataset.bound==='1'){return;} if(!root||root.dataset.bound==='1'){return;}
root.dataset.bound='1'; root.dataset.bound='1';
var form=root.querySelector('form');
var tbody=root.querySelector('tbody'); var tbody=root.querySelector('tbody');
var status=root.querySelector('.status'); var status=root.querySelector('.status');
window.addEventListener('message',function(event){
if(event&&event.data&&event.data.type==='belevoTariffSave'){
status.textContent=event.data.message||'Gespeichert.';
}
});
function bindDelete(row){ function bindDelete(row){
var btn=row.querySelector('[data-action=delete]'); var btn=row.querySelector('[data-action=delete]');
if(btn){btn.onclick=function(){row.parentNode.removeChild(row);};} if(btn){btn.onclick=function(){row.parentNode.removeChild(row);};}
@@ -404,7 +452,7 @@ root.querySelector('[data-action=add]').onclick=function(){
tbody.appendChild(row); tbody.appendChild(row);
bindDelete(row); bindDelete(row);
}; };
root.querySelector('[data-action=save]').onclick=function(){ form.onsubmit=function(){
var rows=[]; var rows=[];
Array.prototype.forEach.call(tbody.querySelectorAll('tr'),function(row){ Array.prototype.forEach.call(tbody.querySelectorAll('tr'),function(row){
var type=row.querySelector('[data-field=unit_type]').value; var type=row.querySelector('[data-field=unit_type]').value;
@@ -415,17 +463,10 @@ root.querySelector('[data-action=save]').onclick=function(){
rows.push({unit_type:type,start:start,end:end,price:price}); rows.push({unit_type:type,start:start,end:end,price:price});
} }
}); });
var payload=JSON.stringify(rows); form.querySelector('input[name=payload]').value=JSON.stringify(rows);
status.textContent='Speichern...'; status.textContent='Speichern...';
if(typeof requestAction==='function'){ window.setTimeout(function(){status.textContent='Gesendet. Tabelle wird nach dem Speichern aktualisiert.';},800);
requestAction(" . $payloadID . ",payload); return true;
status.textContent='Speichern gesendet.';
}else if(window.parent&&typeof window.parent.requestAction==='function'){
window.parent.requestAction(" . $payloadID . ",payload);
status.textContent='Speichern gesendet.';
}else{
status.textContent='Fehler: requestAction nicht verfügbar.';
}
}; };
})(); })();
</script> </script>
@@ -599,6 +640,23 @@ root.querySelector('[data-action=save]').onclick=function(){
} }
} }
public function ProcessHookData()
{
$message = '';
try {
$payload = $_POST['payload'] ?? '';
$message = $this->SaveTariffTableFromPayload($payload);
SetValue($this->GetIDForIdent('VisuTariffStatus'), $message);
$this->UpdateTariffOverview();
} catch (Throwable $e) {
$message = 'Fehler beim Speichern der Tarifliste: ' . $e->getMessage();
SetValue($this->GetIDForIdent('VisuTariffStatus'), $message);
}
header('Content-Type: text/html; charset=utf-8');
echo '<!doctype html><html><body><script>try{parent.postMessage({type:"belevoTariffSave",message:' . json_encode($message) . '}, "*");}catch(e){}</script>' . $this->h($message) . '</body></html>';
}
// ====================== PDF-Erstellung ====================== // ====================== PDF-Erstellung ======================
public function GenerateInvoices() public function GenerateInvoices()