34576-vm/inventory_list.php
Flatlogic Bot c92b13307b Inventory
2025-10-02 00:56:41 +00:00

158 lines
7.4 KiB
PHP

<?php
require_once 'db/config.php';
$pdo = db_connect();
// Get distinct values for filters
$categories = $pdo->query("SELECT DISTINCT category FROM inventory_items ORDER BY category")->fetchAll(PDO::FETCH_COLUMN);
$statuses = $pdo->query("SELECT DISTINCT asset_status FROM inventory_items ORDER BY asset_status")->fetchAll(PDO::FETCH_COLUMN);
$locations = $pdo->query("SELECT DISTINCT location_city FROM inventory_items ORDER BY location_city")->fetchAll(PDO::FETCH_COLUMN);
// Base query
$sql = "SELECT id, item_name, item_code, category, asset_status, location_city, acquisition_price, acquisition_date FROM inventory_items";
$params = [];
$where_clauses = [];
// Search
$search = $_GET['search'] ?? '';
if ($search) {
$search_term = "%{$search}%";
$where_clauses[] = "(item_name LIKE :search OR item_code LIKE :search OR po_number LIKE :search OR item_user_name LIKE :search OR user_department LIKE :search OR location_city LIKE :search OR company_origin LIKE :search)";
$params[':search'] = $search_term;
}
// Filters
$filter_category = $_GET['category'] ?? '';
if ($filter_category) {
$where_clauses[] = "category = :category";
$params[':category'] = $filter_category;
}
$filter_status = $_GET['status'] ?? '';
if ($filter_status) {
$where_clauses[] = "asset_status = :status";
$params[':status'] = $filter_status;
}
$filter_location = $_GET['location'] ?? '';
if ($filter_location) {
$where_clauses[] = "location_city = :location";
$params[':location'] = $filter_location;
}
if (!empty($where_clauses)) {
$sql .= " WHERE " . implode(' AND ', $where_clauses);
}
// Sorting
$sort_columns = ['item_name', 'item_code', 'category', 'asset_status', 'location_city', 'acquisition_price', 'acquisition_date'];
$sort_column = $_GET['sort'] ?? 'acquisition_date';
$sort_direction = $_GET['dir'] ?? 'desc';
if (!in_array($sort_column, $sort_columns)) {
$sort_column = 'acquisition_date';
}
$sort_direction = strtolower($sort_direction) === 'asc' ? 'asc' : 'desc';
$sql .= " ORDER BY `$sort_column` $sort_direction";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$items = $stmt->fetchAll();
function get_sort_link($column, $current_sort, $current_dir) {
$dir = ($current_sort === $column && $current_dir === 'asc') ? 'desc' : 'asc';
$query_params = http_build_query(array_merge($_GET, ['sort' => $column, 'dir' => $dir]));
return '?' . $query_params;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Inventory List</title>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&family=Roboto+Slab:wght@700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css">
</head>
<body>
<div class="container">
<header class="header">
<h1>Inventory List</h1>
<a href="add_item.php" class="btn btn-primary">Add New Item</a>
</header>
<form method="get" action="inventory_list.php" class="filter-form">
<div class="search-and-filter-section">
<div class="search-bar">
<input type="text" id="search" name="search" placeholder="Search by keyword..." value="<?= htmlspecialchars($search) ?>">
</div>
<div class="filter-controls">
<select name="category">
<option value="">All Categories</option>
<?php foreach ($categories as $category): ?>
<option value="<?= htmlspecialchars($category) ?>" <?= $filter_category === $category ? 'selected' : '' ?>><?= htmlspecialchars($category) ?></option>
<?php endforeach; ?>
</select>
<select name="status">
<option value="">All Statuses</option>
<?php foreach ($statuses as $status): ?>
<option value="<?= htmlspecialchars($status) ?>" <?= $filter_status === $status ? 'selected' : '' ?>><?= htmlspecialchars($status) ?></option>
<?php endforeach; ?>
</select>
<select name="location">
<option value="">All Locations</option>
<?php foreach ($locations as $location): ?>
<option value="<?= htmlspecialchars($location) ?>" <?= $filter_location === $location ? 'selected' : '' ?>><?= htmlspecialchars($location) ?></option>
<?php endforeach; ?>
</select>
<button type="submit" class="btn">Filter</button>
<a href="/inventory_list.php" class="btn btn-sm" style="background-color: #95a5a6;">Reset</a>
</div>
</div>
</form>
<div class="inventory-table-container">
<table class="inventory-table">
<thead>
<tr>
<th><a href="<?= get_sort_link('item_name', $sort_column, $sort_direction) ?>">Item Name</a></th>
<th><a href="<?= get_sort_link('item_code', $sort_column, $sort_direction) ?>">Item Code</a></th>
<th><a href="<?= get_sort_link('category', $sort_column, $sort_direction) ?>">Category</a></th>
<th><a href="<?= get_sort_link('asset_status', $sort_column, $sort_direction) ?>">Status</a></th>
<th><a href="<?= get_sort_link('location_city', $sort_column, $sort_direction) ?>">Location</a></th>
<th><a href="<?= get_sort_link('acquisition_price', $sort_column, $sort_direction) ?>">Price</a></th>
<th><a href="<?= get_sort_link('acquisition_date', $sort_column, $sort_direction) ?>">Acquisition Date</a></th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($items)): ?>
<tr>
<td colspan="8" style="text-align:center;">No inventory items found matching your criteria.</td>
</tr>
<?php else: ?>
<?php foreach ($items as $item): ?>
<tr>
<td><?= htmlspecialchars($item['item_name']) ?></td>
<td><?= htmlspecialchars($item['item_code']) ?></td>
<td><?= htmlspecialchars($item['category']) ?></td>
<td><span class="status status-<?= strtolower(str_replace(' ', '-', $item['asset_status'])) ?>"><?= htmlspecialchars($item['asset_status']) ?></span></td>
<td><?= htmlspecialchars($item['location_city']) ?></td>
<td>$<?= number_format($item['acquisition_price'], 2) ?></td>
<td><?= htmlspecialchars($item['acquisition_date']) ?></td>
<td>
<a href="edit_item.php?id=<?= $item['id'] ?>" class="btn btn-sm">Edit</a>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
<script src="assets/js/main.js"></script>
</body>
</html>