ok for add entry for materiel not listed in movement

This commit is contained in:
JBthePenguin 2021-09-01 14:48:42 +02:00
parent b9abf86388
commit 3fda0bfcf0
3 changed files with 115 additions and 3 deletions

View File

@ -0,0 +1,16 @@
<!-- materiel not listed part for add movement form -->
<fieldset>
<legend><h3>Matériel</h3></legend>
<dl>
<dt><label for="f_cat">Catégorie</label> <b>(obligatoire)</b></dt>
<dd>
<select name="category_id" id="f_cat">
{foreach from=$cats item="cat"}
<option value="{$cat.id}"{if $selected_cat == $cat.id} selected="selected"{/if}>{$cat.name}</option>
{/foreach}
</select>
</dd>
{input type="text" name="designation" label="Désignation" required=true maxlength="255"}
</dl>
</fieldset>
<!-- -->

View File

@ -0,0 +1,97 @@
<?php
// add entry for materiel not listed
namespace Garradin;
use Garradin\Plugin\Materiels\Category;
use Garradin\Plugin\Materiels\Equipment;
use Garradin\Utils;
require_once __DIR__ . '/../_inc.php';
// get the list of entry's kinds
$kinds = $mvt->listEntryKinds();
$selected_kind = $kinds[0];
// make default date (now), default number (1), and default comment (empty)
$date = new \DateTime;
$date->setTimestamp(time());
$default_date = $date;
$default_number = "1";
$default_comment = "";
// get list of all categories and make the first one as default
$cat = new Category;
$cats = $cat->listAll();
$selected_cat = $cats[0]->id;
// check if add form is submitted
$csrf_key = 'add_entry';
if (f('save'))
{
// keep the datas submitted as defaults
$selected_kind = f('kind');
$default_date = f('entry_date');
$default_number = f('equipment_number');
$selected_cat = f('category_id');
$default_comment = f('additional_comment');
if ($form->check($csrf_key) && !$form->hasErrors())
{
// try to add new equipment, get his id, add new entry
// and if error catched add it in form
try
{
// add new equipment and get his id
$eqmt = new Equipment;
$eqmt_id = $eqmt->add([
'category_id' => (int) f('category_id'),
'designation' => ucfirst(strtolower(f('designation'))),
]);
// make the entry date in the good format
$mvt_date_format = date_create_from_format(
"d/m/Y", f('mvt_date'))->format("Y-m-d");
// add new entry
$mvt->add([
'side' => 0,
'kind' => f('kind'),
'equipment_number' => (int) f('equipment_number'),
'equipment_id' => $eqmt_id,
'mvt_date' => $mvt_date_format,
'additional_comment' => f('additional_comment'),
]);
Utils::redirect(PLUGIN_URL . 'mouvements/entrees/index.php');
}
catch (\RuntimeException $e)
{
if (strstr($e->getMessage(), 'UNIQUE constraint failed'))
{
$form->addError('Un matériel avec cette désignation est déjà répertorié.');
} else
{
$form->addError($e->getMessage());
}
}
}
}
// 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 = "non répertorié";
$tpl_materiel_name = "non_repertorie";
// send all to template
$tpl->assign(compact(
'kinds', 'cats', 'selected_kind', 'default_date',
'default_number', 'default_comment', 'selected_cat',
'cancel_link', 'legend_part', 'tpl_materiel_name', 'csrf_key'
));
$tpl->display(PLUGIN_ROOT . '/templates/mouvements/entrees/ajouter_entree.tpl');

View File

@ -1,5 +1,7 @@
<?php <?php
// add entry for materiel listed
namespace Garradin; namespace Garradin;
require_once __DIR__ . '/../_inc.php'; require_once __DIR__ . '/../_inc.php';
@ -8,7 +10,6 @@ use Garradin\Plugin\Materiels\Equipment;
use Garradin\Utils; use Garradin\Utils;
// check if add form is submitted // check if add form is submitted
$csrf_key = 'add_entry'; $csrf_key = 'add_entry';
if (f('save') && $form->check($csrf_key) && !$form->hasErrors()) if (f('save') && $form->check($csrf_key) && !$form->hasErrors())
@ -33,7 +34,6 @@ $eqmt = new Equipment;
$eqmts_by_cat = $eqmt->listAllByCategory(); $eqmts_by_cat = $eqmt->listAllByCategory();
// get the list of entry's kinds // get the list of entry's kinds
$kinds = $mvt->listEntryKinds(); $kinds = $mvt->listEntryKinds();
$selected_kind = $kinds[0]; $selected_kind = $kinds[0];
@ -47,7 +47,6 @@ $default_comment = "";
// make cancel link, legend for the title of the form // make cancel link, legend for the title of the form
// and the template name for equipment to use in form // and the template name for equipment to use in form
$cancel_link = PLUGIN_URL . 'mouvements/entrees/index.php'; $cancel_link = PLUGIN_URL . 'mouvements/entrees/index.php';
$legend_part = "répertorié"; $legend_part = "répertorié";
$tpl_materiel_name = "repertorie"; $tpl_materiel_name = "repertorie";