<?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 Footer2()
{
        //Position at 1.5 cm from bottom
        $this->SetY(-15);
        //Arial italic 8
        $this->SetFont('Arial','I',8);
        //Page number
//        $this->Cell(0,10,'Pagina '.$this->PageNo().'/{nb}',0,0,'C');
}
}

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


$lst="(0";

for($i=1;$i<=$maxi;$i++){
  $tmp="txtcargo$i";
  if($$tmp){
    
    $lst.=",".$txtidc[$i];
  }
}
$lst.=")";

if($lst=="(0)"){
  alert("Debe seleccionar al menos 1 cargo para generar una boleta","frm_cp_generarboletaxcargo.php?txtalumno=$idalumno");
}

          $sql="select alumnos.*, grados.grado as dgrado, niveles.nivel as dnivel
                from alumnos inner join grados ON alumnos.grado_estudio = grados.id
                inner join niveles on grados.nivel = niveles.id
                where alumnos.carnet = '$idalumno'";

$q=mysql_query($sql);

if(!$q){
 echo mysql_error();
 echo "<br>".$sql;
 exit();
}

$info=mysql_fetch_array($q);

$info[fdep]=$txtfechadep;
//$txtboletai=get_numboletadp()+1;

$paper_width=Array();
$paper_width[0]=139.7;
$paper_width[1]=215.9;

$boletas=Array();
$boletas[0]="B1";
$boletas[1]="B2";
$boletas[2]="B3";

$info[observaciones]="";
$dvalores=Array();
$dvalores[coleg]=0.00;
$dvalores[trans]=0.00;
$dvalores[otros]=0.00;
$dtotal=0.00;

$pdf=new PDF('P','mm',$paper_width);
$pdf->SetMargins(10,7.00,0.00);

if(!$year_cargo){$year_cargo=2007;}
if(!$txtdiadp){$txtdiadp=8;}

for($n=1;$n<=1;$n++){
 $sql="select * 
       from cp_cargos 
       where 
       carnet='$idalumno' and saldo > 0
       and (tipo not like '%A&B%') and (tipo not like '%CIAV%') 
       and id in $lst";
 $qib=mysql_query($sql);
 
 
 if(mysql_num_rows($qib)==0){
  continue;
 }
 
 if(!$qib){ echo mysql_error()."<br>$sql<hr>";}
 // $qinfo=mysql_query("select * from alumnos where carnet='$idalumno'");
 // $info=mysql_fetch_array($qinfo);

 $dvalores[coleg]=0;
 $dvalores[trans]=0;
 $dvalores[otros]=0;

 $info[observaciones]="";
 $info[pdescrip]="";

 while($ib=mysql_fetch_array($qib)){
  
  $sqlupdboleta=mysql_query("update cp_cargos set boletadp='$txtboletai' where id = '{$ib[id]}'");
  
  if($ib[tipo]=="ME"){
           $dvalores[coleg]+=$ib[saldo];
           $info[observaciones].=$ib[descripcion]."/ ";
           $info[pdescrip].=$ib[desc_boleta];
  }elseif(substr($ib[tipo],0,4)=="TRES"){
           $dvalores[trans]+=$ib[saldo];
           $info[observaciones].=$ib[descripcion]."/ ";
           $info[pdescrip].=$ib[desc_boleta];
  }else{
           $dvalores[otros]+=$ib[saldo];
           $info[observaciones].=$ib[descripcion]."/ ";
           $info[pdescrip].=$ib[desc_boleta];
  }
 }
     if($dvalores[otros]+$dvalores[trans]+$dvalores[coleg]==0){
      continue;
     }

     //$pdf->AliasNbPages();
     $pdf->AddPage();
     $pdf->SetFont('Arial','',10);
     $pdf->Cell(20,5,"",0,0);
     $pdf->Cell(90,5,$info[nombre]." ".$info[apellido],0,0,"L");
     $pdf->Cell(15,5,$txtboletai,0,1);
     //$pdf->Cell(5,5,$txtboletai,0,1);
     $pdf->SetFont('Arial','',10);
     $pdf->Cell(15,5,"",0,0);
     $pdf->Cell(90,5,$info[carnet]." ".$info[dgrado]." ".$info[dnivel],0,1,"L");
     $pdf->Cell(20,5,"",0,0);
     $pdf->Cell(90,5,$info[observaciones],0,1,"L");
     $pdf->Ln(13);

     $pdf->SetFont('Arial','B',10);
     $pdf->Cell(27,5,$info[pdescrip],0,0);
     $pdf->SetFont('Arial','',08);
     $pdf->Cell(19,5,$txtfechadep,0,0,"R");
     $pdf->Cell(27,5,$info[carnet],0,1,"R");

     $pdf->SetFont('Arial','',10);

     $pdf->Ln(8);
     $pdf->Cell(20,5,moneda($dvalores[coleg],""),0,0,"R");
     $pdf->Cell(35,5,moneda($dvalores[trans],""),0,0,"R");
     $pdf->Cell(35,5,moneda($dvalores[otros],""),0,0,"R");
     $pdf->SetStyle("B",true);
     $pdf->Cell(30,5,moneda(($dvalores[otros]+$dvalores[trans]+$dvalores[coleg])),0,0,"R");
     $pdf->SetStyle("B",false);
     $pdf->Ln(18);//
     // SEGUNDA PARTE
     $pdf->Cell(75,5,$info[nombre]." ".$info[apellido],0,0,"L");
     //$pdf->Cell(0,5,$info[carnet],0,0,"L");
     $pdf->Ln(11);
     $pdf->SetFont('Arial','B',10);
     $pdf->Cell(27,5,$info[pdescrip],0,0);
     $pdf->SetFont('Arial','',08);
     $pdf->Cell(19,5,$txtdiadp."-".$n."-".$year_cargo,0,0,"R");
     $pdf->Cell(27,5,$info[carnet],0,1,"R");
     $pdf->SetFont('Arial','',10);
     $pdf->Ln(7);
     $pdf->Cell(20,5,moneda($dvalores[coleg]),0,0,"R");
     $pdf->Cell(35,5,moneda($dvalores[trans]),0,0,"R");
     $pdf->Cell(35,5,moneda($dvalores[otros]),0,0,"R");
     $pdf->SetStyle("B",true);
     $pdf->Cell(30,5,moneda(($dvalores[otros]+$dvalores[trans]+$dvalores[coleg]),""),0,0,"R");
     $pdf->SetStyle("B",false);
     $pdf->Ln(8);
     $pdf->Write(0,"***".num2letras($dvalores[otros]+$dvalores[trans]+$dvalores[coleg])."***",0,0);
     $pdf->Ln(28);
     $pdf->SetStyle("B",true);
     $pdf->Cell(126,5,moneda(($dvalores[otros]+$dvalores[trans]+$dvalores[coleg]),""),0,0,"R");
     $pdf->SetStyle("B",false);
     $pdf->Ln(45);
     // TERCERA PARTE
     $pdf->SetStyle("B",true);
     $pdf->Cell(126,5,moneda(($dvalores[otros]+$dvalores[trans]+$dvalores[coleg]),""),0,0,"R");
     $pdf->SetStyle("B",false);
     $pdf->Ln();
     $pdf->Write(0,"***".num2letras($dvalores[otros]+$dvalores[trans]+$dvalores[coleg])."***",0,0);
     $total_boleta=$dvalores[otros]+$dvalores[trans]+$dvalores[coleg];

     $sqlbol="insert into cp_boletas
              set carnet='$idalumno',
              numero='$txtboletai',
              tipo='P',
              fecha='$fechahora',
              anulado='N',
              total='$total_boleta'";
     $ins_boleta=mysql_query($sqlbol);

}

//BOLETAS DE CIAV Y ALIMENTOS & BEBIDAS

for($n=1;$n<=1;$n++){

 $sql="select * 
       from cp_cargos 
       where carnet='$idalumno' and saldo > 0 
       and id in $lst       
       and ((tipo like '%A&B%') or (tipo like '%CIAV%'))";
 $qib=mysql_query($sql);
 
 if(mysql_num_rows($qib)==0){
  continue;
 }
 
 $txtboletai++;
 //echo mysql_num_rows($qib);
 //echo $sql;
 if(!$qib){echo mysql_error()."<br>$sql<hr>";}

 $dvalores[coleg]=0;
 $dvalores[trans]=0;
 $dvalores[otros]=0;

 $info[observaciones]="";
 $info[pdescrip]="";

 while($ib=mysql_fetch_array($qib)){
  if($ib[tipo]=="ME"){
           $dvalores[coleg]+=$ib[saldo];
           $info[observaciones].=$ib[descripcion]."/ ";
           $info[pdescrip].=$ib[desc_boleta];
  }elseif(substr($ib[tipo],0,4)=="TRES"){
           $dvalores[trans]+=$ib[saldo];
           $info[observaciones].=$ib[descripcion]."/ ";
           $info[pdescrip].=$ib[desc_boleta];
  }else{
           $dvalores[otros]+=$ib[saldo];
           $info[observaciones].=$ib[descripcion]."/ ";
           $info[pdescrip].=$ib[desc_boleta];
  }
 }
     if($dvalores[otros]+$dvalores[trans]+$dvalores[coleg]==0){
      continue;
     }

     //$pdf->AliasNbPages();
     $pdf->AddPage();
     $pdf->SetFont('Arial','',10);
     $pdf->Cell(20,5,"",0,0);
     $pdf->Cell(90,5,$info[nombre]." ".$info[apellido],0,0,"L");
     $pdf->Cell(20,5,$txtboletai,0,1);
     $pdf->SetFont('Arial','',10);
     $pdf->Cell(20,5,"",0,0);
     $pdf->Cell(90,5,$info[carnet]." ".$info[dgrado]." ".$info[dnivel],0,1,"L");
     $pdf->Cell(20,5,"",0,0);
     $pdf->Cell(90,5,$info[observaciones],0,1,"L");
     $pdf->Ln(13);

     $pdf->SetFont('Arial','B',10);
     $pdf->Cell(27,5,$info[pdescrip],0,0);
     $pdf->SetFont('Arial','',08);
     $pdf->Cell(19,5,$txtfechadep,0,0,"R");
     $pdf->Cell(27,5,$info[carnet],0,1,"R");

     $pdf->SetFont('Arial','',10);

     $pdf->Ln(8);
     $pdf->Cell(20,5,moneda($dvalores[coleg],""),0,0,"R");
     $pdf->Cell(35,5,moneda($dvalores[trans],""),0,0,"R");
     $pdf->Cell(35,5,moneda($dvalores[otros],""),0,0,"R");
     $pdf->SetStyle("B",true);
     $pdf->Cell(30,5,moneda(($dvalores[otros]+$dvalores[trans]+$dvalores[coleg])),0,0,"R");
     $pdf->SetStyle("B",false);
     $pdf->Ln(18);//
     // SEGUNDA PARTE
     $pdf->Cell(75,5,$info[nombre]." ".$info[apellido],0,0,"L");
     //$pdf->Cell(0,5,$info[carnet],0,0,"L");
     $pdf->Ln(11);
     $pdf->SetFont('Arial','B',10);
     $pdf->Cell(27,5,$info[pdescrip],0,0);
     $pdf->SetFont('Arial','',08);
     $pdf->Cell(19,5,$txtdiadp."-".$n."-".$year_cargo,0,0,"R");
     $pdf->Cell(27,5,$info[carnet],0,1,"R");
     $pdf->SetFont('Arial','',10);
     $pdf->Ln(7);
     $pdf->Cell(20,5,moneda($dvalores[coleg]),0,0,"R");
     $pdf->Cell(35,5,moneda($dvalores[trans]),0,0,"R");
     $pdf->Cell(35,5,moneda($dvalores[otros]),0,0,"R");
     $pdf->SetStyle("B",true);
     $pdf->Cell(30,5,moneda(($dvalores[otros]+$dvalores[trans]+$dvalores[coleg]),""),0,0,"R");
     $pdf->SetStyle("B",false);
     $pdf->Ln(8);
     $pdf->Write(0,"***".num2letras($dvalores[otros]+$dvalores[trans]+$dvalores[coleg])."***",0,0);
     $pdf->Ln(28);
     $pdf->SetStyle("B",true);
     $pdf->Cell(126,5,moneda(($dvalores[otros]+$dvalores[trans]+$dvalores[coleg]),""),0,0,"R");
     $pdf->SetStyle("B",false);
     $pdf->Ln(45);
     // TERCERA PARTE
     $pdf->SetStyle("B",true);
     $pdf->Cell(126,5,moneda(($dvalores[otros]+$dvalores[trans]+$dvalores[coleg]),""),0,0,"R");
     $pdf->SetStyle("B",false);
     $pdf->Ln();
     $pdf->Write(0,"***".num2letras($dvalores[otros]+$dvalores[trans]+$dvalores[coleg])."***",0,0);
     $total_boleta=$dvalores[otros]+$dvalores[trans]+$dvalores[coleg];

     $sqlbol="insert into cp_boletas
              set carnet='$idalumno',
              numero='$txtboletai',
              tipo='P',
              fecha='$fechahora',
              anulado='N',
              total='$total_boleta'";
     $ins_boleta=mysql_query($sqlbol);
     echo mysql_error();

}

     $upd_g=mysql_query("update general set boleta_deposito = $txtboletai");
     $txtboletai++;
     
$pdf->Output();
//$pdf->Output("boletas.pdf","D");
exit();
?>
