Files
finanzen/public/js/billing.js
Markus 995e2ec111 redesign for mulituser
change validation concept (client and server)
using entities
2026-10-06 08:55:35 +02:00

296 lines
10 KiB
JavaScript

document.addEventListener("DOMContentLoaded", function(event) {
document.myModal = document.getElementById('mymodal');
document.myModalDelete = document.getElementById('mymodalDelete');
document.myModalDelete.addEventListener('show.bs.modal', function (event) {
var button = event.relatedTarget;
var id = button.getAttribute('data-bs-id');
var tab = button.getAttribute('data-bs-table');
document.myModalDelete.setAttribute('data-bs-id',id);
document.myModalDelete.setAttribute('data-bs-table',tab);
});
document.myModal.addEventListener('show.bs.modal', function (event) {
var button = event.relatedTarget;
var id = button.getAttribute('data-bs-id');
var tab = button.getAttribute('data-bs-table');
document.myModal.setAttribute('data-bs-id',id);
document.myModal.setAttribute('data-bs-table',tab);
});
document.getElementById('modal_delete').addEventListener('click', deleteEntry );
var event = new Event('change');
switchMulti();
// dropdown elemente anpassen wenn parent sich ändert
document.querySelectorAll(".parent").forEach((papa) => {
papa.addEventListener("change", hdlDropDownChange);
});
document.querySelectorAll(".dt-amount.input,.dt-amount.output , .dt-subamount").forEach((amount) => {
amount.addEventListener("change", hdlAmountChange);
amount.dispatchEvent(event);
});
document.querySelectorAll(".dt-amount.input,.dt-amount.output,.dt-subamount, .parent").forEach((amount) => {
amount.addEventListener("focus", hdlValidityStateLine);
});
const form = document.getElementById('newbill');
form.addEventListener('formdata', function(event) {
const formData = event.formData;
formData.set('validate', formData.has('validate') ? '1' : '0');
// console.log(formData);
});
});
function loadPage(ev){
let url = ev.relatedTarget.getAttribute('url');
window.location.href="url";
}
function switchMulti(){
//const multi = document.getElementById("multi");
if (document.getElementById("linecont").childElementCount>1) {
document.getElementById("multiadd").text = "+Zeile";
const singles = document.querySelectorAll(".single").forEach((single) => {
single.classList.add("d-none");
});
const multis = document.querySelectorAll(".multi").forEach((multi) => {
multi.classList.remove("d-none");
});
// addLine();
} else {
document.getElementById("multiadd").text = "Multi";
const singles = document.querySelectorAll(".single").forEach((single) => {
single.classList.remove("d-none");
});
const multis = document.querySelectorAll(".multi").forEach((ele) => {
ele.classList.add("d-none");
});
}
}
function recalcRow(einnahme, ausgabe){
let value = einnahme.value - ausgabe.value;
if (value < 0) {
ausgabe.value = convertOutput(value *(-1));
einnahme.value = '';
} else {
einnahme.value = convertOutput(value);
ausgabe.value = '';
}
}
function hdlAmountChange(event) {
var temp = convertInput(event.target.value);
event.target.value = convertOutput(temp);
let zeile = event.target.parentElement.parentElement;
var einnahme = zeile.querySelector(".input");
var ausgabe = zeile.querySelector(".output");
if (event.target.classList.contains("input")) {
if ( ausgabe.value !== '')
recalcRow(einnahme,ausgabe);
}else {
if ( einnahme.value !== '')
recalcRow(einnahme, ausgabe);
}
calcAmount();
if (document.getElementById("linecont").childElementCount>1) {
recalcOpenAmount();
}
}
function hdlDropDownChange(event) {
var $sub = event.target.parentElement.parentElement.querySelector(".sub");
while ($sub.options.length) {
$sub.remove(0);
}
var output = '';
var liste = subsdata[(event.target.value=="")?0:event.target.value];
var sort = Object.entries(liste).sort((a, b) => (a[1] > b[1]) ? 1 : (b[1] > a[1]) ? -1 : 0);
sort.forEach((value) => {
output += '<option value="' + value[0] + '">' + value[1] + '</option>';
});
$sub.innerHTML = output;
}
function hdlValidityState(ev){
form = document.getElementById("newbill");
this.classList.remove("is-invalid");
this.setCustomValidity("");
form.classList.remove('was-validated');
}
function hdlValidityStateLine(ev){
let form = document.getElementById("newbill");
if (form.classList.contains('was-validated')) {
//ev.target.parentNode.parentNode
//document.getElementById("linecont").
form.querySelectorAll(".dt-amount, .dt-subamount, .parent, .sub").forEach((amount) => {
amount.setCustomValidity("");
amount.classList.remove("is-invalid");
amount.classList.remove("is-valid");
});
form.classList.remove('was-validated');
}
}
function logData(event) {
var data = document.getElementById("billfile");
console.log(data.value);
}
function addLine() {
//let ele =document.querySelector("#linecont > div:last-child");
var clonedDiv = document.getElementById("template").cloneNode(true);
clonedDiv.removeAttribute("id");
clonedDiv.classList.remove("d-none");
clonedDiv.querySelector(".parent").addEventListener("change", hdlDropDownChange);
clonedDiv.querySelectorAll(".dt-subamount").forEach((amount) => {
amount.addEventListener("change", hdlAmountChange);
});
clonedDiv.querySelectorAll(".dt-subamount, .parent").forEach((amount) => {
amount.addEventListener("focus", hdlValidityStateLine);
});
document.getElementById("linecont").appendChild(clonedDiv);
if (document.getElementById("linecont").childElementCount==2){
switchMulti();
}
}
function removeBillFile(e) {
e.classList.add("d-none");
document.getElementById("billfile").classList.remove("d-none");
const billFile = document.getElementById("billfileRef");
const form = document.getElementById('newbill');
var DOB = document.createElement("input");
DOB.setAttribute("name", "removeBillFile");
DOB.setAttribute("value", billFile.innerText);
DOB.setAttribute("type", "hidden");
form.appendChild(DOB);
billFile.innerText = "REMOVED FILE: "+billFile.innerText;
billFile.classList.add("text-decoration-line-through");
billFile.classList.add("text-danger");
billFile.removeAttribute("href");
}
function convertInput(number) {
var zahl = number.replace(/\,/g, ".");
var czahl = eval(zahl);
return czahl;
}
function convertOutput(number) {
if (typeof number !== 'undefined') {
var string = number.toFixed(2);
return string.replace(/\,/g, ".");
}
else
return "";
}
function calcAmount() {
//let amount = convertInput(document.getElementsByName("amount")[0].value);
let amount = 0.0;
document.querySelectorAll(".dt-subamount").forEach((subamount) => {
var b = convertInput(subamount.value);
if (typeof b !== 'undefined') {
b = subamount.classList.contains("output") ? b * (-1) : b;
amount = amount + b;
}
});
document.getElementsByName("amount")[0].value = convertOutput(amount);
}
function recalcOpenAmount() {
form = document.getElementById('newbill');
let summe = convertInput(document.getElementsByName("amount")[0].value);
console.log("Re calculating open amount");
var total_in = convertInput(document.getElementById("total_in").value || "0.0");
var total_out = convertInput(document.getElementById("total_out").value || "0.0");
let val = convertOutput(Math.round((total_in - total_out - summe) * 100) / 100);
if (parseFloat(val) !== 0.0){
setValState(form.openamount,"Muss 0 sein");
} else {
setValState(form.openamount,"Muss 0 sein",true);
}
document.getElementById("openamount").value = val;
}
function setValState(ele, message,valid=false){
if (valid){
ele.classList.add("is-valid");
ele.classList.remove("is-invalid");
ele.setCustomValidity("");
} else {
ele.classList.add("is-invalid");
ele.classList.remove("is-valid");
ele.setCustomValidity(message);
}
}
function checkForm(e,form) {
const subinputs = form.elements;
//for new transfer
if (form.receiver.value =='' && form.transfer.value)
form.receiver.value = subinputs['parent_id[]'].selectedOptions[0].text+"-"+subinputs['account_id[]'].selectedOptions[0].text;
if (!form.total_in.parentElement.parentElement.classList.contains("d-none")) {
//Gesamtbetrag fehlt
if ((form.total_in.value == "" ) && (form.total_out.value == "")) {
if (parseFloat(form.amount.value) > 0.0){
setValState(form.total_in, "Notwendig");
} else {
setValState(form.total_out, "Notwendig");
}
}
//Restbetrag
if (parseFloat(form.openamount.value) !== 0.0){
setValState(form.openamount, "Offener Betrag!");
form.openamount.readOnly = false;
}else{
setValState(form.openamount, "Offener Betrag!",true);
}
subinputs['account_id[]'].forEach((line,index) => {
//wenn Kategorie gewählt muss Betrag da sein
if (line.value > 0){
if (!((parseFloat(subinputs['output[]'][index].value) > 0.0) || (parseFloat(subinputs['input[]'][index].value) > 0.0))){
setValState(subinputs['output[]'][index],"Notwendig");
setValState(subinputs['input[]'][index],"Notwendig");
} else {
setValState(subinputs['output[]'][index],"Notwendig",true);
setValState(subinputs['input[]'][index],"Notwendig",true);
}
}
else if ((subinputs['output[]'][index].value !=="") || (subinputs['input[]'][index].value !=="")){
setValState(line,"Notwendig");
setValState(subinputs['parent_id[]'][index],"Notwendig");
}
else {
setValState(line,"Notwendig",true);
setValState(subinputs['parent_id[]'][index],"Notwendig",true);
}
});
}
if (!form.checkValidity()) {
e.preventDefault();
e.stopPropagation();
form.openamount.readOnly = true;
}
form.classList.add('was-validated');
}