ok for add entry for materiel returned in movement

This commit is contained in:
JBthePenguin 2021-09-01 16:02:02 +02:00
parent 3fda0bfcf0
commit 9f16177989
6 changed files with 138 additions and 4 deletions

View File

@ -181,14 +181,20 @@ class Equipment
} }
public function listAllReleasedRentByCategory() public function listAllReleasedRentByCategory()
// return list of equipments for possible return (loan or rent) ordered by category
{ {
// get the list of all equipments ordered by category
$eqmts_by_cat = $this->listAllByCategory(); $eqmts_by_cat = $this->listAllByCategory();
// construct list of equipments for possible return for all categories
$eqmts_released_by_cat = array(); $eqmts_released_by_cat = array();
foreach ($eqmts_by_cat as $cat => $eqmts) { foreach ($eqmts_by_cat as $cat => $eqmts) {
// construct list of equipments for possible return for one category
$eqmts_released = array(); $eqmts_released = array();
foreach ($eqmts as $eqmt) { foreach ($eqmts as $eqmt) {
// for each equipment calculte number of possible return
$released = $this->CalculateOwnedOut($eqmt->id); $released = $this->CalculateOwnedOut($eqmt->id);
if ($released) { if ($released) {
// add to the list if at least one is possible for return
$eqmt->released = $released; $eqmt->released = $released;
array_push($eqmts_released, $eqmt); array_push($eqmts_released, $eqmt);
} }
@ -201,11 +207,12 @@ class Equipment
} }
public function CalculateOutOfStockByDate($id, $date) public function CalculateOutOfStockByDate($id, $date)
// return number of a specific equipment is out at a specific date
{ {
$entries = DB::getInstance()->firstColumn( $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}';"); "SELECT sum(equipment_number) FROM plugin_materiels_movement WHERE side = '0' AND kind = '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 = 'Location / Prêt' AND equipment_id = '{$id}' AND output_date <= '{$date}';"); "SELECT sum(equipment_number) FROM plugin_materiels_movement WHERE side = '1' AND kind = 'Location / Prêt' AND equipment_id = '{$id}' AND mvt_date <= '{$date}';");
return $outputs - $entries; return $outputs - $entries;
} }

View File

@ -36,4 +36,31 @@ class Movement
} }
return $mvts; return $mvts;
} }
public function PossibilityRentEqmtEntry($id, $eqmt_number, $date)
// check if it's possible to add a return entry
// at specific date for a specific number
// return true if it's ok else return false
{
// get all entry dates after the specific date and make a list with its
$after_entry_dates = DB::getInstance()->get(
"SELECT mvt_date FROM plugin_materiels_movement WHERE side = '0' AND kind = 'Retour de location / prêt' AND equipment_id = '{$id}' AND mvt_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) {
// for each date check if it's possible to add this number of return entries
$out_of_stock_eqmt = $eqmt->CalculateOutOfStockByDate($id, $entry_date);
if ($out_of_stock_eqmt - $eqmt_number < 0)
{
// not possible
return false;
}
}
// possible for all dates
return true;
}
} }

View File

@ -0,0 +1,20 @@
<!-- materiel in return part for add movement form -->
<fieldset>
<legend><h3>Matériel</h3></legend>
<dl>
<dt><label for="f_eqmt"></label> <b>(obligatoire)</b></dt>
<dd>
<select name="equipment_id" id="f_eqmt">
{foreach from=$eqmts_by_cat key='cat' item="eqmts"}
{if $eqmts}
<optgroup label="-- {$cat} --">
{foreach from=$eqmts item="eqmt"}
<option value="{$eqmt.id}"{if ($selected_eqmt !== "") && ($selected_eqmt == $eqmt.id)} selected="selected"{/if}>{$eqmt.designation} - sortie: {$eqmt.released}</option>
{/foreach}
</optgroup>
{/if}
{/foreach}
</select>
</dd>
</dl>
</fieldset>

View File

@ -8,7 +8,7 @@
{form_errors} {form_errors}
<!-- --> <!-- -->
<dl> <dl>
{if $tpl_materiel_name != 'retour'} {if $kinds}
<!-- kind --> <!-- kind -->
<dt><label for="f_kind">Type</label> <b>(obligatoire)</b></dt> <dt><label for="f_kind">Type</label> <b>(obligatoire)</b></dt>
<dd> <dd>

View File

@ -0,0 +1,80 @@
<?php
// add entry for materiel in return
namespace Garradin;
require_once __DIR__ . '/../_inc.php';
use Garradin\Plugin\Materiels\Equipment;
use Garradin\Utils;
// get the list of equipments for possible return (loan or rent)
$eqmt = new Equipment;
$eqmts_by_cat = $eqmt->listAllReleasedRentByCategory();
$selected_eqmt = "";
// make default date (now) and default comment (empty)
$date = new \DateTime;
$date->setTimestamp(time());
$default_date = $date;
$default_comment = "";
// check if add form is submitted
$csrf_key = 'add_entry';
if (f('save') && $form->check($csrf_key) && !$form->hasErrors())
{
$eqmt_id = f('equipment_id');
$eqmt_number = (int) f('equipment_number');
$mvt_date_format = date_create_from_format(
"d/m/Y", f('mvt_date'))->format("Y-m-d");
// check if it's possible to add this return entry
if ($mvt->PossibilityRentEqmtEntry($eqmt_id, $eqmt_number, $mvt_date_format))
{
// it's possible, add new entry
$mvt->add([
'side' => 0,
'kind' => 'Retour de location / prêt',
'equipment_number' => $eqmt_number,
'equipment_id' => $eqmt_id,
'mvt_date' => $mvt_date_format,
'additional_comment' => f('additional_comment'),
]);
Utils::redirect(PLUGIN_URL . 'mouvements/entrees/index.php');
} else
{
// not possible, keep the datas submitted as defaults and add error to form
$mvt_date = f('mvt_date');
$selected_eqmt = $eqmt_id;
$default_date = $mvt_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) $mvt_date . '.');
}
}
// make default date (now), default number (1)
$default_number = "1";
// make cancel link, legend for the title of the form
// and the template name for equipment to use in form
$cancel_link = PLUGIN_URL . 'mouvements/entrees/index.php';
$legend_part = "en retour de location / prêt";
$tpl_materiel_name = "retour";
// no kinds needed
$kinds = false;
$selected_kind = false;
// send all to template
$tpl->assign(compact(
'eqmts_by_cat', 'default_date', 'default_number',
'default_comment', 'cancel_link', 'legend_part',
'tpl_materiel_name', 'csrf_key', 'selected_eqmt', 'kinds', 'selected_kind',
));
$tpl->display(PLUGIN_ROOT . '/templates/mouvements/entrees/ajouter_entree.tpl');