Path : /home/vishqocm/pcib.in/student/
File Upload :
Current File : /home/vishqocm/pcib.in/student/download_enrollment.php

<?php
session_start();
require_once '../config/database.php';
require_once '../vendor/autoload.php';

// Check if student is logged in
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'student') {
    header('HTTP/1.1 403 Forbidden');
    echo 'Access denied';
    exit();
}

$userId = $_SESSION['user_id'];

// Check if enrollment ID is provided
if (!isset($_GET['id']) || empty($_GET['id'])) {
    header('HTTP/1.1 400 Bad Request');
    echo 'Enrollment ID is required';
    exit();
}

$enrollmentId = intval($_GET['id']);

try {
    // Create connection
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // Get enrollment details
    $stmt = $conn->prepare("
        SELECT 
            e.id, 
            e.user_id,
            e.course_id,
            e.status,
            e.enrollment_date,
            e.verification_token,
            c.title as course_title,
            c.price as course_price,
            c.duration as course_duration,
            u.first_name,
            u.last_name,
            u.email,
            u.phone,
            u.address,
            u.city,
            u.state,
            u.zip_code,
            u.country
        FROM 
            enrollments e
        JOIN 
            users u ON e.user_id = u.id
        JOIN 
            courses c ON e.course_id = c.id
        WHERE 
            e.id = ? AND e.user_id = ?
    ");
    $stmt->execute([$enrollmentId, $userId]);
    $enrollment = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if (!$enrollment) {
        header('HTTP/1.1 404 Not Found');
        echo 'Enrollment not found or you do not have access to this enrollment';
        exit();
    }
    
    // Get payment details
    $stmt = $conn->prepare("
        SELECT 
            id,
            payment_date,
            amount,
            payment_method,
            transaction_id,
            status,
            payment_details
        FROM 
            payments
        WHERE 
            user_id = ? AND course_id = ? AND status = 'completed'
        ORDER BY 
            payment_date DESC
        LIMIT 1
    ");
    $stmt->execute([$userId, $enrollment['course_id']]);
    $payment = $stmt->fetch(PDO::FETCH_ASSOC);
    
    // Create PDF using TCPDF
    $pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);
    
    // Set document information
    $pdf->SetCreator('Popular Computer Institute');
    $pdf->SetAuthor('Popular Computer Institute');
    $pdf->SetTitle('Enrollment Receipt');
    $pdf->SetSubject('Enrollment Receipt');
    
    // Remove header and footer
    $pdf->setPrintHeader(false);
    $pdf->setPrintFooter(false);
    
    // Set margins
    $pdf->SetMargins(15, 15, 15);
    
    // Add a page
    $pdf->AddPage();
    
    // Logo and Institute Name
    $pdf->Image('../assets/img/logo.png', 15, 15, 25, 25, 'PNG');
    $pdf->SetFont('helvetica', 'B', 18);
    $pdf->SetXY(45, 15);
    $pdf->Cell(0, 10, 'Popular Computer Institute', 0, false, 'L');
    $pdf->SetFont('helvetica', '', 10);
    $pdf->SetXY(45, 25);
    $pdf->Cell(0, 5, 'Excellence in Education Since 1990', 0, false, 'L');
    $pdf->SetXY(45, 30);
    $pdf->Cell(0, 5, 'www.popularcomputerinstitute.com | [email protected]', 0, false, 'L');
    
    // Receipt Header
    $pdf->SetFont('helvetica', 'B', 16);
    $pdf->SetXY(15, 45);
    $pdf->Cell(0, 10, 'ENROLLMENT RECEIPT', 0, false, 'C');
    
    // Draw a horizontal decorative line
    $pdf->SetDrawColor(0, 102, 204);
    $pdf->SetLineWidth(0.5);
    $pdf->Line(15, 55, 195, 55);
    $pdf->SetDrawColor(0, 0, 0);
    $pdf->SetLineWidth(0.2);
    
    // Enrollment Status and ID
    $pdf->SetFont('helvetica', 'B', 12);
    $pdf->SetXY(15, 60);
    $pdf->Cell(90, 8, 'Enrollment ID: ' . $enrollmentId, 0, false, 'L');
    
    // Format status display and set status color
    $statusDisplay = ucfirst(str_replace('_', ' ', $enrollment['status']));
    switch($enrollment['status']) {
        case 'active':
            $pdf->SetTextColor(76, 175, 80);
            break;
        case 'completed':
            $pdf->SetTextColor(33, 150, 243);
            break;
        case 'suspended':
            $pdf->SetTextColor(255, 152, 0);
            break;
        case 'cancelled':
            $pdf->SetTextColor(244, 67, 54);
            break;
        default:
            $pdf->SetTextColor(97, 97, 97);
    }
    
    $pdf->SetXY(130, 60);
    $pdf->Cell(65, 8, 'Status: ' . $statusDisplay, 0, false, 'R');
    $pdf->SetTextColor(0, 0, 0);
    
    // Date and Verification Token
    $pdf->SetFont('helvetica', '', 10);
    $pdf->SetXY(15, 68);
    $pdf->Cell(90, 7, 'Enrollment Date: ' . date('F j, Y', strtotime($enrollment['enrollment_date'])), 0, false, 'L');
    $pdf->SetXY(130, 68);
    $pdf->Cell(65, 7, 'Receipt Generated: ' . date('F j, Y'), 0, false, 'R');
    
    // Draw a light gray box for student and course info
    $pdf->SetFillColor(245, 245, 245);
    $pdf->Rect(15, 80, 180, 50, 'F');
    
    // Student Information Section
    $pdf->SetFont('helvetica', 'B', 12);
    $pdf->SetXY(20, 85);
    $pdf->Cell(80, 8, 'Student Information', 0, false, 'L');
    
    $pdf->SetFont('helvetica', 'B', 10);
    $pdf->SetXY(20, 95);
    $pdf->Cell(25, 7, 'Name:', 0, false, 'L');
    $pdf->SetFont('helvetica', '', 10);
    $pdf->SetXY(45, 95);
    $pdf->Cell(65, 7, $enrollment['first_name'] . ' ' . $enrollment['last_name'], 0, false, 'L');
    
    $pdf->SetFont('helvetica', 'B', 10);
    $pdf->SetXY(20, 102);
    $pdf->Cell(25, 7, 'Email:', 0, false, 'L');
    $pdf->SetFont('helvetica', '', 10);
    $pdf->SetXY(45, 102);
    $pdf->Cell(65, 7, $enrollment['email'], 0, false, 'L');
    
    $pdf->SetFont('helvetica', 'B', 10);
    $pdf->SetXY(20, 109);
    $pdf->Cell(25, 7, 'Phone:', 0, false, 'L');
    $pdf->SetFont('helvetica', '', 10);
    $pdf->SetXY(45, 109);
    $pdf->Cell(65, 7, $enrollment['phone'], 0, false, 'L');
    
    // Course Information Section
    $pdf->SetFont('helvetica', 'B', 12);
    $pdf->SetXY(115, 85);
    $pdf->Cell(75, 8, 'Course Information', 0, false, 'L');
    
    $pdf->SetFont('helvetica', 'B', 10);
    $pdf->SetXY(115, 95);
    $pdf->Cell(25, 7, 'Course:', 0, false, 'L');
    $pdf->SetFont('helvetica', '', 10);
    $pdf->SetXY(140, 95);
    $pdf->Cell(50, 7, $enrollment['course_title'], 0, false, 'L');
    
    $pdf->SetFont('helvetica', 'B', 10);
    $pdf->SetXY(115, 102);
    $pdf->Cell(25, 7, 'Duration:', 0, false, 'L');
    $pdf->SetFont('helvetica', '', 10);
    $pdf->SetXY(140, 102);
    $pdf->Cell(50, 7, $enrollment['course_duration'], 0, false, 'L');
    
    $pdf->SetFont('helvetica', 'B', 10);
    $pdf->SetXY(115, 109);
    $pdf->Cell(25, 7, 'Price:', 0, false, 'L');
    $pdf->SetFont('helvetica', '', 10);
    $pdf->SetXY(140, 109);
    $pdf->Cell(50, 7, '₹' . number_format($enrollment['course_price'], 2), 0, false, 'L');
    
    // Verification Token in a box
    $pdf->SetFont('helvetica', 'B', 10);
    $pdf->SetXY(20, 116);
    $pdf->Cell(170, 7, 'Verification Token: ' . $enrollment['verification_token'], 0, false, 'C');
    
    // Payment Information Section
    $pdf->SetFont('helvetica', 'B', 14);
    $pdf->SetXY(15, 140);
    $pdf->Cell(0, 8, 'Payment Details', 0, false, 'L');
    
    // Payment information in table format
    $pdf->SetFont('helvetica', 'B', 10);
    $pdf->SetXY(15, 150);
    $pdf->Cell(35, 8, 'Receipt No.', 1, false, 'C', true);
    $pdf->Cell(35, 8, 'Payment Date', 1, false, 'C', true);
    $pdf->Cell(35, 8, 'Amount', 1, false, 'C', true);
    $pdf->Cell(40, 8, 'Payment Method', 1, false, 'C', true);
    $pdf->Cell(35, 8, 'Transaction ID', 1, false, 'C', true);
    
    $pdf->SetFont('helvetica', '', 10);
    $pdf->SetXY(15, 158);
    
    if ($payment) {
        $pdf->Cell(35, 8, 'PCI-' . str_pad($payment['id'], 6, '0', STR_PAD_LEFT), 1, false, 'C');
        $pdf->Cell(35, 8, date('d/m/Y', strtotime($payment['payment_date'])), 1, false, 'C');
        $pdf->Cell(35, 8, '₹' . number_format($payment['amount'], 2), 1, false, 'C');
        $pdf->Cell(40, 8, ucfirst($payment['payment_method']), 1, false, 'C');
        $pdf->Cell(35, 8, $payment['transaction_id'], 1, false, 'C');
        
        // Additional payment details if available
        if (!empty($payment['payment_details'])) {
            $paymentDetails = json_decode($payment['payment_details'], true);
            if ($paymentDetails) {
                $pdf->SetXY(15, 170);
                $pdf->SetFont('helvetica', 'B', 10);
                $pdf->Cell(0, 8, 'Additional Payment Information:', 0, false, 'L');
                
                $y = 178;
                foreach ($paymentDetails as $key => $value) {
                    if (!empty($value) && !is_array($value)) {
                        $pdf->SetXY(15, $y);
                        $pdf->SetFont('helvetica', 'B', 9);
                        $pdf->Cell(50, 7, ucwords(str_replace('_', ' ', $key)) . ':', 0, false, 'L');
                        $pdf->SetFont('helvetica', '', 9);
                        $pdf->Cell(130, 7, is_string($value) ? $value : json_encode($value), 0, false, 'L');
                        $y += 6;
                    }
                }
            }
        }
    } else {
        $pdf->Cell(180, 8, 'No payment records found.', 1, false, 'C');
    }
    
    // Terms and Conditions
    $pdf->SetXY(15, 190);
    $pdf->SetFont('helvetica', 'B', 12);
    $pdf->Cell(0, 8, 'Terms and Conditions', 0, false, 'L');
    
    $pdf->SetXY(15, 198);
    $pdf->SetFont('helvetica', '', 9);
    $terms = "1. This receipt confirms your enrollment in the course mentioned above.\n";
    $terms .= "2. The enrollment is valid for the duration mentioned above from the date of enrollment.\n";
    $terms .= "3. The enrollment is non-transferable and non-refundable.\n";
    $terms .= "4. The institute reserves the right to cancel or reschedule the course.\n";
    $terms .= "5. Students are expected to follow the institute's code of conduct.\n";
    $terms .= "6. For verification purposes, please quote your Verification Token when contacting the institute.";
    
    $pdf->MultiCell(180, 5, $terms, 0, 'L');
    
    // Contact Information
    $pdf->SetXY(15, 230);
    $pdf->SetFont('helvetica', 'B', 12);
    $pdf->Cell(0, 8, 'Contact Information', 0, false, 'L');
    
    $pdf->SetXY(15, 238);
    $pdf->SetFont('helvetica', '', 9);
    $contact = "Popular Computer Institute\n";
    $contact .= "123 Education Street, Learning Plaza\n";
    $contact .= "Tech City - 123456\n";
    $contact .= "Phone: +91 9876543210\n";
    $contact .= "Email: [email protected]\n";
    $contact .= "Website: www.popularcomputerinstitute.com";
    
    $pdf->MultiCell(85, 5, $contact, 0, 'L');
    
    // QR Code with verification link
    $verificationUrl = 'https://www.popularcomputerinstitute.com/verify?token=' . $enrollment['verification_token'];
    $style = array(
        'border' => 0,
        'padding' => 0,
        'fgcolor' => array(0, 0, 0),
        'bgcolor' => false
    );
    
    $pdf->write2DBarcode($verificationUrl, 'QRCODE,L', 150, 235, 40, 40, $style);
    $pdf->SetXY(130, 275);
    $pdf->SetFont('helvetica', '', 8);
    $pdf->Cell(60, 5, 'Scan to verify enrollment', 0, false, 'C');
    
    // Footer
    $pdf->SetDrawColor(0, 102, 204);
    $pdf->SetLineWidth(0.5);
    $pdf->Line(15, 285, 195, 285);
    $pdf->SetDrawColor(0, 0, 0);
    
    $pdf->SetFont('helvetica', 'I', 8);
    $pdf->SetXY(15, 286);
    $pdf->Cell(0, 5, 'This is a computer-generated document and does not require a signature.', 0, false, 'C');
    
    // Output PDF
    $fileName = 'Enrollment_Receipt_' . $enrollmentId . '.pdf';
    $pdf->Output($fileName, 'D');
    exit();
    
} catch (PDOException $e) {
    header('HTTP/1.1 500 Internal Server Error');
    echo 'Database error: ' . $e->getMessage();
    exit();
} catch (Exception $e) {
    header('HTTP/1.1 500 Internal Server Error');
    echo 'Error: ' . $e->getMessage();
    exit();
}
?>