diff --git a/src/lib/Equipment.php b/src/lib/Equipment.php index 225dcb3..a646c38 100644 --- a/src/lib/Equipment.php +++ b/src/lib/Equipment.php @@ -168,15 +168,16 @@ class Equipment } public function CalculateAvailableByDate($id, $date) + // return the number of available equipments (owned) at a specific date { $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}' AND entry_date <= '{$date}';"); + '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 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}' AND output_date <= '{$date}';"); + 'Location / Prêt') AND equipment_id = '{$id}' AND mvt_date <= '{$date}';"); return $entries - $outputs; } @@ -237,11 +238,12 @@ class Equipment } public function CalculateNoOwnedByDate($id, $date) + // return the number of equipments no owned at a specific 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}';"); + "SELECT sum(equipment_number) FROM plugin_materiels_movement WHERE side = '0' AND kind = '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 = 'Retour de location / prêt' AND equipment_id = '{$id}' AND output_date <= '{$date}';"); + "SELECT sum(equipment_number) FROM plugin_materiels_movement WHERE side = '1' AND kind = 'Retour de location / prêt' AND equipment_id = '{$id}' AND mvt_date <= '{$date}';"); return $entries - $outputs; } } diff --git a/src/lib/Movement.php b/src/lib/Movement.php index 7bb9ffe..2a4e0c5 100644 --- a/src/lib/Movement.php +++ b/src/lib/Movement.php @@ -13,6 +13,18 @@ class Movement DB::getInstance()->insert('plugin_materiels_movement', $data); } + public function get($id) + // get and return a specific movement + { + return DB::getInstance()->first('SELECT * FROM plugin_materiels_movement WHERE id = ?;', $id); + } + + public function delete($id) + // delete specific movement + { + DB::getInstance()->delete('plugin_materiels_movement', 'id = ' . $id); + } + public function listEntryKinds() // return list of entry's kinds { @@ -48,7 +60,7 @@ class Movement $entry_dates = array($date); foreach ($after_entry_dates as $row) { - array_push($entry_dates, $row->entry_date); + array_push($entry_dates, $row->mvt_date); } $eqmt = new Equipment; foreach ($entry_dates as $entry_date) { @@ -63,4 +75,74 @@ class Movement // possible for all dates return true; } + + public function PossibilityOwnedEqmtOutput($id, $eqmt_number, $date) + // check if it's possible to add an output for a specific equipment owned + // at specific date for a specific number + // return true if it's ok else return false + { + // get all output dates after the specific date and make a list with its + $after_output_dates = DB::getInstance()->get( + "SELECT mvt_date FROM plugin_materiels_movement WHERE side = '1' AND kind IN ( + 'Vente', 'Don', 'Besoin', 'Autre (perte, vol, ...)', + 'Location / Prêt') AND equipment_id = '{$id}' AND mvt_date > '{$date}';"); + $output_dates = array($date); + foreach ($after_output_dates as $row) + { + array_push($output_dates, $row->mvt_date); + } + $eqmt = new Equipment; + foreach ($output_dates as $output_date) { + // for each date check if it's possible to add this number of output + $available_eqmt = $eqmt->CalculateAvailableByDate($id, $output_date); + if ($available_eqmt - $eqmt_number < 0) + { + // not possible + return false; + } + } + // possible for all dates + return true; + } + + public function PossibilityNoOwnedEqmtOutput($id, $eqmt_number, $date) + // check if it's possible to add an output for a specific equipment no owned + // at specific date for a specific number + // return true if it's ok else return false + { + // get all output dates after the specific date and make a list with its + $after_output_dates = DB::getInstance()->get( + "SELECT mvt_date FROM plugin_materiels_movement WHERE side = '1' AND kind = 'Retour de location / prêt' AND equipment_id = '{$id}' AND mvt_date > '{$date}';"); + $output_dates = array($date); + foreach ($after_output_dates as $row) + { + array_push($output_dates, $row->mvt_date); + } + $eqmt = new Equipment; + foreach ($output_dates as $output_date) { + // for each date check if it's possible to add this number of output + $borrowed_eqmt = $eqmt->CalculateNoOwnedByDate($id, $output_date); + if ($borrowed_eqmt - $eqmt_number < 0) + { + // not possible + return false; + } + } + // possible for all dates + return true; + } + + public function PossibilityDeleteEntry($entry) + // return true if it's possible to delete this entry, else return false + { + if ($entry->kind == 'Location / Prêt') + { + return $this->PossibilityNoOwnedEqmtOutput( + $entry->equipment_id, $entry->equipment_number, $entry->mvt_date); + } else + { + return $this->PossibilityOwnedEqmtOutput( + $entry->equipment_id, $entry->equipment_number, $entry->mvt_date); + } + } } diff --git a/src/templates/mouvements/entrees/supprimer_entree.tpl b/src/templates/mouvements/entrees/supprimer_entree.tpl new file mode 100644 index 0000000..3994c67 --- /dev/null +++ b/src/templates/mouvements/entrees/supprimer_entree.tpl @@ -0,0 +1,13 @@ + +{include file="%s_nav.tpl"|args:$plugin_tpl current_nav="entrees"} + + +{include file="%scommon/delete_form.tpl"|args:$plugin_tpl + legend="Supprimer cette entrée de matériels ?" + warning="Êtes-vous sûr de vouloir supprimer l'entrée de « %s » ?"|args:$entry_string + alert="Attention, la suppression de cette entrée supprimera le matériel « %s » du répertoire s'il n'a pas d'autre entrée."|args:$eqmt_name +} + + +{include file="admin/_foot.tpl"} + diff --git a/src/www/admin/mouvements/entrees/supprimer_entree.php b/src/www/admin/mouvements/entrees/supprimer_entree.php new file mode 100644 index 0000000..db2daba --- /dev/null +++ b/src/www/admin/mouvements/entrees/supprimer_entree.php @@ -0,0 +1,60 @@ +get((int) qg('id')); +if (!$entry_to_delete) +{ + throw new UserException("Cette entrée n'existe pas."); +} + +// check if delete form is submitted +$eqmt = new Equipment; + +$csrf_key = 'delete_entry_' . $entry_to_delete->id; + +if (f('delete') && $form->check($csrf_key) && !$form->hasErrors()) +{ + // delete the movement required + $mvt->delete($entry_to_delete->id); + // try to delete the corresponding equipment + // and pass if it's not possible (used in an another movement) + try + { + $eqmt->delete($entry_to_delete->equipment_id); + } + catch (\RuntimeException $e){ + } + Utils::redirect(PLUGIN_URL . 'mouvements/entrees/index.php'); +} + +$corresponding_eqmt = $eqmt->get($entry_to_delete->equipment_id); + +// check if it's possible to delete this entry +if ($mvt->PossibilityDeleteEntry($entry_to_delete)) +{ + // it's possible + $cancel_link = PLUGIN_URL . 'mouvements/entrees/index.php'; + // construct string to send to template + $entry_string = (string) $entry_to_delete->equipment_number . " " . $corresponding_eqmt->designation . " à la date du " . date_create_from_format( + "Y-m-d", $entry_to_delete->mvt_date)->format("d/m/y"); + + $eqmt_name = $corresponding_eqmt->designation; + + $tpl->assign(compact('entry_string', 'eqmt_name', 'csrf_key', 'cancel_link')); + + $tpl->display(PLUGIN_ROOT . '/templates/mouvements/entrees/supprimer_entree.tpl'); + +} else { + // not possible, display error message + throw new UserException( + "Cette entrée ne peut pas être supprimée car ça rendrait impossible l'historique des entrées et sorties de « " . $corresponding_eqmt->designation . " ». --- plus de sorties que d'entrées ! ---"); +}