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(); ?>Loan Collection
= round($loanPercent, 2) ?>% CompletedRecurring Collection
= round($recurringPercent, 2) ?>% CompletedLoan Target: ₹ = number_format($report['collectable_loan_amount'], 2) ?>
Loan Collected: ₹ = number_format($report['collected_loan_amount'], 2) ?>
Recurring Target: ₹ = number_format($report['collectable_recurring_amount'], 2) ?>
Recurring Collected: ₹ = number_format($report['collected_recurring_amount'], 2) ?>