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

ob_start();

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

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

// Copiar exactamente el codigo de generador_final.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";

class PDF extends FPDF
{
function Header()
{

$select_obs_dir_prm="		select observacion.tipo,round(AVG(observacion_dirgrupo.nota )),observacion.descripcion
					from observacion_dirgrupo,observacion 
					where observacion_dirgrupo.estado='A' 
					and observacion_dirgrupo.id_observacion=observacion.Id 
					and observacion_dirgrupo.id_planilla in (select id from planilla where anno='".date("Y")."')
					and observacion_dirgrupo.id_curso=".$_POST['cur2']." 
					and observacion_dirgrupo.id__estudiante=".$_POST['estudiante2']." 
					group by observacion.tipo";
					$res_select_obs_dir_prm=mysql_query($select_obs_dir_prm);
					$filas3 = mysql_num_rows($res_select_obs_dir_prm);

$today_h = date('d-m-Y');	

$sel_estudiante="select nombre,usuario,clave from estudiante where estudiante_id=".$_POST['estudiante2']." ";
$res_sel_estudiante=mysql_query($sel_estudiante);
$estudiante=mysql_fetch_array($res_sel_estudiante);

$sel_curso="select codigo,nombre from curso where curso_id=".$_POST['cur2']." ";
$res_sel_curso=mysql_query($sel_curso);
$curso=mysql_fetch_array($res_sel_curso);

$sel_planilla="select anno,periodo from planilla where id=".$_POST['per2']." ";
$res_sel_planilla=mysql_query($sel_planilla);
$planilla=mysql_fetch_array($res_sel_planilla);

    if($this->PageNo()==1){   
	$this->Ln(0.2);
	$this->SetFillColor(212,212,212);
	$this->SetFont('Arial','B',15); 
    $this->Cell(9,1,' COLEGIO DE LA ESPERANZA',0,0,'L',TRUE); 
    $this->Cell(0.1,1,' ',0,0,'C');
    $this->Cell(11.4,1,' INFORME FINAL DE NOTAS',0,1,'C',TRUE);

	$this->Ln(0.1);
	$this->Cell(9.1,1,' ',0,0,'C');
	$this->SetFont('Arial','b',10); 
    $this->Cell(11.4,0.5,'NOMBRE DEL ALUMNO',0,1,'C',TRUE);

	$this->Ln(0.1);
	$this->Cell(9.1,1,' ',0,0,'C');
	$this->SetFont('Arial','',10); 
    $this->Cell(12.4,2.5,''.utf8_decode($estudiante[0]),0,1,'C',false);
    $imagen_nueva_v = '../img/foto/'.$estudiante[3];
    $this->Ln(0.1);
    
	$this->Ln(0.1);
	$this->Cell(9.1,1,' ',0,0,'C');
	$this->SetFont('Arial','b',10); 
    $this->Cell(2,0.5,'CODIGO',0,0,'C',true);
    $this->Cell(0.1,1,' ',0,0,'C');        
    $this->Cell(2,0.5,'CURSO',0,0,'C',true);
    $this->Cell(0.1,1,' ',0,0,'C');        
    $this->Cell(2,0.5,'FECHA',0,0,'C',true);
    $this->Cell(0.1,1,' ',0,0,'C');        
    $this->Cell(2,0.5,'PERIODO',0,0,'C',true);
    $this->Cell(0.1,1,' ',0,0,'C');        
    $this->Cell(3,0.5,utf8_decode('AÑO ESCOLAR'),0,1,'C',true);        

	$this->Cell(9.1,1,' ',0,0,'C');
	$this->SetFont('Arial','',10); 
    $this->Cell(2,0.5,''.$_POST['estudiante2'],0,0,'C',false);
    $this->Cell(0.1,1,' ',0,0,'C');        
    $this->Cell(2,0.5,''.$curso[0].' - '.$curso[1],0,0,'C',false);
    $this->Cell(0.1,1,' ',0,0,'C');        
    $this->Cell(2,0.5,''.$today_h,0,0,'C',false);
    $this->Cell(0.1,1,' ',0,0,'C');        
    $this->Cell(2,0.5,''.$planilla[1],0,0,'C',false);
    $this->Cell(0.1,1,' ',0,0,'C');        
    $this->Cell(3,0.5,''.$planilla[0],0,1,'C',false);        

	$this->Ln(1);
	$this->SetFillColor(255,255,255);
	$this->Cell(9.1,0.5,' ',0,0,'C');
	$this->SetFont('Arial','b',10); 
    $this->Cell(3.8,0.3,' ',0,0,'C',true);
    $this->Cell(3.8,0.3,' ',0,0,'C',true);
    $this->Cell(3.8,0.3,'_______________________',0,1,'C',true);

	$this->Cell(9.1,0.5,' ',0,0,'C');
	$this->SetFont('Arial','b',8); 
    $this->Cell(3.8,0.3,'RECTOR',0,0,'C',true);
    $this->Cell(3.8,0.3,'SECRETARIA ACADEMICA',0,0,'C',true);
    $this->Cell(3.8,0.3,'DIRECTOR DE GRUPO',0,1,'C',true);
    
    $this->Image('../img/escudo.jpg',0.5,1.8,3);
    $this->Ln(0.1);

    }
}
}

$pdf=new PDF('L','cm','legal');
$pdf->AddPage();

$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){
$color=242;$c=0;
    while($rs_sel_pen=mysql_fetch_array($res_seleccion_pen)){
        $c++;
        
        $select_curso_nota=" select asignatura.nombre,planilla_consulta.pensum,
                        IFNULL(planilla_consulta.D,0),
                        IFNULL(planilla_consulta.I,0),
                        IFNULL(planilla_consulta.F,0),
                        IFNULL(planilla_consulta.IP,0),
                        IFNULL(planilla_consulta.FP,0),
                        IFNULL(planilla_consulta.C1,0),
                        IFNULL(planilla_consulta.C2,0),
                        IFNULL(planilla_consulta.CG,0),pensum.intensidad,
                        IFNULL(planilla_consulta.niv,0)
                        from estudiante,pensum,curso,planilla_consulta,asignatura 
                        where pensum.class_id=".$_POST['cur2']." 
                        and estudiante.estudiante_id=".$_POST['estudiante2']." 
                        and planilla_consulta.periodo=".$_POST['per2']." 
                        and pensum.class_id=curso.curso_id 
                        and estudiante.curso_id=pensum.class_id 
                        and pensum.estado='A' 
                        and pensum.subject_id=".$rs_sel_pen[0]."
                        and planilla_consulta.pensum=pensum.subject_id 
                        and planilla_consulta.estudiante=estudiante.estudiante_id 
                        and asignatura.subject_id=pensum.asignatura_id
                        order by pensum.nombre";

        $res_select_curso_nota=mysql_query($select_curso_nota);
        $num_filas_not = mysql_num_rows($res_select_curso_nota);
        
        if($num_filas_not>0){            
            while($rs_curso_nota=mysql_fetch_array($res_select_curso_nota)){
                $pdf->SetFont('Arial','',9);
                $pdf->Cell(0,5,$rs_curso_nota[0],0,1);
            }
        }
    }
}

$output = $pdf->Output('', 'S');
ob_end_clean();

echo "PDF size: " . strlen($output) . " bytes\n";
header('Content-Type: application/pdf');
echo $output;
?>