Files
YMS/webgui/provisions.php
T

276 lines
9.4 KiB
PHP

<?php
/******************************************************************************
* YMS - Yacht Management Software
* Copyright (C) 2024 Thomas Hooge
*
* SPDX-License-Identifier: WTFPL
******************************************************************************/
require 'globals.inc';
$pagetitle=_('Provisions');
$id = gpc_get_int($_REQUEST, 'id', 0);
$opt_storage = db_get_opt_storage($user->vid);
$opt_category = db_get_options(5);
$opt_unit = db_get_options(6);
// ========== ACTIONS START ===================================================
switch ($submit = form_get_action()) {
case NULL: break;
case 'add': $action = ACT_ADD; break;
case 'view': $action = ACT_VIEW; break;
case 'edit': $action = ACT_EDIT; break;
case 'del': $action = ACT_DELETE; break;
case 'filter':
$flt = array();
$flt['cat'] = gpc_get_enum($_POST, 'flt_cat', array_merge(array_keys($opt_category), ['-2', '-1']));
$flt['txt'] = gpc_get_string($_POST, 'flt_txt');
db_save_filter($flt, 202);
$action = ACT_DEFAULT;
break;
case 'freset':
db_clear_filter(202);
$action = ACT_DEFAULT;
break;
case 'insert':
$p[':provname'] = gpc_get_string($_POST, 'provname');
$p[':number'] = gpc_get_int($_POST, 'number');
$p[':unit'] = gpc_get_int($_POST, 'unit');
$p[':storedate'] = gpc_get_string($_POST, 'storedate');
$p[':shelflife'] = gpc_get_string($_POST, 'shelflife');
$id = db_exec_insert('provisions', $p);
$action = ACT_VIEW;
break;
case 'update':
$p[':provid'] = $id;
$p[':provname'] = gpc_get_string($_POST, 'provname');
$p[':provcat'] = gpc_get_int($_POST, 'provcat');
$p[':number'] = gpc_get_int($_POST, 'number');
$p[':unit'] = gpc_get_int($_POST, 'unit');
$p[':storedate'] = gpc_get_string($_POST, 'storedate');
$p[':shelflife'] = gpc_get_string($_POST, 'shelflife');
$p[':sid'] = gpc_get_int($_POST, 'sid');
db_exec_update('provisions', $p, 'provid');
$action = ACT_VIEW;
break;
case 'delete':
// Security token needed!
if (gpc_get_string($_POST, 'token', 16) != $_SESSION['token']) {
$g_error->Add(_('Delete prohibited, invalid security token!'));
$action = ACT_VIEW;
break;
}
$action = ACT_DEFAULT;
break;
default:
$g_error->Add(sprintf(_('Unknown function!'), $submit));
$valid = FALSE;
}
// ========== ACTIONS END =====================================================
require 'header.php';
// ========== PAGE CONTENT ====================================================
if ($action == ACT_DEFAULT):
// ========== VARIANT: default behavior =======================================
// load filter from db
$flt = db_get_filter($user->id, 202);
$w = array('(vid=:vid OR sid IS NULL)');
$p = array(':vid' => $user->vid);
if (strlen($flt->txt) > 1) {
$w[] = 'provname LIKE :txt';
$p[':txt'] = '%'.$flt->txt.'%';
}
$where = join(' AND ', $w);
$order = ' ORDER BY provid';
// provisions can be assigned to a store or be free
$sql = "SELECT provid, provname, number, unit, storedate, shelflife, sid,"
. " DATEDIFF(shelflife, storedate) AS d0, DATEDIFF (CURDATE(), storedate) AS d1 "
. "FROM provisions LEFT OUTER JOIN storage USING (sid)";
$sql .= ' WHERE ' . $where;
$sql .= $order;
$sth = $pdo->prepare($sql);
$sth->execute($p);
$res = $sth->fetchAll();
page_caption_search($pagetitle);
?>
<form method="post" action="<?=$g_scriptname?>">
<div id="filter" class="card">
<div class="card-header">Filter</div>
<div class="card-body">
<?php
filter_create_select('flt_cat', _('Category'), $g_opt_all + $g_opt_none + $opt_category, $flt->cat);
?>
<label for="flt_txt">Text</label>
<input type="text" name="flt_txt" size="15" maxlength="30" value="<?=$flt->txt?>">
<button name="submit[filter]" class="btn btn-sm btn-primary" title="<?=_('Apply')?>"><?=_('Go')?></button>
<button name="submit[freset]" class="btn btn-sm btn-secondary float-end" title="<?=_('Reset filter to defaults')?>"><?=_('Clear')?></button>
</div>
</div>
</form>
<?php
echo '<table class="table table-striped">', "\n";
echo "<thead>\n";
echo "<tr>\n";
echo '<th>', _('Description'), "</th>";
echo '<th>', _('Number'), "</th>";
echo '<th>', _('Unit'), "</th>";
echo '<th>', _('Stored'), "</th>";
echo '<th>', _('Best before'), "</th>";
echo "<th></th>";
echo "<th></th>\n";
echo "</tr>\n";
echo "</thead>\n";
foreach ($res as $row) {
echo "<tr>\n";
echo "<td>", ! isset($row['sid']) ? '<i class="bi bi-basket"></i> ' : '', $row['provname'], "</td>\n";
echo "<td>". $row['number'] ."</td>\n";
echo "<td>". $opt_unit[$row['unit']] ."</td>\n";
echo "<td>". $row['storedate'] ."</td>\n";
echo "<td>". $row['shelflife'] ."</td>\n";
echo "<td>";
echo '<meter min="0" max="', $row['d0'], '" value="', $row['d1'], '">';
// Action buttons
form_action_buttons($g_scriptname, $row['provid']);
echo "</td>\n";
echo "</tr>\n";
}
// Table summary
echo "<tfoot>\n";
echo '<tr><td colspan="6">', sprintf(_('%d records'), count($res)), '</td></tr>', "\n";
echo "</tfoot>\n";
echo "</table>\n";
form_add_button($g_scriptname);
elseif ($action == ACT_ADD):
// ========== VARIANT: add record =============================================
echo '<h2>', _('Add provisions'), "</h2>\n";
?>
<form method="post" action="<?=$g_scriptname?>">
<div class="mb-3">
<label for="provname" class="form-label"><?=_('Name')?></label>
<input type="text" class="form-control" id="provname" name="provname" required autofocus>
</div>
<div class="mb-3">
<label for="number"><?=_('Number')?></label>
<input type="number" min="0" max="1000" class="form-control" id="number" name="number" value="1">
</div>
<?php form_create_select('unit', _('Unit'), $opt_unit); ?>
<div class="mb-3">
<label for="storedate"><?=_('Storedate')?></label>
<input type="date" class="form-control" id="storedate" name="storedate" value="<?php echo date('Y-m-d'); ?>">
</div>
<div class="mb-3">
<label for="shelflife"><?=_('Shelflife')?></label>
<input type="date" class="form-control" id="shelflife" name="shelflife">
</div>
<?php form_create_select('sid', _('Storage'), [-1 => '- none -'] + $opt_storage); ?>
<div class="container-fluid px-0 my-3">
<button type="submit" name="submit[insert]" class="btn btn-primary"><?=_('Save')?></button>
<a href="<?=$g_scriptname?>" class="btn btn-secondary"><?=_('Back')?></a>
</div>
</form>
<?php
elseif ($action == ACT_VIEW):
// ========== VARIANT: view single record =====================================
$sql = "SELECT provname, provcat, number, unit, storedate, shelflife, sid "
. "FROM provisions "
. "WHERE provid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$prov = $sth->fetch(PDO::FETCH_OBJ);
echo '<h2>', $prov->provname, "</h2>\n";
echo '<table class="table">', "\n";
echo '<tr><th style="width:20%">', _('Number'),"</th><td>", $prov->number, "</td></tr>\n";
echo '<tr><th>', _('Unit'),"</th><td>", $opt_unit[$prov->unit], "</td></tr>\n";
echo '<tr><th>', _('Category'),"</th><td>", $opt_category[$prov->provcat] ?? 'n/a', "</td></tr>\n";
echo '<tr><th>', _('Storedate'),"</th><td>", $prov->storedate, "</td></tr>\n";
echo '<tr><th>', _('Shelflife'),"</th><td>$prov->shelflife</td></tr>\n";
echo '<tr><th>', _('Storage'),"</th><td>", ($opt_storage[$prov->sid] ?? ''), "</td></tr>\n";
echo "</table>\n";
form_view_buttons($g_scriptname, $id);
elseif ($action == ACT_EDIT):
// ========== VARIANT: edit single record =====================================
$sql = "SELECT provname, provcat, number, unit, storedate, shelflife, sid "
. "FROM provisions "
. "WHERE provid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$prov = $sth->fetch(PDO::FETCH_OBJ);
echo '<h2>', _('Edit provision'), "</h2>\n";
?>
<form method="post" action="<?=$g_scriptname?>">
<input type="hidden" name="id" value="<?=$id?>">
<div class="mb-3">
<label for="provname" class="form-label"><?=_('Name')?></label>
<input type="text" class="form-control" id="provname" name="provname" value="<?=$prov->provname ?>">
</div>
<div class="mb-3">
<label for="number"><?=_('Number')?></label>
<input type="number" min="0" max="1000" class="form-control" id="number" name="number" value="<?=$prov->number ?>">
</div>
<?php
form_create_select('unit', _('Unit'), $opt_unit, $prov->unit);
form_create_select('provcat', _('Category'), array_merge($g_opt_none, $opt_category), $prov->provcat);
?>
<div class="mb-3">
<label for="storedate"><?=_('Storedate')?></label>
<input type="date" class="form-control" id="storedate" name="storedate" value="<?=$prov->storedate ?>">
</div>
<div class="mb-3">
<label for="shelflife"><?=_('Shelflife')?></label>
<input type="date" class="form-control" id="shelflife" name="shelflife" value="<?=$prov->shelflife ?>">
</div>
<?php
form_create_select('sid', _('Storage'), $opt_storage, $prov->sid);
form_edit_buttons($g_scriptname, $id);
echo "</form>\n";
elseif ($action == ACT_DELETE):
// ========== VARIANT: delete record ==========================================
echo '<h2>', _('Delete provisions'), "</h2>\n";
else:
// ========== ERROR UNKNOWN VARIANT ===========================================
echo '<p>', _('Unknown function call: Please report to system development!'), "</p>\n";
endif; // $action == ...
// ========== END OF VARIANTS =================================================
include 'footer.php';