ok for available equipments output in movement

This commit is contained in:
JBthePenguin 2021-09-02 11:09:00 +02:00
parent 964806a0c0
commit 98cbe7f4a8
9 changed files with 186 additions and 72 deletions

View File

@ -135,23 +135,28 @@ class Equipment
}
public function CalculateAvailable($id)
// return the number of available for a specific equipment
{
$entries = DB::getInstance()->firstColumn(
"SELECT sum(equipment_number) FROM plugin_materiels_entry WHERE kind IN (
"SELECT sum(equipment_number) FROM plugin_materiels_movement WHERE side = '0' AND kind IN (
'Achat', 'Don', 'Récupération',
'Retour de location / prêt') AND equipment_id = ?;", $id);
$outputs = DB::getInstance()->firstColumn(
"SELECT sum(equipment_number) FROM plugin_materiels_output WHERE kind IN (
"SELECT sum(equipment_number) FROM plugin_materiels_movement WHERE side = '1' AND kind IN (
'Vente', 'Don', 'Besoin', 'Autre (perte, vol, ...)',
'Location / Prêt') AND equipment_id = ?;", $id);
return $entries - $outputs;
}
public function ListAllAvailableByCategory()
// return list of all available equipments ordered by category
{
// get list of all equipments ordered by category
$eqmts_by_cat = $this->listAllByCategory();
// construct list of all available equipments ordered by category
$eqmts_available_by_cat = array();
foreach ($eqmts_by_cat as $cat => $eqmts) {
// for each category construct list of his available equipments
$eqmts_available = array();
foreach ($eqmts as $eqmt) {
$available = $this->CalculateAvailable($eqmt->id);

View File

@ -36,6 +36,18 @@ class Movement
);
}
public function listOutputKinds()
// return list of output's kinds
{
return array(
'Vente',
'Don',
'Besoin',
'Autre (perte, vol, ...)',
'Location / Prêt',
);
}
public function listAllOneSide($side)
// return all entries if side is 0 or all outputs if side is 1 ordered by date
{

View File

@ -2,7 +2,7 @@
{include file="%s_nav.tpl"|args:$plugin_tpl current_nav="entrees"}
<!-- -->
<!-- add entry form -->
{include file="%smouvements/formulaire_mouvement.tpl"|args:$plugin_tpl legend="entrée d'un matériel %s"|args:$legend_part tpl_materiel_name=$tpl_materiel_name kinds=$kinds selected_kind=$selected_kind default_date=$default_date label_date="Date d'entrée" default_number=$default_number tpl_materiel_path="entrees" default_comment=$default_comment comment_placeholder="ex: don reçu de la part de..." csrf_key=$csrf_key cancel_link=$cancel_link}
{include file="%smouvements/formulaire_mouvement.tpl"|args:$plugin_tpl legend="entrée d'un matériel %s"|args:$legend_part tpl_materiel_name=$tpl_materiel_name kinds=$kinds selected_kind=$selected_kind default_date=$default_date label_date="Date d'entrée" default_number=$default_number tpl_materiel_path="entrees" default_comment=$default_comment comment_placeholder=$comment_placeholder csrf_key=$csrf_key cancel_link=$cancel_link}
<!-- -->
<!-- footer -->
{include file="admin/_foot.tpl"}

View File

@ -0,0 +1,9 @@
<!-- nav bar -->
{include file="%s_nav.tpl"|args:$plugin_tpl current_nav="sorties"}
<!-- -->
<!-- add output form -->
{include file="%smouvements/formulaire_mouvement.tpl"|args:$plugin_tpl legend="sortie d'un matériel %s"|args:$legend_part tpl_materiel_name=$tpl_materiel_name kinds=$kinds selected_kind=$selected_kind default_date=$default_date label_date="Date de sortie" default_number=$default_number tpl_materiel_path="sorties" default_comment=$default_comment comment_placeholder=$comment_placeholder csrf_key=$csrf_key cancel_link=$cancel_link}
<!-- -->
<!-- footer -->
{include file="admin/_foot.tpl"}
<!-- -->

View File

@ -0,0 +1,19 @@
<!-- materiel available for output part for add movement form -->
<fieldset>
<legend><h3>Matériel</h3></legend>
<dl>
<dt><label for="f_eqmt"></label> <b>(obligatoire)</b></dt>
<dd>
<select name="equipment_id" id="f_eqmt">
{foreach from=$eqmts_by_cat key='cat' item="eqmts"}
<optgroup label="-- {$cat} --">
{foreach from=$eqmts item="eqmt"}
<option value="{$eqmt.id}"{if ($selected_eqmt !== "") && ($selected_eqmt == $eqmt.id)} selected="selected"{/if}>{$eqmt.designation} - dispo: {$eqmt.available}</option>
{/foreach}
</optgroup>
{/foreach}
</select>
</dd>
</dl>
</fieldset>
<!-- -->

View File

@ -11,37 +11,18 @@ use Garradin\Utils;
require_once __DIR__ . '/../_inc.php';
// get the list of entry's kinds
$kinds = $mvt->listEntryKinds();
$selected_kind = $kinds[0];
// make default date (now), default number (1), and default comment (empty)
$date = new \DateTime;
$date->setTimestamp(time());
$default_date = $date;
$default_number = "1";
$default_comment = "";
// get list of all categories and make the first one as default
$cat = new Category;
$cats = $cat->listAll();
$selected_cat = $cats[0]->id;
// check if add form is submitted
$csrf_key = 'add_entry';
if (f('save'))
{
// keep the datas submitted as defaults
$selected_kind = f('kind');
$default_date = f('entry_date');
$default_number = f('equipment_number');
$selected_cat = f('category_id');
$default_comment = f('additional_comment');
if ($form->check($csrf_key) && !$form->hasErrors())
if (f('save') && $form->check($csrf_key) && !$form->hasErrors())
{
// try to add new equipment, get his id, add new entry
// and if error catched add it in form
@ -76,9 +57,21 @@ if (f('save'))
{
$form->addError($e->getMessage());
}
// keep the datas submitted as selected
$selected_kind = f('kind');
$selected_cat = f('category_id');
}
}
}
// make default date (now), default number (1), and default comment (empty)
$date = new \DateTime;
$date->setTimestamp(time());
$default_date = $date;
$default_number = "1";
$default_comment = "";
// make comment placeholder
$comment_placeholder = "ex: don reçu de la part de...";
// make cancel link, legend for the title of the form
// and the template name for equipment to use in form
@ -90,7 +83,7 @@ $tpl_materiel_name = "non_repertorie";
$tpl->assign(compact(
'kinds', 'cats', 'selected_kind', 'default_date',
'default_number', 'default_comment', 'selected_cat',
'default_number', 'default_comment', 'comment_placeholder', 'selected_cat',
'cancel_link', 'legend_part', 'tpl_materiel_name', 'csrf_key'
));

View File

@ -41,10 +41,12 @@ $selected_kind = $kinds[0];
$date = new \DateTime;
$date->setTimestamp(time());
$default_date = $date;
$default_number = "1";
$default_comment = "";
// make comment placeholder
$comment_placeholder = "ex: don reçu de la part de...";
// make cancel link, legend for the title of the form
// and the template name for equipment to use in form
$cancel_link = PLUGIN_URL . 'mouvements/entrees/index.php';
@ -55,7 +57,7 @@ $tpl_materiel_name = "repertorie";
$tpl->assign(compact(
'kinds', 'eqmts_by_cat', 'selected_kind', 'default_date',
'default_number', 'default_comment', 'cancel_link',
'default_number', 'default_comment', 'comment_placeholder', 'cancel_link',
'legend_part', 'tpl_materiel_name', 'csrf_key'
));

View File

@ -14,13 +14,6 @@ $eqmt = new Equipment;
$eqmts_by_cat = $eqmt->listAllReleasedRentByCategory();
$selected_eqmt = "";
// make default date (now) and default comment (empty)
$date = new \DateTime;
$date->setTimestamp(time());
$default_date = $date;
$default_comment = "";
// check if add form is submitted
$csrf_key = 'add_entry';
@ -45,20 +38,24 @@ if (f('save') && $form->check($csrf_key) && !$form->hasErrors())
Utils::redirect(PLUGIN_URL . 'mouvements/entrees/index.php');
} else
{
// not possible, keep the datas submitted as defaults and add error to form
$mvt_date = f('mvt_date');
$selected_eqmt = $eqmt_id;
$default_date = $mvt_date;
$default_comment = f('additional_comment');
// not possible, add error to form
$equiment = $eqmt->get($eqmt_id);
$form->addError(
"Il est impossible de rentrer " . (string) $eqmt_number . " " . $equiment->designation . " à la date du " . (string) $mvt_date . '.');
"Il est impossible de rentrer " . (string) $eqmt_number . " " . $equiment->designation . " à la date du " . (string) f('mvt_date') . '.');
// keep the datas submitted as selected
$selected_eqmt = $eqmt_id;
}
}
// make default date (now), default number (1)
// make default date (now), default number (1) and default comment (empty)
$default_number = "1";
$date = new \DateTime;
$date->setTimestamp(time());
$default_date = $date;
$default_comment = "";
// make comment placeholder
$comment_placeholder = "ex: retour de prêt...";
// make cancel link, legend for the title of the form
// and the template name for equipment to use in form
@ -73,7 +70,7 @@ $selected_kind = false;
// send all to template
$tpl->assign(compact(
'eqmts_by_cat', 'default_date', 'default_number',
'default_comment', 'cancel_link', 'legend_part',
'default_comment', 'cancel_link', 'legend_part', 'comment_placeholder',
'tpl_materiel_name', 'csrf_key', 'selected_eqmt', 'kinds', 'selected_kind',
));

View File

@ -0,0 +1,77 @@
<?php
// add output for an available equipment
namespace Garradin;
require_once __DIR__ . '/../_inc.php';
use Garradin\Plugin\Materiels\Equipment;
use Garradin\Utils;
// get the list of all available equipments ordered by category
$eqmt = new Equipment;
$eqmts_by_cat = $eqmt->ListAllAvailableByCategory();
$selected_eqmt = "";
// get the list of output's kinds
$kinds = $mvt->listOutputKinds();
$selected_kind = $kinds[0];
// check if add form is submitted
$csrf_key = 'add_output';
if (f('save') && $form->check($csrf_key) && !$form->hasErrors())
{
$eqmt_id = f('equipment_id');
$eqmt_number = (int) f('equipment_number');
$mvt_date_format = date_create_from_format(
"d/m/Y", f('mvt_date'))->format("Y-m-d");
// check if it's possible to output this return entry
if ($mvt->PossibilityOwnedEqmtOutput($eqmt_id, $eqmt_number, $mvt_date_format))
{
// it's possible, add new output
$mvt->add([
'side' => 1,
'kind' => f('kind'),
'equipment_number' => $eqmt_number,
'equipment_id' => $eqmt_id,
'mvt_date' => $mvt_date_format,
'additional_comment' => f('additional_comment'),
]);
Utils::redirect(PLUGIN_URL . 'mouvements/sorties/index.php');
} else
{
// not possible, add error to form
$equiment = $eqmt->get($eqmt_id);
$form->addError(
"Il est impossible de sortir " . (string) $eqmt_number . " " . $equiment->designation . " à la date du " . (string) f('mvt_date') . '.');
// keep the datas submitted as selected
$selected_eqmt = $eqmt_id;
$selected_kind = f('kind');
}
}
// make default date (now), default number (1), and default comment (empty)
$date = new \DateTime;
$date->setTimestamp(time());
$default_date = $date;
$default_number = "1";
$default_comment = "";
// make comment placeholder
$comment_placeholder = "ex: don fait à...";
// make cancel link, legend for the title of the form
// and the template name for equipment to use in form
$cancel_link = PLUGIN_URL . 'mouvements/sorties/index.php';
$legend_part = "en stock disponible";
$tpl_materiel_name = "stock_disponible";
$tpl->assign(compact(
'csrf_key', 'cancel_link', 'legend_part', 'tpl_materiel_name',
'kinds', 'selected_eqmt', 'selected_kind', 'default_date', 'default_number',
'default_comment', 'comment_placeholder', 'eqmts_by_cat'));
$tpl->display(PLUGIN_ROOT . '/templates/mouvements/sorties/ajouter_sortie.tpl');