Path : /home/vishqocm/pcib.in/admin/ajax/
File Upload :
Current File : /home/vishqocm/pcib.in/admin/ajax/download_application.php

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

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

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

$application_id = 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 application details
    $stmt = $conn->prepare("
        SELECT 
            a.id, 
            a.user_id,
            a.course_id,
            a.status,
            a.application_date,
            a.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 
            enrollment_applications a
        JOIN 
            users u ON a.user_id = u.id
        JOIN 
            courses c ON a.course_id = c.id
        WHERE 
            a.id = ?
    ");
    $stmt->execute([$application_id]);
    $application = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if (!$application) {
        header('HTTP/1.1 404 Not Found');
        echo 'Application not found';
        exit();
    }
    
    // Get student documents
    $stmt = $conn->prepare("
        SELECT 
            document_type, 
            status,
            upload_date
        FROM 
            student_documents
        WHERE 
            user_id = ?
        ORDER BY 
            document_type
    ");
    $stmt->execute([$application['user_id']]);
    $documents = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    // Get payment details
    $stmt = $conn->prepare("
        SELECT 
            payment_date,
            amount,
            payment_method,
            transaction_id,
            status
        FROM 
            payments
        WHERE 
            user_id = ? AND course_id = ?
        ORDER BY 
            payment_date DESC
    ");
    $stmt->execute([$application['user_id'], $application['course_id']]);
    $payments = $stmt->fetchAll(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 Application #' . $application_id);
    $pdf->SetSubject('Enrollment Application');
    
    // Remove header and footer
    $pdf->setPrintHeader(false);
    $pdf->setPrintFooter(false);
    
    // Set margins
    $pdf->SetMargins(15, 15, 15);
    
    // Add a page
    $pdf->AddPage();
    
    // Set font
    $pdf->SetFont('helvetica', '', 10);
    
    // Logo and Institute Name
    $pdf->Image('../../assets/img/logo.png', 15, 15, 20, 20, 'PNG');
    $pdf->SetFont('helvetica', 'B', 16);
    $pdf->SetXY(40, 15);
    $pdf->Cell(0, 10, 'Popular Computer Institute', 0, false, 'L');
    $pdf->SetFont('helvetica', '', 10);
    $pdf->SetXY(40, 25);
    $pdf->Cell(0, 5, 'Excellence in Education Since 1990', 0, false, 'L');
    
    // Application Details Header
    $pdf->SetFont('helvetica', 'B', 14);
    $pdf->SetXY(15, 40);
    $pdf->Cell(0, 10, 'ENROLLMENT APPLICATION', 0, false, 'C');
    
    // Application Status and ID
    $pdf->SetFont('helvetica', 'B', 10);
    $pdf->SetXY(15, 50);
    $pdf->Cell(90, 10, 'Application ID: ' . $application_id, 0, false, 'L');
    
    // Format status display
    $statusDisplay = ucfirst(str_replace('_', ' ', $application['status']));
    
    // Set status color
    switch($application['status']) {
        case 'submitted':
        case 'pending':
            $statusColor = array(255, 152, 0);
            break;
        case 'document_verification':
            $statusColor = array(33, 150, 243);
            break;
        case 'payment_pending':
            $statusColor = array(156, 39, 176);
            break;
        case 'completed':
            $statusColor = array(76, 175, 80);
            break;
        case 'rejected':
            $statusColor = array(244, 67, 54);
            break;
        default:
            $statusColor = array(97, 97, 97);
    }
    
    $pdf->SetTextColor($statusColor[0], $statusColor[1], $statusColor[2]);
    $pdf->SetXY(110, 50);
    $pdf->Cell(80, 10, 'Status: ' . $statusDisplay, 0, false, 'R');
    $pdf->SetTextColor(0, 0, 0);
    
    // Date and Verification Token
    $pdf->SetFont('helvetica', '', 10);
    $pdf->SetXY(15, 60);
    $pdf->Cell(90, 7, 'Application Date: ' . date('F j, Y', strtotime($application['application_date'])), 0, false, 'L');
    $pdf->SetXY(110, 60);
    $pdf->Cell(80, 7, 'Verification Token: ' . $application['verification_token'], 0, false, 'R');
    
    // Draw a horizontal line
    $pdf->Line(15, 70, 195, 70);
    
    // Student Information Section
    $pdf->SetFont('helvetica', 'B', 12);
    $pdf->SetXY(15, 75);
    $pdf->Cell(0, 8, 'Student Information', 0, false, 'L');
    
    $pdf->SetFont('helvetica', '', 10);
    $pdf->SetXY(15, 85);
    $pdf->Cell(45, 7, 'Name:', 0, false, 'L');
    $pdf->SetXY(60, 85);
    $pdf->Cell(130, 7, $application['first_name'] . ' ' . $application['last_name'], 0, false, 'L');
    
    $pdf->SetXY(15, 92);
    $pdf->Cell(45, 7, 'Email:', 0, false, 'L');
    $pdf->SetXY(60, 92);
    $pdf->Cell(130, 7, $application['email'], 0, false, 'L');
    
    $pdf->SetXY(15, 99);
    $pdf->Cell(45, 7, 'Phone:', 0, false, 'L');
    $pdf->SetXY(60, 99);
    $pdf->Cell(130, 7, $application['phone'], 0, false, 'L');
    
    $pdf->SetXY(15, 106);
    $pdf->Cell(45, 7, 'Address:', 0, false, 'L');
    $pdf->SetXY(60, 106);
    
    $address = $application['address'];
    if (!empty($application['city'])) {
        $address .= ', ' . $application['city'];
    }
    if (!empty($application['state'])) {
        $address .= ', ' . $application['state'];
    }
    if (!empty($application['zip_code'])) {
        $address .= ' - ' . $application['zip_code'];
    }
    if (!empty($application['country'])) {
        $address .= ', ' . $application['country'];
    }
    
    $pdf->MultiCell(130, 7, $address, 0, 'L');
    
    // Course Information Section
    $pdf->SetFont('helvetica', 'B', 12);
    $pdf->SetXY(15, 120);
    $pdf->Cell(0, 8, 'Course Information', 0, false, 'L');
    
    $pdf->SetFont('helvetica', '', 10);
    $pdf->SetXY(15, 130);
    $pdf->Cell(45, 7, 'Course:', 0, false, 'L');
    $pdf->SetXY(60, 130);
    $pdf->Cell(130, 7, $application['course_title'], 0, false, 'L');
    
    $pdf->SetXY(15, 137);
    $pdf->Cell(45, 7, 'Price:', 0, false, 'L');
    $pdf->SetXY(60, 137);
    $pdf->Cell(130, 7, '₹' . number_format($application['course_price'], 2), 0, false, 'L');
    
    $pdf->SetXY(15, 144);
    $pdf->Cell(45, 7, 'Duration:', 0, false, 'L');
    $pdf->SetXY(60, 144);
    $pdf->Cell(130, 7, $application['course_duration'], 0, false, 'L');
    
    // Documents Section
    $pdf->SetFont('helvetica', 'B', 12);
    $pdf->SetXY(15, 155);
    $pdf->Cell(0, 8, 'Document Status', 0, false, 'L');
    
    $pdf->SetFont('helvetica', 'B', 10);
    $pdf->SetXY(15, 165);
    $pdf->Cell(80, 7, 'Document Type', 1, false, 'C');
    $pdf->Cell(50, 7, 'Status', 1, false, 'C');
    $pdf->Cell(50, 7, 'Upload Date', 1, false, 'C');
    
    $pdf->SetFont('helvetica', '', 10);
    $y = 172;
    
    $documentTypes = [
        'id_proof' => 'ID Proof',
        'address_proof' => 'Address Proof',
        'qualification_certificate' => 'Qualification Certificate',
        'passport_photo' => 'Passport Size Photo'
    ];
    
    $documentStatuses = [];
    foreach ($documents as $document) {
        $documentStatuses[$document['document_type']] = $document;
    }
    
    foreach ($documentTypes as $type => $displayName) {
        $pdf->SetXY(15, $y);
        $pdf->Cell(80, 7, $displayName, 1, false, 'L');
        
        if (isset($documentStatuses[$type])) {
            $status = ucfirst($documentStatuses[$type]['status']);
            $uploadDate = date('M d, Y', strtotime($documentStatuses[$type]['upload_date']));
            
            // Set status color
            switch($documentStatuses[$type]['status']) {
                case 'pending':
                    $pdf->SetTextColor(255, 152, 0);
                    break;
                case 'verified':
                    $pdf->SetTextColor(76, 175, 80);
                    break;
                case 'rejected':
                    $pdf->SetTextColor(244, 67, 54);
                    break;
                default:
                    $pdf->SetTextColor(97, 97, 97);
            }
            
            $pdf->Cell(50, 7, $status, 1, false, 'C');
            $pdf->SetTextColor(0, 0, 0);
            $pdf->Cell(50, 7, $uploadDate, 1, false, 'C');
        } else {
            $pdf->SetTextColor(244, 67, 54);
            $pdf->Cell(50, 7, 'Not Uploaded', 1, false, 'C');
            $pdf->SetTextColor(0, 0, 0);
            $pdf->Cell(50, 7, 'N/A', 1, false, 'C');
        }
        
        $y += 7;
    }
    
    // Payment Information Section
    $pdf->SetFont('helvetica', 'B', 12);
    $pdf->SetXY(15, $y + 5);
    $pdf->Cell(0, 8, 'Payment Information', 0, false, 'L');
    
    if (count($payments) > 0) {
        $pdf->SetFont('helvetica', 'B', 10);
        $pdf->SetXY(15, $y + 15);
        $pdf->Cell(40, 7, 'Date', 1, false, 'C');
        $pdf->Cell(40, 7, 'Amount', 1, false, 'C');
        $pdf->Cell(40, 7, 'Method', 1, false, 'C');
        $pdf->Cell(40, 7, 'Transaction ID', 1, false, 'C');
        $pdf->Cell(20, 7, 'Status', 1, false, 'C');
        
        $pdf->SetFont('helvetica', '', 10);
        $paymentY = $y + 22;
        
        foreach ($payments as $payment) {
            $pdf->SetXY(15, $paymentY);
            $pdf->Cell(40, 7, date('M d, Y', strtotime($payment['payment_date'])), 1, false, 'C');
            $pdf->Cell(40, 7, '₹' . number_format($payment['amount'], 2), 1, false, 'C');
            $pdf->Cell(40, 7, ucfirst($payment['payment_method']), 1, false, 'C');
            $pdf->Cell(40, 7, $payment['transaction_id'], 1, false, 'C');
            
            // Set status color
            switch($payment['status']) {
                case 'completed':
                case 'success':
                    $pdf->SetTextColor(76, 175, 80);
                    break;
                case 'pending':
                    $pdf->SetTextColor(255, 152, 0);
                    break;
                case 'failed':
                    $pdf->SetTextColor(244, 67, 54);
                    break;
                default:
                    $pdf->SetTextColor(97, 97, 97);
            }
            
            $pdf->Cell(20, 7, ucfirst($payment['status']), 1, false, 'C');
            $pdf->SetTextColor(0, 0, 0);
            
            $paymentY += 7;
        }
    } else {
        $pdf->SetFont('helvetica', '', 10);
        $pdf->SetXY(15, $y + 15);
        $pdf->Cell(0, 7, 'No payment records found.', 0, false, 'L');
    }
    
    // Footer
    $pdf->SetFont('helvetica', 'I', 8);
    $pdf->SetXY(15, 270);
    $pdf->Cell(0, 5, 'This document is computer-generated and does not require a signature.', 0, false, 'C');
    $pdf->SetXY(15, 275);
    $pdf->Cell(0, 5, 'Popular Computer Institute - ' . date('F j, Y'), 0, false, 'C');
    
    // Output PDF
    $fileName = 'Enrollment_Application_' . $application_id . '.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();
}
?>