117 lines
4.2 KiB
JavaScript
117 lines
4.2 KiB
JavaScript
'use strict';
|
||
|
||
const _assets = JSON.parse(document.getElementById('modal-anlagevermoegen')?.dataset.assets || '[]');
|
||
|
||
// Formatierung Datum de-DE
|
||
function _fmtDate(str) {
|
||
if (!str) return '–';
|
||
const [y, m, d] = str.split('-');
|
||
return d + '.' + m + '.' + y;
|
||
}
|
||
function _fmtEur(n) {
|
||
return n.toLocaleString('de-DE', {minimumFractionDigits:2, maximumFractionDigits:2}) + ' €';
|
||
}
|
||
|
||
// Wird aufgerufen wenn Kategorie-Konto geändert wird
|
||
function checkAnlagevermoegen() {
|
||
const sel = document.getElementById('kategorie_konto_id');
|
||
if (!sel) return;
|
||
const opt = sel.options[sel.selectedIndex];
|
||
if (!opt || opt.value === '') return;
|
||
|
||
const art = opt.getAttribute('data-art') || '';
|
||
if (art !== 'Anlagevermögen') {
|
||
// Kein AV-Konto – Asset-Auswahl zurücksetzen
|
||
document.getElementById('asset_id_hidden').value = '';
|
||
return;
|
||
}
|
||
|
||
// AV-Konto gewählt → Modal öffnen
|
||
const accountId = parseInt(opt.value, 10);
|
||
_openAnlageModal(accountId);
|
||
}
|
||
|
||
function _openAnlageModal(accountId) {
|
||
// Radio auf "neu" zurücksetzen
|
||
document.getElementById('anlage_radio_neu').checked = true;
|
||
document.getElementById('anlage_liste_wrap').style.display = 'none';
|
||
document.getElementById('asset_id_hidden').value = '';
|
||
|
||
// Liste für dieses Konto befüllen
|
||
const passend = _assets.filter(a => a.account_id === accountId);
|
||
_renderAnlageListe(passend);
|
||
|
||
document.getElementById('modal-anlagevermoegen').classList.add('open');
|
||
}
|
||
|
||
function _renderAnlageListe(list) {
|
||
const tbody = document.getElementById('anlage_liste_tbody');
|
||
const leer = document.getElementById('anlage_liste_leer');
|
||
const table = document.getElementById('anlage_liste_table_wrap');
|
||
|
||
tbody.innerHTML = '';
|
||
|
||
if (list.length === 0) {
|
||
leer.style.display = '';
|
||
table.style.display = 'none';
|
||
return;
|
||
}
|
||
leer.style.display = 'none';
|
||
table.style.display = '';
|
||
|
||
list.forEach(a => {
|
||
const tr = document.createElement('tr');
|
||
tr.style.cursor = 'pointer';
|
||
tr.dataset.assetId = a.id;
|
||
tr.innerHTML =
|
||
'<td class="td-mono text-sm">' + a.lfd_nr + '</td>' +
|
||
'<td>' + _esc(a.bezeichnung) + '</td>' +
|
||
'<td class="td-muted text-sm">' + _fmtDate(a.anschaffungsdatum) + '</td>' +
|
||
'<td class="td-num">' + _fmtEur(a.anschaffungskosten) + '</td>' +
|
||
'<td><input type="radio" name="anlage_auswahl" value="' + a.id + '"></td>';
|
||
tbody.appendChild(tr);
|
||
|
||
// Klick auf Zeile wählt Radio
|
||
tr.addEventListener('click', () => {
|
||
const radio = tr.querySelector('input[type="radio"]');
|
||
if (radio) radio.checked = true;
|
||
tbody.querySelectorAll('tr').forEach(r => r.classList.remove('row-selected'));
|
||
tr.classList.add('row-selected');
|
||
});
|
||
});
|
||
}
|
||
|
||
function _esc(s) {
|
||
return String(s).replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
|
||
}
|
||
|
||
// Radio-Wahl: neu / vorhanden
|
||
document.addEventListener('change', e => {
|
||
if (e.target.name !== 'anlage_wahl') return;
|
||
const wrap = document.getElementById('anlage_liste_wrap');
|
||
wrap.style.display = e.target.value === 'vorhanden' ? '' : 'none';
|
||
if (e.target.value === 'neu') {
|
||
document.getElementById('asset_id_hidden').value = '';
|
||
document.querySelectorAll('input[name="anlage_auswahl"]').forEach(r => r.checked = false);
|
||
document.querySelectorAll('#anlage_liste_tbody tr').forEach(r => r.classList.remove('row-selected'));
|
||
}
|
||
});
|
||
|
||
// OK-Button
|
||
document.getElementById('anlage_modal_ok').addEventListener('click', () => {
|
||
const wahl = document.querySelector('input[name="anlage_wahl"]:checked')?.value;
|
||
|
||
if (wahl === 'vorhanden') {
|
||
const gewählt = document.querySelector('input[name="anlage_auswahl"]:checked');
|
||
if (!gewählt) {
|
||
alert('Bitte ein Anlagegut aus der Liste auswählen.');
|
||
return;
|
||
}
|
||
document.getElementById('asset_id_hidden').value = gewählt.value;
|
||
} else {
|
||
// "neu" – asset_id bleibt leer, Backend legt neues Gut an
|
||
document.getElementById('asset_id_hidden').value = '';
|
||
}
|
||
|
||
document.getElementById('modal-anlagevermoegen').classList.remove('open');
|
||
});
|