<?php
// Start session
session_start();
// Check if user has admin privileges
if (!isset($_SESSION['role']) || ($_SESSION['role'] !== 'admin' && $_SESSION['role'] !== 'director')) {
echo json_encode(['success' => false, 'message' => 'Unauthorized access']);
exit;
}
// Include database configuration
require_once '../../admin/database/db_config.php';
// Check if enrollment ID is provided
if (!isset($_GET['enrollment_id']) || empty($_GET['enrollment_id'])) {
echo json_encode(['success' => false, 'message' => 'Invalid enrollment ID']);
exit;
}
$enrollment_id = intval($_GET['enrollment_id']);
try {
// First get the user_id and course_id from the enrollment
$query = "SELECT user_id, course_id FROM enrollments WHERE id = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $enrollment_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 0) {
throw new Exception('Enrollment not found');
}
$enrollment = $result->fetch_assoc();
$user_id = $enrollment['user_id'];
$course_id = $enrollment['course_id'];
// Get all payments for this user and course
$payments_query = "SELECT p.*,
c.title as course_title,
u.first_name, u.last_name, u.email
FROM payments p
LEFT JOIN courses c ON p.course_id = c.id
LEFT JOIN users u ON p.user_id = u.id
WHERE p.user_id = ? AND p.course_id = ?
ORDER BY p.payment_date DESC";
$stmt = $conn->prepare($payments_query);
$stmt->bind_param("ii", $user_id, $course_id);
$stmt->execute();
$payments_result = $stmt->get_result();
$payments = [];
$total_paid = 0;
while ($payment = $payments_result->fetch_assoc()) {
// Format the payment data
$payment['payment_date_formatted'] = date('M d, Y g:i A', strtotime($payment['payment_date']));
$payment['amount_formatted'] = number_format($payment['amount'], 2);
// Parse payment details if exists
if (!empty($payment['payment_details'])) {
$payment['details'] = json_decode($payment['payment_details'], true);
} else {
$payment['details'] = null;
}
// Define status badges
$payment_statuses = [
'completed' => 'success',
'pending' => 'warning',
'failed' => 'danger',
'refunded' => 'info'
];
$payment['status_badge'] = isset($payment_statuses[$payment['status']]) ?
$payment_statuses[$payment['status']] :
'secondary';
$payments[] = $payment;
// Add to total if payment is completed
if ($payment['status'] === 'completed') {
$total_paid += $payment['amount'];
}
}
// Get course price for comparison
$course_query = "SELECT price, discount_price FROM courses WHERE id = ?";
$stmt = $conn->prepare($course_query);
$stmt->bind_param("i", $course_id);
$stmt->execute();
$course = $stmt->get_result()->fetch_assoc();
$course_price = !empty($course['discount_price']) ? $course['discount_price'] : $course['price'];
$payment_progress = $course_price > 0 ? min(100, round(($total_paid / $course_price) * 100)) : 0;
// Get payment summary
$stats = [
'total_payments' => count($payments),
'total_paid' => $total_paid,
'total_paid_formatted' => number_format($total_paid, 2),
'course_price' => $course_price,
'course_price_formatted' => number_format($course_price, 2),
'payment_progress' => $payment_progress,
'payment_status' => $total_paid >= $course_price ? 'fully_paid' : ($total_paid > 0 ? 'partially_paid' : 'unpaid')
];
echo json_encode([
'success' => true,
'payments' => $payments,
'stats' => $stats
]);
} catch (Exception $e) {
echo json_encode([
'success' => false,
'message' => $e->getMessage()
]);
}
?>