prepare("SELECT * FROM users WHERE username = ?"); $stmt->bind_param("s", $username); $stmt->execute(); $result = $stmt->get_result(); $user = $result->fetch_assoc(); if ($user && password_verify($password, $user['password'])) { $_SESSION['user_id'] = $user['id']; $_SESSION['username'] = $user['username']; $_SESSION['role'] = $user['role']; echo json_encode([ "status" => "success", "user" => ["username" => $user['username'], "role" => $user['role']] ]); } else { echo json_encode(["status" => "error", "message" => "ভুল ইউজারনেম বা পাসওয়ার্ড!"]); } exit; } // সেসন চেক if (!isset($_SESSION['user_id']) && $action !== 'login') { echo json_encode(["status" => "unauthorized"]); exit; } // ২. লগআউট if ($action === 'logout') { session_destroy(); echo json_encode(["status" => "success"]); exit; } // ৩. নতুন ইউজার যোগ করা (Admin Only) if ($action === 'add_user') { if ($_SESSION['role'] !== 'admin') { echo json_encode(["status" => "error", "message" => "শুধুমাত্র এডমিন নতুন ইউজার যোগ করতে পারবেন।"]); exit; } $data = json_decode(file_get_contents("php://input"), true); $newUser = $data['username']; $newPass = password_hash($data['password'], PASSWORD_BCRYPT); $newRole = $data['role'] ?? 'staff'; $stmt = $conn->prepare("INSERT INTO users (username, password, role) VALUES (?, ?, ?)"); $stmt->bind_param("sss", $newUser, $newPass, $newRole); if ($stmt->execute()) { echo json_encode(["status" => "success"]); } else { echo json_encode(["status" => "error", "message" => "ইউজারনেমটি আগে থেকেই রয়েছে!"]); } exit; } // ৪. কাস্টমার ও ব্যালেন্স ডাটা লোড if ($action === 'get_all') { $custResult = $conn->query("SELECT * FROM customers ORDER BY created_at DESC"); $customers = []; while ($row = $custResult->fetch_assoc()) { $custId = $row['id']; $histResult = $conn->query("SELECT entry_id as entryId, type, amount, details, files, timestamp FROM history WHERE cust_id = '$custId' ORDER BY id ASC"); $history = []; while ($hRow = $histResult->fetch_assoc()) { $hRow['files'] = json_decode($hRow['files']) ?? []; $history[] = $hRow; } $row['history'] = $history; $customers[] = $row; } $settResult = $conn->query("SELECT cash, wallet FROM settings WHERE id=1"); $settings = $settResult->fetch_assoc(); echo json_encode([ "status" => "success", "currentUser" => ["username" => $_SESSION['username'], "role" => $_SESSION['role']], "customers" => $customers, "cash" => (float)($settings['cash'] ?? 0), "wallet" => (float)($settings['wallet'] ?? 0) ]); exit; } // ৫. কাস্টমার সেভ / আপডেট if ($action === 'save_customer') { $data = json_decode(file_get_contents("php://input"), true); $stmt = $conn->prepare("INSERT INTO customers (id, name, phone, acc, aadhar, address) VALUES (?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE name=?, phone=?, acc=?, aadhar=?, address=?"); $stmt->bind_param("sssssssssss", $data['id'], $data['name'], $data['phone'], $data['acc'], $data['aadhar'], $data['address'], $data['name'], $data['phone'], $data['acc'], $data['aadhar'], $data['address']); $stmt->execute(); if (isset($data['history']) && count($data['history']) > 0) { $first = $data['history'][0]; $filesJson = json_encode($first['files'] ?? []); $hStmt = $conn->prepare("INSERT IGNORE INTO history (entry_id, cust_id, type, amount, details, files, timestamp) VALUES (?, ?, ?, ?, ?, ?, ?)"); $hStmt->bind_param("sssssss", $first['entryId'], $data['id'], $first['type'], $first['amount'], $first['details'], $filesJson, $first['timestamp']); $hStmt->execute(); } echo json_encode(["status" => "success"]); exit; } // ৬. ট্রানজাকশন সেভ করা if ($action === 'save_transaction') { $data = json_decode(file_get_contents("php://input"), true); $filesJson = json_encode($data['files'] ?? []); $stmt = $conn->prepare("INSERT INTO history (entry_id, cust_id, type, amount, details, files, timestamp) VALUES (?, ?, ?, ?, ?, ?, ?)"); $stmt->bind_param("sssssss", $data['entryId'], $data['custId'], $data['type'], $data['amount'], $data['details'], $filesJson, $data['timestamp']); $stmt->execute(); $conn->query("UPDATE settings SET cash = '{$data['newCash']}', wallet = '{$data['newWallet']}' WHERE id=1"); echo json_encode(["status" => "success"]); exit; } // ৭. ক্যাশ ও ওয়ালেট সেটিং আপডেট if ($action === 'save_settings') { $data = json_decode(file_get_contents("php://input"), true); $conn->query("UPDATE settings SET cash = '{$data['cash']}', wallet = '{$data['wallet']}' WHERE id=1"); echo json_encode(["status" => "success"]); exit; } // ৮. টাইমলাইন হিস্ট্রি মোছা if ($action === 'delete_history') { $entryId = $_GET['entry_id'] ?? ''; $stmt = $conn->prepare("DELETE FROM history WHERE entry_id = ?"); $stmt->bind_param("s", $entryId); $stmt->execute(); echo json_encode(["status" => "success"]); exit; } ?>