window.location.href = '/Agent/agent-login'"; exit; } function getTotalAmount(array $rows): float { $amounts = array_column($rows, 'AT_AMOUNT'); return array_sum($amounts); } $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 = ""; // success/error message holder /* === Save button click handle === */ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_collection'])) { $rowId = intval($_POST['row_id']); $collectedAmount = floatval($_POST['collected_amount']); $stmt = $conn->prepare("UPDATE agent_collections SET collected_amount = ? WHERE id = ? AND agent = ? AND date = ?"); $stmt->bind_param("diss", $collectedAmount, $rowId, $userId, $today); if ($stmt->execute()) { $message = "
✅ Collection saved successfully!
"; } else { $message = "❌ Failed to save collection!
"; } $stmt->close(); } /* === Data fetch === */ $targetSql = "SELECT * FROM agent_collections WHERE agent = '$userId' AND date = '$today'"; $targetResult = $conn->query($targetSql); $targetRows = $targetResult->fetch_all(MYSQLI_ASSOC); $sql = "SELECT * FROM {$GLOBALS['arif_tran']} WHERE AT_ADMIN = '$userId' AND DATE(AT_TIMESTAMP) = '$today'"; $result = $conn->query($sql); $rows = $result ? $result->fetch_all(MYSQLI_ASSOC) : []; $totalAmount = getTotalAmount($rows); $collectable = $targetRows[0]['collectable_amount'] ?? 0; $remaining = $collectable - $totalAmount; $remaining = $remaining < 0 ? 0 : $remaining; $percent = $collectable > 0 ? ($totalAmount / $collectable) * 100 : 0; // Fetch 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); // echo json_encode($reportRows); $conn->close(); ?>Total Collection
= round($percent, 2) ?>% CompletedTotal Collectable
Target: ₹ = number_format($report['collectable_amount'], 2) ?>
Collected: ₹ = number_format($report['collected_amount'], 2) ?>
Remaining: ₹ = number_format($report['collectable_amount'] - $report['collected_amount'], 2) ?>