diff --git a/materiels.tar.gz b/materiels.tar.gz index 1886c2b..9b42823 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 1350e69..b3b671d 100644 --- a/src/lib/Equipment.php +++ b/src/lib/Equipment.php @@ -132,8 +132,23 @@ class Equipment array_push($eqmts_available, $eqmt); } } - $eqmts_available_by_cat[$cat] = $eqmts_available; + if ($eqmts_available) { + $eqmts_available_by_cat[$cat] = $eqmts_available; + } } return $eqmts_available_by_cat; } + + public function CalculateAvailableByDate($id, $date) + { + $entries = DB::getInstance()->firstColumn( + "SELECT sum(equipment_number) FROM plugin_materiels_entry WHERE kind IN ( + 'Achat', 'Don', 'Récupération', + 'Retour de 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 IN ( + 'Vente', 'Don', 'Besoin', 'Autre (perte, vol, ...)', + '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 de94cec..b8234e4 100644 --- a/src/lib/Output.php +++ b/src/lib/Output.php @@ -3,6 +3,7 @@ namespace Garradin\Plugin\Materiels; use Garradin\DB; +use Garradin\Plugin\Materiels\Equipment; class Output { @@ -32,4 +33,26 @@ class Output { return DB::getInstance()->get('SELECT * FROM plugin_materiels_output ORDER BY output_date DESC;'); } + + public function PossibilityOwnedEqmtOutput($id, $eqmt_number, $date) + { + $after_output_dates = DB::getInstance()->get( + "SELECT output_date FROM plugin_materiels_output WHERE kind IN ( + 'Vente', 'Don', 'Besoin', 'Autre (perte, vol, ...)', + '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) { + $available_eqmt = $eqmt->CalculateAvailableByDate($id, $output_date); + if ($available_eqmt - $eqmt_number < 0) + { + return false; + } + } + return true; + } } diff --git a/src/templates/sorties/index.tpl b/src/templates/sorties/index.tpl index b4f83fb..cecde2c 100644 --- a/src/templates/sorties/index.tpl +++ b/src/templates/sorties/index.tpl @@ -20,7 +20,7 @@ {foreach from=$outputs item="output"} - {$entry.output_date|date_format:'%d/%m/%y'} + {$output.output_date|date_format:'%d/%m/%y'} {$output.kind} {$output.equipment_number} {$output.equipment} diff --git a/src/templates/sorties/stock_disponible.tpl b/src/templates/sorties/stock_disponible.tpl index 1d6e4cc..c13c432 100644 --- a/src/templates/sorties/stock_disponible.tpl +++ b/src/templates/sorties/stock_disponible.tpl @@ -5,13 +5,11 @@
diff --git a/src/www/admin/sorties/stock_disponible.php b/src/www/admin/sorties/stock_disponible.php index 3013298..4e45c48 100644 --- a/src/www/admin/sorties/stock_disponible.php +++ b/src/www/admin/sorties/stock_disponible.php @@ -10,23 +10,9 @@ require_once __DIR__ . '/../_inc.php'; $output = new Output; $eqmt = new Equipment; + $eqmts_by_cat = $eqmt->GetAvailableListByCategory(); - -$csrf_key = 'add_output'; - -if (f('save') && $form->check($csrf_key) && !$form->hasErrors()) -{ - $output_date_format = date_create_from_format( - "d/m/Y", f('entry_date'))->format("Y-m-d"); - $output->add([ - 'kind' => f('kind'), - 'equipment_number' => (int) f('equipment_number'), - 'equipment_id' => f('equipment_id'), - 'output_date' => $output_date_format, - 'additional_comment' => f('additional_comment'), - ]); - Utils::redirect(PLUGIN_URL . 'entrees/index.php'); -} +$selected_eqmt = ""; $kinds = $output->listKinds(); $selected_kind = $kinds[0]; @@ -37,13 +23,45 @@ $default_date = $date; $default_comment = ""; +$csrf_key = 'add_output'; + +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->PossibilityOwnedEqmtOutput($eqmt_id, $eqmt_number, $output_date_format)) + { + $output->add([ + 'kind' => f('kind'), + '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; + $selected_kind = f('kind'); + $default_date = $output_date; + $default_comment = f('additional_comment'); + + $equiment = $eqmt->get(f('equipment_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 = "en stock disponible"; $tpl_materiel_name = "stock_disponible"; $tpl->assign(compact( 'csrf_key', 'cancel_link', 'legend_part', 'tpl_materiel_name', - 'kinds', 'selected_kind', 'default_date', 'default_comment', + 'kinds', 'selected_eqmt', 'selected_kind', 'default_date', 'default_comment', 'eqmts_by_cat')); $tpl->display(PLUGIN_ROOT . '/templates/sorties/ajouter_sortie.tpl');