vid); $opt_box = db_get_opt_box($user->vid); $opt_invcond = db_load_enum('inventory', 'invcond', true, true); $opt_equipment = db_get_opt_equip($user->vid, [-1 => _('― none ―')]); // ========== 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['stor'] = gpc_get_string($_POST, 'flt_stor'); //$flt['box'] = gpc_get_string($_POST, 'flt_box'); $flt['cond'] = gpc_get_string($_POST, 'flt_cond'); $flt['txt'] = gpc_get_string($_POST, 'flt_txt'); db_save_filter($flt, 201); $action = ACT_DEFAULT; break; case 'insert': $p[':invname'] = gpc_get_string($_POST, 'invname'); $p[':number'] = gpc_get_int($_POST, 'number'); $p[':conttype'] = gpc_get_string($_POST, 'conttype'); $p[':sid'] = gpc_get_int($_POST, 'sid'); $p[':boxid'] = gpc_get_int($_POST, 'boxid'); $p[':invcond'] = 'good'; $id = db_exec_insert('inventory', $p); $action = ACT_VIEW; break; case 'update': $p[':invid'] = $id; $p[':invname'] = gpc_get_string($_POST, 'invname'); $p[':number'] = gpc_get_int($_POST, 'number'); $p[':conttype'] = gpc_get_string($_POST, 'conttype'); $p[':sid'] = gpc_get_int($_POST, 'sid'); $p[':boxid'] = gpc_get_int($_POST, 'boxid'); $p[':weight'] = min(gpc_get_float($_POST, 'weight'), 999.99); // limit max value $p[':price'] = gpc_get_currency($_POST, 'price'); $p[':purchdate'] = gpc_get_date($_POST, 'purchdate'); $p[':invcond'] = gpc_get_string($_POST, 'invcond'); $p[':remarks'] = gpc_get_string($_POST, 'remarks', 150); $p[':eid'] = gpc_get_int($_POST, 'eid'); db_exec_update('inventory', $p, 'invid'); $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 ======================================= page_caption_search($pagetitle); $flt = db_get_filter($user->id, 201); /* 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. */ $w = array('vid=:vid'); $p = array(':vid' => $user->vid); if ($flt->stor == -1) { $w[] = 'i.sid IS NULL'; } elseif ($flt->stor > 0) { $w[] = 'i.sid=:sid'; $p[':sid'] = $flt->stor; } if (strlen($flt->cond) > 2 ) { $w[] = 'invcond=:invcond'; $p[':invcond'] = $flt->cond; } if (strlen($flt->txt) > 1) { $w[] = 'invname LIKE :txt'; $p[':txt'] = '%'.$flt->txt.'%'; } $where = join(' AND ', $w); $order = ' ORDER BY invname'; $sql = "SELECT " . "(SELECT COUNT(*) FROM inventory AS i JOIN storage AS s ON (i.conttype='storage' AND i.sid=s.sid) WHERE $where)" . "+" . "(SELECT COUNT(*) FROM inventory AS i JOIN box AS b ON (i.conttype='box' AND i.boxid=b.boxid) JOIN storage AS s ON (b.sid=s.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)); } $numrows = $sth->fetchColumn(); $lastpage = ceil($numrows/$g_rows_pp); $page = gpc_get_int($_REQUEST, 'p', 1); $sql = "SELECT i.invid, i.invname, i.number, i.invcond, s.sname AS container " . "FROM inventory AS i JOIN storage AS s ON (i.conttype='storage' AND i.sid=s.sid)"; $sql .= ' WHERE ' . $where; $sql .= " UNION SELECT i.invid, i.invname, i.number, i.invcond, b.label AS container " . "FROM inventory AS i JOIN box AS b ON (i.conttype='box' AND i.boxid=b.boxid) " . " JOIN storage AS s ON (b.sid=s.sid) "; $sql .= ' WHERE ' . $where; $sql .= $order; // if pagination: $sql .= ' LIMIT ' . ($page - 1) * $g_rows_pp . ',' . $g_rows_pp; $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)); } $res = $sth->fetchAll(); // Filter $opt_special = array( -2 => _('― all ―'), -1 => _('― none ―') ); ?>
Filter
', "\n"; echo "\n"; echo "\n"; echo "", _('Name'), "\n"; echo "", _('Container'), "\n"; echo "", _('Number'), "\n"; echo "", _('Condition'), "\n"; echo "", _('Tags'), "\n"; echo "\n"; echo "\n"; echo "\n"; foreach ($res as $row) { echo "\n"; echo '', $row['invname'], "\n"; echo "", $row['container'], "\n"; echo "", $row['number'], "\n"; echo "", ($opt_invcond[$row['invcond']] ?? ''), "\n"; echo "", _('No tags assigned'), "\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"; echo $pagination; 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); // container type can be storage or box switch ($inventory->conttype) { 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 '\n"; // Container with link echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; $tags = db_load_taglist('inv', $id); form_tag_assignment($g_scriptname, $id, $tags); echo "
', _('Box type'),"", $inventory->conttype, "
", _('Container'),"", sprintf(_('%s in %s'), $box['label'], $storagename); echo "
", _('Location ID'), "", $inventory->locationid, "
", _('Number'), "", $inventory->number, "
", _('Weight'), "", format_float($inventory->weight, 2, 'kg'), "
", _('Price'), "", format_currency($inventory->price), "
", _('Purchase date'), "", $inventory->purchdate, "
", _('Condition'), "", $opt_invcond[$inventory->invcond], "
", _('Remarks'), "", $inventory->remarks, "
\n"; form_view_buttons($g_scriptname, $id); elseif ($action == ACT_EDIT): // ========== VARIANT: edit single record ===================================== $sql = "SELECT invname, conttype, sid, boxid, number, weight, price, purchdate, " . " invcond, eid, remarks " . "FROM inventory " . "WHERE invid=?"; $sth = $pdo->prepare($sql); $sth->execute([$id]); $inventory = $sth->fetch(PDO::FETCH_OBJ); ?>

conttype); form_create_select('sid', _('Storage'), $opt_storage, $inventory->sid); form_create_select('boxid', _('Box'), $opt_box, $inventory->boxid); ?>
eid); ?>
\n"; elseif ($action == ACT_DELETE): // ========== VARIANT: delete record ========================================== $sql = "SELECT invname FROM inventory WHERE invid=?"; $sth = $pdo->prepare($sql); $sth->execute([$id]); $inventory = $sth->fetch(PDO::FETCH_OBJ); echo '

', _('Delete Inventory'), "

\n"; echo '

', sprintf(_('Record no. %d'), $id), "

\n"; echo '

Name: ', $inventory->invname, "

"; echo '

', _('Deleting an inventory item is final. There is no way back. Only delete if you are absolute sure.'), "

\n"; $_SESSION['token'] = bin2hex(random_bytes(8)); form_delete_buttons($g_scriptname, $id, $_SESSION['token']); else: // ========== ERROR UNKNOWN VARIANT =========================================== echo '

', _('Unknown function call: Please report to system development!'), "

\n"; endif; // $action == ... // ========== END OF VARIANTS ================================================= include 'footer.php';