<?php
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,5,$this->nombre_institucion,0,1,"C");
        $this->Cell(80,5);
        $this->Cell(30,5,$texto,0,0,'C');
        //Line break
        $this->Ln(10);
}

//Page footer
function Footer()
{
        //Position at 1.5 cm from bottom
        $this->SetY(-15);
        //Arial italic 8
        $this->SetFont('Arial','I',8);
        //Page number
        //$this->Cell(0,10,'Página '.$this->PageNo().'/{nb}',0,0,'C');
}
}

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

$pdf=new PDF('P','mm','Letter');
$pdf->AliasNbPages();

$sqlg="select * from alumnos where grado_estudio = '$txtgrado' and alumnos.year_estudio = '$df_year_actual' order by seccion, apellido, nombre";
$qgr=mysql_query($sqlg);

while($ial=mysql_fetch_array($qgr)){
$idalumno=$ial[carnet];
//$qalm=mysql_query("select * from alumnos where carnet='$idalumno'");
//$ial=mysql_fetch_array($qalm);
$pdf->AddPage();
$pdf->Header2("Estado de Cuenta");
$pdf->SetFont('Times','',12);
$pdf->Ln(10);
$borde=0;
$pdf->Cell(30,5,"Carnet:",$borde,0);
$pdf->Cell(100,5,"$idalumno",$borde,0);
$pdf->Cell(60,5,"Fecha Corte: $fechal",$borde,1,"R");
$pdf->Cell(30,5,"Apellido(s):",$borde,0);
$pdf->Cell(100,5,$ial[apellido],$borde,1);
$pdf->Cell(30,5,"Nombre(s):",$borde,0);
$pdf->Cell(100,5,$ial[nombre],$borde,1);
$pdf->Cell(30,5,"Grado:",$borde,0);
$pdf->Cell(100,5,ggrado(enc($ial[grado_estudio])).", Nivel: ".gnivel(enc($ial[nivel_estudio])).", Sección: ".$ial[seccion],$borde,1);
$pdf->Ln();

$borde=1;

$pdf->SetStyle("B",true);
$pdf->Cell(30,5,"Fecha de pago",$borde,0);
$pdf->Cell(135,5,"Descripción del cargo",$borde,0);
$pdf->Cell(25,5,"Saldo",$borde,1,"C");
$pdf->SetStyle("B",false);
$borde=0;

$tmpf=explode("/",$fechal);
$fecha2=$tmpf[2]."-".$tmpf[1]."-".$tmpf[0];

$qdet=mysql_query("select * from cp_cargos where carnet='$idalumno' and fecha_cargo <='$fecha2 23:59:59' and saldo > 0 order by fecha_cargo");
$total=0.00;
while($idet=mysql_fetch_array($qdet)){
 $total+=$idet[saldo];
 $pdf->Cell(30,5,fecha($idet[fecha_cargo]),$borde,0);
 $pdf->Cell(135,5,$idet[descripcion],$borde,0);
 $pdf->Cell(25,5,moneda($idet[saldo],"Q"),$borde,1,"R");
}
 $pdf->Cell(190,0.01,"",1,1);
 $pdf->SetStyle("B",true);
 $pdf->Cell(30,5,"TOTAL",$borde,0);
 $pdf->Cell(135,5,"",$borde,0);
 $pdf->Cell(25,5,moneda($total,"Q"),$borde,1,"R");
 $pdf->SetStyle("B",false);
 $pdf->Cell(190,0.01,"",1,1);
 $pdf->Cell(190,0.2,"",1,1);

//$pdf->Output();
}//fin del ciclo de grados
 $pdf->Output("estado_cuenta_$idalumno.pdf","I");
exit();
?>
