Mutualisation calcul des entrées/sorties à une date donnée

This commit is contained in:
Jean-Christophe Engel 2023-11-20 11:18:50 +01:00
parent 2376a2d648
commit 0b356b6617
5 changed files with 158 additions and 210 deletions

134
_calcul_dispo.html Normal file
View File

@ -0,0 +1,134 @@
{{*
Calcul des entrées/sorties de matériels à une date donnée
paramètres :
- liste de catégories
- date
*}}
{{* liste des catégories *}}
{{if $categories === null}}
{{#select
key
FROM module_data_equipment as cat
WHERE json_extract(cat.document, '$.type') = 'category'}}
{{:assign var="categories." value=$key}}
{{/select}}
{{/if}}
{{* date *}}
{{if $date === null}}
{{:assign date=$now|date:"Y-m-d"}}
{{/if}}
{{* :debug date=$date *}}
{{* Extraire et compiler les infos de la base *}}
{{#select
json_extract(mvt.document, '$.date') as 'date',
eqpmt.key as 'eqpmt_key',
json_extract(eqpmt.document, '$.designation') as 'designation',
cat.key as 'cat_key',
json_extract(cat.document, '$.name') as 'category',
mvt.key as 'mvt_key',
json_extract(mvt.document, '$.direction') as 'direction',
CASE json_extract(mvt.document, '$.direction')
WHEN 'entrée' THEN json_extract(mvt.document, '$.inputNature')
WHEN 'sortie' THEN json_extract(mvt.document, '$.outputNature')
END
AS 'operation',
json_extract(mvt.document, '$.amount') AS 'nombre'
FROM module_data_equipment as eqpmt
INNER JOIN module_data_equipment as cat
ON json_extract(eqpmt.document, '$.category') = cat.key
INNER JOIN module_data_equipment as mvt
ON json_extract(mvt.document, '$.equipment') = eqpmt.key
WHERE json_extract(eqpmt.document, '$.type') = "equipment"
AND !categories
AND json_extract(mvt.document, '$.date') <= :date
ORDER BY
json_extract(cat.document, '$.name'),
json_extract(eqpmt.document, '$.designation'),
json_extract(mvt.document, '$.date');
!categories='cat_key'|sql_where:'IN':$categories
:date=$date
}}
{{:assign
var="equipments.%s.name"|args:$cat_key
value=$category}}
{{:assign
var="equipments.%s.eqpmt.%s.designation"|args:$cat_key:$eqpmt_key
value=$designation}}
{{* matériel propriété de l'asso en stock *}}
{{:assign
var="stock"
from="equipments.%s.eqpmt.%s.stock"|args:$cat_key:$eqpmt_key}}
{{if $stock === null}}
{{:assign stock=0}}
{{/if}}
{{* matériel propriété de l'asso à l'extérieur *}}
{{:assign
var="exterieur"
from="equipments.%s.eqpmt.%s.exterieur"|args:$cat_key:$eqpmt_key}}
{{if $exterieur === null}}
{{:assign exterieur=0}}
{{/if}}
{{* matériel non propriété de l'asso *}}
{{:assign
var="nonproprio"
from="equipments.%s.eqpmt.%s.nonproprio"|args:$cat_key:$eqpmt_key}}
{{if $nonproprio === null}}
{{:assign nonproprio=0}}
{{/if}}
{{if $direction === 'entrée'}}
{{* chercher le type d'entrée parmi les types de la config *}}
{{#foreach from=$module.config.inputNature item="elem"}}
{{if $operation == $elem.label}}
{{if $elem.type == 'définitif'}}
{{:assign stock="%d+%d"|math:$stock:$nombre}}
{{:assign
var="equipments.%s.eqpmt.%s.stock"|args:$cat_key:$eqpmt_key
from=stock}}
{{elseif $elem.type == 'retour'}}
{{:assign exterieur="%d-%d"|math:$exterieur:$nombre}}
{{:assign
var="equipments.%s.eqpmt.%s.exterieur"|args:$cat_key:$eqpmt_key
from=exterieur}}
{{elseif $elem.type == 'temporaire'}}
{{:assign nonproprio="%d+%d"|math:$nonproprio:$nombre}}
{{:assign
var="equipments.%s.eqpmt.%s.nonproprio"|args:$cat_key:$eqpmt_key
from=nonproprio}}
{{/if}}
{{:break}}
{{/if}}
{{/foreach}}
{{elseif $direction === 'sortie'}}
{{* chercher le type de sortie parmi les types de la config *}}
{{#foreach from=$module.config.outputNature item="elem"}}
{{if $operation == $elem.label}}
{{if $elem.type == 'définitif'}}
{{:assign stock="%d-%d"|math:$stock:$nombre}}
{{:assign
var="equipments.%s.eqpmt.%s.stock"|args:$cat_key:$eqpmt_key
from=stock}}
{{elseif $elem.type == 'temporaire'}}
{{:assign exterieur="%d+%d"|math:$exterieur:$nombre}}
{{:assign
var="equipments.%s.eqpmt.%s.exterieur"|args:$cat_key:$eqpmt_key
from=exterieur}}
{{elseif $elem.type == 'retour'}}
{{:assign nonproprio="%d-%d"|math:nonproprio:$nombre}}
{{:assign
var="equipments.%s.eqpmt.%s.nonproprio"|args:$cat_key:$eqpmt_key
from=nonproprio}}
{{/if}}
{{:break}}
{{/if}}
{{/foreach}}
{{/if}}
{{/select}}

View File

@ -3,14 +3,6 @@
{{* barre de navigation *}}
{{:include file="/%s/_nav.html"|args:$module.name current="index"}}
{{* liste des catégories *}}
{{#select
key
FROM module_data_equipment as cat
WHERE json_extract(cat.document, '$.type') = 'category'}}
{{:assign var="categories." value=$key}}
{{/select}}
{{:include file="./inventaire.html" print_cat_name=true}}
{{:admin_footer}}

View File

@ -1,115 +1,7 @@
{{* inventaire des entrées/sorties des matériels des catégories de la variable $categories *}}
{{:assign base_path="/m/%s"|args:$module.name}}
{{* Extraire et compiler les infos de la base *}}
{{#select
json_extract(mvt.document, '$.date') as 'date',
eqpmt.key as 'eqpmt_key',
json_extract(eqpmt.document, '$.designation') as 'designation',
cat.key as 'cat_key',
json_extract(cat.document, '$.name') as 'category',
mvt.key as 'mvt_key',
json_extract(mvt.document, '$.direction') as 'direction',
CASE json_extract(mvt.document, '$.direction')
WHEN 'entrée' THEN json_extract(mvt.document, '$.inputNature')
WHEN 'sortie' THEN json_extract(mvt.document, '$.outputNature')
END
AS 'operation',
json_extract(mvt.document, '$.amount') AS 'nombre'
FROM module_data_equipment as eqpmt
INNER JOIN module_data_equipment as cat
ON json_extract(eqpmt.document, '$.category') = cat.key
INNER JOIN module_data_equipment as mvt
ON json_extract(mvt.document, '$.equipment') = eqpmt.key
WHERE json_extract(eqpmt.document, '$.type') = "equipment"
AND !categories
ORDER BY
json_extract(cat.document, '$.name'),
json_extract(eqpmt.document, '$.designation'),
json_extract(mvt.document, '$.date');
!categories='cat_key'|sql_where:'IN':$categories
}}
{{:assign
var="equipments.%s.name"|args:$cat_key
value=$category}}
{{:assign
var="equipments.%s.eqpmt.%s.designation"|args:$cat_key:$eqpmt_key
value=$designation}}
{{* matériel propriété de l'asso en stock *}}
{{:assign
var="stock"
from="equipments.%s.eqpmt.%s.stock"|args:$cat_key:$eqpmt_key}}
{{if $stock === null}}
{{:assign stock=0}}
{{/if}}
{{* matériel propriété de l'asso à l'extérieur *}}
{{:assign
var="exterieur"
from="equipments.%s.eqpmt.%s.exterieur"|args:$cat_key:$eqpmt_key}}
{{if $exterieur === null}}
{{:assign exterieur=0}}
{{/if}}
{{* matériel non propriété de l'asso *}}
{{:assign
var="nonproprio"
from="equipments.%s.eqpmt.%s.nonproprio"|args:$cat_key:$eqpmt_key}}
{{if $nonproprio === null}}
{{:assign nonproprio=0}}
{{/if}}
{{if $direction === 'entrée'}}
{{* chercher le type d'entrée parmi les types de la config *}}
{{#foreach from=$module.config.inputNature item="elem"}}
{{if $operation == $elem.label}}
{{if $elem.type == 'définitif'}}
{{:assign stock="%d+%d"|math:$stock:$nombre}}
{{:assign
var="equipments.%s.eqpmt.%s.stock"|args:$cat_key:$eqpmt_key
from=stock}}
{{elseif $elem.type == 'retour'}}
{{:assign exterieur="%d-%d"|math:$exterieur:$nombre}}
{{:assign
var="equipments.%s.eqpmt.%s.exterieur"|args:$cat_key:$eqpmt_key
from=exterieur}}
{{elseif $elem.type == 'temporaire'}}
{{:assign nonproprio="%d+%d"|math:$nonproprio:$nombre}}
{{:assign
var="equipments.%s.eqpmt.%s.nonproprio"|args:$cat_key:$eqpmt_key
from=nonproprio}}
{{/if}}
{{:break}}
{{/if}}
{{/foreach}}
{{elseif $direction === 'sortie'}}
{{* chercher le type de sortie parmi les types de la config *}}
{{#foreach from=$module.config.outputNature item="elem"}}
{{if $operation == $elem.label}}
{{if $elem.type == 'définitif'}}
{{:assign stock="%d-%d"|math:$stock:$nombre}}
{{:assign
var="equipments.%s.eqpmt.%s.stock"|args:$cat_key:$eqpmt_key
from=stock}}
{{elseif $elem.type == 'temporaire'}}
{{:assign exterieur="%d+%d"|math:$exterieur:$nombre}}
{{:assign
var="equipments.%s.eqpmt.%s.exterieur"|args:$cat_key:$eqpmt_key
from=exterieur}}
{{elseif $elem.type == 'retour'}}
{{:assign nonproprio="%d-%d"|math:nonproprio:$nombre}}
{{:assign
var="equipments.%s.eqpmt.%s.nonproprio"|args:$cat_key:$eqpmt_key
from=nonproprio}}
{{/if}}
{{:break}}
{{/if}}
{{/foreach}}
{{/if}}
{{/select}}
{{:include file="./_calcul_dispo.html" keep="equipments"}}
{{* Afficher les résultats *}}
{{if $equipments === null}}
@ -152,6 +44,7 @@
</thead>
<tbody>
{{:assign base_path="/m/%s"|args:$module.name}}
{{* itérer sur les matériels de la catégorie *}}
{{#foreach from=$category.eqpmt key="eqpmt_key" item="eqpmt"}}

View File

@ -1,9 +0,0 @@
{{* cumuler les entrées et les sorties de ce matériel *}}
{{:assign var="dispo" value=0}}
{{#load type="movement" where="$$.equipment = :eqpmt_key" :eqpmt_key=$equipment.key assign="movement"}}
{{if $movement.direction === 'entrée'}}
{{:assign dispo="%d+%d"|math:$dispo:$movement.amount}}
{{else}}
{{:assign dispo="%d-%d"|math:$dispo:$movement.amount}}
{{/if}}
{{/load}}

View File

@ -11,7 +11,7 @@
{{* types de sorties *}}
{{#foreach from=$module.config.outputNature item="elem"}}
{{:assign var='types_sorties.' value="%s"|args:$elem.label}}
{{:assign var='types_sorties.' value="%s"|args:$elem.label}}
{{/foreach}}
{{* Traiter l'envoi du formulaire *}}
@ -19,48 +19,24 @@
{{* récupérer les infos du matériel *}}
{{#load type="equipment" key=$_POST.equipment assign="equipment"}}
{{:assign designation=$equipment.designation}}
{{:assign var="categories." value=$equipment.category}}
{{/load}}
{{:assign date=$_POST.date|parse_date}}
{{* Extraire et compiler les infos de la base *}}
{{:include file="/%s/_calcul_dispo.html"|args:$module.name keep="equipments"}}
{{* déterminer la quantité disponible de ce matériel à la date donnée *}}
{{:assign var="dispo" value=0}}
{{#load
type="movement"
where="$$.equipment = :eqpmt_key AND $$.date <= :date"
:eqpmt_key=$equipment.key
:date=$_POST.date|parse_date
assign="movement"
}}
{{if $movement.direction === 'entrée'}}
{{* chercher le type d'entrée parmi les types de la config *}}
{{#foreach from=$module.config.inputNature item="elem"}}
{{if $movement.inputNature == $elem.label}}
{{if $elem.type == 'définitif' || $elem.type == 'retour'}}
{{:assign dispo="%d+%d"|math:$dispo:$movement.amount}}
{{/if}}
{{:break}}
{{/if}}
{{/foreach}}
{{else}}
{{* chercher le type d'entrée parmi les types de la config *}}
{{#foreach from=$module.config.outputNature item="elem"}}
{{if $movement.outputNature == $elem.label}}
{{if $elem.type == 'définitif' || $elem.type == 'temporaire'}}
{{:assign dispo="%d-%d"|math:$dispo:$movement.amount}}
{{/if}}
{{:break}}
{{/if}}
{{/foreach}}
{{/if}}
{{else}}
{{:error message="Erreur : il n'y a aucun mouvement pour le matériel « %s » avant la date du %s."|args:$equipment.designation:$_POST.date}}
{{/load}}
{{:assign var=stock from="equipments.%s.eqpmt.%s.stock"|args:$equipment.category:$_POST.equipment}}
{{:assign var=exterieur from="equipments.%s.eqpmt.%s.exterieur"|args:$equipment.category:$_POST.equipment}}
{{:assign dispo="%d-%d"|math:$stock:$exterieur}}
{{if $_POST.quantite|intval > $dispo}}
{{:error message="Erreur : la quantité indiquée (%s) est supérieure à celle disponible (%d) à la date du %s"|args:$_POST.quantite:$dispo:$_POST.date}}
{{/if}}
{{* Enregistrer le mouvement *}}
{{:assign mvt_key=""|uuid}}
{{:assign var="operation" from="types_sorties.%d|args:$_POST.type_operation}}
{{:save
@ -79,59 +55,21 @@
{{:form_errors}}
{{/form}}
{{* itérer sur les catégories *}}
{{#load type="category" assign="category" order="$$.name"}}
{{:assign nom_cat=$category.name}}
{{* Extraire et compiler les infos de la base *}}
{{:include file="/%s/_calcul_dispo.html"|args:$module.name keep="equipments"}}
{{* lister les matériels de la catégorie *}}
{{#load type="equipment" where="$$.category = :key" :key=$category.key assign="equipment" order="$$.designation"}}
{{:assign designation=$equipment.designation}}
{{* déterminer la quantité disponible de ce matériel,
ie propriété de l'asso et pas en prêt à la date du jour *}}
{{:assign var="dispo" value=0}}
{{#load
type="movement"
where="$$.equipment = :eqpmt_key AND $$.date <= :date"
:eqpmt_key=$equipment.key
:date=$now|date:"Y-m-d"
assign="movement"
{{* calculer les disponibilités *}}
{{#foreach from=$equipments key="cat_key" item="category"}}
{{#foreach from=$category.eqpmt key="eqpmt_key" item="eqpmt"}}
{{:assign dispo="%d-%d"|math:$eqpmt.stock:$eqpmt.exterieur}}
{{:assign
var="disponibilites.%s.%s"|args:$category.name:$eqpmt_key
value="%s (dispo : %d)"|args:$eqpmt.designation:$dispo
}}
{{if $movement.direction === 'entrée'}}
{{* chercher le type d'entrée parmi les types de la config *}}
{{#foreach from=$module.config.inputNature item="elem"}}
{{if $movement.inputNature == $elem.label}}
{{if $elem.type == 'définitif' || $elem.type == 'retour'}}
{{:assign dispo="%d+%d"|math:$dispo:$movement.amount}}
{{/if}}
{{:break}}
{{/if}}
{{/foreach}}
{{else}}
{{* chercher le type d'entrée parmi les types de la config *}}
{{#foreach from=$module.config.outputNature item="elem"}}
{{if $movement.outputNature == $elem.label}}
{{if $elem.type == 'définitif' || $elem.type == 'temporaire'}}
{{:assign dispo="%d-%d"|math:$dispo:$movement.amount}}
{{/if}}
{{:break}}
{{/if}}
{{/foreach}}
{{/if}}
{{else}}
<p class="block alert">Il n'y a aucun movement dans cette période.</p>
{{/load}}
{{if $dispo > 0}}
{{:assign var="equipments.%s.%s"|args:$nom_cat:$equipment.key value="%s (dispo : %d)"|args:$designation:$dispo}}
{{/if}}
{{/load}}
{{/load}}
{{/foreach}}
{{/foreach}}
{{* formulaire de sortie de matériel *}}
<form method="post" action="">
<fieldset class="sortie">
@ -142,7 +80,7 @@
<fieldset>
<legend>Matériel</legend>
<p><span class="alert">La quantité disponible est celle à la date du jour</span></p>
{{:input type="select_groups" name="equipment" label="Matériel" required=true options=$equipments onchange="fixerValeurMax('f_equipment', 'f_quantite')"}}
{{:input type="select_groups" name="equipment" label="Matériel" required=true options=$disponibilites onchange="fixerValeurMax('f_equipment', 'f_quantite')"}}
{{:input type="textarea" name="remarques" label="Remarques" cols="40" rows="3" required=false}}
</fieldset>
</fieldset>