Files
YMS/webgui/storage.php
T
2024-04-10 20:13:37 +02:00

326 lines
10 KiB
PHP

<?php
/******************************************************************************
* YMS - Yacht Management Software
* Copyright (C) 2024 Thomas Hooge
*
* SPDX-License-Identifier: WTFPL
******************************************************************************/
require 'globals.inc';
$pagetitle = _('Storage');
$id = gpc_get_int($_REQUEST, 'id', 0);
$stype = db_get_options(3);
// ========== 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 'insert':
$sname = gpc_get_string($_POST, 'sname');
$stype = gpc_get_int($_POST, 'stype');
$sql = "INSERT INTO storage "
. " (vid, sname, stype) "
. "VALUES "
. " (:vid, :sname, :stype)";
$sth = $pdo->prepare($sql);
$sth->bindValue(':vid', $user->vid, PDO::PARAM_INT);
$sth->bindValue(':sname', $sname, PDO::PARAM_STR);
$sth->bindValue(':stype', $stype, PDO::PARAM_INT);
$sth->execute();
$id = $pdo->lastInsertId();
$action = ACT_VIEW;
break;
case 'update':
$sname = gpc_get_string($_POST, 'sname');
$x = gpc_get_int($_POST, 'x');
$y = gpc_get_int($_POST, 'y');
$z = gpc_get_int($_POST, 'z');
$remarks = gpc_get_string($_POST, 'remarks', 150);
$sql = "UPDATE storage "
. "SET sname=:sname,"
. " x=:x, y=:y, z=:z,"
. " remarks=:remarks "
. "WHERE sid=:sid";
$sth = $pdo->prepare($sql);
$sth->bindValue(':sid', $id, PDO::PARAM_INT);
$sth->bindValue(':sname', $sname, PDO::PARAM_STR);
$sth->bindValue(':x', $x, PDO::PARAM_INT);
$sth->bindValue(':y', $y, PDO::PARAM_INT);
$sth->bindValue(':z', $z, PDO::PARAM_INT);
$sth->bindValue(':remarks', $remarks, PDO::PARAM_STR);
$sth->execute();
$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;
}
unset($_SESSION['token']);
// check if storage is empty
// - no assigned boxes
$sth = $pdo->prepare("SELECT COUNT(*) FROM box WHERE sid=?");
$sth->execute([$id]);
if ($sth->fetchColumn() > 0) {
$g_error->Add(_('Delete failed, assigned boxes detected!'));
$action = ACT_VIEW;
break;
}
// - no assigned equipment
$sth = $pdo->prepare("SELECT COUNT(*) FROM inventory WHERE boxtype='storage' AND sid=?");
$sth->execute([$id]);
if ($sth->fetchColumn() > 0) {
$g_error->Add(_('Delete failed, assigned equipment detected!'));
$action = ACT_VIEW;
break;
}
$sth = $pdo->prepare("DELETE FROM storage WHERE sid=?");
$sth->execute([$id]);
$g_message->Add(sprintf(_('Deleted storage no. %d'), $id));
$action = ACT_DEFAULT;
break;
default:
$g_error->Add(sprintf(_("Unknown function '%s'!"), $submit));
$valid = FALSE;
}
// ========== ACTIONS END =====================================================
require 'header.php';
// ========== PAGE CONTENT ====================================================
if ($action == ACT_DEFAULT):
// ========== VARIANT: default behavior =======================================
echo '<h1>', $pagetitle, "</h1>\n";
// Storage
// stype from dropdown
$sql = "SELECT sid, sname, remarks "
. "FROM storage "
. "WHERE vid=? "
. "ORDER BY sid";
$sth = $pdo->prepare($sql);
$sth->execute([$user->vid]);
$res = $sth->fetchAll();
echo '<table class="table table-striped">', "\n";
echo "<thead>\n";
echo "<tr>";
echo "<th>" . _('Name') . "</th>";
echo "<th>" . _('Remarks') . "</th>";
echo "<th>&nbsp;</th>";
echo "</tr>\n";
echo "</thead>\n";
foreach ($res as $row) {
echo "<tr>\n";
echo "<td>". $row['sname'] ."</td>\n";
echo "<td>". $row['remarks'] ."</td>\n";
form_action_buttons($g_scriptname, $row['sid']);
echo "</tr>\n";
}
echo "<tfoot>\n";
echo '<tr><td colspan="3">', sprintf(_('%d records'), count($res)), '</td></tr>', "\n";
echo "</tfoot>\n";
echo "</table>\n";
form_add_button($g_scriptname);
// Boxes
echo "<h1>", _('Box'), "</h1>\n";
$sql = "SELECT boxid, sid, label, color, content, remarks "
. "FROM box "
. "ORDER BY label";
$sth = $pdo->query($sql);
$res = $sth->fetchAll();
echo '<table class="table table-striped">', "\n";
echo "<thead>\n";
echo "<tr>";
echo "<th>" . _('Label') . "</th>";
echo "<th>" . _('Color') . "</th>";
echo "<th>" . _('Content') . "</th>";
echo "<td></td>";
echo "</tr>\n";
foreach ($res as $row) {
echo "<tr>\n";
echo "<td>". $row['label'] ."</td>\n";
echo '<td><span style="background-color:#', $row['color'],'">'. $row['color'] ."</span></td>\n";
echo "<td>". $row['content'] ."</td>\n";
form_action_buttons('box.php', $row['boxid']);
echo "</tr>\n";
}
// Table summary
echo "<tfoot>\n";
echo '<tr><td colspan="4">', sprintf(_('%d records'), count($res)), '</td></tr>', "\n";
echo "</tfoot>\n";
echo "</table>\n";
form_add_button('box.php');
elseif ($action == ACT_ADD):
// ========== VARIANT: add record =============================================
echo '<h2>', _('Add Storage'), "</h2>\n";
?>
<form method="post" action="<?=$g_scriptname?>">
<div class="mb-3">
<label for="sname" class="form-label"><?=_('Name')?></label>
<input type="text" class="form-control" id="sname" name="sname">
</div>
<div class="mb-3">
<select name="stype" class="form-control">
<?php
foreach ($stype as $k => $v) {
echo '<option value="', $k ,'">', $v, "</option>\n";
}
?>
</select>
</div>
<button type="submit" name="submit[insert]" class="btn btn-primary"><?=_('Save')?></button>
<a href="<?=$g_scriptname?>" class="btn btn-secondary"><?=_('Back')?></a>
</form>
<?php
elseif ($action == ACT_VIEW):
// ========== VARIANT: view single record =====================================
$opt_unit = db_get_options(6);
$sql = "SELECT sid, sname, stype, capacity, capaunit, x, y, z, remarks "
. "FROM storage "
. "WHERE sid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$storage = $sth->fetch(PDO::FETCH_OBJ);
echo '<h2>', $storage->sname, "</h2>\n";
echo '<table class="table">', "\n";
echo '<tr><th style="width:20%">', _('Typ'),"</th><td>", $stype[$storage->stype], "</td></tr>\n";
echo '<tr><th>', _('Capacity'),"</th><td>", $storage->capacity, ' ', $storage->capaunit, "</td></tr>\n";
echo '<tr><th>', _('Location (x, y, z)'),"</th><td>(", $storage->x ?? '?', ', ', $storage->y ?? '?', ', ', $storage->z, ")</td></tr>\n";
echo '<tr><th>', _('Remarks'),"</th><td>", $storage->remarks, "</td></tr>\n";
echo "</table>\n";
form_view_buttons($g_scriptname, $id);
// Zugeordnete Kisten hier anzeigen
echo '<h3>', _('Boxes in storage'), "</h3>\n";
$sql = "SELECT boxid, label, color FROM box WHERE sid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$res = $sth->fetchall();
if (count($res) > 0) {
foreach ($res as $row) {
echo "$boxid $label $color";
}
} else {
echo '<p>', _('No boxes contained'), "</p>\n";
}
echo '<h3>', _('Provisions in storage'), "</h3>\n";
$sql = "SELECT provid, provname, number, unit, storedate, shelflife FROM provisions WHERE sid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$res = $sth->fetchall();
if (count($res) > 0) {
echo '<table class="table table-sm">';
foreach ($res as $row) {
echo "<tr>";
echo "<td>", $row['provname'], "</td>";
echo "<td>", $row['number'], ' ', $opt_unit[$row['unit']], "</td>";
echo "<td>", $row['storedate'], ' - ', $row['shelflife'], "</td>";
echo "</tr>\n";
}
echo "</table>\n";
} else {
echo '<p>', _('No provisions contained'), "</p>\n";
}
elseif ($action == ACT_EDIT):
// ========== VARIANT: edit single record =====================================
echo '<h2>', _('Edit Storage'), "</h2>\n";
$sql = "SELECT sid, sname, stype, remarks "
. "FROM storage "
. "WHERE sid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$storage = $sth->fetch(PDO::FETCH_OBJ);
?>
<form method="post" action="<?=$g_scriptname?>">
<input type="hidden" name="id" value="<?=$id?>">
<div class="mb-3">
<label for="sname" class="form-label"><?=_('Name')?></label>
<input type="text" class="form-control" id="sname" name="sname" value="<?=$storage->sname ?>">
</div>
<div class="mb-3">
<label for="x" class="form-label"><?=_('Position x')?></label>
<input type="number" class="form-control" id="x" name="x" value="<?=$storage->x ?>">
</div>
<div class="mb-3">
<label for="y" class="form-label"><?=_('Position y')?></label>
<input type="number" class="form-control" id="y" name="y" value="<?=$storage->y ?>">
</div>
<div class="mb-3">
<label for="z" class="form-label"><?=_('Position z')?></label>
<input type="number" class="form-control" id="z" name="z" value="<?=$storage->z ?>">
</div>
<div class="mb-3">
<label for="remarks" class="form-label"><?=_('Remarks')?></label>
<textarea class="form-control" id="remarks" name="remarks" rows="3"><?=$storage->remarks ?></textarea>
</div>
<button type="submit" name="submit[update]" class="btn btn-primary"><?=_('Save')?></button>
<a href="<?=$g_scriptname?>?f=view&id=<?=$id?>" class="btn btn-secondary"><?=_('Back')?></a>
</form>
<?php
elseif ($action == ACT_DELETE):
// ========== VARIANT: delete record ==========================================
$_SESSION['token'] = bin2hex(random_bytes(8));
$sql = "SELECT sname, remarks FROM storage WHERE sid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$storage = $sth->fetch(PDO::FETCH_OBJ);
echo '<h2>', _('Delete Storage'), "</h2>\n";
echo '<p>', sprintf(_('Record no. %d'), $id), "</p>\n";
echo '<p>Name: ', $storage->sname, "</p>";
echo '<p>Remarks: ', $storage->remarks, "</p>";
echo '<p>', _('Deleting a storage is final. There is no way back. Only delete if you are absolute sure.'), "</p>\n";
form_delete_buttons($g_scriptname, $id, $_SESSION['token']);
else:
// ========== ERROR UNKNOWN VARIANT ===========================================
echo '<p>', _('Unknown function call: Please report to system development!'), "</p>\n";
endif; // $action == ...
// ========== END OF VARIANTS =================================================
include 'footer.php';