vid);
$opt_box = db_get_opt_box($user->vid);
// ========== 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':
$invname = gpc_get_string($_POST, 'invname');
$number = gpc_get_int($_POST, 'number');
$boxtype = gpc_get_string($_POST, 'boxtype');
$sid = gpc_get_int($_POST, 'sid');
$boxid = gpc_get_int($_POST, 'boxid');
$sql = "INSERT INTO inventory "
. " (invname, invcond, number, boxtype, sid, boxid) "
. "VALUES "
. " (:invname, 'good', :number, :boxtype, :sid, :boxid)";
$sth = $pdo->prepare($sql);
$sth->bindValue(':invname', $invname, PDO::PARAM_STR);
$sth->bindValue(':number', $number, PDO::PARAM_INT);
$sth->bindValue(':boxtype', $boxtype, PDO::PARAM_STR);
$sth->bindValue(':sid', $sid, PDO::PARAM_INT);
$sth->bindValue(':boxid', $boxid, PDO::PARAM_INT);
try {
$sth->execute();
} catch (PDOException $e) {
$g_error->Add('SQL-Error: '. $e->getMessage());
}
$id = $pdo->LastInsertId();
$action = ACT_VIEW;
break;
case 'update':
$invname = gpc_get_string($_POST, 'invname');
$number = gpc_get_int($_POST, 'number');
$boxtype = gpc_get_string($_POST, 'boxtype');
$sid = gpc_get_int($_POST, 'sid');
$boxid = gpc_get_int($_POST, 'boxid');
$weight = min(gpc_get_float($_POST, 'weight'), 999.99); // limit max value
$price = gpc_get_currency($_POST, 'price');
$purchdate = gpc_get_date($_POST, 'purchdate');
$invcond = gpc_get_string($_POST, 'invcond');
$sql = "UPDATE inventory "
. "SET invname=:invname,"
. " number=:number,"
. " boxtype=:boxtype,"
. " sid=:sid,"
. " boxid=:boxid,"
. " weight=:weight,"
. " price=:price,"
. " purchdate=:purchdate,"
. " invcond=:invcond "
. "WHERE invid=:invid";
$sth = $pdo->prepare($sql);
$sth->bindValue(':invid', $id, PDO::PARAM_INT);
$sth->bindValue(':invname', $invname, PDO::PARAM_STR);
$sth->bindValue(':number', $number, PDO::PARAM_INT);
$sth->bindValue(':boxtype', $boxtype, PDO::PARAM_STR);
$sth->bindValue(':sid', $sid, PDO::PARAM_INT);
$sth->bindValue(':boxid', $boxid, PDO::PARAM_INT);
$sth->bindValue(':weight', $weight);
$sth->bindValue(':price', $price);
$sth->bindValue(':purchdate', $purchdate);
$sth->bindValue(':invcond', $invcond, PDO::PARAM_STR);
try {
$sth->execute();
} catch (PDOException $e) {
$g_error->Add('SQL-Error: '. $e->getMessage());
}
$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']);
$sth = $pdo->prepare("DELETE FROM inventory WHERE invid=?");
$sth->execute([$id]);
$g_message->Add(sprintf(_('Deleted inventory no. %d'), $id));
$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 =======================================
/*
The only way to find out whether the inventory is on the current boat is to use
the box assignment. But this is also intentional to allow easy rearrangement.
*/
echo "
$pagetitle
\n";
$sql = "SELECT i.invid, i.invname, i.number, i.invcond, s.sname AS container "
. "FROM inventory AS i JOIN storage AS s ON (i.boxtype='storage' AND i.sid=s.sid)"
. "WHERE s.vid=:vid "
. "UNION "
. "SELECT i.invid, i.invname, i.number, i.invcond, b.label AS container "
. "FROM inventory AS i JOIN box AS b ON (i.boxtype='box' AND i.boxid=b.boxid) "
. " JOIN storage AS s ON (b.sid=s.sid) "
. "WHERE s.vid=:vid "
. "ORDER BY invname";
$sth = $pdo->prepare($sql);
$sth->bindValue(':vid', $user->vid, PDO::PARAM_INT);
$sth->execute();
$res = $sth->fetchAll();
echo '', "\n";
echo "\n";
echo "\n";
echo "| " . _('Name') . " | \n";
echo "" . _('Container') . " | ";
echo "" . _('Number') . " | ";
echo "" . _('Condition') . " | ";
echo "
\n";
echo "\n";
foreach ($res as $row) {
echo "\n";
echo '| ', $row['invname'], " | \n";
echo "". $row['container'] ." | \n";
echo "". $row['number'] ." | \n";
echo "". $row['invcond'] ." | \n";
form_action_buttons($g_scriptname, $row['invid']);
echo "
\n";
}
// Table summary
echo "\n";
echo '| ', sprintf(_('%d records'), count($res)), ' |
', "\n";
echo "\n";
echo "
\n";
form_add_button($g_scriptname);
elseif ($action == ACT_ADD):
// ========== VARIANT: add record =============================================
echo '', _('Add Inventory'), "
\n";
?>
prepare($sql);
$sth->execute([$id]);
$inventory = $sth->fetch(PDO::FETCH_OBJ);
// boxtype can be storage or box
switch ($inventory->boxtype) {
case 'storage':
$sql = "SELECT sname, vid FROM storage WHERE sid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$inventory->sid]);
$row = $sth->fetch();
$storagename = $row['sname'];
$vid = $row['vid'];
break;
case 'box':
// Future: Boxes can also be nested!
// Recursive Function "get_storage_for_box()"
$sql = "SELECT sid, label FROM box WHERE boxid=?";
// TODO Load additional box data
$sth = $pdo->prepare($sql);
$sth->execute([$inventory->boxid]);
$box = $sth->fetch();
$sql = "SELECT sname, vid FROM storage WHERE sid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$box['sid']]);
$row = $sth->fetch();
$storagename = $row['sname'];
$vid = $row['vid'];
break;
}
echo '', $inventory->invname, "
\n";
echo '', "\n";
echo '| ', _('Box type')," | ", $inventory->boxtype, " |
\n";
// Container with link
echo "| ", _('Container')," | ", sprintf(_('%s in %s'), $box['label'], $storagename);
echo " |
\n";
echo "| ", _('Location ID'), " | ", $inventory->locationid, " |
\n";
echo "| ", _('Number'), " | ", $inventory->number, " |
\n";
echo "| ", _('Weight'), " | ", format_float($inventory->weight, 2, 'kg'), " |
\n";
echo "| ", _('Price'), " | ", format_currency($inventory->price), " |
\n";
echo "| ", _('Purchase date'), " | ", $inventory->purchdate, " |
\n";
echo "| ", _('Condition'), " | ", $inventory->invcond, " |
\n";
echo "
\n";
form_view_buttons($g_scriptname, $id);
elseif ($action == ACT_EDIT):
// ========== VARIANT: edit single record =====================================
$sql = "SELECT invname, boxtype, sid, boxid, number, weight, price, purchdate, invcond "
. "FROM inventory "
. "WHERE invid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$inventory = $sth->fetch(PDO::FETCH_OBJ);
?>
=_('Edit Inventory')?>