Files
YMS/webgui/box.php
T
2025-04-01 14:49:03 +02:00

255 lines
8.6 KiB
PHP

<?php
/******************************************************************************
* YMS - Yacht Management Software
* Copyright (C) 2024 Thomas Hooge
*
* SPDX-License-Identifier: WTFPL
******************************************************************************/
require 'globals.inc';
$pagetitle = _('Boxes');
$id = gpc_get_int($_REQUEST, 'id', 0);
// Storage options
$sql = "SELECT sid, sname FROM storage WHERE vid=? ORDER BY sname";
$sth = $pdo->prepare($sql);
$sth->execute([$user->vid]);
$opt_storage = array();
foreach ($sth->fetchAll() as $row) {
$opt_storage[$row['sid']] = $row['sname'];
}
$opt_boxtype = db_get_options(9);
// ========== 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':
$p[':label'] = gpc_get_string($_POST, 'label');
$p[':sid'] = gpc_get_int($_POST, 'sid');
$p[':content'] = gpc_get_string($_POST, 'content');
$p[':remarks'] = gpc_get_string($_POST, 'remarks');
$id = db_exec_insert('box', $p);
$action = ACT_VIEW;
break;
case 'update':
$p[':boxid'] = $id;
$p[':boxtype'] = gpc_get_int($_POST, 'boxtype');
if ($p[':boxtype'] == -1) $p[':boxtype'] = NULL;
$p[':label'] = gpc_get_string($_POST, 'label');
$p[':content'] = gpc_get_string($_POST, 'content');
$p[':color'] = gpc_get_color($_POST, 'color');
$p[':weight'] = min(gpc_get_float($_POST, 'weight'), 99.99);
$p[':remarks'] = gpc_get_string($_POST, 'remarks', 150);
db_exec_update('box', $p, 'boxid');
$action = ACT_VIEW;
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";
$sql = "SELECT boxid, boxtype, sid, label, color, content, weight, 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>" . _('Type') . "</th>";
echo "<th>" . _('Color') . "</th>";
echo "<th>" . _('Content') . "</th>";
echo '<th class="text-end">', _('Weight'), "</th>";
echo "<td></td>";
echo "</tr>\n";
foreach ($res as $row) {
echo "<tr>\n";
echo '<td>', $row['label'], "</td>\n";
echo '<td>', ($opt_boxtype[$row['boxtype']] ?? ''), "</td>\n";
echo '<td>', format_color(strtoupper($row['color'])), "</td>\n";
echo '<td>', $row['content'], "</td>\n";
echo '<td class="text-end">', format_float($row['weight'], 2), "</td>\n";
form_action_buttons($g_scriptname, $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 Box'), "</h2>\n";
?>
<form method="post" action="<?=$g_Scriptname?>">
<div class="mb-3">
<label for="label" class="form-label"><?=_('Label')?></label>
<input type="text" maxlength="20" class="form-control" id="label" name="label" autofocus>
</div>
<div class="mb-3">
<label for="sid" class="form-label"><?=_('Storage')?></label>
<select name="sid" id="sid" class="form-control">
<?php
foreach ($opt_storage as $k => $v) {
echo '<option value="', $k, '">', $v, "</option>\n";
}
?>
</select>
</div>
<div class="mb-3">
<label for="content" class="form-label"><?=_('Content')?></label>
<input type="text" maxlength="60" class="form-control" id="content" name="content">
</div>
<div class="mb-3">
<label for="remarks" class="form-label"><?=_('Remarks')?></label>
<textarea class="form-control" id="remarks" name="remarks" rows="3"></textarea>
</div>
<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 boxtype, label, content, color, weight, box.remarks, box.sid, sname "
. "FROM box LEFT JOIN storage USING (sid) "
. "WHERE boxid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$box = $sth->fetch(PDO::FETCH_OBJ);
echo '<h2>', $box->label, "</h2>\n";
echo '<table class="table">', "\n";
echo '<tr><th style="width:20%">', _('Box type'),"</th><td>", ($opt_boxtype[$box->boxtype] ?? ''), "</td></tr>\n";
echo '<tr><th>', _('Content'),"</th><td>", $box->content, "</td></tr>\n";
echo '<tr><th>', _('Color'), '</th><td>', format_color($box->color), "</td></tr>\n";
echo '<tr><th>', _('Weight'), '</th><td>', format_float($box->weight, 2, 'kg'), "</td></tr>\n";
echo '<tr><th>', _('Storage'),"</th><td>", $box->sname, '&nbsp;';
echo '<a title="', _('View'), '" href="storage.php?f=view&id=', $box->sid, '"><i class="bi-eye"></i></a>';
echo "</td></tr>\n";
echo '<tr><th>', _('Remarks'),"</th><td>", $box->remarks, "</td></tr>\n";
echo "</table>\n";
form_view_buttons($g_scriptname, $id);
// Inventory in box
// WIP
$sql = "SELECT invid, invname, weight FROM inventory WHERE conttype='box' AND boxid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$res = $sth->fetchAll();
echo '<h3>', _('Inventory in box'), "</h3>\n";
$weight_total = 0.0;
if ($res) {
echo '<table class="table table-striped">', "\n";
echo "<thead>\n";
echo "<tr>";
echo '<th>', _('Inventory'), "</th>";
echo '<th>', _('Weight'), "</th>";
echo "<td></td>";
echo "</tr>\n";
foreach ($res as $row) {
echo "<tr>\n";
echo "<td>", $row['invname'], "</td>\n";
echo "<td>", format_float($row['weight'], 2, 'kg'), "</td>\n";
form_action_buttons('inventory.php', $row['invid']);
echo "</tr>\n";
$weight_total += $row['weight'];
}
echo "<tfoot>";
echo '<tr><td></td><td>', format_float($weight_total, 2, 'kg'), '</td>', "\n";
echo "</tfoot>\n";
echo "</table>\n";
} else {
echo '<p>', _('Box is empty'), "</p>\n";
}
elseif ($action == ACT_EDIT):
// ========== VARIANT: edit single record =====================================
$sql = "SELECT boxtype, label, content, color, weight, remarks "
. "FROM box "
. "WHERE boxid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$box = $sth->fetch(PDO::FETCH_OBJ);
$opt_none = [ -1 => _('&horbar; none &horbar;')];
echo '<h2>', _('Edit Box'), "</h2>\n";
?>
<form method="post" action="<?=$g_scriptname?>">
<input type="hidden" name="id" value="<?=$id?>">
<div class="mb-3">
<label for="label" class="form-label"><?=_('Label')?></label>
<input type="text" class="form-control" id="label" name="label" value="<?=$box->label ?>">
</div>
<?php form_create_select('boxtype', _('Box type'), $opt_none + $opt_boxtype, $box->boxtype); ?>
<div class="mb-3">
<label for="content" class="form-label"><?=_('Content')?></label>
<input type="text" class="form-control" id="content" name="content" value="<?=$box->content ?>">
</div>
<div class="mb-3">
<label for="color"><?=_('Color')?></label>
<input type="color" class="form-control" id="color" name="color" value="#<?=$box->color ?>">
</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($box->weight); ?>">
</div>
<div class="mb-3">
<label for="remarks" class="form-label"><?=_('Remarks')?></label>
<textarea class="form-control" id="remarks" name="remarks" rows="3"><?=$box->remarks ?></textarea>
</div>
<?php
form_edit_buttons($g_scriptname, $id);
echo "</form>\n";
elseif ($action == ACT_DELETE):
// ========== VARIANT: delete record ==========================================
else:
// ========== ERROR UNKNOWN VARIANT ===========================================
echo '<p>', _('Unknown function call: Please report to system development!'), "</p>\n";
endif; // $action == ...
// ========== END OF VARIANTS =================================================
include 'footer.php';