update agent dashboard, agent target assign,

This commit is contained in:
2025-09-26 14:19:31 +00:00
parent 32562554ad
commit ab034aeb72
5 changed files with 144 additions and 546 deletions

View File

@@ -5,11 +5,6 @@ if (!isset($_SESSION['user_id']) || empty($_SESSION['user_id'])) {
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);
@@ -19,12 +14,47 @@ $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']);
$collectedAmount = floatval($_POST['collected_amount']);
$collectedLoan = $totalLoan;
$collectedRecurring = $totalRecurring;
$stmt = $conn->prepare("UPDATE agent_collections SET collected_amount = ? WHERE id = ? AND agent = ? AND date = ?");
$stmt->bind_param("diss", $collectedAmount, $rowId, $userId, $today);
$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 = "<p class='success-msg'>✅ Collection saved successfully!</p>";
@@ -34,22 +64,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_collection'])) {
$stmt->close();
}
$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;
$repStmt = "SELECT * FROM agent_collections WHERE agent = '$userId' ORDER BY date DESC LIMIT 10;";
/* ---------------- 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);
@@ -64,76 +80,94 @@ $conn->close();
<?= $message ?>
<div class="dashboard-total-section">
<!-- Loan Box -->
<div class="card-box highlight">
<h3>₹ <?= number_format($totalAmount, 2) ?></h3>
<p>Total Collection</p>
<h3>₹ <?= number_format($totalLoan, 2) ?></h3>
<p>Loan Collection</p>
<div class="progress-bar">
<div class="progress-fill" style="width: <?= $percent ?>%;"></div>
<div class="progress-fill" style="width: <?= $loanPercent ?>%;"></div>
</div>
<small><?= round($loanPercent, 2) ?>% Completed</small>
<div class="remaining-text">
Target: ₹ <?= number_format($collectableLoan, 2) ?> |
Remaining: ₹ <?= number_format($remainingLoan, 2) ?>
</div>
<small><?= round($percent, 2) ?>% Completed</small>
</div>
<!-- Recurring Box -->
<div class="card-box normal">
<h3>₹ <?= number_format($collectable, 2) ?></h3>
<p>Total Collectable</p>
<h3>₹ <?= number_format($totalRecurring, 2) ?></h3>
<p>Recurring Collection</p>
<div class="progress-bar">
<div class="progress-fill" style="width: <?= $recurringPercent ?>%;"></div>
</div>
<small><?= round($recurringPercent, 2) ?>% Completed</small>
<div class="remaining-text">
Remaining: <strong>₹ <?= number_format($remaining, 2) ?></strong>
Target: ₹ <?= number_format($collectableRecurring, 2) ?> |
Remaining: ₹ <?= number_format($remainingRecurring, 2) ?>
</div>
</div>
</div>
<!-- Buttons -->
<div style="display: flex; flex-direction: row; width: 100%; justify-content: space-between; margin-top: 20px;">
<a class="btn btn-primary w-100" href="/Agent/Receive">
<i class="fa-solid fa-credit-card"></i>
Receive New Payment
<i class="fa-solid fa-credit-card"></i> Receive New Payment
</a>
<?php if (!empty($targetRows)) : ?>
<form method="post" class="w-100">
<input type="hidden" name="save_collection" value="1">
<input type="hidden" name="row_id" value="<?= $targetRows[0]['id'] ?>">
<input type="hidden" name="collected_amount" value="<?= $totalAmount ?>">
<button type="submit" class="btn btn-success w-100"><i class="fa-solid fa-save"></i> Save Collection</button>
<button type="submit" class="btn btn-success w-100">
<i class="fa-solid fa-save"></i> Save Collection
</button>
</form>
<?php endif; ?>
</div>
<!-- Report Section -->
<?php if (!empty($reportRows)) : ?>
<div class="report-section">
<h4 class="report-title">📊 Last 10 Days Report</h4>
<div class="report-cards">
<?php foreach ($reportRows as $report): ?>
<?php
$loanPercentDone = $report['collectable_loan_amount'] > 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;
?>
<div class="report-card">
<div class="report-date">
<?= date("d M Y", strtotime($report['date'])) ?>
</div>
<div class="report-details">
<p><strong>Target:</strong> ₹ <?= number_format($report['collectable_amount'], 2) ?></p>
<p><strong>Collected:</strong> ₹ <?= number_format($report['collected_amount'], 2) ?></p>
<p><strong>Remaining:</strong> ₹ <?= number_format($report['collectable_amount'] - $report['collected_amount'], 2) ?></p>
<p><strong>Loan Target:</strong> ₹ <?= number_format($report['collectable_loan_amount'], 2) ?></p>
<p><strong>Loan Collected:</strong> ₹ <?= number_format($report['collected_loan_amount'], 2) ?></p>
<p><strong>Recurring Target:</strong> ₹ <?= number_format($report['collectable_recurring_amount'], 2) ?></p>
<p><strong>Recurring Collected:</strong> ₹ <?= number_format($report['collected_recurring_amount'], 2) ?></p>
</div>
<div class="report-progress">
<?php
$percentDone = $report['collectable_amount'] > 0
? ($report['collected_amount'] / $report['collectable_amount']) * 100
: 0;
?>
<div class="progress-bar">
<div class="progress-fill" style="width: <?= $percentDone ?>%;"></div>
<div class="progress-fill" style="width: <?= $loanPercentDone ?>%; background: #e95420;"></div>
</div>
<small><?= round($percentDone, 2) ?>% Completed</small>
<small>Loan: <?= round($loanPercentDone, 2) ?>% Completed</small>
<div class="progress-bar">
<div class="progress-fill" style="width: <?= $recurringPercentDone ?>%; background: #007bff;"></div>
</div>
<small>Recurring: <?= round($recurringPercentDone, 2) ?>% Completed</small>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
<?php endif; ?>
<div class="android-links">
<a href="/Agent/commission">Commission</a>
<!-- <a href="/Agent/Report">Report</a>
<a href="/Agent/Profile">Profile</a> -->
</div>
</div>
<style>
.welcome-text {
font-size: 20px;