379 lines
13 KiB
HTML
379 lines
13 KiB
HTML
{{* -*- brindille -*- *}}
|
||
|
||
{{*
|
||
Créer une écriture d'amortissement
|
||
@param immo_line_id : id de la ligne d'immobilisation
|
||
@param immo_doc_id : id du doc associé à l'immo
|
||
*}}
|
||
|
||
{{* Infos de l'immobilisation *}}
|
||
{{#select
|
||
line.id_account as account_id,
|
||
line.debit as montant,
|
||
line.id_project as project_id,
|
||
line.label as line_label,
|
||
trans.id as immo_trans_id,
|
||
trans.label,
|
||
trans.date,
|
||
account.code as account_code
|
||
FROM acc_transactions_lines AS line
|
||
INNER JOIN acc_transactions AS trans ON line.id_transaction = trans.id
|
||
INNER JOIN acc_accounts AS account ON line.id_account = account.id
|
||
LEFT JOIN acc_projects AS project ON line.id_project = project.id
|
||
WHERE line.id = :line_id;
|
||
:line_id = $_GET.immo_line_id|intval
|
||
assign="ligne_immo"
|
||
}}
|
||
{{else}}
|
||
{{:error message="Immobilisation non trouvée"}}
|
||
{{/select}}
|
||
{{:assign date_debut=$ligne_immo.date}}
|
||
|
||
{{#load id=$_GET.immo_doc_id|intval}}
|
||
{{:assign duree=$duration}}
|
||
{{if $date_mes != null}}
|
||
{{:assign date_debut=$date_mes}}
|
||
{{/if}}
|
||
{{:assign status=$status}}
|
||
{{:assign amort_label=$label|or:$ligne_immo.label}}
|
||
{{if $amount != null}}
|
||
{{:assign var="ligne_immo.montant" value=$amount}}
|
||
{{/if}}
|
||
{{else}}
|
||
{{:error message="Informations de l'immobilisation « %s » non trouvées ; vous devez d'abord les renseigner"|args:$ligne_immo.label}}
|
||
{{/load}}
|
||
{{if $ligne_immo.line_label != null && $ligne_immo.line_label != $amort_label}}
|
||
{{:assign amort_label=$amort_label|cat:" — "|cat:$ligne_immo.line_label}}
|
||
{{/if}}
|
||
{{:assign var="amort_label" value="Amortissement %s"|args:$amort_label}}
|
||
|
||
{{if $status == "amortized" || $status == "archived"}}
|
||
{{:error message="Cette immobilisation est complètement amortie ; il est impossible d'ajouter une écriture d'amortissement"}}
|
||
{{/if}}
|
||
|
||
{{*
|
||
chercher des écritures liées à l'immo courante au crédit du même compte
|
||
et déduire leur montant de celui de l'immo
|
||
*}}
|
||
{{:assign total_credits=0}}
|
||
{{#load
|
||
type="immo_link"
|
||
where="$$.immo_doc_id = :immo_doc_id" :immo_doc_id = $_GET.immo_doc_id|intval
|
||
}}
|
||
{{if $amount == null}}
|
||
{{#select credit FROM acc_transactions_lines WHERE id = :credit_line_id;
|
||
:credit_line_id=$credit_line_id
|
||
}}
|
||
{{:assign total_credits="%d+%d"|math:$total_credits:$credit}}
|
||
{{/select}}
|
||
{{else}}
|
||
{{:assign total_credits="%d+%d"|math:$total_credits:$amount}}
|
||
{{/if}}
|
||
{{/load}}
|
||
{{:assign var="ligne_immo.montant" value="%d-%d"|math:$ligne_immo.montant:$total_credits}}
|
||
{{:assign solde=$ligne_immo.montant}}
|
||
|
||
{{* chercher des écritures d'amortissement liées à la ligne d'immobilisation *}}
|
||
{{#load
|
||
type="amort_link"
|
||
where="$$.immo_doc_id = :immo_doc_id"
|
||
order="$$.date"
|
||
:immo_doc_id=$_GET.immo_doc_id|intval
|
||
}}
|
||
{{#select
|
||
line.credit,
|
||
trans.date as amort_date,
|
||
account.code as account_code,
|
||
account.label as account_label
|
||
FROM acc_transactions_lines AS line
|
||
INNER JOIN acc_transactions AS trans ON line.id_transaction = trans.id
|
||
INNER join acc_accounts AS account on line.id_account = account.id
|
||
WHERE line.id = :amort_line_id
|
||
;
|
||
:amort_line_id = $amort_line_id
|
||
assign="amort_line"
|
||
}}
|
||
{{:assign var="amort_line.amort_amount" value=$amount|or:$credit}}
|
||
{{:assign var="linked_amort.%s_%d"|args:$amort_date:$id value=$amort_line}}
|
||
{{/select}}
|
||
{{/load}}
|
||
|
||
{{:assign total_amort=0}}
|
||
{{:assign valeur_residuelle=$ligne_immo.montant}}
|
||
{{#foreach from=$linked_amort|ksort item="line"}}
|
||
{{:assign valeur_residuelle="%d-%d"|math:$valeur_residuelle:$line.amort_amount}}
|
||
{{:assign total_amort="%d+%d"|math:$total_amort:$line.amort_amount}}
|
||
{{:assign date_debut=$line.amort_date}}
|
||
{{:assign code_amort=$line.account_code}}
|
||
{{:assign amort_account_label=$line.account_label}}
|
||
{{/foreach}}
|
||
|
||
{{if $valeur_residuelle == 0}}
|
||
{{:error message="Cette immobilisation est complètement amortie ; il est impossible d'ajouter une écriture d'amortissement"}}
|
||
{{/if}}
|
||
|
||
{{if $total_amort == 0}}
|
||
{{* 1er amortissement *}}
|
||
{{* 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}}
|
||
{{:include
|
||
file="./_get_amort_code.html"
|
||
code_immo=$amort_account.code
|
||
keep="code_amort"
|
||
}}
|
||
{{:assign var="amort_account_label" value="Amortissement %s"|args:$amort_account.label}}
|
||
{{:assign var=libelle value="Amortissement %s"|args:$ligne_immo.label}}
|
||
{{else}}
|
||
{{* amortissements suivants *}}
|
||
{{:assign date_debut=$date_debut|strtotime}}
|
||
{{:assign date_debut="%d+(60*60*24)"|math:$date_debut|date:"Y-m-d"}}
|
||
{{/if}}
|
||
|
||
{{*
|
||
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="start_date <= :debut AND :debut <= end_date"
|
||
:debut=$date_debut assign=years.}}
|
||
{{if $total_amort == 0}}
|
||
{{:assign ts_debut=$date_debut|strtotime}}
|
||
{{else}}
|
||
{{:assign ts_debut=$start_date|strtotime}}
|
||
{{/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}}
|
||
{{: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"}}
|
||
{{/years}}
|
||
|
||
{{* Traiter l'envoi du formulaire *}}
|
||
{{#form on="save"}}
|
||
{{* vérifier que la date est située dans un exercice compatible *}}
|
||
{{:assign ts_date = $_POST.date_amort|parse_date|strtotime}}
|
||
{{:assign ok=false}}
|
||
{{#foreach from=$years}}
|
||
{{if $id == $_POST.id_year}}
|
||
{{:assign selected_chart=$id_chart}}
|
||
{{if $start_date|strtotime <= $ts_date && $ts_date <= $end_date|strtotime}}
|
||
{{:assign ok=true}}
|
||
{{:break}}
|
||
{{/if}}
|
||
{{/if}}
|
||
{{/foreach}}
|
||
{{if ! $ok}}
|
||
{{:error message="La date choisie n'est dans aucun exercice compatible !\nExercices compatibles : %s"|args:$msg_years}}
|
||
{{/if}}
|
||
{{* déterminer le numéro des comptes au cas où la clé serait un identifiant *}}
|
||
{{:include
|
||
file="_get_codes.html"
|
||
account=$_POST.debit_account
|
||
keep="account_code"
|
||
}}
|
||
{{:assign debit_account=$account_code}}
|
||
{{:include
|
||
file="_get_codes.html"
|
||
account=$_POST.credit_account
|
||
keep="account_code"
|
||
}}
|
||
{{:assign credit_account=$account_code}}
|
||
{{* vérifier :
|
||
- que le compte d'amortissement débute par un préfixe correct (280, 281, ...)
|
||
- est présent dans le PC de l'exercice correspondant à la date
|
||
*}}
|
||
{{:include file="_get_config.html" keep="module.config.prefixes"}}
|
||
{{#foreach from=$module.config.prefixes item="code"}}
|
||
{{* déterminer le numéro du compte d'amortissement associé au compte d'immobilisation *}}
|
||
{{:include
|
||
file="./_get_amort_code.html"
|
||
code_immo=$code
|
||
keep="code_amort"
|
||
}}
|
||
{{:assign var="amort_array." value=$code_amort|intval}}
|
||
{{/foreach}}
|
||
|
||
{{:include
|
||
file="_check_account.html"
|
||
account=$credit_account
|
||
chart_id=$selected_chart
|
||
prefix_array=$amort_array
|
||
keep="account_ok"
|
||
}}
|
||
{{if $account_ok == null}}
|
||
{{:assign compte=$credit_account|implode:""}}
|
||
{{:redirect url="add_account.html?account=%s&chart=%s"|args:$compte:$selected_chart}}
|
||
{{/if}}
|
||
|
||
{{* vérifier que le montant ne dépasse pas le solde restant *}}
|
||
{{:assign solde="%d-%d"|math:$ligne_immo.montant:$total_amort}}
|
||
{{if $_POST.montant|trim|money_int > $solde|intval}}
|
||
{{:assign solde=$solde|money_raw}}
|
||
{{:error message="Le montant indiqué « %s » dépasse le solde à amortir « %s »"|args:$_POST.montant:$solde}}
|
||
{{/if}}
|
||
|
||
{{* enregistrer l'écriture *}}
|
||
{{:assign
|
||
var="lines."
|
||
debit=$_POST.montant
|
||
account=$debit_account|keys|value:0
|
||
id_project=$ligne_immo.project_id
|
||
label=$_POST.designation
|
||
}}
|
||
{{:assign
|
||
var="lines."
|
||
credit=$_POST.montant
|
||
account=$credit_account|keys|value:0
|
||
id_project=$ligne_immo.project_id
|
||
label=$_POST.designation
|
||
}}
|
||
{{:api
|
||
method="POST"
|
||
path="accounting/transaction"
|
||
assign="result"
|
||
assign_code="result_code"
|
||
id_year=$_POST.id_year
|
||
type="advanced"
|
||
date=$_POST.date_amort
|
||
label=$_POST.designation
|
||
lines=$lines
|
||
linked_transactions=$ligne_immo.immo_trans_id|intval
|
||
}}
|
||
|
||
{{* enregistrer la liaison des lignes *}}
|
||
{{#select
|
||
trans.id AS trans_id,
|
||
line.id AS line_id
|
||
FROM acc_transactions AS trans
|
||
INNER JOIN acc_transactions_lines AS line ON line.id_transaction = trans.id
|
||
WHERE trans.id = :trans_id
|
||
;
|
||
:trans_id = $result.id
|
||
}}
|
||
{{:assign amort_trans_id=$trans_id}}
|
||
{{:assign amort_line_id=$line_id}}
|
||
{{/select}}
|
||
{{if $amort_trans_id != null}}
|
||
{{:save
|
||
key=""|uuid
|
||
type="amort_link"
|
||
immo_doc_id=$_GET.immo_doc_id|intval
|
||
amort_line_id=$amort_line_id|intval
|
||
}}
|
||
{{/if}}
|
||
|
||
{{:redirect force="amortization.html?immo_line_id=%s&immo_doc_id=%s&ok=1&msg=amortissement"|args:$_GET.immo_line_id:$_GET.immo_doc_id}}
|
||
{{/form}}
|
||
|
||
{{:admin_header title="Ajout amortissement" custom_css="./style.css" current="module_amortization"}}
|
||
|
||
{{* barre de navigation *}}
|
||
{{if ! $dialog}}
|
||
{{:include file="_nav.html" current="index" subcurrent="managed" subsubcurrent="amortization"}}
|
||
{{/if}}
|
||
|
||
{{:form_errors}}
|
||
|
||
{{:assign var=selected_year value=$years.0.id}}
|
||
{{:assign var=date_amort value=$years.0.end_date}}
|
||
{{:include file="_calcul_dates.html" date_debut=$date_debut date_fin=$date_amort keep="nbjours"}}
|
||
|
||
{{* montant de l'amortissement *}}
|
||
{{:assign montant_amort="%f/%f/365*%d"|math:$ligne_immo.montant:$duree:$nbjours|intval}}
|
||
{{:assign montant_amort="min(%f, %d-%d)"|math:$montant_amort:$ligne_immo.montant:$total_amort}}
|
||
{{:assign var="debit_account.6811" value="6811 — Dot. aux amortissements des immobilisations"}}
|
||
{{:assign var="credit_account.%s"|args:$code_amort value="%s — "|args:$code_amort|cat:$amort_account_label}}
|
||
|
||
{{* -------------------------------------------------------------------------------- *}}
|
||
|
||
<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="">
|
||
<fieldset class="ajout_amort">
|
||
<legend>Ajouter une écriture d'amortissement</legend>
|
||
<dl>
|
||
{{:input type="select" default=$selected_year name="id_year" label="Exercice" required=true default_empty="— Faire un choix —" options=$open_years}}
|
||
{{:input type="date" name="date_amort" label="Date" required=true default=$date_amort|date_short}}
|
||
{{:input type="text" name="designation" label="Libellé" required=true default=$amort_label size="50"}}
|
||
{{:input type="money" name="montant" label="Montant" required=true default=$montant_amort}}
|
||
{{:input
|
||
type="list"
|
||
name="debit_account"
|
||
label="Compte de débit"
|
||
required=true
|
||
target="!acc/charts/accounts/selector.php?codes=%s&id_year=%d"|args:"68*":$selected_year
|
||
default=$debit_account
|
||
}}
|
||
{{:input
|
||
type="list"
|
||
name="credit_account"
|
||
label="Compte d'amortissement (28xx)"
|
||
required=true
|
||
target="!acc/charts/accounts/selector.php?codes=%s&id_year=%d"|args:"28*":$selected_year
|
||
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=$total_amort}}
|
||
{{:input type="select" name="years_data" options=$years_data}}
|
||
</div>
|
||
</dl>
|
||
</fieldset>
|
||
|
||
<p class="submit">
|
||
{{:button type="submit" name="save" label="Enregistrer" shape="right" class="main"}}
|
||
</p>
|
||
</form>
|
||
{{:admin_footer}}
|
||
|
||
<script type="text/javascript" src="scripts.js"></script>
|
||
<script type="text/javascript">
|
||
|
||
/*
|
||
afficher la date de fin de l'exercice choisi
|
||
mettre à jour le numéro d'exercice dans les sélecteur de comptes
|
||
recalculer le montant de l'amortissement
|
||
*/
|
||
function redisplayData(evt,
|
||
f_immo = 'f_montant_immo',
|
||
f_duree = 'f_duree_amort',
|
||
f_amort = 'f_somme_amort',
|
||
f_years_data = 'f_years_data',
|
||
f_years_select = 'f_id_year',
|
||
f_date = 'f_date_amort',
|
||
f_montant = 'f_montant',
|
||
f_erreur = 'f_erreur'
|
||
)
|
||
{
|
||
setDateEnd(f_years_select, f_date, f_years_data);
|
||
setSelectorYear(['credit_account', 'debit_account'], f_years_select);
|
||
displayAmort(f_immo, f_duree, f_amort, f_years_data, f_years_select, f_montant, f_erreur);
|
||
}
|
||
|
||
function redisplayAmort(evt,
|
||
f_immo = 'f_montant_immo',
|
||
f_duree = 'f_duree_amort',
|
||
f_amort = 'f_somme_amort',
|
||
f_years_data = 'f_years_data',
|
||
f_years_select = 'f_id_year',
|
||
f_date = 'f_date_amort',
|
||
f_montant = 'f_montant',
|
||
f_erreur = 'f_erreur'
|
||
)
|
||
{
|
||
displayAmort(f_immo, f_duree, f_amort, f_years_data, f_years_select, f_montant, f_erreur, f_date);
|
||
}
|
||
|
||
(function () {
|
||
setDateEnd('f_id_year', 'f_date_amort', 'f_years_data');
|
||
document.getElementById('f_id_year').onchange = redisplayData;
|
||
document.getElementById('f_date_amort').onchange = redisplayAmort;
|
||
})();
|
||
</script>
|