ok for delete entry in movement

This commit is contained in:
JBthePenguin 2021-09-01 18:53:10 +02:00
parent 9f16177989
commit 964806a0c0
4 changed files with 164 additions and 7 deletions

View File

@ -168,15 +168,16 @@ class Equipment
} }
public function CalculateAvailableByDate($id, $date) public function CalculateAvailableByDate($id, $date)
// return the number of available equipments (owned) at a specific date
{ {
$entries = DB::getInstance()->firstColumn( $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', '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( $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, ...)', '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; return $entries - $outputs;
} }
@ -237,11 +238,12 @@ class Equipment
} }
public function CalculateNoOwnedByDate($id, $date) public function CalculateNoOwnedByDate($id, $date)
// return the number of equipments no owned at a specific date
{ {
$entries = DB::getInstance()->firstColumn( $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( $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; return $entries - $outputs;
} }
} }

View File

@ -13,6 +13,18 @@ class Movement
DB::getInstance()->insert('plugin_materiels_movement', $data); 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() public function listEntryKinds()
// return list of entry's kinds // return list of entry's kinds
{ {
@ -48,7 +60,7 @@ class Movement
$entry_dates = array($date); $entry_dates = array($date);
foreach ($after_entry_dates as $row) foreach ($after_entry_dates as $row)
{ {
array_push($entry_dates, $row->entry_date); array_push($entry_dates, $row->mvt_date);
} }
$eqmt = new Equipment; $eqmt = new Equipment;
foreach ($entry_dates as $entry_date) { foreach ($entry_dates as $entry_date) {
@ -63,4 +75,74 @@ class Movement
// possible for all dates // possible for all dates
return true; 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);
}
}
} }

View File

@ -0,0 +1,13 @@
<!-- nav bar -->
{include file="%s_nav.tpl"|args:$plugin_tpl current_nav="entrees"}
<!-- -->
<!-- delete form -->
{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
}
<!-- -->
<!-- footer -->
{include file="admin/_foot.tpl"}
<!-- -->

View File

@ -0,0 +1,60 @@
<?php
// delete a specific entry
namespace Garradin;
require_once __DIR__ . '/../_inc.php';
use Garradin\Plugin\Materiels\Equipment;
use Garradin\Utils;
// get the entry to delete
$entry_to_delete = $mvt->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 ! ---");
}