Finalisation sortie du bilan
This commit is contained in:
parent
dec6c8d676
commit
3fc96927a5
4 changed files with 416 additions and 332 deletions
|
|
@ -2,246 +2,219 @@
|
|||
|
||||
{{*
|
||||
Sortir une immobilisation du bilan
|
||||
@param :
|
||||
- immo_id : numéro de ligne de l'immo
|
||||
@param immo_id : id de la ligne d'immobilisation
|
||||
*}}
|
||||
|
||||
{{* récupérer les infos de l'immobilisation *}}
|
||||
{{* Infos de l'immobilisation *}}
|
||||
{{#select
|
||||
line.id as line_id,
|
||||
line.debit as montant,
|
||||
line.id_project as project_id,
|
||||
trans.id as trans_id,
|
||||
trans.date as trans_date,
|
||||
trans.label as trans_label,
|
||||
acc.id as account_id,
|
||||
y.id as year_id
|
||||
trans.date as date_achat,
|
||||
trans.label
|
||||
FROM acc_transactions_lines AS line
|
||||
INNER JOIN acc_transactions AS trans ON line.id_transaction = trans.id
|
||||
INNER JOIN acc_accounts as acc ON line.id_account = acc.id
|
||||
INNER JOIN acc_years AS y ON trans.id_year = y.id
|
||||
WHERE line.id = :line_id;
|
||||
:line_id = $_GET.immo_id|intval
|
||||
assign=ligne_immo
|
||||
assign="ligne_immo"
|
||||
}}
|
||||
{{else}}
|
||||
{{:error message="Immobilisation %s non trouvée"|args:$_GET.immo_id}}
|
||||
{{/select}}
|
||||
|
||||
{{:assign date_debut=$ligne_immo.date_achat}}
|
||||
{{:assign ts_mes=$ligne_immo.date_achat|strtotime}}
|
||||
|
||||
{{#load type="immo" assign="info_immo" where="$$.line = :line_id" :line_id=$_GET.immo_id|intval}}
|
||||
{{:assign ts_mes=$date|strtotime}}
|
||||
{{:assign date_debut=$date}}
|
||||
{{:assign duree_amort=$duration}}
|
||||
{{/load}}
|
||||
|
||||
{{* voir s'il existe des écritures d'amortissement associées *}}
|
||||
{{* chercher des écritures d'amortissement liées à l'immobilisation *}}
|
||||
{{#select
|
||||
sum(l_amort.credit) as amort_amount,
|
||||
SUM(l_amort.credit) as amort_amount,
|
||||
CASE
|
||||
WHEN links.id_related = t_immo.id THEN links.id_transaction
|
||||
WHEN links.id_related = t_immo.id
|
||||
THEN links.id_transaction
|
||||
ELSE links.id_related
|
||||
END as amort_trans_id
|
||||
END as amort_trans_id,
|
||||
MAX(trans.date) as last_amort_date,
|
||||
MIN(trans.date) as first_amort_date,
|
||||
COUNT(trans.id) as amort_number
|
||||
FROM acc_transactions_lines as l_immo
|
||||
INNER JOIN acc_transactions as t_immo on t_immo.id = l_immo.id_transaction
|
||||
INNER JOIN acc_transactions_links as links
|
||||
ON (
|
||||
t_immo.id = links.id_transaction
|
||||
ON (t_immo.id = links.id_transaction
|
||||
OR
|
||||
t_immo.id = links.id_related
|
||||
)
|
||||
t_immo.id = links.id_related)
|
||||
INNER JOIN acc_transactions_lines as l_amort on amort_trans_id = l_amort.id_transaction
|
||||
INNER join acc_transactions as trans on l_amort.id_transaction = trans.id
|
||||
INNER JOIN acc_accounts AS account ON l_amort.id_account = account.id
|
||||
WHERE
|
||||
l_immo.id = :line_id
|
||||
AND
|
||||
l_amort.credit <> 0
|
||||
AND
|
||||
account.code LIKE '28%';
|
||||
WHERE l_immo.id = :line_id AND account.code LIKE '28%'
|
||||
LIMIT 1;
|
||||
:line_id = $_GET.immo_id|intval
|
||||
assign=amort_line
|
||||
}}
|
||||
{{if $amort_amount == null}}
|
||||
{{:assign amort_amount=0}}
|
||||
{{else}}
|
||||
{{:assign amort_amount=$amort_amount}}
|
||||
{{/if}}
|
||||
{{/select}}
|
||||
|
||||
{{if $amort_line.amort_number == 0}}
|
||||
{{:assign amort_amount=0}}
|
||||
{{else}}
|
||||
{{:assign amort_amount=$amort_line.amort_amount}}
|
||||
{{:assign date_debut=$amort_line.last_amort_date|strtotime}}
|
||||
{{:assign date_debut="%d+(60*60*24)"|math:$date_debut|date:"Y-m-d"}}
|
||||
{{/if}}
|
||||
{{:assign ts_debut=$date_debut|strtotime}}
|
||||
{{:assign valeur_nette="%f-%f"|math:$ligne_immo.montant:$amort_amount}}
|
||||
|
||||
{{*
|
||||
lister les exercices qui englobent la date de début du prochain amortissement
|
||||
*}}
|
||||
{{:assign default_exit_date=""}}
|
||||
{{#years closed=false order="start_date" where="start_date <= :debut AND :debut <= end_date"
|
||||
:debut=$date_debut assign=years.}}
|
||||
{{if $start_date|strtotime <= $now && $now <= $end_date|strtotime}}
|
||||
{{:assign default_exit_date=$now|date_short}}
|
||||
{{/if}}
|
||||
{{:assign ts_fin=$end_date|strtotime}}
|
||||
{{: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}}
|
||||
{{else}}
|
||||
{{:error message="Aucun exercice ouvert pour enregistrer un amortissement"}}
|
||||
{{/years}}
|
||||
{{if $years|count == 1}}
|
||||
{{:assign default_year=$years.0.id}}
|
||||
{{else}}
|
||||
{{:assign default_year=""}}
|
||||
{{/if}}
|
||||
|
||||
{{* Traiter l'envoi du formulaire *}}
|
||||
{{#form on="proceed"}}
|
||||
|
||||
{{*
|
||||
vérifier que la date de sortie est située après la date de mise en
|
||||
service ou à défaut de la date d'acquisition de l'immobilisation
|
||||
*}}
|
||||
{{if $info_immo.date != null}}
|
||||
{{if $_POST.date_sortie|parse_date < $info_immo.date}}
|
||||
{{:assign immo_date=$info_immo.date|date_short}}
|
||||
{{:error message="La date choisie doit être postérieure à la date de mise en service de l'immobilisation (%s)"|args:$immo_date}}
|
||||
{{/if}}
|
||||
{{else}}
|
||||
{{if $_POST.date_sortie|parse_date < $ligne_immo.trans_date}}
|
||||
{{:assign immo_date=$ligne_immo.trans_date|date_short}}
|
||||
{{:error message="La date choisie doit être postérieure à la date d'acquisition de l'immobilisation (%s)"|args:$immo_date}}
|
||||
{{if $_POST.date_sortie == null}}
|
||||
{{:error message="Vous devez renseigner la date de sortie"}}
|
||||
{{/if}}
|
||||
|
||||
{{* vérifier la validité de la date de mise en service *}}
|
||||
{{if $_POST.date_mes != null}}
|
||||
{{:assign ts_mes=$_POST.date_mes|parse_date|strtotime}}
|
||||
{{if $ts_mes < $ligne_immo.date_achat|strtotime || $ts_mes > $amort_line.first_amort_date|strtotime}}
|
||||
{{:assign immo_date=$ligne_immo.date_achat|date_short}}
|
||||
{{:assign amort_date=$amort_line.first_amort_date|date_short}}
|
||||
{{:error message="La date de mise en service doit être postérieure à la date d'acquisition de l'immobilisation (%s) et antérieure à la date du premier amortissement (%s)"|args:$immo_date:$amort_date}}
|
||||
{{/if}}
|
||||
{{/if}}
|
||||
|
||||
{{* vérifier que la date est située dans un exercice ouvert *}}
|
||||
{{:include
|
||||
file="_check_date.html"
|
||||
date=$_POST.date_sortie
|
||||
keep="open_years, selected_year, selected_chart"
|
||||
}}
|
||||
{{if $selected_year == null}}
|
||||
{{:assign msg_years=""}}
|
||||
{{#foreach from=$open_years}}
|
||||
{{:assign debut=$start_date|date_short}}
|
||||
{{:assign fin=$end_date|date_short}}
|
||||
{{:assign msg_years=$msg_years|cat:"\n"|cat:" - "|cat:$label|cat:" : "|cat:$debut|cat:" à "|cat:$fin}}
|
||||
{{/foreach}}
|
||||
{{:error message="La date choisie n'est dans aucun exercice ouvert !!\nExercices ouverts : %s"|args:$msg_years}}
|
||||
{{/if}}
|
||||
|
||||
{{* vérifier la validité de la durée d'immobilisation *}}
|
||||
{{if $valeur_nette > 0}}
|
||||
{{if $info_immo.duration == null}}
|
||||
{{if $_POST.duree == null}}
|
||||
{{:error message="Vous devez renseigner la durée de l'amortissement"}}
|
||||
{{:error message="Vous devez renseigner la durée de l'immobilisation"}}
|
||||
{{elseif $_POST.duree < $amort_line.amort_number}}
|
||||
{{:error message="La durée de l'amortissement ne peut être inférieure au nombre d'écritures d'amortissement (%d)"|args:$amort_line.amort_number}}
|
||||
{{else}}
|
||||
{{:assign var=info_immo.duration value=$_POST.duree}}
|
||||
{{:assign duree_amort=$_POST.duree}}
|
||||
{{/if}}
|
||||
{{/if}}
|
||||
{{/if}}
|
||||
|
||||
{{* Cession de l'immobilisation
|
||||
{{if $_POST.cession}}
|
||||
{{if $_POST.montant_cession == ""}}
|
||||
{{:error message="Le montant de la cession doit être renseigné"}}
|
||||
{{* vérifier la validité de la date de sortie *}}
|
||||
{{if $info_immo.date != null}}
|
||||
{{if $_POST.date_sortie|parse_date < $info_immo.date}}
|
||||
{{:assign immo_date=$info_immo.date|date_short}}
|
||||
{{:error message="La date de sortie doit être postérieure à la date de mise en service de l'immobilisation (%s)"|args:$immo_date}}
|
||||
{{/if}}
|
||||
{{if $_POST.montant_cession <= 0}}
|
||||
{{:error message="Le montant de la cession doit être strictement positif"}}
|
||||
{{elseif $_POST.date_mes != null}}
|
||||
{{if $_POST.date_sortie|parse_date < $_POST.date_mes|parse_date}}
|
||||
{{:error message="La date de sortie doit être postérieure à la date de mise en service de l'immobilisation (%s)"|args:$_POST.date_mes}}
|
||||
{{/if}}
|
||||
{{elseif $_POST.date_sortie|parse_date < $ligne_immo.date_achat}}
|
||||
{{:assign immo_date=$ligne_immo.date_achat|date_short}}
|
||||
{{:error message="La date de sortie doit être postérieure à la date d'acquisition de l'immobilisation (%s)"|args:$immo_date}}
|
||||
{{/if}}
|
||||
*}}
|
||||
{{:redirect to="compute_exit_data.html?immo_id=%s&amort_amount=%s&year=%s&exit_date=%s&duree_amort=%s"|args:$_GET.immo_id:$amort_amount:$selected_year:$_POST.date_sortie:$_POST.duree}}
|
||||
{{*
|
||||
{{:redirect to="write_exit.html?immo_id=%s&amort_amount=%s&year=%s&exit_date=%s&cession=%s&duree_amort=%s"|args:$_GET.immo_id:$amort_amount:$selected_year:$_POST.date_sortie:$_POST.montant_cession:$_POST.duree}}
|
||||
*}}
|
||||
{{if $amort_line.amort_number != 0 && $_POST.date_sortie|parse_date <= $amort_line.last_amort_date}}
|
||||
{{:assign last_amort_date=$amort_line.last_amort_date|date_short}}
|
||||
{{:error message="La date de sortie doit être postérieure à la date de la dernière immobilisation (%s)"|args:$last_amort_date}}
|
||||
{{/if}}
|
||||
|
||||
{{* vérifier que la date de sortie est située dans un exercice ouvert *}}
|
||||
{{:assign ts_exit = $_POST.date_sortie|parse_date|strtotime}}
|
||||
{{:assign ok=false}}
|
||||
{{#foreach from=$years}}
|
||||
{{if $id == $_POST.id_year}}
|
||||
{{:assign selected_year=$id}}
|
||||
{{if $start_date|strtotime <= $ts_exit && $ts_exit <= $end_date|strtotime}}
|
||||
{{:assign ok=true}}
|
||||
{{:break}}
|
||||
{{/if}}
|
||||
{{/if}}
|
||||
{{/foreach}}
|
||||
{{if ! $ok}}
|
||||
{{:error message="La date choisie n'est pas dans l'exercice sélectionné !"}}
|
||||
{{/if}}
|
||||
|
||||
{{:redirect to="compute_exit_data.html?immo_id=%s&amort_amount=%s&year=%s&date_mes=%s&date_debut=%s&exit_date=%s&duree_amort=%s"|args:$_GET.immo_id:$amort_amount:$selected_year:$ts_mes:$ts_debut:$ts_exit:$duree_amort}}
|
||||
{{/form}}
|
||||
|
||||
{{:admin_header title="Sortie de bilan" custom_css="./style.css" current="module_amortissement"}}
|
||||
{{* barre de navigation *}}
|
||||
{{if ! $dialog}}
|
||||
{{:include file="_nav.html" current="index"}}
|
||||
{{:include file="_nav.html" current="index" subsubcurrent="balance_exit"}}
|
||||
{{/if}}
|
||||
{{:form_errors}}
|
||||
|
||||
{{*:debug ligne_immo=$ligne_immo amort_amount=$amort_amount*}}
|
||||
|
||||
{{* formulaire de sortie du bilan *}}
|
||||
<h3>Sortir l'immobilisation « {{$ligne_immo.trans_label}} » du bilan</h3>
|
||||
<h3>Sortir l'immobilisation « {{$ligne_immo.label}} » du bilan</h3>
|
||||
<form method="post" action="">
|
||||
|
||||
<div class="informations">
|
||||
<legend>Données de l'imobilisation</legend>
|
||||
<dl class="describe">
|
||||
<dt><label>Montant</label></dt>
|
||||
<dt>Montant</dt>
|
||||
<dd id="montant_immo" class="money">{{$ligne_immo.montant|money_currency:false}}</dd>
|
||||
<dt><label>Date d'acquisition</label></dt>
|
||||
<dd >{{$ligne_immo.trans_date|date_short}}</dd>
|
||||
{{if $info_immo.date != null && $info_immo.date != $ligne_immo.trans_date}}
|
||||
<dt><label>Date de mise en service</label></dt>
|
||||
<dt>Date d'acquisition</dt>
|
||||
<dd >{{$ligne_immo.date_achat|date_short}}</dd>
|
||||
{{if $info_immo.date != null && $info_immo.date != $ligne_immo.date_achat}}
|
||||
<dt>Date de mise en service</dt>
|
||||
<dd>{{$info_immo.date|date_short}}</dd>
|
||||
{{/if}}
|
||||
{{if $info_immo.duration != null}}
|
||||
<dt>Durée de l'amortissement</dt>
|
||||
<dd id="duree_amort" class="num">{{$info_immo.duration}} ans</dd>
|
||||
{{/if}}
|
||||
<dt><label>Montant des amortissements</label></dt>
|
||||
<dt>Montant des amortissements</dt>
|
||||
<dd id="montant_amort" class="money">{{$amort_amount|money_currency:false}} <span class="help">(à la date de début de l'exercice)</span></dd>
|
||||
<dt><label>Valeur nette résiduelle</label></dt>
|
||||
<dt>Valeur nette résiduelle</dt>
|
||||
<dd class="money">{{$valeur_nette|money_currency:false}} <span class="help">(à la date de début de l'exercice)</span></dd>
|
||||
</dl>
|
||||
</div>
|
||||
|
||||
{{if $valeur_nette > 0}}
|
||||
{{if $info_immo.date == null || $info_immo.duration == null}}
|
||||
<fieldset>
|
||||
<legend>Informations sur l'immobilisation</legend>
|
||||
<dl>
|
||||
{{if $info_immo.date == null}}
|
||||
{{:input type="date" name="date_mes" label="Date de mise en service" help="C'est la date de première utilisation ; à renseigner uniquement si différente de la date d'acquisition"}}
|
||||
{{/if}}
|
||||
{{if $info_immo.duration == null}}
|
||||
{{:input type="number" name="duree" label="Durée d'amortissement" required=true min=1 help="La durée est nécessaire pour calculer l'amortissement complémentaire"}}
|
||||
{{/if}}
|
||||
</dl>
|
||||
</fieldset>
|
||||
{{/if}}
|
||||
{{/if}}
|
||||
<fieldset>
|
||||
<legend>Informations de sortie</legend>
|
||||
<dl>
|
||||
{{:input type="date" name="date_sortie" label="Date de sortie du bilan" required=true default=$now|date_short}}
|
||||
{{if $info_immo.date == null && $valeur_nette > 0}}
|
||||
{{:input type="date" name="date_mes" label="Date de mise en service" help="à renseigner uniquement si différente de la date d'acquisition"}}
|
||||
{{/if}}
|
||||
{{if $info_immo.duration == null && $valeur_nette > 0}}
|
||||
{{:input type="number" name="duree" label="Durée d'amortissement" required=true min=1 onchange="computeAmort()" help="La durée est nécessaire pour calculer l'amortissement résiduel"}}
|
||||
{{/if}}
|
||||
{{*
|
||||
{{:input type="checkbox" value=1 name="cession" label="Cession" help="Cocher si la sortie est due à une cession" onclick="toggleHiddenAndFocus('f_cession_1', ['infos_cession'], 'f_montant_cession')"}}
|
||||
<div id="infos_cession" class="hidden">
|
||||
{{:input type="money" name="montant_cession" label="Montant de la cession" required=true}}
|
||||
</div>
|
||||
*}}
|
||||
{{:input type="select" default=$default_year name="id_year" label="Exercice" required=true default_empty="— Faire un choix —" options=$open_years}}
|
||||
{{:input type="date" name="date_sortie" label="Date de sortie du bilan" required=true default=$default_exit_date}}
|
||||
</dl>
|
||||
</fieldset>
|
||||
|
||||
<p class="submit">
|
||||
{{:button type="submit" name="proceed" label="Poursuivre" shape="right" class="main"}}
|
||||
</p>
|
||||
|
||||
</form>
|
||||
|
||||
<script type="text/javascript">
|
||||
/*
|
||||
- activer/désactiver les champs passés en paramètres
|
||||
- donner le focus au champ de saisie du montant
|
||||
*/
|
||||
function toggleHiddenAndFocus(idcb, idfields, idinput) {
|
||||
const cession = document.getElementById(idcb);
|
||||
for (let id of idfields) {
|
||||
const field = document.getElementById(id);
|
||||
if (cession.checked) {
|
||||
field.setAttribute("class", "");
|
||||
document.getElementById(idinput).focus();
|
||||
}
|
||||
else {
|
||||
field.setAttribute("class","hidden")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getDate(name) {
|
||||
const elem = document.getElementsByName(name)[0].value;
|
||||
const jma = elem.split('/');
|
||||
const dd = new Date(jma[2], jma[1]-1, jma[0]);
|
||||
return dd;
|
||||
}
|
||||
|
||||
function getNumber(text) {
|
||||
return num = Number(text.replace(/ /g, '').replace(/,/, '.').replace(/€/, ''));
|
||||
}
|
||||
|
||||
function computeAmort() {
|
||||
const exitDate = getDate('date_sortie');
|
||||
// console.log('exit = ' + exitDate);
|
||||
const year = exitDate.getFullYear();
|
||||
const yearStart = new Date(year, 0);
|
||||
const oneDay = 24 * 60 * 60 * 1000;
|
||||
const nbDays = Math.round((exitDate - yearStart) / oneDay);
|
||||
console.log("nb jours = " + nbDays);
|
||||
const montantImmo = getNumber(document.getElementById('montant_immo').innerText);
|
||||
// console.log('montant_immo= ' + montantImmo);
|
||||
// durée d'amortissement
|
||||
let elem = document.getElementById('duree_amort');
|
||||
let duree_amort;
|
||||
if (elem == null) {
|
||||
duree_amort = document.getElementById('f_duree').value;
|
||||
} else {
|
||||
duree_amort = Number(elem.innerText.replace(/ .*/, ''));
|
||||
}
|
||||
const annuite = montantImmo / duree_amort;
|
||||
const amort_comp = annuite / 365 * nbDays;
|
||||
console.log("durée amort = " + duree_amort + ", annuité=" + annuite + ", amort comp = " + amort_comp);
|
||||
}
|
||||
/*
|
||||
(function () {
|
||||
toggleHiddenAndFocus('f_cession_1', ['infos_cession'], 'f_montant_cession');
|
||||
})();
|
||||
*/
|
||||
</script>
|
||||
|
||||
{{:admin_footer}}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue