window.location.href = '/Agent/agent-login'"; exit; } $conn = new mysqli($GLOBALS['host'], $GLOBALS['user'], $GLOBALS['pass'], $GLOBALS['db']); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $today = date("Y-m-d"); $userId = $_SESSION['user_id']; $message = ""; /* ---------------- Loan & Recurring Transactions (Today) ---------------- */ $sqlLoan = "SELECT SUM(AT_AMOUNT) as total FROM {$GLOBALS['arif_tran']} WHERE AT_ADMIN = '$userId' AND DATE(AT_TIMESTAMP) = '$today' AND AT_ACID LIKE '%L%'"; $resLoan = $conn->query($sqlLoan); $totalLoan = $resLoan->fetch_assoc()['total'] ?? 0; $sqlRecurring = "SELECT SUM(AT_AMOUNT) as total FROM {$GLOBALS['arif_tran']} WHERE AT_ADMIN = '$userId' AND DATE(AT_TIMESTAMP) = '$today' AND AT_ACID LIKE '%R%'"; $resRecurring = $conn->query($sqlRecurring); $totalRecurring = $resRecurring->fetch_assoc()['total'] ?? 0; /* ---------------- Get Today's Target ---------------- */ $targetSql = "SELECT * FROM agent_collections WHERE agent = '$userId' AND date = '$today'"; $targetResult = $conn->query($targetSql); $targetRows = $targetResult->fetch_all(MYSQLI_ASSOC); $collectableLoan = $targetRows[0]['collectable_loan_amount'] ?? 0; $collectableRecurring = $targetRows[0]['collectable_recurring_amount'] ?? 0; $remainingLoan = max($collectableLoan - $totalLoan, 0); $remainingRecurring = max($collectableRecurring - $totalRecurring, 0); $loanPercent = $collectableLoan > 0 ? ($totalLoan / $collectableLoan) * 100 : 0; $recurringPercent = $collectableRecurring > 0 ? ($totalRecurring / $collectableRecurring) * 100 : 0; /* ---------------- Save Collection ---------------- */ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_collection'])) { $rowId = intval($_POST['row_id']); $collectedLoan = $totalLoan; $collectedRecurring = $totalRecurring; $stmt = $conn->prepare("UPDATE agent_collections SET collected_loan_amount = ?, collected_recurring_amount = ? WHERE id = ? AND agent = ? AND date = ?"); $stmt->bind_param("ddiss", $collectedLoan, $collectedRecurring, $rowId, $userId, $today); if ($stmt->execute()) { $message = "

✅ Collection saved successfully!

"; } else { $message = "

❌ Failed to save collection!

"; } $stmt->close(); } /* ---------------- Last 10 Days Report ---------------- */ $repStmt = "SELECT * FROM agent_collections WHERE agent = '$userId' ORDER BY date DESC LIMIT 10"; $reportsStmt = $conn->query($repStmt); $reportRows = $reportsStmt->fetch_all(MYSQLI_ASSOC); $conn->close(); ?>

Welcome, 👋

Loan Collection

% Completed
Target: ₹ | Remaining: ₹

Recurring Collection

% Completed
Target: ₹ | Remaining: ₹
Receive New Payment

📊 Last 10 Days Report

0 ? ($report['collected_loan_amount'] / $report['collectable_loan_amount']) * 100 : 0; $recurringPercentDone = $report['collectable_recurring_amount'] > 0 ? ($report['collected_recurring_amount'] / $report['collectable_recurring_amount']) * 100 : 0; ?>

Loan Target:

Loan Collected:

Recurring Target:

Recurring Collected:

Loan: % Completed
Recurring: % Completed