346 lines
12 KiB
PHP
346 lines
12 KiB
PHP
<?php
|
|
/******************************************************************************
|
|
* YMS - Yacht Management Software
|
|
* Copyright (C) 2024 Thomas Hooge
|
|
*
|
|
* SPDX-License-Identifier: WTFPL
|
|
******************************************************************************/
|
|
|
|
require 'globals.inc';
|
|
require 'lib/gpc.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[':weight'] = min(gpc_get_float($_POST, 'weight'), 999.99); // limit max value
|
|
$p[':weight_net'] = min(gpc_get_float($_POST, 'weight_net'), 999.99);
|
|
$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');
|
|
if ($p[':sid'] <= 0) $p[':sid'] = NULL;
|
|
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 =======================================
|
|
|
|
page_caption_search($pagetitle);
|
|
|
|
// 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 ($flt->cat == -1) {
|
|
$w[] = 'provcat IS NULL';
|
|
} elseif ($flt->cat > 0) {
|
|
$w[] = 'provcat=:provcat';
|
|
$p[':provcat'] = $flt->cat;
|
|
}
|
|
if (strlen($flt->txt) > 1) {
|
|
$w[] = 'provname LIKE :txt';
|
|
$p[':txt'] = '%'.$flt->txt.'%';
|
|
}
|
|
$where = join(' AND ', $w);
|
|
|
|
$order = ' ORDER BY provid';
|
|
|
|
// get total record count for pagination and limit
|
|
$sql = "SELECT COUNT(*) FROM provisions LEFT OUTER JOIN storage USING (sid) WHERE " . $where;
|
|
$sth = $pdo->prepare($sql);
|
|
try {
|
|
$sth->execute($p);
|
|
} catch(PDOException $e) {
|
|
$g_error->Add($e->getMessage());
|
|
$g_error->Add($sql);
|
|
$g_error->Add(print_r($p, true));
|
|
$g_error->PrintOut();
|
|
}
|
|
$numrows = $sth->fetchColumn();
|
|
$page = gpc_get_int($_REQUEST, 'p', 1);
|
|
$rows_pp = gpc_get_int($_REQUEST, 'n', $user->rows_pp);
|
|
$lastpage = ceil($numrows/$rows_pp);
|
|
|
|
// provisions can be assigned to a store or be free
|
|
// d0 is the maximum number of days age
|
|
// d1 is the current age in days. If negative then it is over limit
|
|
// TODO display over with warning levels: 50% over, 100% over etc.
|
|
$sql = "SELECT provid, provname, number, unit, storedate, shelflife, sid,"
|
|
. " DATEDIFF(shelflife, storedate) AS d0, DATEDIFF (shelflife, CURDATE()) AS d1 "
|
|
. "FROM provisions LEFT OUTER JOIN storage USING (sid)";
|
|
$sql .= ' WHERE ' . $where;
|
|
$sql .= $order;
|
|
|
|
// if pagination:
|
|
$sql .= ' LIMIT ' . ($page - 1) * $rows_pp . ',' . $rows_pp;
|
|
|
|
$sth = $pdo->prepare($sql);
|
|
$sth->execute($p);
|
|
$res = $sth->fetchAll();
|
|
|
|
?>
|
|
<form method="post" action="<?=$g_scriptname?>">
|
|
<div id="filter" class="card">
|
|
<div class="card-header"><i class="bi bi-funnel me-2"></i>Filter</div>
|
|
<div class="card-body d-flex flex-wrap gap-3">
|
|
<?php
|
|
filter_create_select('flt_cat', _('Category'), $g_opt_all + $g_opt_none + $opt_category, $flt->cat);
|
|
?>
|
|
<div class="d-flex flex-nowrap gap-2">
|
|
<label for="flt_txt">Text</label>
|
|
<input type="text" class="form-control" name="flt_txt" maxlength="30" value="<?=$flt->txt;?>">
|
|
</div>
|
|
</div>
|
|
<div class="card-footer">
|
|
<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
|
|
// Pagination on top
|
|
$pagination = get_pagination($g_scriptname, '', $page, $lastpage);
|
|
echo $pagination;
|
|
|
|
echo '<table class="table table-striped">', "\n";
|
|
echo "<thead>\n";
|
|
echo "<tr>\n";
|
|
echo '<th>#</th>';
|
|
echo '<th>', _('Description'), "</th>";
|
|
echo '<th>', _('Number'), "</th>";
|
|
echo '<th>', _('Unit'), "</th>";
|
|
echo '<th>', _('Stored'), "</th>";
|
|
echo '<th>', _('Best before'), "</th>";
|
|
echo "<th></th>"; // Meter
|
|
echo "<th></th>\n"; // Buttons
|
|
echo "</tr>\n";
|
|
echo "</thead>\n";
|
|
|
|
$i = ($page - 1) * $rows_pp;
|
|
$i0 = $i + 1;
|
|
foreach ($res as $row) {
|
|
$i++; // record number
|
|
echo "<tr>\n";
|
|
echo "<td>", $i;
|
|
echo '<a class="text-nowrap" title="', _('View'), '" href="', $g_scriptname, '?f=view&id=', $row['provid'], '"><i class="bi bi-eye ms-2"></i></a>', "\n";
|
|
echo "</td>\n";
|
|
echo "<td>", ! isset($row['sid']) ? '<i class="bi bi-cart3"></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";
|
|
// TODO shelflife in der Vergangenheit
|
|
echo "<td>";
|
|
if ($row['d1'] > 0) {
|
|
echo '<meter min="0" max="', $row['d0'], '" value="', $row['d1'], '"></meter>';
|
|
} else {
|
|
if ($row['d1'] < 0) {
|
|
echo '<i class="bi bi-exclamation-triangle-fill"></i> ', abs($row['d1']);
|
|
}
|
|
}
|
|
echo "</td>\n";
|
|
// Edit current record button
|
|
echo "<td>";
|
|
echo '<a title="', _('Edit'), '" href="', $g_scriptname, '?f=edit&id=', $row['provid'], '"><i class="bi-pencil"></i></a>';
|
|
echo "</td>\n";
|
|
|
|
echo "</tr>\n";
|
|
}
|
|
// Table summary
|
|
table_rec_summary($i0, $i, $numrows, 8);
|
|
echo "</table>\n";
|
|
|
|
echo $pagination;
|
|
|
|
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'), $g_opt_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, weight, weight_net, 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>", _('Weight'), "</th><td>", format_float($prov->weight, 2, 'kg'), "</td></tr>\n";
|
|
echo "<tr><th>", _('Weight net'), "</th><td>", format_float($prov->weight_net, 2, 'kg'), "</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, weight, weight_net, 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>
|
|
<div class="mb-3">
|
|
<label for="weight" class="form-label"><?=_('Weight, kg')?></label>
|
|
<input type="text" class="form-control" id="weight" name="weight" value="<?=format_float($prov->weight); ?>">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="weight_net" class="form-label"><?=_('Weight net, kg')?></label>
|
|
<input type="text" class="form-control" id="weight" name="weight_net" value="<?=format_float($prov->weight_net); ?>">
|
|
</div>
|
|
<?php
|
|
form_create_select('unit', _('Unit'), $opt_unit, $prov->unit);
|
|
form_create_select('provcat', _('Category'), $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'), $g_opt_none + $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';
|