diff --git a/materiels.tar.gz b/materiels.tar.gz index 11982c1..4ed84ab 100644 Binary files a/materiels.tar.gz and b/materiels.tar.gz differ diff --git a/src/lib/Equipment.php b/src/lib/Equipment.php index eafd9e1..98f9b21 100644 --- a/src/lib/Equipment.php +++ b/src/lib/Equipment.php @@ -200,4 +200,13 @@ class Equipment } return $eqmts_borrowed_by_cat; } + + public function CalculateNoOwnedByDate($id, $date) + { + $entries = DB::getInstance()->firstColumn( + "SELECT sum(equipment_number) FROM plugin_materiels_entry WHERE kind = 'Location / Prêt' AND equipment_id = '{$id}' AND entry_date <= '{$date}';"); + $outputs = DB::getInstance()->firstColumn( + "SELECT sum(equipment_number) FROM plugin_materiels_output WHERE kind = 'Retour de location / prêt' AND equipment_id = '{$id}' AND output_date <= '{$date}';"); + return $entries - $outputs; + } } diff --git a/src/lib/Output.php b/src/lib/Output.php index b8234e4..d7e4b57 100644 --- a/src/lib/Output.php +++ b/src/lib/Output.php @@ -55,4 +55,24 @@ class Output } return true; } + + public function PossibilityNoOwnedEqmtOutput($id, $eqmt_number, $date) + { + $after_output_dates = DB::getInstance()->get( + "SELECT output_date FROM plugin_materiels_output WHERE kind = 'Retour de location / prêt' AND equipment_id = '{$id}' AND output_date > '{$date}';"); + $output_dates = array($date); + foreach ($after_output_dates as $row) + { + array_push($output_dates, $row->output_date); + } + $eqmt = new Equipment; + foreach ($output_dates as $output_date) { + $borrowed_eqmt = $eqmt->CalculateNoOwnedByDate($id, $output_date); + if ($borrowed_eqmt - $eqmt_number < 0) + { + return false; + } + } + return true; + } } diff --git a/src/templates/entrees/retour.tpl b/src/templates/entrees/retour.tpl index 1fc3c03..f3c0b98 100644 --- a/src/templates/entrees/retour.tpl +++ b/src/templates/entrees/retour.tpl @@ -8,7 +8,7 @@ {if $eqmts} {foreach from=$eqmts item="eqmt"} - + {/foreach} {/if} diff --git a/src/www/admin/sorties/emprunte.php b/src/www/admin/sorties/emprunte.php index cdc3522..fc229ac 100644 --- a/src/www/admin/sorties/emprunte.php +++ b/src/www/admin/sorties/emprunte.php @@ -23,6 +23,35 @@ $csrf_key = 'add_output'; $eqmts_by_cat = $eqmt->ListAllBorrowedByCategory(); +if (f('save') && $form->check($csrf_key) && !$form->hasErrors()) +{ + $eqmt_id = f('equipment_id'); + $eqmt_number = (int) f('equipment_number'); + $output_date_format = date_create_from_format( + "d/m/Y", f('output_date'))->format("Y-m-d"); + if ($output->PossibilityNoOwnedEqmtOutput($eqmt_id, $eqmt_number, $output_date_format)) + { + $output->add([ + 'kind' => 'Retour de location / prêt', + 'equipment_number' => $eqmt_number, + 'equipment_id' => $eqmt_id, + 'output_date' => $output_date_format, + 'additional_comment' => f('additional_comment'), + ]); + Utils::redirect(PLUGIN_URL . 'sorties/index.php'); + } else + { + $output_date = f('output_date'); + $selected_eqmt = $eqmt_id; + $default_date = $output_date; + $default_comment = f('additional_comment'); + + $equiment = $eqmt->get($eqmt_id); + $form->addError( + "Il est impossible de sortir " . (string) $eqmt_number . " " . $equiment->designation . " à la date du " . (string) $output_date . '.'); + } +} + $cancel_link = PLUGIN_URL . 'sorties/index.php'; $legend_part = "emprunté"; $tpl_materiel_name = "emprunte";