162 lines
5.6 KiB
PHP
162 lines
5.6 KiB
PHP
<?php
|
|
session_start();
|
|
date_default_timezone_set('Asia/Kolkata');
|
|
|
|
if (!isset($_SESSION['user_id']) || empty($_SESSION['user_id'])) {
|
|
echo "<script>window.location.href = '/Agent/agent-login'</script>";
|
|
exit;
|
|
}
|
|
|
|
// Set default date range: 1st of current month to today
|
|
$firstDayOfMonth = date('Y-m-01');
|
|
$today = date('Y-m-d');
|
|
$defaultFrom = $firstDayOfMonth;
|
|
$defaultTo = $today;
|
|
|
|
// Use submitted dates or defaults
|
|
$dFrom = $_GET['dFrom'] ?? $defaultFrom;
|
|
$dTo = $_GET['dTo'] ?? $defaultTo;
|
|
?>
|
|
|
|
<!-- Generate Report Form -->
|
|
<div class="container mt-4">
|
|
<div class="card shadow-lg p-4 rounded-3">
|
|
<h4 class="mb-3">Generate Report</h4>
|
|
<form method="get" class="row g-3">
|
|
<div class="col-md-3">
|
|
<label class="form-label">From</label>
|
|
<input value="<?= $dFrom ?>" type="date" name="dFrom" class="form-control" required>
|
|
</div>
|
|
<div class="col-md-3">
|
|
<label class="form-label">To</label>
|
|
<input value="<?= $dTo ?>" type="date" name="dTo" class="form-control" required>
|
|
</div>
|
|
|
|
<?php
|
|
// Show agent dropdown for admin users
|
|
if($_SESSION['type'] === 'admin'){
|
|
$conn = new mysqli($GLOBALS['host'], $GLOBALS['user'], $GLOBALS['pass'], $GLOBALS['db']);
|
|
if ($conn->connect_error) die("Connection failed: " . $conn->connect_error);
|
|
|
|
$agents = $conn->query("SELECT * FROM `".$GLOBALS['arif_users']."` WHERE `type`='agent' ORDER BY user_name ASC");
|
|
?>
|
|
<div class="col-md-3">
|
|
<label class="form-label">Agent</label>
|
|
<select name="agent" id="agent" class="form-control">
|
|
<option value="">-- All Agents --</option>
|
|
<?php
|
|
if($agents && $agents->num_rows > 0){
|
|
while($a = $agents->fetch_assoc()){
|
|
$selected = (isset($_GET['agent']) && $_GET['agent']==$a['user_id']) ? "selected" : "";
|
|
echo "<option value='".$a['user_id']."' $selected>".$a['user_name']." (".$a['user_id'].")</option>";
|
|
}
|
|
}
|
|
?>
|
|
</select>
|
|
</div>
|
|
<?php } ?>
|
|
|
|
<div class="col-md-3 d-flex align-items-end" style="margin-top: 25px; z-index: -1;">
|
|
<button type="submit" class="btn btn-info w-100">Generate Report</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<?php
|
|
function report_view($fromDate, $toDate) {
|
|
$conn = new mysqli($GLOBALS['host'], $GLOBALS['user'], $GLOBALS['pass'], $GLOBALS['db']);
|
|
if ($conn->connect_error) die("Connection failed: " . $conn->connect_error);
|
|
|
|
// Display alert for report period
|
|
echo '<div class="container mt-4">
|
|
<div class="alert alert-primary shadow-sm">
|
|
<h5 class="mb-0">Report Period: '.$fromDate.' → '.$toDate.' (Up to)</h5>
|
|
</div>
|
|
</div>';
|
|
|
|
$totalAmount = 0;
|
|
|
|
echo '
|
|
<div class="container mt-3" style="margin-bottom: 105px;">
|
|
<div class="card shadow-sm rounded-3">
|
|
<div class="card-body">
|
|
<h5 class="mb-3">Transaction Report</h5>
|
|
<div class="table-responsive">
|
|
<table class="table table-bordered table-hover align-middle" id="reportTable" style="font-size: 12px;">
|
|
<thead class="table-light">
|
|
<tr>
|
|
<th>Transaction ID</th>
|
|
'.($_SESSION['type'] === 'admin' ? "<th>Agent</th>" : "").'
|
|
<th>Time</th>
|
|
<th>AC No</th>
|
|
<th>Name</th>
|
|
<th>Amount</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>';
|
|
|
|
// Base query
|
|
$sql = "SELECT t.*, a.AA_NAME, a.AA_AGENT
|
|
FROM `".$GLOBALS['arif_tran']."` t
|
|
INNER JOIN `".$GLOBALS['arif_ac']."` a ON t.AT_ACID = a.AA_ACNO
|
|
WHERE DATE(CONVERT_TZ(t.AT_TIMESTAMP,'+00:00','+05:30')) BETWEEN '$fromDate' AND '$toDate'";
|
|
|
|
// Add agent filter if admin has selected a specific agent
|
|
if ($_SESSION['type'] === 'admin' && isset($_GET['agent']) && !empty($_GET['agent'])) {
|
|
$agentId = $conn->real_escape_string($_GET['agent']);
|
|
$sql .= " AND a.AA_AGENT = '$agentId'";
|
|
} else if ($_SESSION['type'] === 'agent') {
|
|
// For agents, only show their own transactions - FIXED
|
|
$sql .= " AND a.AA_AGENT = '".$_SESSION['user_id']."'";
|
|
}
|
|
|
|
$sql .= " ORDER BY t.AT_ID DESC";
|
|
|
|
$result = $conn->query($sql);
|
|
|
|
if ($result && $result->num_rows > 0) {
|
|
while($row = $result->fetch_assoc()) {
|
|
// Convert UTC timestamp to Kolkata time
|
|
$kolkataTime = date("d M Y, h:i A", strtotime($row["AT_TIMESTAMP"] . " +5 hours 30 minutes"));
|
|
|
|
echo "
|
|
<tr>
|
|
<td>".$row["AT_ID"]."</td>";
|
|
|
|
// Show agent column for admin
|
|
if ($_SESSION['type'] === 'admin') {
|
|
echo "<td>".$row["AA_AGENT"]."</td>";
|
|
}
|
|
|
|
echo "
|
|
<td>".$kolkataTime."</td>
|
|
<td>".$row["AT_ACID"]."</td>
|
|
<td>".$row["AA_NAME"]."</td>
|
|
<td>".$row["AT_AMOUNT"]."</td>
|
|
</tr>";
|
|
|
|
$totalAmount += $row["AT_AMOUNT"];
|
|
}
|
|
} else {
|
|
echo "<tr><td colspan='".($_SESSION['type'] === 'admin' ? 6 : 5)."' class='text-center text-muted'>No results found</td></tr>";
|
|
}
|
|
|
|
$conn->close();
|
|
|
|
echo '
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<hr>
|
|
<h5 class="text-end">Total Transaction Amount : <b>'.$totalAmount.'</b></h5>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
';
|
|
}
|
|
|
|
// ---- Call report function ----
|
|
// Always show report with default or selected dates
|
|
report_view($dFrom, $dTo);
|
|
?>
|