diff --git a/src/lib/Equipment.php b/src/lib/Equipment.php index a646c38..8e98599 100644 --- a/src/lib/Equipment.php +++ b/src/lib/Equipment.php @@ -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); diff --git a/src/lib/Movement.php b/src/lib/Movement.php index 2a4e0c5..cc9cd6e 100644 --- a/src/lib/Movement.php +++ b/src/lib/Movement.php @@ -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 { diff --git a/src/templates/mouvements/entrees/ajouter_entree.tpl b/src/templates/mouvements/entrees/ajouter_entree.tpl index 3104a2d..5c68bd7 100644 --- a/src/templates/mouvements/entrees/ajouter_entree.tpl +++ b/src/templates/mouvements/entrees/ajouter_entree.tpl @@ -2,7 +2,7 @@ {include file="%s_nav.tpl"|args:$plugin_tpl current_nav="entrees"} -{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} {include file="admin/_foot.tpl"} diff --git a/src/templates/mouvements/sorties/ajouter_sortie.tpl b/src/templates/mouvements/sorties/ajouter_sortie.tpl new file mode 100644 index 0000000..8b8b05a --- /dev/null +++ b/src/templates/mouvements/sorties/ajouter_sortie.tpl @@ -0,0 +1,9 @@ + +{include file="%s_nav.tpl"|args:$plugin_tpl current_nav="sorties"} + + +{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} + + +{include file="admin/_foot.tpl"} + diff --git a/src/templates/mouvements/sorties/stock_disponible.tpl b/src/templates/mouvements/sorties/stock_disponible.tpl new file mode 100644 index 0000000..ba27683 --- /dev/null +++ b/src/templates/mouvements/sorties/stock_disponible.tpl @@ -0,0 +1,19 @@ + +
+

Matériel

+
+
(obligatoire)
+
+ +
+
+
+ diff --git a/src/www/admin/mouvements/entrees/non_repertorie.php b/src/www/admin/mouvements/entrees/non_repertorie.php index a1e7399..8832a89 100644 --- a/src/www/admin/mouvements/entrees/non_repertorie.php +++ b/src/www/admin/mouvements/entrees/non_repertorie.php @@ -11,75 +11,68 @@ 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')) +if (f('save') && $form->check($csrf_key) && !$form->hasErrors()) { - // 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()) + // try to add new equipment, get his id, add new entry + // and if error catched add it in form + try { - // try to add new equipment, get his id, add new entry - // and if error catched add it in form - try + // add new equipment and get his id + $eqmt = new Equipment; + $eqmt_id = $eqmt->add([ + 'category_id' => (int) f('category_id'), + 'designation' => ucfirst(strtolower(f('designation'))), + ]); + // make the entry date in the good format + $mvt_date_format = date_create_from_format( + "d/m/Y", f('mvt_date'))->format("Y-m-d"); + // add new entry + $mvt->add([ + 'side' => 0, + 'kind' => f('kind'), + 'equipment_number' => (int) f('equipment_number'), + 'equipment_id' => $eqmt_id, + 'mvt_date' => $mvt_date_format, + 'additional_comment' => f('additional_comment'), + ]); + Utils::redirect(PLUGIN_URL . 'mouvements/entrees/index.php'); + } + catch (\RuntimeException $e) + { + if (strstr($e->getMessage(), 'UNIQUE constraint failed')) { - // add new equipment and get his id - $eqmt = new Equipment; - $eqmt_id = $eqmt->add([ - 'category_id' => (int) f('category_id'), - 'designation' => ucfirst(strtolower(f('designation'))), - ]); - // make the entry date in the good format - $mvt_date_format = date_create_from_format( - "d/m/Y", f('mvt_date'))->format("Y-m-d"); - // add new entry - $mvt->add([ - 'side' => 0, - 'kind' => f('kind'), - 'equipment_number' => (int) f('equipment_number'), - 'equipment_id' => $eqmt_id, - 'mvt_date' => $mvt_date_format, - 'additional_comment' => f('additional_comment'), - ]); - Utils::redirect(PLUGIN_URL . 'mouvements/entrees/index.php'); - } - catch (\RuntimeException $e) + $form->addError('Un matériel avec cette désignation est déjà répertorié.'); + } else { - if (strstr($e->getMessage(), 'UNIQUE constraint failed')) - { - $form->addError('Un matériel avec cette désignation est déjà répertorié.'); - } else - { - $form->addError($e->getMessage()); - } + $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 $cancel_link = PLUGIN_URL . 'mouvements/entrees/index.php'; @@ -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' )); diff --git a/src/www/admin/mouvements/entrees/repertorie.php b/src/www/admin/mouvements/entrees/repertorie.php index e0f3114..dd38e3f 100644 --- a/src/www/admin/mouvements/entrees/repertorie.php +++ b/src/www/admin/mouvements/entrees/repertorie.php @@ -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' )); diff --git a/src/www/admin/mouvements/entrees/retour.php b/src/www/admin/mouvements/entrees/retour.php index 6b3ad18..8acdc67 100644 --- a/src/www/admin/mouvements/entrees/retour.php +++ b/src/www/admin/mouvements/entrees/retour.php @@ -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', )); diff --git a/src/www/admin/mouvements/sorties/stock_disponible.php b/src/www/admin/mouvements/sorties/stock_disponible.php new file mode 100644 index 0000000..447f04c --- /dev/null +++ b/src/www/admin/mouvements/sorties/stock_disponible.php @@ -0,0 +1,77 @@ +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');