<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Simular POST
$_POST['per2'] = '9';
$_POST['cur2'] = '12';
$_POST['estudiante2'] = '160008';

include '../conector.php';
include 'fpdf.php';

$seleccion_pen=" select distinct pensum.subject_id
            from estudiante,pensum,curso,planilla_detalle,asignatura 
                         where pensum.class_id=".$_POST['cur2']." 
                        and estudiante.estudiante_id=".$_POST['estudiante2']." 
                        and planilla_detalle.planilla=".$_POST['per2']." 
                        and pensum.class_id=curso.curso_id 
                        and estudiante.curso_id=pensum.class_id 
                        and pensum.estado='A' 
                        and planilla_detalle.codigo_pensum=pensum.subject_id 
                        and planilla_detalle.codigo_est=estudiante.estudiante_id 
                        and asignatura.subject_id=pensum.asignatura_id
                        order by pensum.nombre";

$res_seleccion_pen = mysql_query($seleccion_pen);
$num_filas_pen = mysql_num_rows($res_seleccion_pen);
echo "Materias: $num_filas_pen\n";

if($num_filas_pen > 0){
    $rs_sel_pen = mysql_fetch_array($res_seleccion_pen);
    
    // Nueva query usando planilla_detalle
    $select_curso_nota=" select distinct a.nombre as materia, 
                                pd.nota, pd.concepto, pd.comp_1, pd.comp_2, pd.comp_3,
                                pensum.intensidad
                    from pensum 
                    LEFT JOIN planilla_detalle pd ON pd.codigo_pensum = pensum.subject_id AND pd.planilla = ".$_POST['per2']." AND pd.codigo_est = ".$_POST['estudiante2']."
                    JOIN estudiante e ON e.estudiante_id = ".$_POST['estudiante2']."
                    JOIN curso c ON e.curso_id = c.curso_id
                    JOIN asignatura a ON a.subject_id = pensum.asignatura_id
                    where pensum.class_id=".$_POST['cur2']." 
                    and pensum.estado='A' 
                    and pensum.subject_id=".$rs_sel_pen[0]."
                    order by a.nombre";
    
    $res_select_curso_nota = mysql_query($select_curso_nota);
    $num_filas_not = mysql_num_rows($res_select_curso_nota);
    echo "Notas: $num_filas_not\n";
    
    if($num_filas_not > 0){
        while($rs = mysql_fetch_array($res_select_curso_nota)){
            echo "Materia: " . $rs[0] . " | Nota: " . $rs[1] . " | Concepto: " . $rs[2] . "\n";
        }
    }
}

echo "\nTest completo OK\n";
?>