{{* -*- brindille -*- *}}
{{:admin_header title="Gestion des amortissements" custom_css=$custom_css current="module_amortissement"}}

{{* barre de navigation *}}
{{:include file="_nav.html" current="index"}}

{{if $_GET.ok}}
	{{if $_GET.msg|match:"immobilisation"}}
		{{:assign msg="Immobilisation enregistrée"}}
	{{elseif $_GET.msg|match:"durée"}}
		{{:assign msg="Durée enregistrée"}}
	{{/if}}
	<p class="block confirm">{{$msg}}</p>
{{elseif $_GET.err}}
	<p class="block error">Échec enregistrement immobilisation</p>
{{/if}}

{{* supprimer les documents sans écriture associée *}}
{{#load type="immo"}}
	{{#select
		line.id as line_id
		FROM acc_transactions_lines as line
		INNER JOIN acc_transactions AS trans on line.id_transaction = trans.id
		WHERE line.id = :line_id;
		:line_id = $line
		}}
	{{else}}
		{{:delete key=$key}}
	{{/select}}
{{/load}}

<section class="immobilisation">
	<h2 class="ruler">Liste des immobilisations</h2>

	<table class="list">
		<thead>
			<tr>
				<th>N°</th>
				<th>Date</th>
				<th>Libellé</th>
				<th>Montant</th>
				<th>Durée</th>
				<th>N° compte</th>
				<th>Compte</th>
				<th>Projet</th>
				<th class="actions"></th>
			</tr>
		</thead>
		<tbody>

			{{* lister les immobilisations *}}
			{{:read file="./defaut.json" assign="config_json"}}
			{{:assign config_defaut=$config_json|json_decode}}
			{{:assign var="prefix_array" value=$config_defaut.prefixes|keys}}
			{{:assign quote="'"}}
			{{:assign condition="("}}
			{{#foreach from=$prefix_array item="code"}}
				{{:assign condition=$condition|cat:" account.code LIKE "|cat:$quote|cat:$code|cat:"%"|cat:$quote|cat:" OR "}}
			{{/foreach}}
			{{:assign condition=$condition|cat:"0)"}}

			{{#select
				trans.id as trans_id,
				trans.label as trans_label,
				trans.date as trans_date,
				line.id as immo_id,
				account.id as account_id,
				account.code as account_code,
				account.label as account_label,
				line.debit AS debit,
				project.label as project_label,
				trans.id_year as trans_id_year
				FROM acc_transactions AS trans
				INNER JOIN acc_transactions_lines AS line ON line.id_transaction = trans.id
				INNER JOIN acc_accounts AS account ON line.id_account = account.id
				INNER JOIN acc_years AS years ON trans.id_year = years.id
				LEFT JOIN acc_projects AS project ON line.id_project = project.id
				WHERE !condition
				ORDER BY trans.date DESC;
				!condition=$condition
			}}
				{{:assign trans_url="%s/acc/transactions/details.php?id=%s"|args:$admin_url:$trans_id}}
				{{:assign compte_url="%s/acc/accounts/journal.php?id=%s&year=%s"|args:$admin_url:$account_id:$trans_id_year}}
				{{#load type="immo" where="$$.line = :line_id" :line_id=$immo_id|intval}}
					{{:assign duration=$duration}}
				{{else}}
					{{:assign duration=null}}
				{{/load}}

				<tr>
					<td class="num"><a href={{$trans_url}}>#{{$trans_id}}</a></td>
					<td>{{$trans_date|date_short}}</td>
					<td>{{$trans_label}}</td>
					<td class="money">{{"%f"|math:$debit|money}}</td>
					<td class="num">{{if $duration != null}}{{$duration}}{{/if}}</td>
					<td><a href={{$compte_url}}>{{$account_code}}</a></td>
					<td>{{$account_label}}</td>
					<td>{{$project_label}}</td>
					<td class="actions">
						{{if $duration == null}}
							{{:linkbutton
								label="Ajouter infos"
								href="add_duration.html?immo_id=%s"|args:$immo_id
								shape="help"
								target="_dialog"
							}}
						{{else}}
							{{:linkbutton
								label="Amortissements"
								href="amortization.html?immo_id=%s"|args:$immo_id
								shape="table"
							}}
						{{/if}}
					</td>
				</tr>
			{{/select}}
		</tbody>
	</table>
</section>

<form method="post" action="">
	<fieldset>
		<legend>Ajouter une immobilisation</legend>
		<p class="submit">
			{{:linkbutton
				label="Ajouter une immobilisation"
				shape="plus"
				href="add_asset.html"
				target="_dialog"
			}}
		</p>
	</fieldset>
</form>

{{:admin_footer}}