Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/attendance/api/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/attendance/api/special_reports_summary.php

<?php
require_once '../config/config.php';

header('Content-Type: application/json');

// Verify access code
$input = json_decode(file_get_contents('php://input'), true);
$access_code = $input['access_code'] ?? '';

if (empty($access_code)) {
    echo json_encode(['success' => false, 'message' => 'Access code required']);
    exit;
}

$db = new Database();
$conn = $db->getConnection();

// Verify special code
$query = "SELECT * FROM special_codes WHERE code = ? AND is_active = 1 
          AND (expires_at IS NULL OR expires_at > NOW())";
$stmt = $conn->prepare($query);
$stmt->execute([$access_code]);
$code_info = $stmt->fetch();

if (!$code_info) {
    echo json_encode(['success' => false, 'message' => 'Invalid access code']);
    exit;
}

try {
    // Build WHERE clause based on filters
    $where_conditions = [];
    $params = [];
    
    if (!empty($input['program'])) {
        $where_conditions[] = "ar.program_id = ?";
        $params[] = $input['program'];
    }
    
    if (!empty($input['district'])) {
        $where_conditions[] = "ar.district_id = ?";
        $params[] = $input['district'];
    }
    
    if (!empty($input['officer_type'])) {
        $where_conditions[] = "ar.officer_type = ?";
        $params[] = $input['officer_type'];
    }
    
    if (!empty($input['from_date'])) {
        $where_conditions[] = "DATE(ar.submitted_at) >= ?";
        $params[] = $input['from_date'];
    }
    
    if (!empty($input['to_date'])) {
        $where_conditions[] = "DATE(ar.submitted_at) <= ?";
        $params[] = $input['to_date'];
    }
    
    $where_clause = !empty($where_conditions) ? 'WHERE ' . implode(' AND ', $where_conditions) : '';
    
    // Get summary statistics
    $stats_query = "SELECT 
                        COUNT(DISTINCT ar.id) as total_attendees,
                        COUNT(DISTINCT ar.program_id) as total_programs,
                        COUNT(DISTINCT ar.district_id) as total_districts,
                        COUNT(DISTINCT CASE WHEN ar.officer_type IS NOT NULL THEN ar.id END) as total_officers
                    FROM attendance_records ar 
                    JOIN programs p ON ar.program_id = p.id 
                    LEFT JOIN locations ld ON ar.district_id = ld.id 
                    $where_clause";
    
    $stats_stmt = $conn->prepare($stats_query);
    $stats_stmt->execute($params);
    $stats = $stats_stmt->fetch();
    
    // Get detailed breakdown data with individual names
    $data_query = "SELECT 
                       p.name as program_name,
                       ar.full_name,
                       ar.email,
                       ar.telephone,
                       ar.officer_type,
                       ld.name as district_name,
                       la.name as assembly_name,
                       ar.submitted_at
                   FROM attendance_records ar 
                   JOIN programs p ON ar.program_id = p.id 
                   LEFT JOIN locations ld ON ar.district_id = ld.id 
                   LEFT JOIN locations la ON ar.assembly_id = la.id 
                   $where_clause
                   ORDER BY p.name, ar.full_name";
    
    $data_stmt = $conn->prepare($data_query);
    $data_stmt->execute($params);
    $data = $data_stmt->fetchAll();
    
    echo json_encode([
        'success' => true,
        'stats' => $stats,
        'data' => $data
    ]);
    
} catch (Exception $e) {
    echo json_encode(['success' => false, 'message' => 'Database error: ' . $e->getMessage()]);
}
?>

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists