arif_grafin/CONTENT/ROOT_URI/Admin/notification.php

502 lines
22 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;
}
// Database connection
$conn = new mysqli($GLOBALS['host'], $GLOBALS['user'], $GLOBALS['pass'], $GLOBALS['db']);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Process status update if form is submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_status'])) {
$id = $conn->real_escape_string($_POST['id']);
$status = $conn->real_escape_string($_POST['status']);
$approved_by = $_SESSION['user_id'];
$remarks = $conn->real_escape_string($_POST['remarks'] ?? '');
try {
$conn->begin_transaction();
// First get the transaction details
$getStmt = $conn->prepare("SELECT * FROM fund_trans WHERE id = ?");
$getStmt->bind_param("i", $id);
$getStmt->execute();
$transaction = $getStmt->get_result()->fetch_assoc();
$getStmt->close();
if ($transaction) {
// Check if the current user has permission to approve this request
$can_approve = false;
// Admin can approve BM requests
if ($_SESSION['type'] === 'admin' && $transaction['request_usr_type'] === 'bm') {
$can_approve = true;
}
// BM can approve Admin requests
elseif ($_SESSION['type'] === 'bm' && $transaction['request_usr_type'] === 'admin') {
$can_approve = true;
}
if (!$can_approve) {
throw new Exception("You don't have permission to approve this request.");
}
$updateStmt = $conn->prepare("UPDATE fund_trans SET status = ?, approved_by = ?, approved_usr_type = ?, remarks = ? WHERE id = ?");
$updateStmt->bind_param("ssssi", $status, $approved_by, $_SESSION['type'], $remarks, $id);
$updateStmt->execute();
// If approved, process the fund transfer
if ($status == 1) {
$transfer_amount = abs($transaction['transfer_amount']); // Make it positive
$rec_ac_number = $transaction['rec_ac_number']; // Recurring Account Number
$loan_ac_number = $transaction['loan_ac_number']; // Loan Account Number
// Check if Recurring Account has sufficient balance
$balanceCheck = $conn->prepare("SELECT AA_BAL FROM `" . $GLOBALS['arif_ac'] . "` WHERE AA_ACNO = ?");
$balanceCheck->bind_param("s", $rec_ac_number);
$balanceCheck->execute();
$balanceCheck->bind_result($current_balance);
$balanceCheck->fetch();
$balanceCheck->close();
if ($current_balance < $transfer_amount) {
throw new Exception("Insufficient balance in Recurring Account.");
}
// Deduct from Recurring Account
$deductStmt = $conn->prepare("UPDATE `" . $GLOBALS['arif_ac'] . "` SET AA_BAL = AA_BAL - ? WHERE AA_ACNO = ?");
$deductStmt->bind_param("ds", $transfer_amount, $rec_ac_number);
$deductStmt->execute();
$deductStmt->close();
// Add to Loan Account
$addStmt = $conn->prepare("UPDATE `" . $GLOBALS['arif_ac'] . "` SET AA_BAL = AA_BAL + ? WHERE AA_ACNO = ?");
$addStmt->bind_param("ds", $transfer_amount, $loan_ac_number);
$addStmt->execute();
$addStmt->close();
// Create transaction records
$userType = $_SESSION['type'];
$table = $GLOBALS['arif_tran'] ?? 'arif_tran';
// Deduction from Recurring Account
$remarksText1 = "$transfer_amount transferred to Loan A/c $loan_ac_number";
$stmt1 = $conn->prepare("INSERT INTO `$table` (AT_ADMIN, AT_ACID, AT_AMOUNT, REMARKS) VALUES (?, ?, ?, ?)");
$negative_amount = -$transfer_amount;
$stmt1->bind_param("ssds", $userType, $rec_ac_number, $negative_amount, $remarksText1);
$stmt1->execute();
$stmt1->close();
// Credit to Loan Account
$remarksText2 = "$transfer_amount received from Recurring A/c $rec_ac_number";
$stmt2 = $conn->prepare("INSERT INTO `$table` (AT_ADMIN, AT_ACID, AT_AMOUNT, REMARKS) VALUES (?, ?, ?, ?)");
$stmt2->bind_param("ssds", $userType, $loan_ac_number, $transfer_amount, $remarksText2);
$stmt2->execute();
$stmt2->close();
}
if ($updateStmt->affected_rows > 0) {
$conn->commit();
$success_message = "Status updated successfully!";
} else {
throw new Exception("No rows affected. Update failed.");
}
$updateStmt->close();
} else {
throw new Exception("Transaction not found.");
}
} catch (Exception $e) {
$conn->rollback();
$error_message = "Error updating status: " . $e->getMessage();
}
}
// Get all pending items where status = 0
$countResult = [];
try {
$table = 'fund_trans';
// Only show requests that the current user can approve
if ($_SESSION['type'] === 'admin') {
$countStmt = $conn->prepare("SELECT * FROM `$table` WHERE status = 0 AND request_usr_type = 'bm' ORDER BY created DESC");
} elseif ($_SESSION['type'] === 'bm') {
$countStmt = $conn->prepare("SELECT * FROM `$table` WHERE status = 0 AND request_usr_type = 'admin' ORDER BY created DESC");
} else {
// For other user types, show nothing
$countStmt = $conn->prepare("SELECT * FROM `$table` WHERE status = 0 AND 1=0 ORDER BY created DESC");
}
$countStmt->execute();
$result = $countStmt->get_result();
while ($row = $result->fetch_assoc()) {
$countResult[] = $row;
}
$countStmt->close();
} catch (Exception $e) {
$error_message = "Error: " . $e->getMessage();
}
// Get approval history
$historyResult = [];
try {
if ($_SESSION['type'] === 'admin' || $_SESSION['type'] === 'bm') {
$historyStmt = $conn->prepare("SELECT * FROM `fund_trans` WHERE status != 0 ORDER BY created DESC LIMIT 20");
$historyStmt->execute();
$history = $historyStmt->get_result();
while ($row = $history->fetch_assoc()) {
$historyResult[] = $row;
}
$historyStmt->close();
}
} catch (Exception $e) {
$history_error = "Error loading history: " . $e->getMessage();
}
$conn->close();
?>
<!-- Notification Section -->
<div class="container mt-4">
<?php if (isset($success_message)): ?>
<div class="alert alert-success alert-dismissible fade in" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
<strong>Success!</strong> <?php echo $success_message; ?>
</div>
<?php endif; ?>
<?php if (isset($error_message)): ?>
<div class="alert alert-danger alert-dismissible fade in" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
<strong>Error!</strong> <?php echo $error_message; ?>
</div>
<?php endif; ?>
<div class="panel panel-default">
<div class="panel-heading">
<div class="panel-title">
<h4 class="pull-left">Pending Fund Transfer Requests</h4>
<span class="badge pull-right"><?php echo count($countResult); ?> Pending</span>
<div class="clearfix"></div>
</div>
</div>
<div class="panel-body">
<?php if (count($countResult) > 0): ?>
<div class="table-responsive">
<table class="table table-bordered table-hover" id="notificationTable" style="font-size: 14px;">
<thead>
<tr>
<th>ID</th>
<th>Requested By</th>
<th>Req. User Type</th>
<th>Recurring Account</th>
<th>Loan Account</th>
<th>Amount</th>
<th>Req. On</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($countResult as $row): ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['request_by']; ?></td>
<td><span class="label label-<?php echo $row['request_usr_type'] === 'admin' ? 'primary' : 'info'; ?>"><?php echo strtoupper($row['request_usr_type']); ?></span></td>
<td><?php echo $row['rec_ac_number']; ?></td>
<td><?php echo $row['loan_ac_number']; ?></td>
<td class="text-danger"><strong><?php echo $row['transfer_amount']; ?></strong></td>
<td><?php echo date("d M Y, h:i A", strtotime($row['created'])); ?></td>
<td>
<div class="btn-group btn-group-sm">
<button class="btn btn-success" onclick="openStatusModal(<?php echo $row['id']; ?>, 1)">
<i class="glyphicon glyphicon-ok"></i> Approve
</button>
<button class="btn btn-danger" onclick="openStatusModal(<?php echo $row['id']; ?>, 2)">
<i class="glyphicon glyphicon-remove"></i> Reject
</button>
<button class="btn btn-info" onclick="viewDetails(<?php echo $row['id']; ?>)">
<i class="glyphicon glyphicon-eye-open"></i> View
</button>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php else: ?>
<div class="alert alert-info text-center">
<i class="glyphicon glyphicon-info-sign" style="font-size: 24px;"></i>
<h4>No pending fund transfer requests</h4>
<p>All requests have been processed or you don't have any requests to approve.</p>
</div>
<?php endif; ?>
</div>
</div>
</div>
<!-- Approval History Section -->
<div class="container mt-4">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">Approval History</h4>
</div>
<div class="panel-body">
<?php if (isset($history_error)): ?>
<div class="alert alert-warning">
<?php echo $history_error; ?>
</div>
<?php elseif (count($historyResult) > 0): ?>
<div class="table-responsive">
<table class="table table-bordered table-hover" id="historyTable" style="font-size: 14px;">
<thead>
<tr>
<th>ID</th>
<th>Requested By</th>
<th>Requested User Type</th>
<th>Approved By</th>
<th>Approved User Type</th>
<th>Recurring Account</th>
<th>Loan Account</th>
<th>Amount</th>
<th>Status</th>
<th>Requested On</th>
<th>Approved On</th>
</tr>
</thead>
<tbody>
<?php foreach ($historyResult as $row): ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['request_by']; ?></td>
<td><span class="label label-<?php echo $row['request_usr_type'] === 'admin' ? 'primary' : 'info'; ?>"><?php echo strtoupper($row['request_usr_type']); ?></span></td>
<td><?php echo $row['approved_by'] ?? 'N/A'; ?></td>
<td>
<?php if ($row['approved_usr_type']): ?>
<span class="label label-<?php echo $row['approved_usr_type'] === 'admin' ? 'primary' : 'info'; ?>"><?php echo strtoupper($row['approved_usr_type']); ?></span>
<?php else: ?>
N/A
<?php endif; ?>
</td>
<td><?php echo $row['rec_ac_number']; ?></td>
<td><?php echo $row['loan_ac_number']; ?></td>
<td class="<?php echo $row['status'] == 1 ? 'text-success' : 'text-danger'; ?>">
<strong><?php echo $row['transfer_amount']; ?></strong>
</td>
<td>
<?php if ($row['status'] == 1): ?>
<span class="label label-success">Approved</span>
<?php elseif ($row['status'] == 2): ?>
<span class="label label-danger">Rejected</span>
<?php else: ?>
<span class="label label-warning">Pending</span>
<?php endif; ?>
</td>
<td><?php echo date("d M Y, h:i A", strtotime($row['created'])); ?></td>
<td>
<?php if ($row['status'] != 0): ?>
<?php echo date("d M Y, h:i A", strtotime($row['created'])); ?>
<?php else: ?>
N/A
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php else: ?>
<div class="alert alert-info text-center">
<i class="glyphicon glyphicon-info-sign" style="font-size: 24px;"></i>
<h4>No approval history found</h4>
<p>There are no approved or rejected requests in the history.</p>
</div>
<?php endif; ?>
</div>
</div>
</div>
<!-- View Details Modal -->
<div class="modal fade" id="viewModal" tabindex="-1" role="dialog" aria-labelledby="viewModalLabel">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
<h4 class="modal-title" id="viewModalLabel">Transaction Details</h4>
</div>
<div class="modal-body">
<div class="row">
<div class="col-md-6">
<p><strong>ID:</strong> <span id="detail-id"></span></p>
<p><strong>Requested By:</strong> <span id="detail-request-by"></span></p>
<p><strong>User Type:</strong> <span id="detail-usr-type"></span></p>
</div>
<div class="col-md-6">
<p><strong>Recurring Account:</strong> <span id="detail-rec-account"></span></p>
<p><strong>Loan Account:</strong> <span id="detail-loan-account"></span></p>
<p><strong>Amount:</strong> <span id="detail-amount" class="text-danger"><strong></strong></span></p>
</div>
</div>
<div class="row mt-3">
<div class="col-12">
<p><strong>Requested On:</strong> <span id="detail-created"></span></p>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- Status Update Modal -->
<div class="modal fade" id="statusModal" tabindex="-1" role="dialog" aria-labelledby="statusModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form method="post" action="">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
<h4 class="modal-title" id="statusModalLabel">Update Transaction Status</h4>
</div>
<div class="modal-body">
<input type="hidden" name="id" id="status-id">
<input type="hidden" name="update_status" value="1">
<div class="form-group">
<label for="status" class="control-label">Status</label>
<select class="form-control" id="status" name="status" required>
<option value="1">Approve</option>
<option value="2">Reject</option>
</select>
</div>
<div class="form-group">
<label for="remarks" class="control-label">Remarks (Optional)</label>
<textarea class="form-control" id="remarks" name="remarks" rows="3"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Update Status</button>
</div>
</form>
</div>
</div>
</div>
<script>
// View transaction details
function viewDetails(id) {
<?php foreach ($countResult as $row): ?>
if (<?php echo $row['id']; ?> === id) {
document.getElementById('detail-id').textContent = <?php echo $row['id']; ?>;
document.getElementById('detail-request-by').textContent = "<?php echo $row['request_by']; ?>";
document.getElementById('detail-usr-type').textContent = "<?php echo $row['request_usr_type']; ?>";
document.getElementById('detail-rec-account').textContent = "<?php echo $row['rec_ac_number']; ?>";
document.getElementById('detail-loan-account').textContent = "<?php echo $row['loan_ac_number']; ?>";
document.getElementById('detail-amount').textContent = "<?php echo $row['transfer_amount']; ?>";
document.getElementById('detail-created').textContent = "<?php echo date("d M Y, h:i A", strtotime($row['created'])); ?>";
}
<?php endforeach; ?>
// Use Bootstrap 3 modal method
$('#viewModal').modal('show');
}
// Open status update modal
function openStatusModal(id, status) {
document.getElementById('status-id').value = id;
document.getElementById('status').value = status;
// Use Bootstrap 3 modal method
$('#statusModal').modal('show');
}
// Initialize DataTable if we have records (if DataTable is available)
document.addEventListener('DOMContentLoaded', function() {
// Check if DataTable is available (if you're using it)
if (typeof $.fn.DataTable !== 'undefined') {
<?php if (count($countResult) > 0): ?>
$('#notificationTable').DataTable({
"pageLength": 10,
"order": [[6, "desc"]],
"language": {
"search": "Search transactions:",
"lengthMenu": "Show _MENU_ entries",
"info": "Showing _START_ to _END_ of _TOTAL_ entries",
"paginate": {
"previous": "Previous",
"next": "Next"
}
}
});
<?php endif; ?>
<?php if (count($historyResult) > 0): ?>
$('#historyTable').DataTable({
"pageLength": 10,
"order": [[9, "desc"]],
"language": {
"search": "Search history:",
"lengthMenu": "Show _MENU_ entries",
"info": "Showing _START_ to _END_ of _TOTAL_ entries",
"paginate": {
"previous": "Previous",
"next": "Next"
}
}
});
<?php endif; ?>
}
});
</script>
<style>
.panel {
border-radius: 4px;
box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}
.table th {
font-weight: 600;
background-color: #f5f5f5;
}
.btn-group-sm > .btn {
padding: 5px 10px;
font-size: 12px;
line-height: 1.5;
border-radius: 3px;
}
.badge {
background-color: #d9534f;
font-size: 14px;
padding: 5px 10px;
margin-top: 5px;
}
.label {
font-size: 85%;
padding: 0.2em 0.6em 0.3em;
}
</style>