false, 'message' => 'Terjadi kesalahan internal. Silakan coba lagi.', 'product' => null, 'cart_html' => '', 'cart_totals' => [ 'items' => 0, 'total' => 0, 'formatted_total' => '0' ] ]); return true; } set_error_handler('handleErrors'); // Try-catch untuk menangkap semua exception try { // Pastikan sesi dimulai if (session_status() === PHP_SESSION_NONE) { session_start(); } // Log request untuk debugging error_log("Barcode scan request received: " . json_encode($_POST)); // Initialize response $response = [ 'success' => false, 'message' => 'Barcode tidak valid', 'product' => null, 'cart_html' => '', 'cart_totals' => [ 'items' => 0, 'total' => 0, 'formatted_total' => '0' ] ]; // Check if barcode is provided if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['barcode'])) { $barcode = trim($_POST['barcode']); // Validate barcode if (empty($barcode)) { $response['message'] = "Barcode tidak boleh kosong"; sendResponse($response); } // Get product by barcode (kode_barang) $product = getProductByBarcode($barcode); if (!$product) { $response['message'] = "Produk tidak ditemukan untuk barcode: " . htmlspecialchars($barcode); sendResponse($response); } // Check stock availability if ($product['stok'] <= 0) { $response['message'] = "Produk " . htmlspecialchars($product['nama_barang']) . " stok habis"; sendResponse($response); } // Initialize cart if not exists if (!isset($_SESSION['cart']) || !is_array($_SESSION['cart'])) { $_SESSION['cart'] = []; error_log("Cart initialized"); } $productId = $product['id_barangK']; $quantity = 1; // Default quantity is 1 // 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; } else { // Stock limit reached $response['success'] = false; $response['message'] = "Tidak dapat menambahkan unit lagi. Batas stok tercapai."; sendResponse($response); } $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'] ]; } // Set success response $response['success'] = true; $response['message'] = 'Produk ditambahkan ke keranjang'; $response['product'] = [ 'id' => $product['id_barangK'], 'name' => $product['nama_barang'], 'code' => $product['kode_barang'], 'price' => $product['harga'], 'stock' => $product['stok'] ]; // Generate cart HTML $response['cart_html'] = generateCartHTML(); // Calculate cart totals $cartItems = count($_SESSION['cart']); $total = 0; foreach ($_SESSION['cart'] as $item) { $total += $item['price'] * $item['quantity']; } $response['cart_totals'] = [ 'items' => $cartItems, 'total' => $total, 'formatted_total' => number_format($total, 0, ',', '.') ]; // Log cart state for debugging error_log("Cart updated. Items: " . $cartItems . ", Total: " . $total); } // Send the response sendResponse($response); } catch (Exception $e) { // Log exception error_log("Exception in get_product_by_barcode.php: " . $e->getMessage()); // Return error response sendResponse([ 'success' => false, 'message' => 'Terjadi kesalahan: ' . $e->getMessage(), 'product' => null, 'cart_html' => '', 'cart_totals' => [ 'items' => 0, 'total' => 0, 'formatted_total' => '0' ] ]); } /** * Generate HTML for cart items * * @return string HTML content for cart */ /** * Generate HTML for cart items * Improved version to ensure proper layout and scrolling * * @return string HTML content for cart */ function generateCartHTML() { if (isset($_SESSION['cart']) && !empty($_SESSION['cart'])) { $html = '
'; foreach ($_SESSION['cart'] as $index => $item) { // Get product details to retrieve image $productDetails = getProductById($item['id']); $productImage = !empty($productDetails['gambar']) ? $productDetails['gambar'] : ''; $html .= ' '; } $html .= '
'; // Add script to setup event handlers and fix layout $html .= ' '; return $html; } else { return '

Keranjang Anda kosong.

'; } }