36036-vm/pos.php
Flatlogic Bot e112ad7b40 Salaam 1.1
2025-11-22 16:45:55 +00:00

272 lines
9.5 KiB
PHP

<?php
declare(strict_types=1);
@ini_set('display_errors', '1');
@error_reporting(E_ALL);
@date_default_timezone_set('UTC');
require_once 'db/config.php';
$pdo = db();
$stmt_products = $pdo->query("SELECT * FROM products WHERE stock_quantity > 0 ORDER BY name ASC");
$products = $stmt_products->fetchAll();
$stmt_customers = $pdo->query("SELECT * FROM customers ORDER BY name ASC");
$customers = $stmt_customers->fetchAll();
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>POS Terminal</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css" rel="stylesheet">
<style>
body {
background-color: #f0f2f5;
}
.sidebar {
position: fixed;
top: 0;
left: 0;
height: 100%;
width: 250px;
background-color: #fff;
padding-top: 1rem;
}
.main-content {
margin-left: 250px;
padding: 2rem;
}
.sidebar .nav-link {
color: #333;
font-weight: 500;
}
.sidebar .nav-link:hover, .sidebar .nav-link.active {
color: #0d6efd;
}
.product-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
gap: 1rem;
}
.product-card {
cursor: pointer;
}
.product-card:hover {
border-color: #0d6efd;
}
</style>
</head>
<body>
<div class="sidebar">
<h4 class="px-3">POS System</h4>
<ul class="nav flex-column">
<li class="nav-item">
<a class="nav-link" href="index.php"><i class="bi bi-grid-fill"></i> Dashboard</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="pos.php"><i class="bi bi-cart-check-fill"></i> POS</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#" data-bs-toggle="collapse" data-bs-target="#inventory-submenu" aria-expanded="false" aria-controls="inventory-submenu"><i class="bi bi-box-seam-fill"></i> Inventory</a>
<div class="collapse" id="inventory-submenu">
<ul class="nav flex-column ms-3">
<li class="nav-item"><a class="nav-link" href="view_items.php">View Items</a></li>
<li class="nav-item"><a class="nav-link" href="add_item.php">Add Item</a></li>
</ul>
</div>
</li>
<li class="nav-item">
<a class="nav-link" href="customers.php"><i class="bi bi-people-fill"></i> Customers</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#"><i class="bi bi-truck"></i> Suppliers</a>
</li>
<li class="nav-item">
<a class="nav-link" href="sales.php"><i class="bi bi-receipt"></i> Sales</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#"><i class="bi bi-wallet-fill"></i> Expenses</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#"><i class="bi bi-person-circle"></i> Users</a>
</li>
</ul>
</div>
<div class="main-content">
<div class="row">
<div class="col-md-7">
<div class="card">
<div class="card-body">
<input type="text" id="productSearch" class="form-control mb-3" placeholder="Search products...">
<div class="product-grid">
<?php foreach ($products as $product): ?>
<div class="card product-card" onclick="addToCart(<?= htmlspecialchars(json_encode($product)) ?>)">
<div class="card-body text-center">
<h6 class="card-title"><?= htmlspecialchars($product['name']) ?></h6>
<p class="card-text text-muted"><?= htmlspecialchars((string)$product['sale_price']) ?></p>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
</div>
<div class="col-md-5">
<div class="card">
<div class="card-body">
<div class="mb-3">
<label for="customer" class="form-label">Customer</label>
<select id="customer" class="form-select">
<option value="">Select Customer</option>
<?php foreach ($customers as $customer): ?>
<option value="<?= $customer['id'] ?>"><?= htmlspecialchars($customer['name']) ?></option>
<?php endforeach; ?>
</select>
</div>
<h5>Cart</h5>
<table class="table">
<thead>
<tr>
<th>Item</th>
<th>Qty</th>
<th>Price</th>
<th>Total</th>
<th></th>
</tr>
</thead>
<tbody id="cart-items">
</tbody>
</table>
<hr>
<div class="d-flex justify-content-between">
<h5>Total:</h5>
<h5 id="cart-total">0.00</h5>
</div>
<div class="d-grid gap-2">
<button class="btn btn-primary" onclick="processPayment()">Pay Now</button>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
let cart = [];
function addToCart(product) {
const existingProduct = cart.find(item => item.id === product.id);
if (existingProduct) {
existingProduct.quantity++;
} else {
cart.push({ ...product, quantity: 1 });
}
renderCart();
}
function removeFromCart(productId) {
const productIndex = cart.findIndex(item => item.id === productId);
if (productIndex > -1) {
cart.splice(productIndex, 1);
}
renderCart();
}
function updateQuantity(productId, change) {
const product = cart.find(item => item.id === productId);
if (product) {
product.quantity += change;
if (product.quantity <= 0) {
removeFromCart(productId);
}
}
renderCart();
}
function renderCart() {
const cartItems = document.getElementById('cart-items');
cartItems.innerHTML = '';
let total = 0;
cart.forEach(item => {
const itemTotal = item.sale_price * item.quantity;
total += itemTotal;
const row = `
<tr>
<td>${item.name}</td>
<td>
<button class="btn btn-sm btn-secondary" onclick="updateQuantity(${item.id}, -1)">-</button>
${item.quantity}
<button class="btn btn-sm btn-secondary" onclick="updateQuantity(${item.id}, 1)">+</button>
</td>
<td>${item.sale_price}</td>
<td>${itemTotal.toFixed(2)}</td>
<td><button class="btn btn-sm btn-danger" onclick="removeFromCart(${item.id})"><i class="bi bi-x-circle"></i></button></td>
</tr>
`;
cartItems.innerHTML += row;
});
document.getElementById('cart-total').innerText = total.toFixed(2);
}
function processPayment() {
if(cart.length === 0) {
alert("Cart is empty!");
return;
}
const customerId = document.getElementById('customer').value;
const saleData = {
cart: cart,
customer_id: customerId
};
fetch('process_sale.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(saleData),
})
.then(response => response.json())
.then(data => {
if (data.success) {
cart = [];
renderCart();
alert(data.message);
// Optionally, redirect to a success page or print receipt
} else {
alert(data.message);
}
})
.catch((error) => {
console.error('Error:', error);
alert('An error occurred while processing the sale.');
});
}
document.getElementById('productSearch').addEventListener('keyup', function() {
const filter = this.value.toLowerCase();
const products = document.querySelectorAll('.product-card');
products.forEach(product => {
const title = product.querySelector('.card-title').innerText.toLowerCase();
if (title.includes(filter)) {
product.style.display = '';
} else {
product.style.display = 'none';
}
});
});
</script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>