diff --git a/src/lib/Equipment.php b/src/lib/Equipment.php index 56cb647..225dcb3 100644 --- a/src/lib/Equipment.php +++ b/src/lib/Equipment.php @@ -181,14 +181,20 @@ class Equipment } public function listAllReleasedRentByCategory() + // return list of equipments for possible return (loan or rent) ordered by category { + // get the list of all equipments ordered by category $eqmts_by_cat = $this->listAllByCategory(); + // construct list of equipments for possible return for all categories $eqmts_released_by_cat = array(); foreach ($eqmts_by_cat as $cat => $eqmts) { + // construct list of equipments for possible return for one category $eqmts_released = array(); foreach ($eqmts as $eqmt) { + // for each equipment calculte number of possible return $released = $this->CalculateOwnedOut($eqmt->id); if ($released) { + // add to the list if at least one is possible for return $eqmt->released = $released; array_push($eqmts_released, $eqmt); } @@ -201,11 +207,12 @@ class Equipment } public function CalculateOutOfStockByDate($id, $date) + // return number of a specific equipment is out at a specific date { $entries = DB::getInstance()->firstColumn( - "SELECT sum(equipment_number) FROM plugin_materiels_entry WHERE kind = 'Retour de location / prêt' AND equipment_id = '{$id}' AND entry_date <= '{$date}';"); + "SELECT sum(equipment_number) FROM plugin_materiels_movement WHERE side = '0' AND kind = 'Retour de location / prêt' AND equipment_id = '{$id}' AND mvt_date <= '{$date}';"); $outputs = DB::getInstance()->firstColumn( - "SELECT sum(equipment_number) FROM plugin_materiels_output WHERE kind = 'Location / Prêt' AND equipment_id = '{$id}' AND output_date <= '{$date}';"); + "SELECT sum(equipment_number) FROM plugin_materiels_movement WHERE side = '1' AND kind = 'Location / Prêt' AND equipment_id = '{$id}' AND mvt_date <= '{$date}';"); return $outputs - $entries; } diff --git a/src/lib/Movement.php b/src/lib/Movement.php index cffa604..7bb9ffe 100644 --- a/src/lib/Movement.php +++ b/src/lib/Movement.php @@ -36,4 +36,31 @@ class Movement } return $mvts; } + + public function PossibilityRentEqmtEntry($id, $eqmt_number, $date) + // check if it's possible to add a return entry + // at specific date for a specific number + // return true if it's ok else return false + { + // get all entry dates after the specific date and make a list with its + $after_entry_dates = DB::getInstance()->get( + "SELECT mvt_date FROM plugin_materiels_movement WHERE side = '0' AND kind = 'Retour de location / prêt' AND equipment_id = '{$id}' AND mvt_date > '{$date}';"); + $entry_dates = array($date); + foreach ($after_entry_dates as $row) + { + array_push($entry_dates, $row->entry_date); + } + $eqmt = new Equipment; + foreach ($entry_dates as $entry_date) { + // for each date check if it's possible to add this number of return entries + $out_of_stock_eqmt = $eqmt->CalculateOutOfStockByDate($id, $entry_date); + if ($out_of_stock_eqmt - $eqmt_number < 0) + { + // not possible + return false; + } + } + // possible for all dates + return true; + } } diff --git a/src/templates/mouvements/entrees/retour.tpl b/src/templates/mouvements/entrees/retour.tpl new file mode 100644 index 0000000..8991cd8 --- /dev/null +++ b/src/templates/mouvements/entrees/retour.tpl @@ -0,0 +1,20 @@ + +
+

Matériel

+
+
(obligatoire)
+
+ +
+
+
diff --git a/src/templates/mouvements/formulaire_mouvement.tpl b/src/templates/mouvements/formulaire_mouvement.tpl index d9c5ec1..9e76444 100644 --- a/src/templates/mouvements/formulaire_mouvement.tpl +++ b/src/templates/mouvements/formulaire_mouvement.tpl @@ -8,7 +8,7 @@ {form_errors}
- {if $tpl_materiel_name != 'retour'} + {if $kinds}
(obligatoire)
diff --git a/src/www/admin/mouvements/entrees/repertorie.php b/src/www/admin/mouvements/entrees/repertorie.php index 3a1ba34..e0f3114 100644 --- a/src/www/admin/mouvements/entrees/repertorie.php +++ b/src/www/admin/mouvements/entrees/repertorie.php @@ -37,7 +37,7 @@ $eqmts_by_cat = $eqmt->listAllByCategory(); $kinds = $mvt->listEntryKinds(); $selected_kind = $kinds[0]; -// make default date (now), default number (1), and default comment (empty) +// make default date (now), default number (1), and default comment (empty) $date = new \DateTime; $date->setTimestamp(time()); $default_date = $date; diff --git a/src/www/admin/mouvements/entrees/retour.php b/src/www/admin/mouvements/entrees/retour.php new file mode 100644 index 0000000..6b3ad18 --- /dev/null +++ b/src/www/admin/mouvements/entrees/retour.php @@ -0,0 +1,80 @@ +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'; + +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 add this return entry + if ($mvt->PossibilityRentEqmtEntry($eqmt_id, $eqmt_number, $mvt_date_format)) + { + // it's possible, add new entry + $mvt->add([ + 'side' => 0, + 'kind' => 'Retour de location / prêt', + 'equipment_number' => $eqmt_number, + 'equipment_id' => $eqmt_id, + 'mvt_date' => $mvt_date_format, + 'additional_comment' => f('additional_comment'), + ]); + 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'); + + $equiment = $eqmt->get($eqmt_id); + $form->addError( + "Il est impossible de rentrer " . (string) $eqmt_number . " " . $equiment->designation . " à la date du " . (string) $mvt_date . '.'); + } +} + +// make default date (now), default number (1) +$default_number = "1"; + +// 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'; +$legend_part = "en retour de location / prêt"; +$tpl_materiel_name = "retour"; + +// no kinds needed +$kinds = false; +$selected_kind = false; + +// send all to template +$tpl->assign(compact( + 'eqmts_by_cat', 'default_date', 'default_number', + 'default_comment', 'cancel_link', 'legend_part', + 'tpl_materiel_name', 'csrf_key', 'selected_eqmt', 'kinds', 'selected_kind', +)); + +$tpl->display(PLUGIN_ROOT . '/templates/mouvements/entrees/ajouter_entree.tpl');