<?php
// FORMATO DE IMPRESION PARA RECIBO CONTINUO POR OTROS INGRESOS

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);
        $this->Ln();
}

//Page footer
function Footer()
{
        //Position at 1.5 cm from bottom
        $this->SetY(-25);
        //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");
include("../num2letras.php");

if(!$txtfi || !$txtff || !$tipodoc){
  alert("Parámetros incorrectos","$goback");
  exit();
}

$txtfi=explode("/",$txtfi);
$txtff=explode("/",$txtff);
$fi=$txtfi[2]."-".$txtfi[1]."-".$txtfi[0];
$ff=$txtff[2]."-".$txtff[1]."-".$txtff[0];

$qtl=mysql_query("select * from cp_talonarios where id = '$tipodoc'");
$itl=mysql_fetch_array($qtl);

$pdf=new PDF('P','mm','Letter');
$pdf->SetMargins(10,10.00,0.00);
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->Encabezado("","CORTE DE CAJA");
$pdf->SetFillColor(255,255,255);
//$pdf->Header2("CORTE DE CAJA");

$pdf->SetFont('Times','',12);
$pdf->Cell(190,5,"Talonario: ".$itl[nombre],0,1,"C");
$pdf->Cell(190,5,"Reporte de ingresos del ".fecha($fi)." al ".fecha($ff),0,1,"C");

             $sql="select numero, fecha, carnet, nit, nombre, total,
                   vtc, tc, vdp, dp, fdp, ef, vchq, chq, anulado
                   from cp_recibos
                   where tipo = $tipodoc
                   and fecha>='$fi 00:00:00'
                   and fecha<='$ff 23:59:00'
                   order by fecha, numero";

$qdoc=mysql_query($sql);
$pdf->Ln();

$pdf->SetFont('Times','',10);
$pdf->Setstyle("B",true);
$border=1;

$celdas=Array();
$celdas[0]=15;
$celdas[1]=20;
$celdas[2]=18;
$celdas[3]=78;
$celdas[4]=20;
$celdas[5]=29;

$ttdepositos=$ttcheques=$tttarjetas=$ttefectivo=$ttnotacredito=$ttvales=0.00;
$depositos=Array();
$cheques=Array();
$tarjetas=Array();
$notascredito=Array();

$pdf->Cell($celdas[0],5,"# DOC.",$border,0,"C");
$pdf->Cell($celdas[1],5,"FECHA",$border,0,"C");
$pdf->Cell($celdas[2],5,"CARNET",$border,0,"C");
$pdf->Cell($celdas[3],5,"DOCUMENTO A NOMBRE DE",$border,0,"C");
$pdf->Cell($celdas[4],5,"TOTAL",$border,0,"C");
$pdf->Cell($celdas[5],5,"FORMA PAGO",$border,1,"C");
$pdf->cell(0,0.2,"",0,1);
$pdf->Setstyle("B",false);
$total=0;
$ln=0;
$cont_trn=0;
$det_new_p=false;

if(mysql_num_rows($qdoc)>=20){
  $det_new_p=true;
}
while($idoc=mysql_fetch_array($qdoc)){
  $ln++;
  $cont_trn++;
  $border=0;
  $forma_pago="";
  $total+=$idoc[total];

  if($idoc[anulado]=="S"){
      $idoc[nombre]="A N U L A D O";
  }

  if ($idoc[vdp]>0){
    $forma_pago="DP-".$idoc[dp];
    $depositos[]=$idoc[fecha]."||".$idoc[dp]."||".$idoc[vdp];
    $ttdepositos+=$idoc[vdp];
  }

  if ($idoc[ef]>0){
      if($tipodoc==5){
          $ttnotacredito+=$idoc[ef];
      }else{
          $forma_pago="EFECTIVO";
          $ttefectivo+=$idoc[ef];
      }
  }

  if ($idoc[vchq]>0){
    $forma_pago="CHQ-".$idoc[chq];
    $cheques[]=$idoc[fecha]."||".$idoc[chq]."||".$idoc[vchq];
    $ttcheques+=$idoc[vchq];
  }

  if ($idoc[vtc]>0){
    $forma_pago="TC-".substr($idoc[tc],-4);
    $tarjetas[]=$idoc[fecha]."||XX".$idoc[dp]."||".$idoc[vtc];
    $tttarjetas+=$idoc[vtc];
  }

  $pdf->Cell($celdas[0],5,$idoc[numero],$border,0);
  $pdf->Cell($celdas[1],5,fecha($idoc[fecha]),$border,0);
  $pdf->Cell($celdas[2],5,$idoc[carnet],$border,0,"C");
  $pdf->Cell($celdas[3],5,$idoc[nombre],$border,0);
  $pdf->Cell($celdas[4],5,moneda($idoc[total]),$border,0,"R",1);
  $pdf->SetFont('Times','',8);
  $pdf->Cell($celdas[5],5,$forma_pago,$border,1,"R");
  $pdf->SetFont('Times','',10);

  if($ln>36){
    $ln=0;
    $pdf->AddPage();
    $pdf->Header2("CORTE DE CAJA");
    $pdf->SetFont('Times','',12);
    $pdf->Cell(190,5,"Reporte de ingresos del ".fecha($fi)." al ".fecha($ff),0,1,"C");
    $pdf->Cell(190,5,"Talonario: ".$itl[nombre],0,1,"C");
    $pdf->Ln(15);
    $border=1;
    $pdf->SetFont('Times','',10);
    $pdf->Setstyle("B",true);
    $pdf->Cell($celdas[0],5,"# DOC.",$border,0,"C");
    $pdf->Cell($celdas[1],5,"FECHA",$border,0,"C");
    $pdf->Cell($celdas[2],5,"CARNET",$border,0,"C");
    $pdf->Cell($celdas[3],5,"DOCUMENTO A NOMBRE DE",$border,0,"C");
    $pdf->Cell($celdas[4],5,"TOTAL",$border,0,"C");
    $pdf->Cell($celdas[5],5,"FORMA PAGO",$border,1,"C");
    $pdf->cell(0,0.2,"",0,1);
    $pdf->Setstyle("B",false);
  }
}

$border=1;
$pdf->Setstyle("B",true);
$pdf->Cell(131,5,"TOTAL:  $cont_trn documento(s)",$border,0);
$pdf->Cell($celdas[4],5,moneda($total),$border,0,"R");
$pdf->Cell($celdas[5],5,"",$border,1,"R");
$pdf->Setstyle("B",false);
$pdf->Ln(3);
$pdf->Cell(180,0.1,"",1,1);


if($det_new_p==true){
 //$pdf->AddPage();
}

$maxd=0;

if(count($depositos)>$maxd){$maxd=count($depositos);}
if(count($cheques)>$maxd){$maxd=count($cheques);}
if(count($tarjetas)>$maxd){$maxd=count($tarjetas);}
// IMPRESION DE RESUMEN DE FORMAS DE PAGO

$pdf->Ln(5);

// ENCABEZADOS FORMA DE PAGO
$tab=0;
$pdf->Setstyle("B",true);
$pdf->Cell(58,5,"DEPOSITOS",$border,0,"C");
$pdf->Cell(2,5,"");
$pdf->Cell(58,5,"CHEQUES",$border,0,"C");
$pdf->Cell(2,5,"");
$pdf->Cell(60,5,"TARJETAS DE CRÉDITO",$border,1,"C");

$pdf->Cell(18,5,"Fecha",$border,0,"C");
$pdf->Cell(20,5,"No. Depósito",$border,0,"C");
$pdf->Cell(20,5,"Valor",$border,0,"C");
$pdf->Cell(2,5,"");

$pdf->Cell(18,5,"Fecha",$border,0,"C");
$pdf->Cell(20,5,"No. Cheque",$border,0,"C");
$pdf->Cell(20,5,"Valor",$border,0,"C");
$pdf->Cell(2,5,"");

$pdf->Cell(20,5,"Fecha",$border,0,"C");
$pdf->Cell(20,5,"No. Tarjeta",$border,0,"C");
$pdf->Cell(20,5,"Valor",$border,1,"C");
$pdf->Setstyle("B",false);
// FIN ENCABEZADOS FORMAS DE PAGO

//DETALLE DE FORMAS DE PAGO
$ttfpdp=$ttfpef=$ttfptc=$ttfpch=0;
for($i=0;$i<=$maxd;$i++){

 if((count($depositos)) == $i){
    $prntdp=true;
    $border=1;
 }else{
    $prntdp=false;
    if(count($depositos) < $i){
        $print=false;
    }else{
        $print=true;
        $prntdp=false;
        $border=0;
    }
 }

 if($prntdp==true){
     $pdf->Setstyle("B",true);
     $pdf->Cell(38,5,"TOTAL: $i Depósito(s)",$border,0);
     $pdf->Cell(20,5,moneda($ttfpdp),$border,0,"R");
     $pdf->Cell(2,5,"");
     $pdf->Setstyle("B",false);
 }else{
     if($print==true){
     $tmpinfo=explode("||",$depositos[$i]);
     $pdf->Cell(18,5,fecha($tmpinfo[0]),$border,0,"C");
     $pdf->Cell(20,5,$tmpinfo[1],$border,0,"C");
     $pdf->Cell(20,5,moneda($tmpinfo[2]),$border,0,"R");
     $pdf->Cell(2,5,"");
     $ttfpdp+=$tmpinfo[2];
     }else{
        $pdf->Cell(60,5,"")  ;
     }
 }

 if((count($cheques)) == $i){
    $prntch=true;
    $border=1;
 }else{
    $prntch=false;
    if(count($cheques) < $i){
        $print=false;
    }else{
        $print=true;
        $prntch=false;
        $border=0;
    }
 }

 if($prntch==true){
     $pdf->Setstyle("B",true);
     $pdf->Cell(38,5,"TOTAL: $i Cheque(s)",$border,0);
     $pdf->Cell(20,5,moneda($ttfpch),$border,0,"R");
     $pdf->Cell(2,5,"");
     $pdf->Setstyle("B",false);
 }else{
     if($print==true){
     $tmpinfo=explode("||",$cheques[$i]);
     $pdf->Cell(18,5,fecha($tmpinfo[0]),$border,0,"C");
     $pdf->Cell(20,5,$tmpinfo[1],$border,0,"C");
     $pdf->Cell(20,5,moneda($tmpinfo[2]),$border,0,"R");
     $pdf->Cell(2,5,"");
     $ttfpch+=$tmpinfo[2];
     }else{
         $pdf->Cell(60,5,"",0,0);
     }
 }

  if((count($tarjetas)) == $i){
    $prnttc=true;
    $border=1;
 }else{
    $prnttc=false;
    if(count($tarjetas) < $i){
        $print=false;
    }else{
        $print=true;
        $prnttc=false;
        $border=0;
    }
 }

 if($prnttc==true){
     $pdf->Setstyle("B",true);
     $pdf->Cell(40,5,"TOTAL: $i Tarjeta(s)",$border,0);
     $pdf->Cell(20,5,moneda($ttfptc),$border,1,"R");
     $pdf->Setstyle("B",false);
 }else{
     if($print==true){
     $tmpinfo=explode("||",$tarjetas[$i]);
     $pdf->Cell(20,5,fecha($tmpinfo[0]),$border,0,"C");
     $pdf->Cell(20,5,$tmpinfo[1],$border,0,"C");
     $pdf->Cell(20,5,moneda($tmpinfo[2]),$border,1,"R");
     $ttfptc+=$tmpinfo[2];
     }else{
         $pdf->Cell(60,5,"",0,1);
     }
 }
} // FIN RESUMEN FORMAS DE PAGO


//RESUMEN GENERAL
$pdf->Ln();
$pdf->Setstyle("B",true);
$pdf->Cell(58,5,"RESUMEN ",$border,1,"C");
$pdf->Setstyle("B",false);
$pdf->Cell(38,5,"Efectivo: ",$border,0);
$pdf->Cell(20,5,moneda($ttefectivo),$border,1,"R");
$pdf->Cell(38,5,"Depósitos:",$border,0);
$pdf->Cell(20,5,moneda($ttfpdp),$border,1,"R");
$pdf->Cell(38,5,"Cheques:",$border,0);
$pdf->Cell(20,5,moneda($ttfpch),$border,1,"R");
$pdf->Cell(38,5,"Tarjetas:",$border,0);
$pdf->Cell(20,5,moneda($ttfptc),$border,1,"R");
$pdf->Cell(38,5,"Notas de crédito:",$border,0);
$pdf->Cell(20,5,moneda($ttnotacredito),$border,1,"R");

$pdf->Setstyle("B",true);
$pdf->Cell(38,5,"TOTAL GENERAL:",$border,0);
$pdf->Cell(20,5,moneda($ttfpdp+$ttfpch+$ttfptc+$ttefectivo+$ttnotacredito),$border,1,"R");
$pdf->Setstyle("B",false);
//FIN RESUMEN GENERAL

$pdf->Ln(5);
$pdf->SetFillColor(255,255,255);
$pdf->setstyle("B",true);
$pdf->cell(180,5,"DETALLE DE GASTOS",0,1);
$pdf->Cell(21,5,"FECHA",1,0,"C",1);
$pdf->Cell(94,5,"DESCRIPCIÓN ",1,0,"C",1);
$pdf->Cell(40,5,"TIPO DE GASTO",1,0,"C",1);
$pdf->Cell(25,5,"VALOR",1,1,"R",1);
$pdf->setstyle("B",false);
$qgst=mysql_query("select g.*, tg.tipo
                   from pr_gastos g inner join pr_tiposgastos tg ON g.tipo_gasto = tg.id
                   where g.fecha between '$fi' and '$ff'
                   order by fecha, id");
$total=$i=0;                   
while($igst=mysql_fetch_array($qgst)){
  $i++;
  $total+=$igst[valor];
  $pdf->Cell(21,5,fecha($igst[fecha]),1,0,"C",1);
  $pdf->Cell(94,5,$igst[descripcion],1,0,"L",1);
  $pdf->Cell(40,5,$igst[tipo],1,0,"L",1);
  $pdf->Cell(25,5,moneda($igst[valor]),1,1,"R",1);
}

$pdf->setstyle("B",true);
$pdf->Cell(21,5,"TOTALES...",1,0,"C",1);
$pdf->Cell(94,5,"$i GASTO(S) ",1,0,"L",1);
$pdf->Cell(40,5,"",1,0,"C",1);
$pdf->Cell(25,5,moneda($total),1,1,"R",1);

$pdf->setstyle("B",false);
$pdf->Output();
exit();
?>
