diff --git a/materiels.tar.gz b/materiels.tar.gz index 9b42823..11982c1 100644 Binary files a/materiels.tar.gz and b/materiels.tar.gz differ diff --git a/src/lib/Entry.php b/src/lib/Entry.php index c9e33ac..41a2270 100644 --- a/src/lib/Entry.php +++ b/src/lib/Entry.php @@ -3,6 +3,7 @@ namespace Garradin\Plugin\Materiels; use Garradin\DB; +use Garradin\Plugin\Materiels\Equipment; class Entry { @@ -31,4 +32,24 @@ class Entry { return DB::getInstance()->get('SELECT * FROM plugin_materiels_entry ORDER BY entry_date DESC;'); } + + public function PossibilityRentEqmtEntry($id, $eqmt_number, $date) + { + $after_entry_dates = DB::getInstance()->get( + "SELECT entry_date FROM plugin_materiels_entry WHERE kind = 'Retour de location / prêt' AND equipment_id = '{$id}' AND entry_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) { + $out_of_stock_eqmt = $eqmt->CalculateOutOfStockByDate($id, $entry_date); + if ($out_of_stock_eqmt - $eqmt_number < 0) + { + return false; + } + } + return true; + } } diff --git a/src/lib/Equipment.php b/src/lib/Equipment.php index b3b671d..fd23201 100644 --- a/src/lib/Equipment.php +++ b/src/lib/Equipment.php @@ -65,7 +65,7 @@ class Equipment return $entries - $outputs; } - public function GetAllListByCategory() + public function AllListsAllByCategory() { $eqmts_by_cat = $this->listAllByCategory(); $eqmts_owned_by_cat = array(); @@ -119,7 +119,7 @@ class Equipment return $entries - $outputs; } - public function GetAvailableListByCategory() + public function ListAllAvailableByCategory() { $eqmts_by_cat = $this->listAllByCategory(); $eqmts_available_by_cat = array(); @@ -151,4 +151,33 @@ class Equipment 'Location / Prêt') AND equipment_id = '{$id}' AND output_date <= '{$date}';"); return $entries - $outputs; } + + public function listAllReleasedRentByCategory() + { + $eqmts_by_cat = $this->listAllByCategory(); + $eqmts_borrowed_by_cat = array(); + foreach ($eqmts_by_cat as $cat => $eqmts) { + $eqmts_borrowed = array(); + foreach ($eqmts as $eqmt) { + $borrowed = $this->CalculateOutOfStock($eqmt->id); + if ($borrowed) { + $eqmt->borrowed = $borrowed; + array_push($eqmts_borrowed, $eqmt); + } + } + if ($eqmts_borrowed) { + $eqmts_borrowed_by_cat[$cat] = $eqmts_borrowed; + } + } + return $eqmts_borrowed_by_cat; + } + + public function CalculateOutOfStockByDate($id, $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}';"); + $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}';"); + return $outputs - $entries; + } } diff --git a/src/templates/entrees/retour.tpl b/src/templates/entrees/retour.tpl index 37795a6..1fc3c03 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/templates/index.tpl b/src/templates/index.tpl index 55dbb3f..e90de43 100644 --- a/src/templates/index.tpl +++ b/src/templates/index.tpl @@ -33,7 +33,7 @@ {/if} {/foreach} -

Matériel dont l'association n'est pas propriétaire (loué ou prêté)

+

Matériel dont l'association n'est pas propriétaire (emprunté)

{foreach from=$eqmts_no_owned_by_cat key='cat' item="eqmts"} {if $eqmts}

{$cat}

diff --git a/src/templates/sorties/stock_disponible.tpl b/src/templates/sorties/stock_disponible.tpl index c13c432..37ff335 100644 --- a/src/templates/sorties/stock_disponible.tpl +++ b/src/templates/sorties/stock_disponible.tpl @@ -7,7 +7,7 @@ {foreach from=$eqmts_by_cat key='cat' item="eqmts"} {foreach from=$eqmts item="eqmt"} - + {/foreach} {/foreach} diff --git a/src/www/admin/entrees/retour.php b/src/www/admin/entrees/retour.php index f9f9711..39e95af 100644 --- a/src/www/admin/entrees/retour.php +++ b/src/www/admin/entrees/retour.php @@ -8,36 +8,51 @@ use Garradin\Utils; require_once __DIR__ . '/../_inc.php'; -$csrf_key = 'add_entry'; - -if (f('save') && $form->check($csrf_key) && !$form->hasErrors()) -{ - if ($form->check($csrf_key) && !$form->hasErrors()) - { - $entry = new Entry; - $entry_date_format = date_create_from_format( - "d/m/Y", f('entry_date'))->format("Y-m-d"); - $entry->add([ - 'kind' => 'Retour de location / prêt', - 'equipment_number' => (int) f('equipment_number'), - 'equipment_id' => f('equipment_id'), - 'entry_date' => $entry_date_format, - 'additional_comment' => f('additional_comment'), - ]); - Utils::redirect(PLUGIN_URL . 'entrees/index.php'); - } -} - +$entry = new Entry; $eqmt = new Equipment; -$eqmts_by_cat = $eqmt->listAllByCategory(); + +$eqmts_by_cat = $eqmt->listAllReleasedRentByCategory(); +$selected_eqmt = ""; $date = new \DateTime; $date->setTimestamp(time()); $default_date = $date; -$default_number = "1"; $default_comment = ""; +$csrf_key = 'add_entry'; + +if (f('save') && $form->check($csrf_key) && !$form->hasErrors()) +{ + $eqmt_id = f('equipment_id'); + $eqmt_number = (int) f('equipment_number'); + $entry_date_format = date_create_from_format( + "d/m/Y", f('entry_date'))->format("Y-m-d"); + if ($entry->PossibilityRentEqmtEntry($eqmt_id, $eqmt_number, $entry_date_format)) + { + $entry->add([ + 'kind' => 'Retour de location / prêt', + 'equipment_number' => $eqmt_number, + 'equipment_id' => $eqmt_id, + 'entry_date' => $entry_date_format, + 'additional_comment' => f('additional_comment'), + ]); + Utils::redirect(PLUGIN_URL . 'entrees/index.php'); + } else + { + $entry_date = f('entry_date'); + $selected_eqmt = $eqmt_id; + $default_date = $entry_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) $entry_date . '.'); + } +} + +$default_number = "1"; + $cancel_link = PLUGIN_URL . 'entrees/index.php'; $legend_part = "en retour de location / prêt"; $tpl_materiel_name = "retour"; @@ -45,7 +60,7 @@ $tpl_materiel_name = "retour"; $tpl->assign(compact( 'eqmts_by_cat', 'default_date', 'default_number', 'default_comment', 'cancel_link', 'legend_part', - 'tpl_materiel_name', 'csrf_key' + 'tpl_materiel_name', 'csrf_key', 'selected_eqmt', )); $tpl->display(PLUGIN_ROOT . '/templates/entrees/ajouter_entree.tpl'); diff --git a/src/www/admin/index.php b/src/www/admin/index.php index 39ae74e..3bc0237 100644 --- a/src/www/admin/index.php +++ b/src/www/admin/index.php @@ -15,7 +15,7 @@ $eqmt = new Equipment; list( $eqmts_owned_by_cat, $eqmts_no_owned_by_cat, - $eqmts_just_listed_by_cat) = $eqmt->GetAllListByCategory(); + $eqmts_just_listed_by_cat) = $eqmt->AllListsAllByCategory(); $tpl->assign(compact( 'eqmts_owned_by_cat', 'eqmts_no_owned_by_cat', diff --git a/src/www/admin/sorties/stock_disponible.php b/src/www/admin/sorties/stock_disponible.php index 4e45c48..2b35c40 100644 --- a/src/www/admin/sorties/stock_disponible.php +++ b/src/www/admin/sorties/stock_disponible.php @@ -11,7 +11,7 @@ require_once __DIR__ . '/../_inc.php'; $output = new Output; $eqmt = new Equipment; -$eqmts_by_cat = $eqmt->GetAvailableListByCategory(); +$eqmts_by_cat = $eqmt->ListAllAvailableByCategory(); $selected_eqmt = ""; $kinds = $output->listKinds(); @@ -49,7 +49,7 @@ if (f('save') && $form->check($csrf_key) && !$form->hasErrors()) $default_date = $output_date; $default_comment = f('additional_comment'); - $equiment = $eqmt->get(f('equipment_id')); + $equiment = $eqmt->get($eqmt_id); $form->addError( "Il est impossible de sortir " . (string) $eqmt_number . " " . $equiment->designation . " à la date du " . (string) $output_date . '.'); }