window.location.href = '/Agent/agent-login'"; exit; } // Database connection $conn = new mysqli($GLOBALS['host'], $GLOBALS['user'], $GLOBALS['pass'], $GLOBALS['db']); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // Process status update if form is submitted if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_status'])) { $id = $conn->real_escape_string($_POST['id']); $status = $conn->real_escape_string($_POST['status']); $approved_by = $_SESSION['user_id']; $remarks = $conn->real_escape_string($_POST['remarks'] ?? ''); try { $conn->begin_transaction(); // First get the transaction details $getStmt = $conn->prepare("SELECT * FROM fund_trans WHERE id = ?"); $getStmt->bind_param("i", $id); $getStmt->execute(); $transaction = $getStmt->get_result()->fetch_assoc(); $getStmt->close(); if ($transaction) { // Check if the current user has permission to approve this request $can_approve = false; // Admin can approve BM requests if ($_SESSION['type'] === 'admin' && $transaction['request_usr_type'] === 'bm') { $can_approve = true; } // BM can approve Admin requests elseif ($_SESSION['type'] === 'bm' && $transaction['request_usr_type'] === 'admin') { $can_approve = true; } if (!$can_approve) { throw new Exception("You don't have permission to approve this request."); } $updateStmt = $conn->prepare("UPDATE fund_trans SET status = ?, approved_by = ?, approved_usr_type = ?, remarks = ? WHERE id = ?"); $updateStmt->bind_param("ssssi", $status, $approved_by, $_SESSION['type'], $remarks, $id); $updateStmt->execute(); // If approved, process the fund transfer if ($status == 1) { $transfer_amount = abs($transaction['transfer_amount']); // Make it positive $rec_ac_number = $transaction['rec_ac_number']; // Recurring Account Number $loan_ac_number = $transaction['loan_ac_number']; // Loan Account Number // Check if Recurring Account has sufficient balance $balanceCheck = $conn->prepare("SELECT AA_BAL FROM `" . $GLOBALS['arif_ac'] . "` WHERE AA_ACNO = ?"); $balanceCheck->bind_param("s", $rec_ac_number); $balanceCheck->execute(); $balanceCheck->bind_result($current_balance); $balanceCheck->fetch(); $balanceCheck->close(); if ($current_balance < $transfer_amount) { throw new Exception("Insufficient balance in Recurring Account."); } // Deduct from Recurring Account $deductStmt = $conn->prepare("UPDATE `" . $GLOBALS['arif_ac'] . "` SET AA_BAL = AA_BAL - ? WHERE AA_ACNO = ?"); $deductStmt->bind_param("ds", $transfer_amount, $rec_ac_number); $deductStmt->execute(); $deductStmt->close(); // Add to Loan Account $addStmt = $conn->prepare("UPDATE `" . $GLOBALS['arif_ac'] . "` SET AA_BAL = AA_BAL + ? WHERE AA_ACNO = ?"); $addStmt->bind_param("ds", $transfer_amount, $loan_ac_number); $addStmt->execute(); $addStmt->close(); // Create transaction records $userType = $_SESSION['type']; $table = $GLOBALS['arif_tran'] ?? 'arif_tran'; // Deduction from Recurring Account $remarksText1 = "₹$transfer_amount transferred to Loan A/c $loan_ac_number"; $stmt1 = $conn->prepare("INSERT INTO `$table` (AT_ADMIN, AT_ACID, AT_AMOUNT, REMARKS) VALUES (?, ?, ?, ?)"); $negative_amount = -$transfer_amount; $stmt1->bind_param("ssds", $userType, $rec_ac_number, $negative_amount, $remarksText1); $stmt1->execute(); $stmt1->close(); // Credit to Loan Account $remarksText2 = "₹$transfer_amount received from Recurring A/c $rec_ac_number"; $stmt2 = $conn->prepare("INSERT INTO `$table` (AT_ADMIN, AT_ACID, AT_AMOUNT, REMARKS) VALUES (?, ?, ?, ?)"); $stmt2->bind_param("ssds", $userType, $loan_ac_number, $transfer_amount, $remarksText2); $stmt2->execute(); $stmt2->close(); } if ($updateStmt->affected_rows > 0) { $conn->commit(); $success_message = "Status updated successfully!"; } else { throw new Exception("No rows affected. Update failed."); } $updateStmt->close(); } else { throw new Exception("Transaction not found."); } } catch (Exception $e) { $conn->rollback(); $error_message = "Error updating status: " . $e->getMessage(); } } // Get all pending items where status = 0 $countResult = []; try { $table = 'fund_trans'; // Only show requests that the current user can approve if ($_SESSION['type'] === 'admin') { $countStmt = $conn->prepare("SELECT * FROM `$table` WHERE status = 0 AND request_usr_type = 'bm' ORDER BY created DESC"); } elseif ($_SESSION['type'] === 'bm') { $countStmt = $conn->prepare("SELECT * FROM `$table` WHERE status = 0 AND request_usr_type = 'admin' ORDER BY created DESC"); } else { // For other user types, show nothing $countStmt = $conn->prepare("SELECT * FROM `$table` WHERE status = 0 AND 1=0 ORDER BY created DESC"); } $countStmt->execute(); $result = $countStmt->get_result(); while ($row = $result->fetch_assoc()) { $countResult[] = $row; } $countStmt->close(); } catch (Exception $e) { $error_message = "Error: " . $e->getMessage(); } // Get approval history $historyResult = []; try { if ($_SESSION['type'] === 'admin' || $_SESSION['type'] === 'bm') { $historyStmt = $conn->prepare("SELECT * FROM `fund_trans` WHERE status != 0 ORDER BY created DESC LIMIT 20"); $historyStmt->execute(); $history = $historyStmt->get_result(); while ($row = $history->fetch_assoc()) { $historyResult[] = $row; } $historyStmt->close(); } } catch (Exception $e) { $history_error = "Error loading history: " . $e->getMessage(); } $conn->close(); ?>
| ID | Requested By | Req. User Type | Recurring Account | Loan Account | Amount | Req. On | Actions |
|---|---|---|---|---|---|---|---|
|
|
All requests have been processed or you don't have any requests to approve.
| ID | Requested By | Requested User Type | Approved By | Approved User Type | Recurring Account | Loan Account | Amount | Status | Requested On | Approved On |
|---|---|---|---|---|---|---|---|---|---|---|
| N/A | Approved Rejected Pending | N/A |
There are no approved or rejected requests in the history.