372 lines
13 KiB
PHP
372 lines
13 KiB
PHP
<?php
|
||
/******************************************************************************
|
||
* YMS - Yacht Management Software
|
||
* Copyright (C) 2024-2026 Thomas Hooge
|
||
*
|
||
* SPDX-License-Identifier: WTFPL
|
||
******************************************************************************/
|
||
|
||
/*
|
||
Box weight can be entered directly and is meant as weight of box itself plus
|
||
weights of contained items.
|
||
The weight of the contained items can be displayed separately
|
||
There can be a difference because not all items have a weight entered
|
||
*/
|
||
|
||
require 'globals.inc';
|
||
require 'lib/db.inc';
|
||
require 'lib/gpc.inc';
|
||
|
||
$pagetitle = _('Boxes');
|
||
|
||
$id = gpc_get_int($_REQUEST, 'id', 0);
|
||
|
||
// TODO use global function
|
||
// 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_uint($_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[':wx'] = gpc_get_int($_POST, 'wx');
|
||
$p[':wy'] = gpc_get_int($_POST, 'wy');
|
||
$p[':wz'] = gpc_get_int($_POST, 'wz');
|
||
$p[':weight'] = min(gpc_get_float($_POST, 'weight'), 99.99);
|
||
$p[':weight_net'] = min(gpc_get_float($_POST, 'weight_net'), 99.99);
|
||
$p[':price'] = gpc_get_currency($_POST, 'price');
|
||
$p[':sid'] = gpc_get_uint($_POST, 'sid');
|
||
$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>#</th>';
|
||
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";
|
||
$i = 0;
|
||
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['boxid'], '"><i class="bi bi-eye ms-2"></i></a>', "\n";
|
||
echo "</td>\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";
|
||
// Edit current record button
|
||
echo "<td>";
|
||
echo '<a title="', _('Edit'), '" href="', $g_scriptname, '?f=edit&id=', $row['boxid'], '"><i class="bi-pencil"></i></a>', "\n";
|
||
echo "</td>\n";
|
||
echo "</tr>\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, box.color, weight, weight_net, price, "
|
||
. "box.wx, box.wy, box.wz, box.sid, box.remarks, 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";
|
||
|
||
// Bootstrap grid
|
||
echo '<div class="container">';
|
||
echo '<div class="row">';
|
||
echo '<div class="col">';
|
||
|
||
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>', _('Dimensions'), '</th><td>', format_measurement(3, 'mm', $box->wx, $box->wy, $box->wz), "</td></tr>\n";
|
||
# TODO also show sum of content weights for comparison
|
||
echo '<tr><th>', _('Weight'), '</th><td>', format_float($box->weight, 2, 'kg'), "</td></tr>\n";
|
||
echo '<tr><th>', _('Empty Weight'), '</th><td>', format_float($box->weight_net, 2, 'kg'), "</td></tr>\n";
|
||
echo '<tr><th>', _('Price'),"</th><td>", format_currency($box->price), "</td></tr>\n";
|
||
echo '<tr><th>', _('Storage'),"</th><td>", $box->sname, ' ';
|
||
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>", h($box->remarks, br:true), "</td></tr>\n";
|
||
echo "</table>\n";
|
||
|
||
form_view_buttons($g_scriptname, $id);
|
||
|
||
echo '</div>'; // Column break
|
||
echo '<div class="col">', "\n";
|
||
|
||
echo '<div class="card mb-2">', "\n";
|
||
echo '<div class="card-header">', _('Images'), "</div>\n";
|
||
echo '<div class="card-body d-flex flex-wrap gap-3">', "\n";
|
||
|
||
$sql = "SELECT d.docid, d.title "
|
||
. "FROM docref AS r INNER JOIN document AS d using (docid) "
|
||
. "WHERE d.doctype='picture' AND r.reftype='box' AND r.refid=? "
|
||
. "ORDER BY d.title";
|
||
$sth = $pdo->prepare($sql);
|
||
$sth->execute([$id]);
|
||
$res = $sth->fetchAll();
|
||
// TODO hübsche Darstellung
|
||
$doccount = 0;
|
||
foreach ($res as $row) {
|
||
$doccount += 1;
|
||
echo '<div class="border rounded p-2 d-flex flex-column" style="width: 150px;">';
|
||
echo '<a href="dl.php?id=', $row['docid'], '">';
|
||
// echo '<img width="120" src="doc/pic-', $row['hash'], '.', $row['extension'],'">';
|
||
echo '<img class="img-fluid rounded mb-2" width="120" src="dl.php?id=', $row['docid'], '" alt="', $row['title'], '">';
|
||
echo "</a>\n";
|
||
// additionally direct download and go to document details page
|
||
echo '<div class="d-flex justify-content-between mt-auto">';
|
||
echo '<a href="documents.php?f=view&id=', $row['docid'], '" title="', _('View image details'), '"> <i class="bi bi-eye"></i></a>';
|
||
echo '<a href="dl.php?id=', $row['docid'], '" title="', _('Download image'), '"><i class="bi bi-download"></i></a>';
|
||
echo "</div>\n";
|
||
echo "</div\n";
|
||
}
|
||
if ($doccount == 0) {
|
||
echo _('No referenced images'), "\n";
|
||
}
|
||
echo "</div>\n"; // card-body
|
||
echo "</div>\n"; // card
|
||
|
||
echo '</div>'; // col
|
||
echo '</div>'; // row
|
||
echo '</div>'; // container
|
||
|
||
// Inventory in box
|
||
// WIP
|
||
$sql = "SELECT invid, invname, weight, number 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>#</th>';
|
||
echo '<th>', _('Inventory'), "</th>";
|
||
echo '<th>', _('Number'), "</th>";
|
||
echo '<th>', _('Weight'), "</th>";
|
||
echo "</thead>\n";
|
||
echo "</tr>\n";
|
||
$i = 1;
|
||
foreach ($res as $row) {
|
||
echo "<tr>\n";
|
||
echo '<td>', $i++, ' ';
|
||
echo '<a class="text-nowrap" title="', _('View'), '" href="inventory.php?f=view&id=', $row['invid'], '"><i class="bi bi-eye ms-2"></i></a>';
|
||
echo "</td>\n";
|
||
echo '<td>', $row['invname'], "</td>\n";
|
||
echo '<td>', $row['number'], "</td>\n";
|
||
echo '<td>';
|
||
$weight_item = $row['weight'] * $row['number'];
|
||
if ($weight_item > 0) {
|
||
$weight_total += $weight_item;
|
||
echo format_float($weight_item, 2, 'kg');
|
||
} else {
|
||
echo '-';
|
||
}
|
||
echo "</td>\n";
|
||
echo "</tr>\n";
|
||
}
|
||
echo "<tfoot>";
|
||
echo '<tr><td colspan="3"></td><td>', format_float($weight_total, 2, 'kg'), '</td>', "\n";
|
||
echo "</tfoot>\n";
|
||
echo "</table>\n";
|
||
} else {
|
||
echo '<p>', _('Box is empty'), "</p>\n";
|
||
}
|
||
|
||
# TODO provisions in box
|
||
|
||
elseif ($action == ACT_EDIT):
|
||
// ========== VARIANT: edit single record =====================================
|
||
|
||
$sql = "SELECT boxtype, label, content, color, weight, weight_net, price, "
|
||
. "sid, wx, wy, wz, remarks "
|
||
. "FROM box "
|
||
. "WHERE boxid=?";
|
||
$sth = $pdo->prepare($sql);
|
||
$sth->execute([$id]);
|
||
$box = $sth->fetch(PDO::FETCH_OBJ);
|
||
|
||
$opt_none = [ -1 => _('― none ―')];
|
||
|
||
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="<?=h($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="<?=h($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 class="form-label"><?=_('Dimensions (L × W × H)')?></label>
|
||
<div class="input-group">
|
||
<input type="number" class="form-control" name="wx"
|
||
value="<?=$box->wx;?>" min="0" step="1"
|
||
aria-label="<?=_('Length')?>">
|
||
<span class="input-group-text">×</span>
|
||
<input type="number" class="form-control" name="wy"
|
||
value="<?=$box->wy;?>" min="0" step="1"
|
||
aria-label="<?=_('Width')?>">
|
||
<span class="input-group-text">×</span>
|
||
<input type="number" class="form-control" name="wz"
|
||
value="<?=$box->wz;?>" min="0" step="1"
|
||
aria-label="<?=_('Height')?>">
|
||
<span class="input-group-text">mm</span>
|
||
</div>
|
||
<div class="form-text">
|
||
<?=_('Length × Width × Height')?>
|
||
</div>
|
||
</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="weight_net" class="form-label"><?=_('Weight net, kg')?></label>
|
||
<input type="text" class="form-control" id="weight_net" name="weight_net" value="<?=format_float($box->weight_net); ?>">
|
||
</div>
|
||
<div class="mb-3">
|
||
<label for="price" class="form-label"><?=sprintf(_('Price, %s'), $g_lconv['currency_symbol']); ?></label>
|
||
<input type="text" class="form-control" id="price" name="price" value="<?=format_float($box->price) ?>">
|
||
</div>
|
||
<?php form_create_select('sid', _('Storage'), $opt_storage, $box->sid); ?>
|
||
<div class="mb-3">
|
||
<label for="remarks" class="form-label"><?=_('Remarks')?></label>
|
||
<textarea class="form-control" id="remarks" name="remarks" rows="3"><?=h($box->remarks, br:true)?></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';
|