vid); $opt_category = db_get_options(5); $opt_unit = db_get_options(6); // ========== 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 'list': $action = ACT_VIEW_LIST; break; case 'filter': $flt = array(); $flt['cat'] = gpc_get_enum($_POST, 'flt_cat', array_merge(array_keys($opt_category), ['-2', '-1'])); $flt['txt'] = gpc_get_string($_POST, 'flt_txt'); db_save_filter($flt, 202); $action = ACT_DEFAULT; break; case 'freset': db_clear_filter(202); $action = ACT_DEFAULT; break; case 'insert': $p[':provname'] = gpc_get_string($_POST, 'provname'); $p[':number'] = gpc_get_int($_POST, 'number'); $p[':unit'] = gpc_get_int($_POST, 'unit'); $p[':storedate'] = gpc_get_string($_POST, 'storedate'); $p[':shelflife'] = gpc_get_string($_POST, 'shelflife'); $id = db_exec_insert('provisions', $p); $action = ACT_VIEW; break; case 'update': $p[':provid'] = $id; $p[':provname'] = gpc_get_string($_POST, 'provname'); $p[':provcat'] = gpc_get_int($_POST, 'provcat'); $p[':number'] = gpc_get_uint($_POST, 'number'); $p[':target'] = gpc_get_uint($_POST, 'target'); $p[':weight'] = min(gpc_get_float($_POST, 'weight'), 999.99); // limit max value $p[':weight_net'] = min(gpc_get_float($_POST, 'weight_net'), 999.99); $p[':unit'] = gpc_get_int($_POST, 'unit'); $p[':calories'] = gpc_get_uint($_POST, 'calories'); $p[':storedate'] = gpc_get_string($_POST, 'storedate'); $p[':shelflife'] = gpc_get_string($_POST, 'shelflife'); $p[':price'] = gpc_get_currency($_POST, 'price'); $p[':sid'] = gpc_get_int($_POST, 'sid'); if ($p[':sid'] <= 0) $p[':sid'] = NULL; $p[':remarks'] = gpc_get_string($_POST, 'remarks', 150); db_exec_update('provisions', $p, 'provid'); $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; } $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); // load filter from db $flt = db_get_filter($user->id, 202); $w = array('(vid=:vid OR sid IS NULL)'); $p = array(':vid' => $user->vid); if ($flt->cat == -1) { $w[] = 'provcat IS NULL'; } elseif ($flt->cat > 0) { $w[] = 'provcat=:provcat'; $p[':provcat'] = $flt->cat; } if (strlen($flt->txt) > 1) { $w[] = 'provname LIKE :txt'; $p[':txt'] = '%'.$flt->txt.'%'; } $where = join(' AND ', $w); $order = ' ORDER BY provid'; // get total record count for pagination and limit $sql = "SELECT COUNT(*) FROM provisions LEFT OUTER JOIN storage USING (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)); $g_error->PrintOut(); } $numrows = $sth->fetchColumn(); $page = gpc_get_int($_REQUEST, 'p', 1); $rows_pp = gpc_get_int($_REQUEST, 'n', $user->rows_pp); $lastpage = ceil($numrows/$rows_pp); // provisions can be assigned to a store or be free // d0 is the maximum number of days age // d1 is the current age in days. If negative then it is over limit // TODO display over with warning levels: 50% over, 100% over etc. $sql = "SELECT provid, provname, number, target, unit, storedate, shelflife, sid," . " DATEDIFF(shelflife, storedate) AS d0, DATEDIFF (shelflife, CURDATE()) AS d1 " . "FROM provisions LEFT OUTER JOIN storage USING (sid)"; $sql .= ' WHERE ' . $where; $sql .= $order; // if pagination: $sql .= ' LIMIT ' . ($page - 1) * $rows_pp . ',' . $rows_pp; $sth = $pdo->prepare($sql); $sth->execute($p); $res = $sth->fetchAll(); ?>
Filter
cat); ?>
', "\n"; echo "\n"; echo "\n"; echo '#'; echo '', _('Description'), ""; echo '', _('Number'), ""; echo '', _('Unit'), ""; echo '', _('Stored'), ""; echo '', _('Best before'), ""; echo ""; // Meter echo "\n"; // Buttons echo "\n"; echo "\n"; $i = ($page - 1) * $rows_pp; $i0 = $i + 1; foreach ($res as $row) { $i++; // record number echo "\n"; echo "", $i; echo '', "\n"; echo "\n"; echo "", ! isset($row['sid']) ? ' ' : '', $row['provname'], "\n"; echo '', $row['number']; if ($row['target']) echo ' (', $row['target'], ')'; echo "\n"; echo "", $opt_unit[$row['unit']], "\n"; echo "", $row['storedate'], "\n"; echo "", $row['shelflife'], "\n"; // TODO shelflife in der Vergangenheit echo ""; if ($row['d1'] > 0) { echo ''; } else { if ($row['d1'] < 0) { echo ' ', abs($row['d1']); } } echo "\n"; // Edit current record button echo ""; echo ''; echo "\n"; echo "\n"; } // Table summary table_rec_summary($i0, $i, $numrows, 8); echo "\n"; echo $pagination; form_add_button($g_scriptname); // Shopping list button echo '
', "\n"; echo ' ',_('Shopping list'), "\n"; echo "
\n"; elseif ($action == ACT_ADD): // ========== VARIANT: add record ============================================= echo '

', _('Add provisions'), "

\n"; ?>
prepare($sql); $sth->execute([$id]); $prov = $sth->fetch(PDO::FETCH_OBJ); echo '

', $prov->provname, "

\n"; echo '', "\n"; echo '\n"; echo "\n"; echo "\n"; echo "\n"; echo '\n"; echo "\n"; echo '\n"; echo '\n"; echo '\n"; echo '\n"; echo '\n"; echo '\n"; echo "
', _('Number'),"", $prov->number, "
", _('Target'), "", $prov->target, "
", _('Weight'), "", format_float($prov->weight, 2, 'kg'), "
", _('Weight net'), "", format_float($prov->weight_net, 2, 'kg'), "
', _('Unit'),"", $opt_unit[$prov->unit], "
", _('Calories'), "", $prov->calories, " kcal
', _('Category'),"", $opt_category[$prov->provcat] ?? 'n/a', "
', _('Storedate'),"", $prov->storedate, "
', _('Shelflife'),"", $prov->shelflife, "
', _('Price'), "", format_currency($prov->price), "
', _('Storage'),"", ($opt_storage[$prov->sid] ?? ''), "
', _('Remarks'),"", nl2br($prov->remarks), "
\n"; form_view_buttons($g_scriptname, $id); elseif ($action == ACT_EDIT): // ========== VARIANT: edit single record ===================================== $sql = "SELECT provname, provcat, number, target, unit, weight, weight_net," . " calories, storedate, shelflife, price, sid, remarks " . "FROM provisions " . "WHERE provid=?"; $sth = $pdo->prepare($sql); $sth->execute([$id]); $prov = $sth->fetch(PDO::FETCH_OBJ); echo '

', _('Edit provision'), "

\n"; ?>
unit); ?>
provcat); ?>
sid); ?>
\n"; elseif ($action == ACT_DELETE): // ========== VARIANT: delete record ========================================== echo '

', _('Delete provisions'), "

\n"; ?>

Not yet implemented!

', _('Shopping list'), "\n"; $sql = "SELECT provid, provname, unit, target - number AS buy " . "FROM provisions " . "WHERE target IS NOT NULL AND number < target " . "ORDER BY provcat, provname"; $sth = $pdo->query($sql); $res = $sth->fetchAll(); echo "
";
foreach ($res as $row) {
    echo $row['provname'];
    echo ' ';
    echo $row['buy'];
    echo ' ';
    echo $opt_unit[$row['unit']], "\n";
}
echo "
"; echo '
', "\n"; echo ' ',_('Use'), "\n"; echo ' ',_('Back'), "\n"; echo "
\n"; else: // ========== ERROR UNKNOWN VARIANT =========================================== echo '

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

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