amortization/scripts.js
2025-05-06 17:07:46 +02:00

99 lines
3.2 KiB
JavaScript

// activer/désactiver les champs passés en paramètres
function toggleInputs(idcb, idfields) {
const noamort = document.getElementById(idcb);
for (let id of idfields) {
const field = document.getElementById(id);
if (noamort.checked) {
field.setAttribute("disabled","disabled");
}
else {
field.removeAttribute("disabled");
}
}
}
// fixer l'exercice des sélecteurs de compte
function setAccountYear(button_names, id_year) {
for (const name of button_names) {
const button = document.querySelector("button[data-name=" + name + "]");
const b_value = button.value;
const new_value = b_value.replace(/id_year=\d+/, 'id_year=' + id_year);
button.setAttribute('value', new_value);
}
}
// renvoyer la valeur en millisecondes d'un champ date
function getDate(idelem) {
const elem = document.getElementById(idelem).value;
const jma = elem.split('/');
const dd = new Date(jma[2], jma[1]-1, jma[0]);
return dd.getTime();
}
// désactiver les options du sélecteur qui ne sont pas dans un tableau de valeurs
function disableOptions(idSelect, init, values) {
for (let i = init; i < idSelect.options.length; ++i) {
const choix = idSelect.options[i];
if (! values.includes(choix.value)) {
choix.setAttribute('disabled', 'true');
choix.removeAttribute('selected');
}
}
}
// choisir les exercices affichés selon la date paramètre
function chooseYears(id_date, id_exercices, id_years)
{
// masquer le message d'erreur
document.getElementById('erreur').setAttribute('class', 'hidden');
// activer toutes les options
const select_field = document.getElementById(id_exercices);
for (const choix of select_field.options) {
choix.removeAttribute('disabled');
choix.removeAttribute('selected');
}
// voir dans quels exercices ouverts se situe la date d'acquisition
const date_achat = getDate(id_date)/1000;
const years_data = document.getElementById(id_years);
let array_years = [];
for (const choix of years_data.options) {
if (choix.value != '') {
const epox = choix.text.split(' ');
if (epox[0] <= date_achat && date_achat <= epox[1]) {
array_years.push(choix.value);
}
}
}
if (array_years.length == 0) {
document.getElementById('erreur').setAttribute('class', '');
disableOptions(select_field, 1, array_years);
select_field.value = '';
setAccountYear(['credit_account', 'debit_account'], 0);
} else if (array_years.length == 1) {
disableOptions(select_field, 0, array_years);
select_field.value = array_years[0];
setAccountYear(['credit_account', 'debit_account'], array_years[0]);
} else {
disableOptions(select_field, 1, array_years);
select_field.value = '';
setAccountYear(['credit_account', 'debit_account'], 0);
}
}
// afficher la date de fin de l'exercice choisi
function setDateEnd(id_exercices, id_date, id_years) {
const selected_year = document.getElementById(id_exercices).value;
const years_data = document.getElementById(id_years);
for (const choix of years_data.options) {
if (choix.value == '') {
document.getElementById(id_date).value = '';
} else if (choix.value == selected_year) {
const epox = choix.text.split(' ');
const date_fin = new Date(epox[1] * 1000);
document.getElementById(id_date).value = date_fin.toLocaleDateString();
break;
}
}
}