<?php
//ini_set("error_reporting",E_ALL);
require('../fpdf.php');

class PDF extends FPDF
{
var $B;
var $I;
var $U;
var $HREF;


function WriteHTML($html)
{
        //HTML parser
        $html=str_replace("\n",' ',$html);
        $a=preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE);
        foreach($a as $i=>$e)
        {
                if($i%2==0)
                {
                        //Text
                        if($this->HREF)
                                $this->PutLink($this->HREF,$e);
                        else
                                $this->Write(5,$e);
//                              $this->Cell(180,5,$e,1,2,'J');
                }
                else
                {
                        //Tag
                        if($e{0}=='/')
                                $this->CloseTag(strtoupper(substr($e,1)));
                        else
                        {
                                //Extract attributes
                                $a2=explode(' ',$e);
                                $tag=strtoupper(array_shift($a2));
                                $attr=array();
                                foreach($a2 as $v)
                                        if(ereg('^([^=]*)=["\']?([^"\']*)["\']?$',$v,$a3))
                                                $attr[strtoupper($a3[1])]=$a3[2];
                                $this->OpenTag($tag,$attr);
                        }
                }
        }
}

function OpenTag($tag,$attr)
{
        //Opening tag
        if($tag=='B' or $tag=='I' or $tag=='U')
                $this->SetStyle($tag,true);
        if($tag=='A')
                $this->HREF=$attr['HREF'];
        if($tag=='BR')
                $this->Ln(5);
}

function CloseTag($tag)
{
        //Closing tag
        if($tag=='B' or $tag=='I' or $tag=='U')
                $this->SetStyle($tag,false);
        if($tag=='A')
                $this->HREF='';
}

function SetStyle($tag,$enable)
{
        //Modify style and select corresponding font
        $this->$tag+=($enable ? 1 : -1);
        $style='';
        foreach(array('B','I','U') as $s)
                if($this->$s>0)
                        $style.=$s;
        $this->SetFont('',$style);
}

function PutLink($URL,$txt)
{
        //Put a hyperlink
        $this->SetTextColor(0,0,255);
        $this->SetStyle('U',true);
        $this->Write(5,$txt,$URL);
        $this->SetStyle('U',false);
        $this->SetTextColor(0);
}
//Page header
function Header2($texto)
{
        //Logo
        $this->Image('../logo_colegio.png',10,8,20);
        $this->Image('../logo_colegio2.png',180,8,18);
        //Arial bold 15
        $this->SetFont('Arial','B',15);
        //Move to the right
        $this->Cell(80);
        //Title
        $this->Cell(30,10,$texto,0,0,'C');
        //Line break
        $this->Ln(20);
}

//Page footer
function Footer()
{
        //Position at 1.5 cm from bottom
        $this->SetY(-25);
        //Arial italic 8
        $this->SetFont('Arial','I',8);
        $this->Cell(90,5,date("d-m-Y"),0,0,"L");
        $this->Cell(60,5,date("H:i:s"),0,0,"R");
        //Page number
//        $this->Cell(0,10,'Pagina '.$this->PageNo().'/{nb}',0,0,'C');
}

function Encabezado($institucion='', $titulo_reporte='')
{
        //Logo
        $this->Image('../logo_colegio.png',30,10,30);
//        $this->Image('../logo_colegio2.png',180,8,18);
        $this->SetFont('Arial','B',15);
                $this->Ln(-20);
        $this->Cell(0,7,$institucion,0,1,'C');
        $this->SetFont('Arial','B',13);
        $this->Cell(0,5,$titulo_reporte,0,1,'C');
//        $this->Ln(-11);
        $this->SetFont('Arial','',10);
//        $this->Cell(0,5,($this->Fecha())."              ",0,1,'R');
//        $this->Cell(0,5,($this->Hora())."              ",0,1,'R');
        //Line break
        $this->Ln(12);
}

}

include("../fnc.php");

$pdf=new PDF('P','mm','Letter');
$pdf->SetMargins(30,30,10);
$id_grado=dec($idg);

$q=mysql_query("select *
                from alumnos
                where grado_estudio = '$id_grado'
                order by apellido, nombre");

$fechacorte=fechabd($fechacorte);

while($ialumno=mysql_fetch_array($q)){
$pdf->AddPage();
$pdf->Encabezado($nombre_colegio,"INFORME DE CALIFICACIONES");
$pdf->SetFont('Arial','B',13);
$pdf->cell(0,5,gunidad(enc($txtunidad)),0,0,"C");
$pdf->Ln();
$pdf->SetFont('Arial','',10);

 $sql="select sum(saldo) as tsaldo
       from cp_cargos
       where fecha_cargo <= '$fechacorte 23:59:00'
       and carnet='{$ialumno[carnet]}'
       having tsaldo";

 $qsaldo=mysql_query($sql);

 $msg="";
 if(mysql_num_rows($qsaldo)==0){
   $msg="SOLVENTE";
 }else{
   $isaldo=mysql_fetch_array($qsaldo);
   if($isaldo[tsaldo]>0){
     $msg="INSOLVENTE";
   }else{
     $msg="SOLVENTE";
   }
 }

// $pdf->cell(180,5,"Fecha de Emisión: ".fecha($fecha),0,1,"R");
 $pdf->Ln(10);
 $pdf->cell(25,5,"Carnet: ",0,0);
 $pdf->cell(75,5,$ialumno[carnet],0,1);
 $pdf->SetStyle("B",true);
 $pdf->Setfont("Arial","B","14");
 $pdf->cell(25,5,"Nombre: ",0,0);
 $pdf->cell(75,5,$ialumno[apellido].", ".$ialumno[nombre],0,1);
 $pdf->Setfont("Arial","","10");
 $pdf->SetStyle("B",false);
 $pdf->cell(25,5,"Grado: ",0,0);
 $pdf->cell(75,5,ggrado(enc($ialumno[grado_estudio]))." ".gnivel(enc($ialumno[nivel_estudio])),0,1);
 $pdf->Ln(7);

 $pdf->cell(100,5,"",0,0,"L");
 $pdf->cell(17,5,"ZONA",0,0,"C");
 $pdf->cell(17,5,"EXAMEN",0,0,"C");
 $pdf->cell(17,5,"NOTA",0,1,"C");
// $pdf->cell(150,0.1,"",1,1);

 $total=0.00;

 $pdf->SetStyle("B",true);
 $pdf->Ln(-5);
 $pdf->cell(10,5,"",0,0);
 $pdf->cell(120,5,"MATERIAS OFICIALES",0,1,"L");
 $pdf->SetStyle("B",false);
 $qmo=mysql_query("select cursos.*
                   from cursos inner join cursos_grados ON cursos.codigo = cursos_grados.curso
                   where cursos_grados.grado = $id_grado
                   and oficial = 'S'
                   and boleta='S'
                   order by cursos.posicion_boleta");
 $cnt_cursos=0;
 $cnt_aprobadas=0;
 $cnt_pm60=0;
 $cnt_pm70=0;
 while($imo=mysql_fetch_array($qmo)){
   $asterisco="";
   $cnt_cursos++;
   $qnota=mysql_query("select *
                       from notas_unidades
                       where
                       alumno = '{$ialumno[carnet]}'
                       and curso = '{$imo[codigo]}'
                       and grado='$id_grado'
                       and unidad='$txtunidad'");

   if(mysql_num_rows($qnota)>0){
    $inota=mysql_fetch_array($qnota);
    if($inota[nota] >= $imo[punteo_aprobacion]){
      $cnt_aprobadas++;
    }
    if($inota[nota] < 60){
      $cnt_pm60++;
      $asterisco="**";
    }
    if($inota[nota] < 70 && $inota[nota] >= 60){
      $cnt_pm70++;
      $asterisco="*";
    }
    $total+=$inota[nota];
    $pdf->cell(15,5,"$cnt_cursos  ",1,0,"R");
    $pdf->cell(84,5,$imo[curso],1,0,"L");
    $pdf->cell(17,5,round($inota[zona],0),1,0,"C");
    $pdf->cell(17,5,round($inota[examen]),1,0,"C");
    $pdf->cell(17,5,round($inota[nota]),1,0,"C");
    $pdf->cell(17,5," ".$asterisco,0,1,"L");
   }else{
    $pdf->cell(15,5,"$cnt_cursos  ",1,0,"R");
    $pdf->cell(84,5,pmayuscula($imo[curso]),1,0,"L");
    $pdf->cell(17,5,"P",1,0,"C");
    $pdf->cell(17,5,"P",1,0,"C");
    $pdf->cell(17,5,"P",1,1,"C");
   }
//    if($cnt_cursos>1){
     $pdf->cell(150,0.001,"",1,1);
//    }

 }

 $cnt_cursos2=0;
 // CURSOS COMPLEMENTARIOS
 $pdf->Ln(10);
 $pdf->SetStyle("B",true);
 $pdf->cell(10,5,"",0,0);

 $pdf->cell(120,5,"CURSOS DE SUPERACION ACADEMICA",0,1,"L");
 $pdf->SetStyle("B",false);
 $qmo=mysql_query("select cursos.*
                   from cursos inner join cursos_grados ON cursos.codigo = cursos_grados.curso
                   where cursos_grados.grado = $id_grado
                   and oficial <> 'S'
                   and boleta='S'
                   order by cursos.posicion_boleta");

 while($imo=mysql_fetch_array($qmo)){
   $asterisco="";
   $cnt_cursos++;
   $cnt_cursos2++;
   $qnota=mysql_query("select *
                       from notas_unidades
                       where
                       alumno = '$ialumno[carnet]'
                       and curso = '{$imo[codigo]}'
                       and grado='$id_grado'
                       and unidad='$txtunidad'");

   if(mysql_num_rows($qnota)>0){
    $inota=mysql_fetch_array($qnota);
    if($inota[nota] >= $imo[punteo_aprobacion]){
      $cnt_aprobadas++;
    }
    if($inota[nota] < 60){
      $cnt_pm60++;
      $asterisco="**";
    }
    if($inota[nota] < 70 && $inota[nota] >= 60){
      $cnt_pm70++;
      $asterisco="*";
    }

    $total+=$inota[nota];

    $pdf->cell(15,5,"$cnt_cursos  ",1,0,"R");
    $pdf->cell(84,5,pmayuscula($imo[curso]),1,0,"L");
    $pdf->cell(17,5,round($inota[zona]),1,0,"C");
    $pdf->cell(17,5,round($inota[examen]),1,0,"C");
    $pdf->cell(17,5,round($inota[nota]),1,0,"C");
    $pdf->cell(17,5," ".$asterisco,0,1,"L");
   }else{
    $pdf->cell(15,5,"$cnt_cursos  ",1,0,"R");
    $pdf->cell(84,5,pmayuscula($imo[curso]),1,0,"L");
    $pdf->cell(17,5,"P",1,0,"C");
    $pdf->cell(17,5,"P",1,0,"C");
    $pdf->cell(17,5,"P",1,1,"C");
   }
//   if($cnt_cursos2>1){
     $pdf->cell(150,0.01,"",1,1);
//    }
 }


 $pdf->cell(150,0.4,"",1,1);
 $pdf->Ln(5);

 $pdf->cell(50,5,"Estado actual: ".$msg." al: ".fecha($fechacorte),0,1,"L");
 $pdf->Ln(-5);

 $izq=15+80+20+15;
 $pdf->cell($izq,4,"PROMEDIO:",0,0,"R");
 $pdf->cell(20,4,(round($total/$cnt_cursos,0)),0,1,"C");
 $pdf->cell($izq,4,"PERDIDAS MENOS DE 60:",0,0,"R");
 $pdf->cell(20,4,$cnt_pm60,0,1,"C");
 $pdf->cell($izq,4,"PERDIDAS MENOS DE 70:",0,0,"R");
 $pdf->cell(20,4,$cnt_pm70,0,1,"C");
 $pdf->cell($izq,4,"MATERIAS APROBADAS:",0,0,"R");
 $pdf->cell(20,4,$cnt_aprobadas,0,1,"C");
 $pdf->Ln(3);

 $pdf->SetStyle("B",true);
 if(!empty($txtmensaje)){
   $pdf->multicell(150,4,"\"$txtmensaje\"",0,"J");
 }
 $pdf->SetStyle("B",false);

 $pdf->Ln(20);
 $pdf->cell(75,4,"f)__________________________",0,1,"C");
 $pdf->cell(75,4,"Profa. Gloria Xicará",0,1,"C");
 $pdf->cell(75,4,"CONTROL DE COBROS",0,1,"C");
 $pdf->Ln(-12);

 $pdf->cell(75,4,"");
 $pdf->cell(75,4,"f)__________________________",0,1,"C");
 $pdf->cell(75,4,"");
 $pdf->cell(75,4,gmaestroguia($idg),0,1,"C");
 $pdf->cell(75,4,"");
 $pdf->cell(75,4,"MAESTRO GUIA",0,1,"C");

 $pdf->Ln(20);
 $pdf->cell(150,4,"f)__________________________",0,1,"C");
 $pdf->cell(150,4,"Licda. Carmen de Pardo",0,1,"C");
 $pdf->cell(150,4,"Directora General",0,1,"C");
}

$pdf->Output();
exit();
?>
