<?php
include("../fnc.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 footer
function Footer()
{
        //Position at 1.5 cm from bottom
        $this->SetY(-20);
        //Arial italic 8
        $this->SetFont('Arial','I',8);
        //Page number
        $this->Cell(0,10,'Página '.$this->PageNo().'/{nb}',0,0,'C');
}
}

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

$pdf=new PDF('P','mm','Letter');
$pdf->SetMargins(10,10.00,0.00);
$pdf->AliasNbPages();
$pdf->AddPage();
//$pdf->Header2("INGRESOS POR TIPO DE CARGO");
$pdf->Encabezado($nombre_colegio,"RESUMEN DE INGRESOS POR TIPO DE CARGO");
$pdf->SetFont('Times','',12);
$pdf->Cell(190,5,"Cobrados del: $fechai al: $fechaf",0,1,"C");
$pdf->SetFont('Times','',10);
$pdf->Ln(6);

$columna=Array();
$columna[1]=20;
$columna[2]=135;
$columna[3]=40;
$borde=1;

$pdf->SetStyle("B",true);
$pdf->Cell($columna[1],5,"CODIGO",$borde,0,"C");
$pdf->Cell($columna[2],5,"DESCRIPCION DEL CARGO",$borde,0,"C");
$pdf->Cell($columna[3],5,"TOTAL",$borde,1,"C");
$pdf->SetStyle("B",false);

$tmpfi=explode("/",$fechai);
$tmpff=explode("/",$fechaf);
$fi=$tmpfi[2]."-".$tmpfi[1]."-".$tmpfi[0];
$ff=$tmpff[2]."-".$tmpff[1]."-".$tmpff[0];

          $sql="select sum(dr.valor) as ttotal, cr.tipo, tc.descripcion
                from cp_cargos cr inner join cp_detalle_recibos dr ON dr.id_cargo = cr.id
                inner join cp_recibos rec on rec.numero = dr.numero_doc and rec.tipo = dr.tipo_doc
                inner join cp_tipos_cargos tc on cr.tipo = tc.id
                where 
                rec.fecha between '$fi 00:00:00' and '$ff 23:59:59'
                group by tipo
                order by cr.tipo";

$q=mysql_query(trim($sql));
//$pdf->multicell(0,5,mysql_error(),1,1);
$total=0.00;
$linea=0;
$arr_codigos = Array();
$arr_descrip = Array();
$arr_totales = Array();
$n=0;
while($idet=mysql_fetch_array($q)){
 $n++;
 $arr_codigos[$n]=$idet[tipo];
 $arr_descrip[$n]=$idet[descripcion];
 $arr_totales[$n]=$idet[ttotal];
}


for($n=1;$n<=count($arr_codigos);$n++){
  $borde=0;
  $linea++;
  $total+=$arr_totales[$n];
  $pdf->Cell($columna[1],5,$arr_codigos[$n],$borde,0,"L");
  $pdf->Cell($columna[2],5,$arr_descrip[$n],$borde,0,"L");
  $pdf->Cell($columna[3],5,moneda($arr_totales[$n]),$borde,1,"R");
  
  if($linea>40){
     $pdf->AddPage();
     //$pdf->Header2("INGRESOS POR TIPO DE CARGO");
     $pdf->Encabezado($nombre_colegio,"INGRESOS POR TIPO DE CARGO");
     $pdf->SetFont('Times','',12);
     $pdf->SetStyle("B",true);
     $pdf->Cell(190,5,"Tipo de cargo: ".get_tipo_cargo($codigocargo),0,1,"C");
     $pdf->SetStyle("B",false);
     $pdf->Cell(190,5,"Cargos que deben ser pagados del: $fechai al: $fechaf",0,1,"C");
     $pdf->SetFont('Times','',10);
     $pdf->Ln(6);

     $borde=1;
     $pdf->SetStyle("B",true);
     $pdf->Cell($columna[1],5,"CODIGO",$borde,0,"C");
     $pdf->Cell($columna[2],5,"DESCRIPCION DEL CARGO",$borde,0,"C");
     $pdf->Cell($columna[3],5,"TOTAL",$borde,1,"C");


     $pdf->SetStyle("B",false);
     $linea=0;
  }
}
$pdf->Cell(195,0.2,"",1,1);
$pdf->SetStyle("B",true);
$pdf->Cell(25,5,"TOTAL...",0,0,"L");
$pdf->Cell(170,5,moneda($total),0,1,"R");
$pdf->SetStyle("B",false);
$pdf->Cell(195,0.2,"",1,1);
$pdf->Output();
exit();
?>
