Skip to content

Commit

Permalink
Fix: Multiple opened years causing mayhem
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorColomb committed Apr 14, 2024
1 parent a3ae0ab commit 52ba7c2
Show file tree
Hide file tree
Showing 13 changed files with 255 additions and 93 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@

## 📝 Roadmap

* ~~Accès aux immobilisations en lecture seule~~
* Amortissement la première année au prorata du nombre de mois, et non de jours
* Lien vers une immobilisation sur la page de ses écritures liées
* Checklist clôture de l'exercice (vérifier que tous les amortissements ont bien été passés)
* Amortissements dégressifs (peut être)
* Amortissements dégressifs _(peut être)_

## ⚖️ License

Expand Down
13 changes: 9 additions & 4 deletions modules/immobilisations/_current_year.tpl
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
{{if $logged_user.preferences.accounting_year}}
{{#years id=$logged_user.preferences.accounting_year assign="current_year"}}
{{#years}}
{{if $id == $logged_user.preferences.accounting_year}}
{{:assign .="current_year}}
{{:break}}
{{/if}}
{{/years}}
{{/if}}
{{if !$current_year}}
{{#years closed=false order="end_date DESC" assign="current_year"}}

{{#years closed=false order="end_date ASC" assign="current_year"}}
{{:break}}
{{else}}
{{#years closed=true order="end_date DESC" assign="current_year"}}
{{#years order="end_date ASC" assign="current_year"}}
{{:break}}
{{/years}}
{{/years}}
{{/if}}
13 changes: 8 additions & 5 deletions modules/immobilisations/_find_year.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
{{:error message="Fonction 'find_year' : paramètre date manquant !"}}
{{/if}}

{{#years where="start_date <= :date AND end_date >= :date" :date=$date}}
{{:assign amort_year_id=$id}}
{{:assign amort_year_label=$label}}
{{:assign amort_year_start=$start_date}}
{{:assign amort_year_end=$end_date}}
{{#years}}
{{if $start_date <= $date && $end_date >= $date}}
{{:assign amort_year_id=$id}}
{{:assign amort_year_label=$label}}
{{:assign amort_year_start=$start_date}}
{{:assign amort_year_end=$end_date}}
{{:break}}
{{/if}}
{{/years}}

{{if !$amort_year_end}}
Expand Down
7 changes: 5 additions & 2 deletions modules/immobilisations/_populate_immo.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@
{{/if}}
{{/foreach}}

{{#years id=$result.id_year}}
{{:assign var="year_label" value=$label}}
{{#years}}
{{if $id == $result.id_year}}
{{:assign var="year_label" value=$label}}
{{:break}}
{{/if}}
{{/years}}
{{:assign date=$result.date|parse_date}}
{{:assign
Expand Down
54 changes: 38 additions & 16 deletions modules/immobilisations/amortisation.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,46 @@
{{:assign var="_POST.entry" value=$key}}
{{/foreach}}

{{#transactions id=$_POST.entry}}
{{:api
path="accounting/transaction/"|cat:$_POST.entry
method="GET"
assign="result"
}}
{{#foreach from=$result.lines item="line"}}
{{if $line.account_code|regexp_match:"/^28[01]/" && $credit > 0}}
{{:assign amort_account=1}}
{{/if}}
{{/foreach}}

{{if !$amort_account}}
{{:error message="L'écriture doit contenir une ligne au crédit sur un compte 280 ou 281 !"}}
{{/if}}
{{else}}
{{:error message="L'écriture sélectionnée n'existe pas !"}}
{{/transactions}}

{{:assign var="immo.amort_entries."|cat:$_POST.entry value=$_POST.entry}}

{{:save validate_schema="./immobilisation.schema.json" key=$_POST.key amort_entries=$immo.amort_entries}}
{{:redirect to="./view.html?key=%s&linked"|args:$_POST.key}}
{{/form}}

{{#form on="save"}}
{{if !$_POST.key || !$_POST.year || !$_POST.label || !$_POST.date || !$_POST.amount || !$_POST.amort_account || !$_POST.debit_account}}
{{if !$_POST.key || !$_POST.year_id || !$_POST.label || !$_POST.date || !$_POST.amount || !$_POST.amort_account || !$_POST.debit_account}}
{{:http code=403}}
{{:error message="Paramètre key, year, label, date, amount, amort_account ou debit_account manquant !"}}
{{:error message="Paramètre key, year_id, label, date, amount, amort_account ou debit_account manquant !"}}
{{/if}}

{{if $_POST.year_start && $_POST.year_end}}
{{:assign date=$_POST.date|parse_date}}
{{if $date < $_POST.year_start || $date > $_POST.year_end}}
{{:error message="La date demandée ne correspond pas aux dates de l'exercice sélectionné !"}}
{{:assign date=$_POST.date|parse_date}}
{{#years}}
{{if $id == $_POST.year_id}}
{{if $date < $start_date || $date > $end_date}}
{{:error message="La date demandée ne correspond pas aux dates de l'exercice sélectionné !"}}
{{/if}}
{{:break}}
{{/if}}
{{/if}}
{{/years}}

{{#foreach from=$_POST.amort_account key="key" item="value"}}
{{if !$value|regexp_match:"/^28[01]/"}}
Expand Down Expand Up @@ -127,9 +149,6 @@ <h3 class="warning">Délier l'écriture d'amortissement pour l'exercice {{$year.
{{:input type="list" name="entry" multiple=false required=true label="Écriture d'amortissement" target="!acc/transactions/selector.php"}}
</dl>
</fieldset>
<p class="help">
Attention, aucune validation n'est effectuée sur l'écriture choisie.
</p>
<p class="submit">
{{:button type="submit" name="link" shape="right" label="Lier" class="main"}}
</p>
Expand All @@ -142,7 +161,7 @@ <h3 class="warning">Délier l'écriture d'amortissement pour l'exercice {{$year.
{{/if}}
{{:include file="./_count_years.tpl" date=$immo.date duration=$immo.duration keep="number_of_years"}}

{{if $immo.date < $current_year.start_date}}
{{if $immo.date > $current_year.start_date}}
{{:error message="L'exercice sélectionné est antérieur à l'immobilisation !"}}
{{/if}}

Expand All @@ -169,21 +188,24 @@ <h3 class="warning">Délier l'écriture d'amortissement pour l'exercice {{$year.
{{:assign amort_account=$immo.account_code|substr:1}}
{{:assign amort_account="28"|cat:$amort_account}}
{{#accounts codes=$amort_account}}
{{:assign var="amort_account.%d"|args:$id value="%s — %s"|args:$code:$label}}
{{if $id_chart == $current_year.id_chart}}
{{:assign var="amort_account.%d"|args:$id value="%s — %s"|args:$code:$label}}
{{:break}}
{{/if}}
{{else}}
{{:assign amort_account=null}}
{{/accounts}}
{{#accounts codes="6811"}}
{{:assign var="debit_account.%d"|args:$id value="%s — %s"|args:$code:$label}}
{{if $id_chart == $current_year.id_chart}}
{{:assign var="debit_account.%d"|args:$id value="%s — %s"|args:$code:$label}}
{{/if}}
{{/accounts}}

<fieldset>
<legend>Créer une écriture d'amortissement</legend>

<dl>
{{:input type="hidden" name="year" default=$current_year.id}}
{{:input type="hidden" name="year_start" default=$current_year.start_date}}
{{:input type="hidden" name="year_end" default=$current_year.end_date}}
{{:input type="hidden" name="year_id" default=$current_year.id}}
<dt>
<label>Exercice sélectionné</label>
</dt>
Expand Down
3 changes: 3 additions & 0 deletions modules/immobilisations/delete.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{{#restrict block=true section="accounting" level="write"}}
{{/restrict}}

{{#form on="delete"}}
{{:delete type="immobilisation" key=$_POST.key}}
{{:redirect to="./"}}
Expand Down
32 changes: 20 additions & 12 deletions modules/immobilisations/index.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{{:admin_header title="Immobilisations" current="home"}}

<nav class="tabs">
<aside>
{{:linkbutton href="link.html" label="Lier une écriture" shape="attach"}}
{{:linkbutton href="new.html" label="Nouvelle immobilisation" shape="plus"}}
</aside>
{{#restrict section="accounting" level="write"}}
<aside>
{{:linkbutton href="link.html" label="Lier une écriture" shape="attach"}}
{{:linkbutton href="new.html" label="Nouvelle immobilisation" shape="plus"}}
</aside>
{{/restrict}}

<ul>
<li{{if $_GET.status === 'all'}} class="current"{{/if}}><a href="./?status=all">Toutes les immobilisations</a></li>
Expand All @@ -25,7 +27,9 @@
<td>Durée d'immobilisation</td>
<td class="money">Montant</td>
<td class="money">VNC</td>
<td></td>
{{#restrict section="accounting" level="write"}}
<td></td>
{{/restrict}}
</thead>
<tbody>
{{#load
Expand All @@ -44,10 +48,12 @@
<td colspan="4" style="text-align: center;">
<i>Écriture d'immobilisation non trouvée !</i>
</td>
<td colspan="2" class="actions">
{{:linkbutton shape="edit" label="Modifier" href="./new.html?key="|cat:$key}}
{{:linkbutton shape="delete" label="Supprimer" href="./delete.html?key="|cat:$key}}
</td>
{{#restrict section="accounting" level="write"}}
<td colspan="2" class="actions">
{{:linkbutton shape="edit" label="Modifier" href="./new.html?key="|cat:$key}}
{{:linkbutton shape="delete" label="Supprimer" href="./delete.html?key="|cat:$key}}
</td>
{{/restrict}}
{{else}}
<td class="num">{{:link href="./view.html?key=%s"|args:$immo.key label=$immo.number}}</td>
<th>{{$immo.label}}</th>
Expand All @@ -59,9 +65,11 @@
{{/if}}
<td class="money">{{$immo.amount|money_currency}}</td>
<td class="money">{{$immo.vnc|money_currency}}</td>
<td class="actions">
{{:linkbutton shape="menu" label="Ouvrir" href="./view.html?key="|cat:$key}}
</td>
{{#restrict section="accounting" level="write"}}
<td class="actions">
{{:linkbutton shape="menu" label="Ouvrir" href="./view.html?key="|cat:$key}}
</td>
{{/restrict}}
{{/if}}
</tr>
{{/load}}
Expand Down
3 changes: 3 additions & 0 deletions modules/immobilisations/link.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{{#restrict block=true section="accounting" level="write"}}
{{/restrict}}

{{#form on="link"}}
{{#foreach from=$_POST.entry key="key"}}
{{:assign var="_POST.entry" value=$key}}
Expand Down
2 changes: 1 addition & 1 deletion modules/immobilisations/module.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ author=Victor Colomb
menu=true
home_button=true
restrict_section=accounting
restrict_level=write
restrict_level=read
54 changes: 45 additions & 9 deletions modules/immobilisations/new.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
{{#restrict block=true section="accounting" level="write"}}
{{/restrict}}

{{#form on="save"}}
{{if !$_POST.date || !$_POST.year_id || !$_POST.account || !$_POST.third_party_account || !$_POST.amount}}
{{:http code=403}}
{{:error message="Le paramètre date, year_id, account, third_party_account ou amount est manquant !"}}
{{/if}}

{{if $_POST.key}}
{{:assign key=$_POST.key}}
{{:assign new_number=$_POST.number|intval}}
Expand All @@ -9,6 +17,16 @@
{{:assign key=""|uuid}}
{{/if}}

{{:assign var="_POST.date" value=$_POST.date|parse_date}}
{{#years}}
{{if $id == $_POST.year_id}}
{{if $start_date > $_POST.date || $end_date < $_POST.date}}
{{:error message="La date demandée ne correspond pas à l'exercice sélectionné !"}}
{{/if}}
{{:break}}
{{/if}}
{{/years}}

{{#foreach from=$_POST.account key="key" item="value"}}
{{:assign var="_POST.account" value=$key}}
{{if !$value|regexp_match:"/^2[01]/"}}
Expand All @@ -33,12 +51,6 @@
{{:assign path="accounting/transaction/%d"|args:$_POST.entry}}
{{else}}
{{:assign path="accounting/transaction"}}

{{#years closed=false where="start_date <= :date AND :date <= end_date" :date=$_POST.date|parse_date}}
{{:assign var=_POST.year_id value=$id}}
{{else}}
{{:error message="Aucun exercice ouvert ne correspond à la date désirée !"}}
{{/years}}
{{/if}}
{{:api
path=$path
Expand Down Expand Up @@ -104,12 +116,33 @@
{{:input type="hidden" name="entry" source=$immo}}
{{:input type="hidden" name="year_id" source=$immo}}
{{/if}}

{{#years}}
{{if $id == $immo.year_id}}
{{:assign .="current_year"}}
{{:break}}
{{/if}}
{{/years}}
{{else}}
{{:include file="./_current_year.tpl" keep="current_year"}}
{{:input type="hidden" name="year_id" default=$current_year.id}}
{{/if}}

<fieldset>
<legend>Identification de l'immobilisation</legend>

<dl>
<dt>
<label>Exercice sélectionné</label>
</dt>
<dd style="margin-bottom: 1em;">
<input type="text" value="{{$current_year.label}}" disabled>
{{if !$immo}}
{{:assign from_url=$request_url|urlencode}}
{{:linkbutton href="!acc/years/select.php?from="|cat:$from_url shape="settings" label="Changer d'exercice"}}
{{/if}}
</dd>

{{:input type="text" name="label" label="Objet" required=true source=$immo}}
{{:input type="textarea" name="description" label="Description" source=$immo help="On peut ici enregistrer un numéro de série ou décrire la composition de l'immobilisation dans le cas d'un lot."}}
{{:input type="date" name="date" label="Date d'entrée en service" required=true source=$immo help="Par défaut, rentrer la date d'acquisition."}}
Expand All @@ -123,12 +156,15 @@

<dl>
{{#accounts codes="404"}}
{{:assign var="third_party_account.%d"|args:$id value="%s — %s"|args:$code:$label}}
{{if $current_year.id_chart == $id_chart}}
{{:assign var="third_party_account.%d"|args:$id value="%s — %s"|args:$code:$label}}
{{:break}}
{{/if}}
{{/accounts}}

{{:input type="text" name="entry_label" label="Libellé de l'écriture" source=$immo help="Laisser vide pour utiliser l'objet de l'immobilisation comme libellé."}}
{{:input type="list" name="account" multiple=false label="Compte d'immobilisation" required=true source=$immo target="!acc/charts/accounts/selector.php"}}
{{:input type="list" name="third_party_account" multiple=false label="Compte de tiers" required=true source=$immo default=$third_party_account target="!acc/charts/accounts/selector.php"}}
{{:input type="list" name="account" multiple=false label="Compte d'immobilisation" required=true source=$immo target="!acc/charts/accounts/selector.php?year="|cat:$current_year.id}}
{{:input type="list" name="third_party_account" multiple=false label="Compte de tiers" required=true source=$immo default=$third_party_account target="!acc/charts/accounts/selector.php?year="|cat:$current_year.id}}
</dl>
<p class="help">Vous pourrez par la suite modifier l'écriture pour saisir une référence, joindre des documents, etc.</p>
</fieldset>
Expand Down
12 changes: 7 additions & 5 deletions modules/immobilisations/test.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{{:admin_header title="Test"}}

{{:assign entry=143}}
{{:include file="./_current_year.tpl" keep="current_year"}}
{{:debug year=$current_year}}

{{#transactions id=$entry assign="result"}}
{{:debug result=$result}}
{{/transactions}}
{{#accounts codes="404"}}
{{:assign .="account"}}
{{:debug account=$account}}
{{/accounts}}


{{:admin_footer}}
{{:admin_footer}}
Loading

0 comments on commit 52ba7c2

Please sign in to comment.