<?php
// Enable error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Start session
session_start();
// Include database configuration
require_once '../admin/database/db_config.php';
// If user is not logged in, redirect to login page
if (!isset($_SESSION['user_id'])) {
header('Location: ../login.php');
exit;
}
// Get payment ID from URL parameter
$payment_id = isset($_GET['payment_id']) ? intval($_GET['payment_id']) : 0;
$user_id = $_SESSION['user_id'];
// Validate the payment exists and belongs to the current user
$payment_query = "SELECT p.*,
c.title as course_title, c.duration, c.level,
u.first_name, u.last_name, u.email, u.phone,
e.payment_plan
FROM payments p
JOIN courses c ON p.course_id = c.id
JOIN users u ON p.user_id = u.id
LEFT JOIN enrollments e ON e.course_id = c.id AND e.user_id = p.user_id
WHERE p.id = ? AND p.user_id = ?";
$stmt = $conn->prepare($payment_query);
$stmt->bind_param("ii", $payment_id, $user_id);
$stmt->execute();
$payment_result = $stmt->get_result();
$payment = $payment_result->fetch_assoc();
// If payment not found or doesn't belong to the user, redirect
if (!$payment) {
header('Location: payments.php');
exit;
}
// Get school information for receipt
$school = [
'site_name' => 'Popular Computer Institute',
'site_address' => 'Bhimpura No. 1, Ballia, Uttar Pradesh 221716, India',
'contact_phone' => '+91 9984878446',
'contact_email' => '[email protected]',
'site_logo' => 'assets/img/logo.png',
'site_url' => 'https://pcib.in'
];
try {
$site_settings_query = "SELECT * FROM site_settings WHERE setting_key IN ('site_name', 'site_address', 'contact_phone', 'contact_email', 'site_logo', 'site_url')";
$site_settings_result = $conn->query($site_settings_query);
if ($site_settings_result && $site_settings_result->num_rows > 0) {
while($row = $site_settings_result->fetch_assoc()) {
// Map site_settings keys to our school array keys
$key_mapping = [
'site_name' => 'site_name',
'site_address' => 'site_address',
'contact_phone' => 'contact_phone',
'contact_email' => 'contact_email',
'site_logo' => 'site_logo',
'site_url' => 'site_url'
];
if (isset($key_mapping[$row['setting_key']])) {
$school[$key_mapping[$row['setting_key']]] = $row['setting_value'];
}
}
}
} catch (Exception $e) {
// Silently handle the error - default values already set
error_log("Settings table error: " . $e->getMessage());
}
// Calculate payment details
$payment_plan = $payment['payment_plan'] ?? 'full';
// Parse course duration to determine payment schedule
$duration_parts = explode(' ', $payment['duration']);
$duration_value = isset($duration_parts[0]) ? intval($duration_parts[0]) : 3; // Default to 3 if not specified
$duration_unit = isset($duration_parts[1]) ? strtolower($duration_parts[1]) : 'months'; // Default to months
// Normalize duration to months for calculation
$duration_in_months = $duration_value;
if ($duration_unit === 'days') {
$duration_in_months = max(1, ceil($duration_value / 30)); // Convert days to months (minimum 1 month)
} else if ($duration_unit === 'weeks') {
$duration_in_months = max(1, ceil($duration_value / 4)); // Convert weeks to months (minimum 1 month)
} else if ($duration_unit === 'years') {
$duration_in_months = $duration_value * 12; // Convert years to months
}
// Calculate payment installment details
if ($payment_plan === 'monthly') {
$total_installments = $duration_in_months;
$payment_frequency = 'Monthly';
} else if ($payment_plan === 'quarterly') {
$total_installments = ceil($duration_in_months / 3);
$payment_frequency = 'Quarterly';
} else {
// Full payment
$total_installments = 1;
$payment_frequency = 'One-time';
}
// Generate a invoice/receipt number
$receipt_number = 'INV-' . date('Ymd', strtotime($payment['payment_date'])) . '-' . sprintf('%04d', $payment_id);
// Format date for the receipt
$payment_date = date('d-m-Y', strtotime($payment['payment_date']));
$payment_time = date('h:i A', strtotime($payment['payment_date']));
// Find which installment this payment is
$installment_query = "SELECT COUNT(*) as installment_number
FROM payments
WHERE user_id = ? AND course_id = ? AND payment_date <= ?
ORDER BY payment_date ASC";
$stmt = $conn->prepare($installment_query);
$stmt->bind_param("iis", $user_id, $payment['course_id'], $payment['payment_date']);
$stmt->execute();
$installment_result = $stmt->get_result();
$installment_data = $installment_result->fetch_assoc();
$installment_number = $installment_data['installment_number'] ?? 1;
// Calculate total course price
$total_payments_query = "SELECT SUM(amount) as total_paid FROM payments
WHERE user_id = ? AND course_id = ? AND status = 'completed'";
$stmt = $conn->prepare($total_payments_query);
$stmt->bind_param("ii", $user_id, $payment['course_id']);
$stmt->execute();
$total_result = $stmt->get_result();
$total_data = $total_result->fetch_assoc();
$total_paid = $total_data['total_paid'] ?? 0;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Payment Receipt - <?php echo htmlspecialchars($receipt_number); ?></title>
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- Font Awesome -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
<!-- Google Fonts -->
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
<style>
body {
font-family: 'Poppins', sans-serif;
background-color: #f8f9fa;
padding: 20px;
}
.receipt-container {
max-width: 800px;
margin: 0 auto;
background-color: white;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
border-radius: 10px;
overflow: hidden;
}
.receipt-header {
padding: 20px 30px;
border-bottom: 1px solid #eee;
}
.receipt-logo {
max-height: 80px;
}
.receipt-body {
padding: 30px;
}
.receipt-title {
font-size: 24px;
font-weight: 600;
color: #333;
margin-bottom: 20px;
}
.invoice-number {
font-size: 16px;
color: #666;
}
.receipt-section {
margin-bottom: 30px;
}
.receipt-section h5 {
border-bottom: 1px solid #eee;
padding-bottom: 10px;
margin-bottom: 15px;
color: #444;
}
.receipt-table {
width: 100%;
}
.receipt-table th {
background-color: #f8f9fa;
padding: 10px;
}
.receipt-table td {
padding: 10px;
}
.receipt-footer {
margin-top: 30px;
padding-top: 20px;
border-top: 1px solid #eee;
text-align: center;
font-size: 14px;
color: #777;
}
.payment-status {
display: inline-block;
padding: 5px 10px;
border-radius: 5px;
font-weight: 500;
font-size: 14px;
text-transform: uppercase;
}
.status-completed {
background-color: #e6f7ee;
color: #0d6832;
}
.status-pending {
background-color: #fff8e6;
color: #8a6d3b;
}
.status-refunded {
background-color: #f8ecec;
color: #a94442;
}
.payment-method {
display: inline-block;
padding: 5px 10px;
background-color: #e9ecef;
border-radius: 5px;
font-size: 14px;
color: #495057;
}
.receipt-divider {
height: 1px;
background-color: #eee;
margin: 20px 0;
}
.bottom-line {
border-top: 1px dashed #ccc;
padding-top: 20px;
margin-top: 20px;
}
.receipt-authorized {
text-align: right;
margin-top: 50px;
}
.authorized-signature {
margin-bottom: 5px;
border-bottom: 1px solid #333;
display: inline-block;
width: 200px;
text-align: center;
padding-bottom: 5px;
}
.print-actions {
margin: 20px 0;
text-align: center;
}
@media print {
body {
background-color: white;
padding: 0;
margin: 0;
}
.receipt-container {
box-shadow: none;
margin: 0;
max-width: 100%;
}
.print-actions {
display: none;
}
}
</style>
</head>
<body>
<div class="print-actions">
<button onclick="window.print()" class="btn btn-primary">
<i class="fas fa-print me-2"></i> Print Receipt
</button>
<a href="payments.php" class="btn btn-secondary">
<i class="fas fa-arrow-left me-2"></i> Back to Payments
</a>
</div>
<div class="receipt-container">
<div class="receipt-header d-flex justify-content-between align-items-center">
<div>
<img src="../<?php echo $school['site_logo']; ?>" alt="<?php echo htmlspecialchars($school['site_name']); ?>" class="receipt-logo">
</div>
<div class="text-end">
<div class="receipt-title">PAYMENT RECEIPT</div>
<div class="invoice-number"><?php echo $receipt_number; ?></div>
</div>
</div>
<div class="receipt-body">
<div class="row">
<div class="col-md-6">
<div class="receipt-section">
<h5>Institute Details</h5>
<p><strong><?php echo htmlspecialchars($school['site_name']); ?></strong></p>
<p><?php echo nl2br(htmlspecialchars($school['site_address'])); ?></p>
<p>Phone: <?php echo htmlspecialchars($school['contact_phone']); ?></p>
<p>Email: <?php echo htmlspecialchars($school['contact_email']); ?></p>
</div>
</div>
<div class="col-md-6">
<div class="receipt-section">
<h5>Student Details</h5>
<p><strong><?php echo htmlspecialchars($payment['first_name'] . ' ' . $payment['last_name']); ?></strong></p>
<p>Email: <?php echo htmlspecialchars($payment['email']); ?></p>
<?php if (!empty($payment['phone'])): ?>
<p>Phone: <?php echo htmlspecialchars($payment['phone']); ?></p>
<?php endif; ?>
</div>
</div>
</div>
<div class="receipt-section">
<h5>Payment Details</h5>
<div class="row">
<div class="col-md-6">
<p><strong>Date:</strong> <?php echo $payment_date; ?></p>
<p><strong>Time:</strong> <?php echo $payment_time; ?></p>
<p><strong>Payment Method:</strong>
<span class="payment-method"><?php echo ucfirst($payment['payment_method']); ?></span>
</p>
<p><strong>Transaction ID:</strong> <?php echo htmlspecialchars($payment['transaction_id']); ?></p>
</div>
<div class="col-md-6">
<p><strong>Payment Plan:</strong> <?php echo $payment_frequency; ?></p>
<p><strong>Installment:</strong> <?php echo $installment_number; ?> of <?php echo $total_installments; ?></p>
<p><strong>Status:</strong>
<span class="payment-status status-<?php echo $payment['status']; ?>">
<?php echo ucfirst($payment['status']); ?>
</span>
</p>
</div>
</div>
</div>
<div class="receipt-section">
<h5>Course Information</h5>
<table class="receipt-table table table-bordered">
<thead>
<tr>
<th>Course</th>
<th>Level</th>
<th>Duration</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo htmlspecialchars($payment['course_title']); ?></td>
<td><?php echo htmlspecialchars(ucfirst($payment['level'])); ?></td>
<td><?php echo htmlspecialchars($payment['duration']); ?></td>
<td class="text-end">₹<?php echo number_format($payment['amount'], 2); ?></td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="3" class="text-end"><strong>Total Amount:</strong></td>
<td class="text-end"><strong>₹<?php echo number_format($payment['amount'], 2); ?></strong></td>
</tr>
</tfoot>
</table>
</div>
<div class="receipt-divider"></div>
<div class="row">
<div class="col-md-6">
<div class="receipt-section">
<h5>Payment Summary</h5>
<p><strong>Total Course Fee:</strong> ₹<?php echo number_format($payment['amount'] * $total_installments, 2); ?></p>
<p><strong>Amount Paid To Date:</strong> ₹<?php echo number_format($total_paid, 2); ?></p>
<?php if ($total_paid < ($payment['amount'] * $total_installments)): ?>
<p><strong>Remaining Balance:</strong> ₹<?php echo number_format(($payment['amount'] * $total_installments) - $total_paid, 2); ?></p>
<?php endif; ?>
</div>
</div>
<div class="col-md-6">
<div class="receipt-authorized">
<div class="authorized-signature">
<?php echo htmlspecialchars($school['site_name']); ?>
</div>
<div>Authorized Signature</div>
</div>
</div>
</div>
<div class="bottom-line">
<p class="mb-0"><strong>Note:</strong> This is an electronically generated receipt and does not require a physical signature.</p>
</div>
</div>
<div class="receipt-footer">
<p>Thank you for your payment!</p>
<p>© <?php echo date('Y'); ?> <?php echo htmlspecialchars($school['site_name']); ?>. All rights reserved.</p>
</div>
</div>
<!-- JavaScript Includes -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>