<?php
// Start session
session_start();
// Check if user is logged in
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'student') {
header('Location: ../login.php');
exit();
}
// Include database connection
require_once('database/db_config.php');
// Get student ID from session
$student_id = $_SESSION['user_id'];
// Fetch all payments for this student
$payments_query = "
SELECT p.*, c.title as course_title, c.image as course_image, e.payment_plan
FROM payments p
JOIN courses c ON p.course_id = c.id
LEFT JOIN enrollments e ON e.course_id = p.course_id AND e.user_id = p.user_id
WHERE p.user_id = ?
ORDER BY p.payment_date DESC
";
$stmt = $conn->prepare($payments_query);
$stmt->bind_param('i', $student_id);
$stmt->execute();
$payments_result = $stmt->get_result();
// Fetch student info
$student_query = "
SELECT CONCAT(first_name, ' ', last_name) AS student_name, email, phone
FROM users
WHERE id = ?
";
$stmt = $conn->prepare($student_query);
$stmt->bind_param('i', $student_id);
$stmt->execute();
$student_result = $stmt->get_result();
$student = $student_result->fetch_assoc();
// Get institute details for receipt
$settings_query = "SELECT * FROM site_settings WHERE setting_key IN ('site_name', 'site_address', 'contact_phone', 'contact_email', 'site_logo', 'site_url')";
$settings_result = $conn->query($settings_query);
$settings = [];
if ($settings_result) {
while ($row = $settings_result->fetch_assoc()) {
$settings[$row['setting_key']] = $row['setting_value'];
}
}
// Include header
include_once 'includes/header.php';
?>
<div class="container py-4">
<div class="d-flex justify-content-between align-items-center mb-4">
<h4><i class="fas fa-history me-2"></i> Payment History</h4>
<a href="payments.php" class="btn btn-outline-primary btn-sm">
<i class="fas fa-credit-card me-1"></i> Make Payment
</a>
</div>
<?php if ($payments_result->num_rows > 0): ?>
<div class="card shadow-sm mb-4">
<div class="card-header bg-light">
<h5 class="mb-0">Transaction History</h5>
</div>
<div class="card-body p-0">
<div class="table-responsive">
<table class="table table-hover mb-0">
<thead class="table-light">
<tr>
<th>Date</th>
<th>Course</th>
<th>Amount</th>
<th>Method</th>
<th>Status</th>
<th class="text-center">Action</th>
</tr>
</thead>
<tbody>
<?php while($payment = $payments_result->fetch_assoc()):
// Format payment date
$payment_date = new DateTime($payment['payment_date']);
$formatted_date = $payment_date->format('d M Y, h:i A');
// Generate receipt number
$receipt_id = !empty($payment['transaction_id']) ? 'RCP-' . substr($payment['transaction_id'], -8) : 'RCP-' . date('Ymd', strtotime($payment['payment_date'])) . '-' . $payment['id'];
?>
<tr>
<td><?php echo $formatted_date; ?></td>
<td>
<div class="d-flex align-items-center">
<?php if (!empty($payment['course_image'])): ?>
<img src="../<?php echo $payment['course_image']; ?>" class="rounded me-2" width="40" height="40" alt="<?php echo htmlspecialchars($payment['course_title']); ?>">
<?php endif; ?>
<div>
<div class="fw-bold"><?php echo htmlspecialchars($payment['course_title']); ?></div>
<small class="text-muted"><?php echo htmlspecialchars($payment['payment_for'] ?? 'Course Fee'); ?></small>
</div>
</div>
</td>
<td class="fw-bold">₹<?php echo number_format($payment['amount'], 2); ?></td>
<td><?php echo ucfirst($payment['payment_method']); ?></td>
<td>
<?php if ($payment['status'] === 'completed'): ?>
<span class="badge bg-success">Completed</span>
<?php elseif ($payment['status'] === 'pending'): ?>
<span class="badge bg-warning">Pending</span>
<?php elseif ($payment['status'] === 'failed'): ?>
<span class="badge bg-danger">Failed</span>
<?php else: ?>
<span class="badge bg-secondary"><?php echo ucfirst($payment['status']); ?></span>
<?php endif; ?>
</td>
<td class="text-center">
<?php if ($payment['status'] === 'completed'): ?>
<button class="btn btn-sm btn-outline-primary"
onclick="viewReceipt(<?php echo htmlspecialchars(json_encode([
'payment_id' => $payment['id'],
'receipt_id' => $receipt_id,
'transaction_id' => $payment['transaction_id'],
'student_name' => $student['student_name'],
'email' => $student['email'],
'phone' => $student['phone'],
'course_id' => $payment['course_id'],
'course_title' => $payment['course_title'],
'amount' => $payment['amount'],
'payment_date' => $payment['payment_date'],
'payment_method' => $payment['payment_method'],
'payment_status' => $payment['status'],
'payment_for' => $payment['payment_for'] ?? 'Course Fee',
'payment_plan' => $payment['payment_plan'] ?? 'full'
])); ?>)">
<i class="fas fa-file-invoice me-1"></i> Receipt
</button>
<?php else: ?>
<span class="text-muted small">No Receipt</span>
<?php endif; ?>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</div>
</div>
</div>
<div class="row mb-4">
<div class="col-md-6">
<div class="card shadow-sm">
<div class="card-header bg-light">
<h5 class="mb-0">Payment Summary</h5>
</div>
<div class="card-body">
<?php
// Reset the result set pointer
$stmt->execute();
$payments_result = $stmt->get_result();
$total_paid = 0;
$payment_count = 0;
$course_count = [];
while($payment = $payments_result->fetch_assoc()) {
if ($payment['status'] === 'completed') {
$total_paid += $payment['amount'];
$payment_count++;
$course_count[$payment['course_id']] = true;
}
}
?>
<div class="row g-3">
<div class="col-6">
<div class="border rounded p-3 text-center h-100">
<h3 class="text-primary mb-0">₹<?php echo number_format($total_paid, 2); ?></h3>
<p class="text-muted mb-0">Total Paid</p>
</div>
</div>
<div class="col-6">
<div class="border rounded p-3 text-center h-100">
<h3 class="text-primary mb-0"><?php echo $payment_count; ?></h3>
<p class="text-muted mb-0">Transactions</p>
</div>
</div>
<div class="col-12">
<div class="border rounded p-3 text-center">
<h3 class="text-primary mb-0"><?php echo count($course_count); ?></h3>
<p class="text-muted mb-0">Courses Enrolled</p>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<div class="card shadow-sm">
<div class="card-header bg-light">
<h5 class="mb-0">Payment Methods</h5>
</div>
<div class="card-body">
<?php
// Reset the result set pointer
$stmt->execute();
$payments_result = $stmt->get_result();
$payment_methods = [];
while($payment = $payments_result->fetch_assoc()) {
if ($payment['status'] === 'completed') {
$method = $payment['payment_method'];
if (!isset($payment_methods[$method])) {
$payment_methods[$method] = ['count' => 0, 'amount' => 0];
}
$payment_methods[$method]['count']++;
$payment_methods[$method]['amount'] += $payment['amount'];
}
}
?>
<div class="table-responsive">
<table class="table table-sm">
<thead>
<tr>
<th>Payment Method</th>
<th>Count</th>
<th class="text-end">Amount</th>
</tr>
</thead>
<tbody>
<?php foreach($payment_methods as $method => $data): ?>
<tr>
<td>
<?php
switch(strtolower($method)) {
case 'razorpay':
echo '<i class="fab fa-cc-visa me-1 text-primary"></i> ';
break;
case 'cash':
echo '<i class="fas fa-money-bill-wave me-1 text-success"></i> ';
break;
case 'bank':
echo '<i class="fas fa-university me-1 text-info"></i> ';
break;
default:
echo '<i class="fas fa-credit-card me-1"></i> ';
}
echo ucfirst($method);
?>
</td>
<td><?php echo $data['count']; ?></td>
<td class="text-end">₹<?php echo number_format($data['amount'], 2); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
<tfoot>
<tr class="table-light">
<th>Total</th>
<th><?php echo $payment_count; ?></th>
<th class="text-end">₹<?php echo number_format($total_paid, 2); ?></th>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
</div>
</div>
<?php else: ?>
<div class="alert alert-info">
<i class="fas fa-info-circle me-2"></i> You don't have any payment records yet.
</div>
<?php endif; ?>
</div>
<!-- Payment Receipt Modal -->
<div class="modal fade" id="receiptModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header bg-primary text-white">
<h5 class="modal-title">
<i class="fas fa-file-invoice me-2"></i> Payment Receipt
</h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body p-4" id="receiptContent">
<div class="receipt-container" id="receiptToPrint">
<div class="receipt-header">
<div class="row align-items-center">
<div class="col-md-3 text-center">
<?php if (!empty($settings['site_logo'])): ?>
<img src="../<?php echo $settings['site_logo']; ?>" class="receipt-logo" alt="Institute Logo" style="max-width: 100px; max-height: 80px;" onerror="this.src='../assets/img/logo.png'; this.onerror='';">
<?php else: ?>
<img src="../assets/img/logo.png" class="receipt-logo" alt="Institute Logo" style="max-width: 100px; max-height: 80px;">
<?php endif; ?>
</div>
<div class="col-md-9 text-center text-md-start">
<h3 class="receipt-title mb-0" id="institute-name"><?php echo htmlspecialchars($settings['site_name'] ?? 'Institute'); ?></h3>
<p class="receipt-subtitle mb-0 small" id="institute-address"><?php echo nl2br(htmlspecialchars($settings['site_address'] ?? '')); ?></p>
<p class="receipt-subtitle small mb-0">
<?php if (!empty($settings['contact_phone'])): ?>Phone: <span id="institute-phone"><?php echo htmlspecialchars($settings['contact_phone']); ?></span> | <?php endif; ?>
<?php if (!empty($settings['contact_email'])): ?>Email: <span id="institute-email"><?php echo htmlspecialchars($settings['contact_email']); ?></span><?php endif; ?>
</p>
</div>
</div>
<div class="text-center mt-3 mb-2">
<h4 class="border-bottom border-top py-2">PAYMENT RECEIPT</h4>
</div>
</div>
<div class="receipt-body">
<div class="row g-3 mb-2">
<div class="col-md-6">
<div class="card h-100">
<div class="card-header bg-light py-1">
<h6 class="mb-0">Student Information</h6>
</div>
<div class="card-body py-2">
<p class="mb-1 small"><strong>Name:</strong> <span id="student-name"></span></p>
<p class="mb-1 small"><strong>Email:</strong> <span id="student-email"></span></p>
<p class="mb-0 small"><strong>Phone:</strong> <span id="student-phone"></span></p>
</div>
</div>
</div>
<div class="col-md-6">
<div class="card h-100">
<div class="card-header bg-light py-1">
<h6 class="mb-0">Payment Information</h6>
</div>
<div class="card-body py-2">
<p class="mb-1 small"><strong>Receipt No:</strong> <span id="receipt-number"></span></p>
<p class="mb-1 small"><strong>Date:</strong> <span id="payment-date"></span></p>
<p class="mb-0 small"><strong>Method:</strong> <span id="payment-method"></span></p>
</div>
</div>
</div>
</div>
<div class="card mb-3">
<div class="card-header bg-light py-1">
<h6 class="mb-0">Course Details & Payment Summary</h6>
</div>
<div class="card-body p-0">
<div class="table-responsive">
<table class="table table-bordered table-sm mb-0">
<thead class="table-light">
<tr>
<th width="40%">Course Description</th>
<th width="15%">Fee Type</th>
<th width="25%">Payment Plan</th>
<th width="20%" class="text-end">Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<strong id="course-title"></strong><br>
<small class="text-muted" id="payment-for"></small>
</td>
<td id="payment-for-type"></td>
<td id="payment-plan-display">
</td>
<td class="text-end" id="payment-amount"></td>
</tr>
<tr>
<td colspan="3" class="text-end"><strong>Total Paid:</strong></td>
<td class="text-end"><strong id="total-paid"></strong></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div id="installment-notice" class="alert alert-info py-2 small mb-3" style="display: none;">
<strong>Note:</strong> This is an installment payment.
Next payment will be due on <span id="next-payment-date"></span>.
</div>
<div class="row">
<div class="col-md-12 d-flex flex-column justify-content-end">
<div class="text-end mt-3">
<p class="mb-5 pt-5 small">Authorized Signature</p>
<hr style="width: 150px; border-top: 1px solid #333; float: right;">
<p class="small mb-0">For <span id="institute-name-2"><?php echo htmlspecialchars($settings['site_name'] ?? 'Institute'); ?></span></p>
</div>
</div>
</div>
</div>
<div class="receipt-footer mt-3 pt-2 border-top small text-center">
<p class="mb-0">This is a computer generated receipt and does not require a physical signature.</p>
<p class="mb-0">For any queries, please contact: <span id="institute-phone-2"><?php echo htmlspecialchars($settings['contact_phone'] ?? 'Your Contact Phone'); ?></span> |
<span id="institute-email-2"><?php echo htmlspecialchars($settings['contact_email'] ?? '[email protected]'); ?></span></p>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" onclick="printReceipt()">
<i class="fas fa-print me-2"></i> Print Receipt
</button>
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<style>
.receipt-container {
background-color: white;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
padding: 20px;
max-width: 800px;
margin: 0 auto;
}
.receipt-logo {
max-height: 80px;
max-width: 100%;
}
.receipt-title {
color: #333;
font-weight: 600;
}
.receipt-subtitle {
color: #666;
}
.receipt-footer {
color: #666;
font-size: 12px;
margin-top: 15px;
padding-top: 10px;
border-top: 1px solid #eee;
}
@media print {
body * {
visibility: hidden;
}
#receiptToPrint, #receiptToPrint * {
visibility: visible;
}
#receiptToPrint {
position: absolute;
left: 0;
top: 0;
width: 100%;
}
.modal {
position: absolute !important;
left: 0 !important;
top: 0 !important;
margin: 0 !important;
padding: 0 !important;
overflow: visible !important;
background: white !important;
}
.modal-dialog {
max-width: 100% !important;
width: 100% !important;
margin: 0 !important;
transform: none !important;
}
.modal-content {
border: none !important;
box-shadow: none !important;
background: white !important;
}
.modal-header, .modal-footer, .btn-close {
display: none !important;
}
.card {
border: 1px solid #ddd !important;
margin-bottom: 15px !important;
}
.card-header {
background-color: #f5f5f5 !important;
padding: 8px 10px !important;
}
.card-body {
padding: 10px !important;
}
h3 { font-size: 18px !important; }
h4 { font-size: 16px !important; }
h5, h6 { font-size: 14px !important; }
p, .card-body p { font-size: 12px !important; margin-bottom: 6px !important; }
.table th, .table td {
padding: 6px !important;
font-size: 12px !important;
}
}
</style>
<script>
function viewReceipt(paymentData) {
// Format the payment date
const paymentDate = new Date(paymentData.payment_date);
const formattedDate = paymentDate.toLocaleDateString('en-IN', {
day: '2-digit',
month: 'short',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
hour12: true
});
// Populate the receipt modal
document.getElementById('student-name').textContent = paymentData.student_name;
document.getElementById('student-email').textContent = paymentData.email;
document.getElementById('student-phone').textContent = paymentData.phone || 'N/A';
document.getElementById('receipt-number').textContent = paymentData.receipt_id;
document.getElementById('payment-date').textContent = formattedDate;
document.getElementById('payment-method').textContent = paymentData.payment_method.charAt(0).toUpperCase() + paymentData.payment_method.slice(1);
document.getElementById('course-title').textContent = paymentData.course_title;
document.getElementById('payment-for').textContent = paymentData.payment_for;
document.getElementById('payment-for-type').textContent = paymentData.payment_for;
// Format the amount
const formattedAmount = new Intl.NumberFormat('en-IN', {
style: 'currency',
currency: 'INR',
minimumFractionDigits: 2
}).format(paymentData.amount);
document.getElementById('payment-amount').textContent = formattedAmount;
document.getElementById('total-paid').textContent = formattedAmount;
// Set payment plan display
let paymentPlanDisplay = 'Full Payment';
let totalInstallments = 1;
let showInstallmentNotice = false;
if (paymentData.payment_plan === 'monthly') {
paymentPlanDisplay = 'Monthly Plan<br><small class="text-muted">(3 monthly payments)</small>';
totalInstallments = 3;
showInstallmentNotice = true;
} else if (paymentData.payment_plan === 'half_duration' || paymentData.payment_plan === 'six_month') {
paymentPlanDisplay = 'Installment Plan<br><small class="text-muted">(2 installments)</small>';
totalInstallments = 2;
showInstallmentNotice = true;
} else if (paymentData.payment_plan === 'quarterly') {
paymentPlanDisplay = 'Quarterly Plan<br><small class="text-muted">(4 quarterly payments)</small>';
totalInstallments = 4;
showInstallmentNotice = true;
} else {
paymentPlanDisplay = 'Full Payment<br><small class="text-muted">(One-time payment)</small>';
}
document.getElementById('payment-plan-display').innerHTML = paymentPlanDisplay;
// Show/hide installment notice
if (showInstallmentNotice) {
document.getElementById('installment-notice').style.display = 'block';
// Calculate next payment date (1 month after the current payment)
const nextPaymentDate = new Date(paymentDate);
nextPaymentDate.setMonth(nextPaymentDate.getMonth() + 1);
const formattedNextDate = nextPaymentDate.toLocaleDateString('en-IN', {
day: '2-digit',
month: 'short',
year: 'numeric'
});
document.getElementById('next-payment-date').textContent = formattedNextDate;
} else {
document.getElementById('installment-notice').style.display = 'none';
}
// Show the modal
const receiptModal = new bootstrap.Modal(document.getElementById('receiptModal'));
receiptModal.show();
}
function printReceipt() {
const printContent = document.getElementById('receiptToPrint').innerHTML;
const originalContent = document.body.innerHTML;
document.body.innerHTML = printContent;
window.print();
document.body.innerHTML = originalContent;
// Re-initialize receipt modal and event handlers
setTimeout(function() {
const receiptModal = new bootstrap.Modal(document.getElementById('receiptModal'));
receiptModal.show();
// Re-attach event handlers
document.querySelectorAll('[onclick^="viewReceipt"]').forEach(button => {
button.addEventListener('click', function() {
eval(button.getAttribute('onclick'));
});
});
}, 500);
}
</script>
<?php include_once 'includes/footer.php'; ?>