38244-vm/products.php
2026-02-07 11:39:06 +00:00

221 lines
12 KiB
PHP

<?php
declare(strict_types=1);
require_once 'db/config.php';
$current_branch_id = (int)($_GET['branch_id'] ?? 1);
$db = db();
// Handle Add Product
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'add') {
$name = $_POST['name'];
$category_id = (int)$_POST['category_id'];
$selling_price = (float)$_POST['selling_price'];
$member_price = (float)$_POST['member_price'];
$stock = (int)$_POST['stock'];
$stmt = $db->prepare("INSERT INTO products (name, category_id, selling_price, member_price, stock, branch_id) VALUES (?, ?, ?, ?, ?, ?)");
try {
$stmt->execute([$name, $category_id, $selling_price, $member_price, $stock, $current_branch_id]);
header("Location: products.php?branch_id=$current_branch_id&success=1");
exit;
} catch (Exception $e) {
$error = "Error adding product: " . $e->getMessage();
}
}
// Fetch Data
$branches = $db->query("SELECT * FROM branches")->fetchAll();
$current_branch = array_filter($branches, fn($b) => $b['id'] == $current_branch_id);
$current_branch = reset($current_branch);
$products = $db->query("SELECT p.*, c.name as category_name FROM products p LEFT JOIN categories c ON p.category_id = c.id WHERE p.branch_id = $current_branch_id ORDER BY p.id DESC")->fetchAll();
$categories = $db->query("SELECT * FROM categories")->fetchAll();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Products - POS Pro</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css">
<link href="assets/css/custom.css?v=<?php echo time(); ?>" rel="stylesheet">
</head>
<body>
<div class="wrapper">
<!-- Sidebar -->
<nav id="sidebar" class="bg-dark text-white">
<div class="sidebar-header p-3">
<h4 class="mb-0">POS<span class="text-success">PRO</span></h4>
<small class="text-muted">Multi-Branch System</small>
</div>
<ul class="list-unstyled components p-2">
<li>
<a href="index.php?branch_id=<?php echo $current_branch_id; ?>" class="nav-link text-white p-2 d-block rounded">
<i class="bi bi-speedometer2 me-2"></i> Dashboard
</a>
</li>
<li>
<a href="pos.php?branch_id=<?php echo $current_branch_id; ?>" class="nav-link text-white p-2 d-block rounded">
<i class="bi bi-cart3 me-2"></i> Cashier (POS)
</a>
</li>
<li class="active">
<a href="products.php?branch_id=<?php echo $current_branch_id; ?>" class="nav-link text-white p-2 d-block rounded">
<i class="bi bi-box-seam me-2"></i> Products
</a>
</li>
<li>
<a href="members.php?branch_id=<?php echo $current_branch_id; ?>" class="nav-link text-white p-2 d-block rounded">
<i class="bi bi-people me-2"></i> Members
</a>
</li>
<li>
<a href="vouchers.php?branch_id=<?php echo $current_branch_id; ?>" class="nav-link text-white p-2 d-block rounded">
<i class="bi bi-ticket-perforated me-2"></i> Vouchers
</a>
</li>
<li>
<a href="#" class="nav-link text-white p-2 d-block rounded">
<i class="bi bi-graph-up me-2"></i> Reports
</a>
</li>
</ul>
</nav>
<!-- Page Content -->
<div id="content">
<nav class="navbar navbar-expand-lg navbar-light bg-white border-bottom p-3">
<div class="container-fluid">
<button type="button" id="sidebarCollapse" class="btn btn-outline-dark me-3">
<i class="bi bi-list"></i>
</button>
<div class="ms-auto d-flex align-items-center">
<div class="dropdown">
<button class="btn btn-light dropdown-toggle" type="button" data-bs-toggle="dropdown">
<i class="bi bi-geo-alt me-1"></i> <?php echo htmlspecialchars($current_branch['name']); ?>
</button>
<ul class="dropdown-menu shadow border-0">
<?php foreach ($branches as $branch): ?>
<li><a class="dropdown-item" href="?branch_id=<?php echo $branch['id']; ?>"><?php echo htmlspecialchars($branch['name']); ?></a></li>
<?php endforeach; ?>
</ul>
</div>
</div>
</div>
</nav>
<div class="container-fluid p-4">
<div class="d-flex justify-content-between align-items-center mb-4">
<h2 class="mb-0">Product Inventory</h2>
<button class="btn btn-primary rounded-pill" data-bs-toggle="modal" data-bs-target="#addProductModal">
<i class="bi bi-plus-lg me-1"></i> Add Product
</button>
</div>
<?php if (isset($_GET['success'])): ?>
<div class="alert alert-success alert-dismissible fade show rounded-4 border-0 shadow-sm" role="alert">
Product added successfully!
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>
<div class="card border-0 shadow-sm rounded-4">
<div class="card-body p-0">
<div class="table-responsive">
<table class="table table-hover align-middle mb-0">
<thead class="bg-light">
<tr class="text-muted small">
<th class="ps-4">PRODUCT NAME</th>
<th>CATEGORY</th>
<th>REGULAR PRICE</th>
<th>MEMBER PRICE</th>
<th>STOCK</th>
<th class="pe-4 text-end">ACTIONS</th>
</tr>
</thead>
<tbody>
<?php foreach ($products as $p): ?>
<tr>
<td class="ps-4 fw-bold"><?php echo htmlspecialchars($p['name']); ?></td>
<td><span class="badge bg-light text-dark border"><?php echo htmlspecialchars($p['category_name'] ?? 'General'); ?></span></td>
<td class="text-muted">Rp <?php echo number_format((float)$p['selling_price'], 0, ',', '.'); ?></td>
<td class="fw-bold text-success">Rp <?php echo number_format((float)$p['member_price'], 0, ',', '.'); ?></td>
<td>
<?php if ($p['stock'] <= 5): ?>
<span class="badge bg-soft-danger text-danger"><?php echo $p['stock']; ?> Low</span>
<?php else: ?>
<span class="badge bg-soft-success text-success"><?php echo $p['stock']; ?> In Stock</span>
<?php endif; ?>
</td>
<td class="pe-4 text-end">
<button class="btn btn-sm btn-light rounded-circle"><i class="bi bi-pencil"></i></button>
<button class="btn btn-sm btn-light rounded-circle text-danger"><i class="bi bi-trash"></i></button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Add Product Modal -->
<div class="modal fade" id="addProductModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content border-0 shadow rounded-4">
<form method="POST">
<input type="hidden" name="action" value="add">
<div class="modal-header border-0 p-4">
<h5 class="modal-title">Add New Product</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body p-4 pt-0">
<div class="mb-3">
<label class="form-label small fw-bold">Product Name</label>
<input type="text" name="name" class="form-control" placeholder="e.g. Kopi Susu" required>
</div>
<div class="mb-3">
<label class="form-label small fw-bold">Category</label>
<select name="category_id" class="form-select" required>
<?php foreach ($categories as $cat): ?>
<option value="<?php echo $cat['id']; ?>"><?php echo htmlspecialchars($cat['name']); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="row mb-3">
<div class="col">
<label class="form-label small fw-bold">Regular Price (Rp)</label>
<input type="number" name="selling_price" class="form-control" placeholder="25000" required>
</div>
<div class="col">
<label class="form-label small fw-bold">Member Price (Rp)</label>
<input type="number" name="member_price" class="form-control" placeholder="22000" required>
</div>
</div>
<div class="mb-3">
<label class="form-label small fw-bold">Initial Stock</label>
<input type="number" name="stock" class="form-control" placeholder="100" required>
</div>
</div>
<div class="modal-footer border-0 p-4 pt-0">
<button type="button" class="btn btn-light rounded-pill px-4" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary rounded-pill px-4">Save Product</button>
</div>
</form>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
document.getElementById('sidebarCollapse').addEventListener('click', function() {
document.getElementById('sidebar').classList.toggle('active');
});
</script>
</body>
</html>