$item) {
if ($item['id'] == $productId) {
// Ensure we don't exceed stock
$newQuantity = $item['quantity'] + $quantity;
if ($newQuantity <= $product['stok']) {
$_SESSION['cart'][$key]['quantity'] = $newQuantity;
} else {
$_SESSION['error_message'] = "Tidak dapat menambahkan {$quantity} unit {$product['nama_barang']}. Total melebihi stok tersedia ({$product['stok']}).";
header('Location: index.php');
exit;
}
$exists = true;
break;
}
}
// If not exists, add to cart
if (!$exists) {
$_SESSION['cart'][] = [
'id' => $productId,
'name' => $product['nama_barang'],
'code' => $product['kode_barang'],
'price' => $product['harga'],
'quantity' => $quantity,
'max_stock' => $product['stok']
];
}
} else {
// Set error message for stock limit
$_SESSION['error_message'] = "Tidak dapat menambahkan {$quantity} unit {$product['nama_barang']}. Hanya {$product['stok']} tersedia.";
}
} else {
$_SESSION['error_message'] = "Produk tidak ditemukan.";
}
// Redirect to avoid form resubmission
header('Location: index.php');
exit;
}
// Process adding multiple products to cart
if (isset($_POST['product_ids']) && is_array($_POST['product_ids'])) {
// Log data for debugging
error_log('Adding multiple products. product_ids: ' . print_r($_POST['product_ids'], true));
foreach ($_POST['product_ids'] as $productId) {
// Validate product ID
$productId = (int)$productId;
if ($productId <= 0) continue;
// Get product details
$product = getProductById($productId);
if ($product) {
// Check stock availability
if ($product['stok'] > 0) {
// Check if product already in cart, update quantity if exists
$exists = false;
foreach ($_SESSION['cart'] as $key => $item) {
if ($item['id'] == $productId) {
// Only add if it won't exceed stock
if ($item['quantity'] < $product['stok']) {
$_SESSION['cart'][$key]['quantity'] += 1; // Default quantity is 1
}
$exists = true;
break;
}
}
// If not exists, add to cart
if (!$exists) {
$_SESSION['cart'][] = [
'id' => $productId,
'name' => $product['nama_barang'],
'code' => $product['kode_barang'],
'price' => $product['harga'],
'quantity' => 1,
'max_stock' => $product['stok']
];
}
} else {
// Log out-of-stock items
error_log("Produk {$product['nama_barang']} (ID: {$productId}) stok habis.");
}
}
}
// Redirect to avoid form resubmission
header('Location: index.php');
exit;
}
// Update cart item quantity
if (isset($_POST['update_cart']) && isset($_POST['product_id'])) {
$productId = (int)$_POST['product_id'];
$quantity = isset($_POST['quantity']) ? (int)$_POST['quantity'] : 1;
// Validate quantity
if ($quantity < 1) {
$quantity = 1;
}
// Get product details for stock check
$product = getProductById($productId);
if ($product) {
// Make sure quantity doesn't exceed stock
if ($quantity > $product['stok']) {
$quantity = $product['stok'];
$_SESSION['error_message'] = "Kuantitas disesuaikan ke stok maksimum yang tersedia ({$product['stok']}).";
}
// Update quantity in cart
foreach ($_SESSION['cart'] as $key => $item) {
if ($item['id'] == $productId) {
$_SESSION['cart'][$key]['quantity'] = $quantity;
$_SESSION['cart'][$key]['max_stock'] = $product['stok']; // Update max stock in case it changed
break;
}
}
}
header('Location: index.php');
exit;
}
// Remove item from cart
if (isset($_GET['remove_item'])) {
$index = $_GET['remove_item'];
if (isset($_SESSION['cart'][$index])) {
unset($_SESSION['cart'][$index]);
$_SESSION['cart'] = array_values($_SESSION['cart']); // Re-index array
}
header('Location: index.php');
exit;
}
// Clear cart
if (isset($_GET['clear_cart'])) {
$_SESSION['cart'] = [];
header('Location: index.php');
exit;
}
// Process checkout
if (isset($_POST['checkout'])) {
// Log untuk debugging
error_log("Checkout process started: " . json_encode($_POST));
$items = [];
$total = 0;
// Validasi keranjang
if (!isset($_SESSION['cart']) || empty($_SESSION['cart'])) {
$_SESSION['error_message'] = "Keranjang Anda kosong. Silakan tambahkan item ke keranjang.";
header('Location: index.php');
exit;
}
foreach ($_SESSION['cart'] as $item) {
$items[] = [
'product_id' => $item['id'],
'quantity' => $item['quantity']
];
$total += $item['price'] * $item['quantity'];
}
// Get cash and change amount
$cashAmount = isset($_POST['cash_amount']) ? (float)$_POST['cash_amount'] : 0;
$changeAmount = isset($_POST['change_amount']) ? (float)$_POST['change_amount'] : 0;
// Validate cash amount
if ($cashAmount < $total) {
$_SESSION['error_message'] = "Jumlah tunai harus sama dengan atau lebih besar dari jumlah total.";
header('Location: index.php');
exit;
}
// Log transaction details for debugging
error_log("Processing checkout: Items: " . count($items) . ", Total: $total, Cash: $cashAmount, Change: $changeAmount");
// Create transaction with cash and change amounts
$result = createTransaction($items, $total, $cashAmount, $changeAmount);
if ($result['success']) {
// Log success for debugging
error_log("Transaction successful. ID: " . $result['transaction_id']);
// Clear cart after successful checkout
$_SESSION['cart'] = [];
$_SESSION['last_transaction'] = $result;
// Redirect to success page
header('Location: transaction_success.php?id=' . $result['transaction_id']);
exit;
} else {
// Log error for debugging
error_log("Transaction failed: " . $result['message']);
$error = $result['message'];
$_SESSION['error_message'] = $error;
header('Location: index.php');
exit;
}
}
// Get all product types
$productTypes = getProductTypes();
// Handle search query
$searchQuery = isset($_GET['search']) ? trim($_GET['search']) : '';
// Get products by selected product type or all products
$selectedType = isset($_GET['type']) ? $_GET['type'] : null;
$products = getProducts($selectedType, $searchQuery);
// Calculate cart totals
$cartItems = count($_SESSION['cart']);
$total = 0;
foreach ($_SESSION['cart'] as $item) {
$total += $item['price'] * $item['quantity'];
}
?>
Ayula Store - POS
Hasil pencarian untuk:
( produk ditemukan)
0): ?>
Tidak ada barang ditemukan.
Keranjang Belanja
Transaksi sedang berlangsung
$item): ?>
-
- Rp.
-
Keranjang Anda kosong.
-

Tunai
Apakah Anda yakin ingin menghapus item ini dari keranjang Anda?
Apakah Anda yakin ingin menghapus semua item dari keranjang Anda?