beginTransaction();
// Record transaction
$stmt = $db->prepare("INSERT INTO inventory_transactions (inventory_id, type, quantity, user_id) VALUES (?, ?, ?, ?)");
$stmt->execute([$itemId, $type, $qty, $userId]);
// Update stock
$mod = ($type === 'in' ? '+' : '-');
$db->exec("UPDATE inventory SET stock_level = stock_level $mod $qty WHERE id = $itemId");
$db->commit();
header("Location: inventory.php?success=1");
exit;
} catch (Exception $e) {
$db->rollBack();
$error = "Error: " . $e->getMessage();
}
}
// Handle New Item
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['new_item'])) {
$name = $_POST['name'];
$cat = $_POST['category'];
$stock = $_POST['stock_level'];
$reorder = $_POST['reorder_level'];
$unit = $_POST['unit'];
$stmt = $db->prepare("INSERT INTO inventory (name, category, stock_level, reorder_level, unit) VALUES (?, ?, ?, ?, ?)");
$stmt->execute([$name, $cat, $stock, $reorder, $unit]);
header("Location: inventory.php?item_added=1");
exit;
}
$inventory = $db->query("SELECT * FROM inventory ORDER BY name ASC")->fetchAll();
$lowStock = array_filter($inventory, fn($i) => $i['stock_level'] <= $i['reorder_level']);
?>
Inventory Management
Low Stock Warning: = count($lowStock) ?> items are below reorder level.
| Item Name |
Category |
Current Stock |
Reorder Level |
Actions |
| No inventory items found. |
|
= htmlspecialchars($item['name']) ?>
= $item['unit'] ?>
|
= strtoupper($item['category']) ?> |
= $item['stock_level'] ?>
|
= $item['reorder_level'] ?> |
|