Recalculer le montant de l'amortissement en fonction de l'exercice et de la date
This commit is contained in:
parent
515eb443fa
commit
99aa29a4a1
2 changed files with 117 additions and 19 deletions
|
@ -72,6 +72,7 @@
|
|||
|
||||
{{if $amort_line.amort_amount == null}}
|
||||
{{* 1er amortissement *}}
|
||||
{{:assign premier_amort=true}}
|
||||
{{* déterminer le compte d'amortissement en fonction du compte d'immobilisation *}}
|
||||
{{#select code, label from acc_accounts where id = :id; :id=$ligne_immo.account_id assign=amort_account}}
|
||||
{{/select}}
|
||||
|
@ -84,6 +85,7 @@
|
|||
{{:assign var=libelle value="Amortissement %s"|args:$ligne_immo.label}}
|
||||
{{else}}
|
||||
{{* amortissements suivants *}}
|
||||
{{:assign premier_amort=false}}
|
||||
{{:assign date_debut=$amort_line.amort_date|strtotime}}
|
||||
{{:assign date_debut="%d+(60*60*24)"|math:$date_debut|date:"Y-m-d"}}
|
||||
{{:assign var=code_amort value=$amort_line.account_code}}
|
||||
|
@ -92,17 +94,21 @@
|
|||
{{/if}}
|
||||
|
||||
{{*
|
||||
lister les exercices dont la date de fin est postérieure à la date
|
||||
de mise en service de l'immo ou à la date du dernier amortissement
|
||||
lister les exercices qui englobent la date de mise en service de l'immo
|
||||
ou la date du dernier amortissement
|
||||
*}}
|
||||
{{:assign msg_years=""}}
|
||||
{{#years closed=false order="start_date" where="end_date > :debut" :debut=$date_debut assign=years.}}
|
||||
{{:assign ts_debut=$start_date|strtotime}}
|
||||
{{#years closed=false order="start_date" where="start_date <= :debut AND :debut <= end_date" :debut=$date_debut assign=years.}}
|
||||
{{if $premier_amort}}
|
||||
{{:assign ts_debut=$date_debut|strtotime}}
|
||||
{{else}}
|
||||
{{:assign ts_debut=$start_date|strtotime}}
|
||||
{{/if}}
|
||||
{{:assign ts_fin=$end_date|strtotime}}
|
||||
{{:assign var="years_data.%d"|args:$id value=$ts_debut|cat:" "|cat:$ts_fin}}
|
||||
{{:assign debut=$start_date|date_short}}
|
||||
{{:assign fin=$end_date|date_short}}
|
||||
{{:assign var="open_years.%d"|args:$id value=$label|cat:" (du "|cat:$debut|cat:" au "|cat:$fin|cat:")"}}
|
||||
{{:assign var="years_data.%d"|args:$id value=$ts_debut|cat:" "|cat:$ts_fin}}
|
||||
{{:assign msg_years=$msg_years|cat:"\n"|cat:" - "|cat:$label|cat:" : "|cat:$debut|cat:" à "|cat:$fin}}
|
||||
{{else}}
|
||||
{{:error message="Aucun exercice ouvert pour enregistrer un amortissement"}}
|
||||
|
@ -229,8 +235,8 @@
|
|||
|
||||
{{* -------------------------------------------------------------------------------- *}}
|
||||
|
||||
<div id="erreur" class="hidden">
|
||||
<p class="block error">La date choisie n'est dans aucun exercice ouvert</p>
|
||||
<div id="f_erreur" class="hidden">
|
||||
<p class="block error">La date saisie n'est pas dans l'exercice choisi</p>
|
||||
</div>
|
||||
|
||||
<form method="post" action="">
|
||||
|
@ -258,6 +264,9 @@
|
|||
default=$credit_account
|
||||
}}
|
||||
<div id="donnees" class="hidden">
|
||||
{{:input type="text" name="montant_immo" default=$ligne_immo.montant}}
|
||||
{{:input type="text" name="duree_amort" default=$duree}}
|
||||
{{:input type="text" name="somme_amort" default=$amort_line.amort_amount}}
|
||||
{{:input type="select" name="years_data" options=$years_data}}
|
||||
</div>
|
||||
</dl>
|
||||
|
@ -272,13 +281,39 @@
|
|||
<script type="text/javascript" src="scripts.js"></script>
|
||||
<script type="text/javascript">
|
||||
|
||||
// afficher la date de fin de l'exercice choisi
|
||||
function setDate(evt, id_exercices = 'f_id_year', id_date = 'f_date_amort', id_years = 'f_years_data') {
|
||||
// afficher la date de fin de l'exercice choisi et recalculer le montant de l'amortissement
|
||||
function redisplayData(evt,
|
||||
id_immo = 'f_montant_immo',
|
||||
id_duree = 'f_duree_amort',
|
||||
id_amort = 'f_somme_amort',
|
||||
id_years = 'f_years_data',
|
||||
id_exercices = 'f_id_year',
|
||||
id_date = 'f_date_amort',
|
||||
id_montant = 'f_montant',
|
||||
id_erreur = 'f_erreur'
|
||||
)
|
||||
{
|
||||
setDateEnd(id_exercices, id_date, id_years);
|
||||
displayAmort(id_immo, id_duree, id_amort, id_years, id_exercices, id_montant, id_erreur);
|
||||
}
|
||||
|
||||
function redisplayAmort(evt,
|
||||
id_immo = 'f_montant_immo',
|
||||
id_duree = 'f_duree_amort',
|
||||
id_amort = 'f_somme_amort',
|
||||
id_years = 'f_years_data',
|
||||
id_exercices = 'f_id_year',
|
||||
id_date = 'f_date_amort',
|
||||
id_montant = 'f_montant',
|
||||
id_erreur = 'f_erreur'
|
||||
)
|
||||
{
|
||||
displayAmort(id_immo, id_duree, id_amort, id_years, id_exercices, id_montant, id_erreur, id_date);
|
||||
}
|
||||
|
||||
(function () {
|
||||
setDateEnd('f_id_year', 'f_date_amort', 'f_years_data');
|
||||
document.getElementById('f_id_year').onchange = setDate;
|
||||
document.getElementById('f_id_year').onchange = redisplayData;
|
||||
document.getElementById('f_date_amort').onchange = redisplayAmort;
|
||||
})();
|
||||
</script>
|
||||
|
|
81
scripts.js
81
scripts.js
|
@ -22,12 +22,19 @@ function setAccountYear(button_names, id_year) {
|
|||
}
|
||||
}
|
||||
|
||||
// renvoyer la valeur en millisecondes d'un champ date
|
||||
function getDate(idelem) {
|
||||
const elem = document.getElementById(idelem).value;
|
||||
const jma = elem.split('/');
|
||||
/**
|
||||
* renvoyer la valeur en secondes d'une date au format j/m/a
|
||||
* @param {string} date
|
||||
*/
|
||||
function str2sec(date) {
|
||||
const jma = date.split('/');
|
||||
const dd = new Date(jma[2], jma[1]-1, jma[0]);
|
||||
return dd.getTime();
|
||||
return dd.getTime()/1000;
|
||||
}
|
||||
|
||||
// renvoyer la valeur en secondes d'un champ date
|
||||
function getDate(idelem) {
|
||||
return str2sec(document.getElementById(idelem).value);
|
||||
}
|
||||
|
||||
// désactiver les options du sélecteur qui ne sont pas dans un tableau de valeurs
|
||||
|
@ -54,7 +61,7 @@ function chooseYears(id_date, id_exercices, id_years)
|
|||
}
|
||||
|
||||
// voir dans quels exercices ouverts se situe la date d'acquisition
|
||||
const date_achat = getDate(id_date)/1000;
|
||||
const date_achat = getDate(id_date);
|
||||
const years_data = document.getElementById(id_years);
|
||||
let array_years = [];
|
||||
for (const choix of years_data.options) {
|
||||
|
@ -85,11 +92,13 @@ function chooseYears(id_date, id_exercices, id_years)
|
|||
// 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;
|
||||
if (selected_year == '') {
|
||||
document.getElementById(id_date).value = '';
|
||||
return;
|
||||
}
|
||||
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) {
|
||||
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();
|
||||
|
@ -97,3 +106,57 @@ function setDateEnd(id_exercices, id_date, id_years) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// renvoyer la valeur numérique d'un montant formatté en €
|
||||
function getNumber(text) {
|
||||
return Number(text.replace(/ /g, '').replace(/,/, '.').replace(/€/, ''));
|
||||
}
|
||||
|
||||
// calculer le montant d'un amortissement
|
||||
// @param montant immo
|
||||
// @param durée immo (années)
|
||||
// @param somme amortissements
|
||||
// @param date début
|
||||
// @param date de fin
|
||||
// @result montant de l'amortissement
|
||||
function computeAmort(montant_immo, duree_immo, somme_amort, date_debut, date_fin) {
|
||||
const nbjours = 1 + (date_fin - date_debut) / (60*60*24);
|
||||
return Math.round(Math.min(montant_immo / duree_immo / 365 * nbjours, montant_immo - somme_amort));
|
||||
}
|
||||
|
||||
// calculer et afficher le montant de l'amortissement
|
||||
function displayAmort(id_immo, id_duree, id_amort, id_years, id_exercices, id_montant, id_erreur, id_date=null)
|
||||
{
|
||||
document.getElementById(id_erreur).setAttribute('class', 'hidden');
|
||||
let case_montant = document.getElementById(id_montant);
|
||||
const selected_year = document.getElementById(id_exercices).value;
|
||||
if (selected_year == '') {
|
||||
case_montant.value = '';
|
||||
return;
|
||||
}
|
||||
const years_data = document.getElementById(id_years);
|
||||
let date_debut, date_fin;
|
||||
for (const choix of years_data.options) {
|
||||
if (choix.value == selected_year) {
|
||||
const epox = choix.text.split(' ');
|
||||
date_debut = epox[0];
|
||||
date_fin = epox[1];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (id_date != null) {
|
||||
const date_choisie = str2sec(document.getElementById(id_date).value);
|
||||
console.log("date_choisie = " + date_choisie + ", date_debut=" + date_debut + ", date_fin = " + date_fin);
|
||||
if (date_debut <= date_choisie && date_choisie <= date_fin) {
|
||||
date_fin = date_choisie;
|
||||
} else {
|
||||
document.getElementById(id_erreur).setAttribute('class', '');
|
||||
}
|
||||
}
|
||||
const montant_immo = Number(document.getElementById(id_immo).value);
|
||||
const duree_immo = Number(document.getElementById(id_duree).value);
|
||||
const somme_amort = Number(document.getElementById(id_amort).value);
|
||||
const montant_amort = computeAmort(montant_immo, duree_immo, somme_amort, date_debut, date_fin);
|
||||
case_montant.value = montant_amort/100;
|
||||
case_montant.innerText = montant_amort;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue