<?php

Yii::import("application.vendors.fpdf.*", true);

class PDF_MC_Table extends FPDF
{
    var $widths;
    var $aligns;

    function SetWidths($w){
        //Set the array of column widths
        $this->widths=$w;
    }

    function SetAligns($a){
        //Set the array of column alignments
        $this->aligns=$a;
    }

    function Row($data){
        //Calculate the height of the row
        $nb=0;
        for($i=0;$i<count($data);$i++){
            $nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));
        }
        $h=9*$nb;
        //Issue a page break first if needed
        $this->CheckPageBreak($h);
        //Draw the cells of the row
        for($i=0;$i<count($data);$i++){
            $w=$this->widths[$i];
            $a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
            $x=$this->GetX();
            $y=$this->GetY();
            $this->Rect($x,$y,$w,$h,'DF');
            $this->MultiCell($w,8,$data[$i],0,$a,0);
            $this->SetXY($x+$w,$y);
        }
        //Go to the next line
        $this->Ln($h);
    }
function Row2($data){
        //Calculate the height of the row
        $nb=0;
        for($i=0;$i<count($data);$i++){
            $nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));
        }
        $h=9*$nb;
        //Issue a page break first if needed
        $this->CheckPageBreak($h);
        //Draw the cells of the row
        for($i=0;$i<count($data);$i++){
            $w=$this->widths[$i];
            $a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
            //Save the current position
            $x=$this->GetX();
            $y=$this->GetY();
            //Draw the border
            $this->Rect($x,$y,$w,$h,'DF');
            $this->MultiCell($w,8,$data[$i],0,$a,0);
            $this->SetXY($x+$w,$y);
        }
        //Go to the next line
        $this->Ln($h);
    }
    function CheckPageBreak($h){
        if($this->GetY()+$h>$this->PageBreakTrigger){
            $this->AddPage($this->CurOrientation);
        }
    }

    function NbLines($w,$txt){
        $cw=&$this->CurrentFont['cw'];
        if($w==0){
            $w=$this->w-$this->rMargin-$this->x;
        }
        $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
        $s=str_replace("\r",'',$txt);
        $nb=strlen($s);
        if($nb>0 and $s[$nb-1]=="\n"){
            $nb--;
        }
        $sep=-1;
        $i=0;
        $j=0;
        $l=0;
        $nl=1;
        while($i<$nb){
            $c=$s[$i];
            if($c=="\n"){
                $i++;
                $sep=-1;
                $j=$i;
                $l=0;
                $nl++;
                continue;
            }
            if($c==' '){
                $sep=$i;
            }
            $l+=$cw[$c];
            if($l>$wmax){
                if($sep==-1){
                    if($i==$j){
                        $i++;
                    }
                }else{
                    $i=$sep+1;
                }
                $sep=-1;
                $j=$i;
                $l=0;
                $nl++;
            }else{
                $i++;
            }
        }
        return $nl;
    }
}
class PDF  extends PDF_MC_Table
{
	function Header()
	{
                $this->Ln(10);
		$this->SetFont('Arial','B',16);
                $this->Cell(35,10,'',0,0,'C');
		$this->Cell(120,10,utf8_decode('ENTREGA DE EVALUACION DE CREDITO'),'B',0,'C');
		$this->Ln(20);
	}

	function Footer()
	{
		$this->SetY(-15);		
		$this->SetFont('Arial','B',7);
                $session = new CHttpSession;
                $session->open();
		$this->Cell(250,10,utf8_decode('Generado a través del Sistema  en Fecha '.date('d/m/Y').' por el Usuario '.$session['nombres'].' '.$session['apellidos'].'  |  Fuente: OSTI - '.date('Y').'  |  Licencia: GPL/GNU'),0,0,'L');
		$this->Cell(30,10,utf8_decode('Página '.$this->PageNo().'/{nb}'),0,0,'R');
	}
        
        
        
        function rendicion($dataProvider,$cliente)
	{
            set_time_limit(0); 
            if(!empty($dataProvider[0]["fecha"])){
                $this->SetFont('Arial','B',16);
                $this->Cell(75,10,'',0,0,'C');
		$this->Cell(40,10,Utility::fecha3($dataProvider[0]["fecha"]),1,0,'C');
		$this->Ln(20);
            }
            
            $this->SetFont('Arial','B',12);
            $this->SetFillColor(236, 112, 99);
            $this->SetTextColor(255,255,255);
            $this->Cell(20,8,utf8_decode('Nº'),1,0,'C',1);
            $this->Cell(90,8,utf8_decode('NOMBRES Y APELLIDOS'),1,0,'C',1);
            $this->Cell(40,8,utf8_decode('CAPTADOR'),1,0,'C',1);
            $this->Cell(40,8,utf8_decode('MONTO'),1,0,'C',1);
            $this->SetFont('Arial','',12);
            $this->SetTextColor(0);
            $this->SetWidths(array(20,90,40,40));
            $this->SetAligns(array('C', 'L', 'C','R'));
           
            $this->Ln();     
            $c=count($dataProvider);   
            $j=0;
            $monto=0;
            $saldo_anterior=0;
            $saldo_capital=0;
            $saldo_total=0;
            $transferencia=0;
            $efectivo=0;
            $deposito=0;
            $total=0;
            $monto4=0;
            for ($i = 0; $i < $c; $i++) {
                
                $saldo_anterior=$saldo_anterior+$dataProvider[$i]["saldo_anterior"];
                $transferencia=$transferencia+$dataProvider[$i]["transferencias"];
                $saldo_capital=$saldo_capital+$dataProvider[$i]["saldo_capital"];
                $monto4=$monto4+$dataProvider[$i]["monto4"];
                
                $saldo_total=$saldo_total+$dataProvider[$i]["sub_total"];
                $efectivo=$efectivo+$dataProvider[$i]["efectivo"];
                $deposito=$deposito+$dataProvider[$i]["deposito"];
                $total=$total+$dataProvider[$i]["total"];
                
                $this->SetFillColor(255,255,255); 
                $this->SetTextColor(0);
                $j=$j+1;
  
            } $this->SetFillColor(255,255,255);    
             $this->SetWidths(array(20,90,40,40));
            $this->SetAligns(array('C', 'L', 'C','R'));
                $j=0;
                for ($i = 0; $i < count($cliente); $i++) {
                $j=$j+1;    
                $monto=$monto+$cliente[$i]["monto"];
                
                $this->Row(array(  
                $j,    
                 
                utf8_decode($cliente[$i]["nombres"].' '.$cliente[$i]["apellidos"] ), 
                  utf8_decode(Captador::descripcion($cliente[$i]["id_captador"])),     
                'S/'.number_format($cliente[$i]["monto"], 2, ',', '.'),     
                ));
            } 
            $this->Cell(120,8,utf8_decode('IMPORTANTE TOTAL ENTREGADO:'),1,0,'L',1);
            $this->Cell(40,8,'S/','LBT',0,'L',1);  
            $this->Cell(30,8,number_format($monto, 2, ',', '.'),'BTR',0,'R',1);  
            
            $this->Ln(20);
            $this->SetFont('Arial','B',10);
            $this->Cell(95,8,utf8_decode('VºBº'),'T',0,'C');
            $this->Cell(95,8,utf8_decode('VºBº'),'T',0,'C');  
            $this->Ln(); 
            $this->SetFont('Arial','B',12);
            $this->Cell(95,8,utf8_decode('JEFE OFICINA'),0,0,'C');
            $this->Cell(95,8,utf8_decode('GERENCIA'),0,0,'C'); 
            $this->Ln(20); 
            
            if(!empty($dataProvider[0]["id_gestor"])){
                $this->SetFont('Arial','B',14);
                $this->Cell(40,10,'',0,0,'C');
                $this->Cell(75,10,'Gestor ',0,0,'C');
		$this->Cell(40,10,Gestor::descripcion($dataProvider[0]["id_gestor"]),0,0,'C');

            }
         
            
            
            $this->Ln(10); 
            $this->Cell(30,8,'',0,0,'C');
            $this->Cell(80,8,'',1,0,'C');
            $this->SetFont('Arial','B',14);
            $this->SetFillColor(133, 193, 233);
            $this->Cell(40,8,'Saldo Anterior',1,0,'L',1);
            $this->SetFont('Arial','',12);
            $this->Cell(40,8,'S/'.number_format($saldo_anterior, 2, ',', '.'),1,0,'R',1);
               
            
            $this->Ln();
            $this->Cell(30,8,'',0,0,'C');
            $this->Cell(80,8,'',1,0,'C');
            $this->SetFont('Arial','B',12);
            $this->Cell(40,8,'Transferencia',1,0,'L',1);
            $this->SetFont('Arial','',12);
            $this->Cell(40,8,'S/'.number_format($transferencia, 2, ',', '.'),1,0,'R',1);
            $this->Ln();
            $this->Cell(30,8,'',0,0,'C');
            $this->Cell(80,8,'',1,0,'C');
            $this->Cell(40,8,'',1,0,'C',1);
            $this->Cell(40,8,'',1,0,'R',1);
            $this->Ln();
            $this->SetFillColor(130, 224, 170);
            $this->Cell(30,8,'',0,0,'C');
            $this->Cell(80,8,'SALDO CAPITAL',1,0,'L',1);
            $this->Cell(40,8,'',1,0,'C');
            $this->SetFont('Arial','',12);
            $this->Cell(10,8,'S/','TBL',0,'R',1);
            $this->Cell(30,8,number_format($saldo_capital, 2, ',', '.'),'TBR',0,'R',1);
            $this->Ln();
            $this->SetFillColor(133, 193, 233);  
            
             
            
            for ($i = 0; $i < $c; $i++) {
                
                if(!empty($dataProvider[$i]["monto1"])){
                  $this->Cell(30,8,'',0,0,'C');
                  $this->Cell(80,8,utf8_decode($dataProvider[$i]["campo1"]),1,0,'L',1);
                  $this->Cell(40,8,'',1,0,'C');
                  $this->Cell(10,8,'S/','TBL',0,'R',1);
                  $this->Cell(30,8,number_format($dataProvider[$i]["monto1"], 2, ',', '.'),'TBR',0,'R',1);
                  $this->Ln();
                  
                }
   
            }   
  
            for ($i = 0; $i < $c; $i++) {
                
                if(!empty($dataProvider[$i]["monto2"])){
                  $this->Cell(30,8,'',0,0,'C');
                  $this->Cell(80,8,utf8_decode($dataProvider[$i]["campo2"]),1,0,'L',1);
                  $this->Cell(40,8,'',1,0,'C');
                  $this->Cell(10,8,'S/','TBL',0,'R',1);
                  $this->Cell(30,8,number_format($dataProvider[$i]["monto2"], 2, ',', '.'),'TBR',0,'R',1);
                  $this->Ln();
                  
                }
   
            }     
              for ($i = 0; $i < $c; $i++) {
                
                if(!empty($dataProvider[$i]["monto3"])){
                  $this->Cell(30,8,'',0,0,'C');
                  $this->Cell(80,8,utf8_decode($dataProvider[$i]["campo3"]),1,0,'L',1);
                  $this->Cell(40,8,'',1,0,'C');
                  $this->Cell(10,8,'S/','TBL',0,'R',1);
                  $this->Cell(30,8,number_format($dataProvider[$i]["monto3"], 2, ',', '.'),'TBR',0,'R',1);
                  $this->Ln();
                  
                }
            }
  for ($i = 0; $i < $c; $i++) {
                
                if(!empty($dataProvider[$i]["monto4"])){
                  $this->SetFillColor(255, 255, 0);
                  $this->Cell(30,8,'',0,0,'C');
                  $this->Cell(80,8,utf8_decode($dataProvider[$i]["campo4"]),1,0,'L',1);
                  $this->Cell(40,8,'',1,0,'C');
                  $this->Cell(10,8,'S/','TBL',0,'R',1);
                  $this->Cell(30,8,number_format($monto4, 2, ',', '.'),'TBR',0,'R',1);
                  $this->Ln();
      }
            }
                  
                  $this->SetFillColor(133, 193, 233);
           
            for ($i = 0; $i < $c; $i++) {
                if(!empty($dataProvider[$i]["monto5"])){
                  $this->Cell(30,8,'',0,0,'C');
                  $this->Cell(80,8,utf8_decode($dataProvider[$i]["campo5"]),1,0,'L',1);
                  $this->Cell(40,8,'',1,0,'C');
                  $this->Cell(10,8,'S/','TBL',0,'R',1);
                  $this->Cell(30,8,number_format($dataProvider[$i]["monto5"], 2, ',', '.'),'TBR',0,'R',1);
                  $this->Ln();
                }
            }
         
            for ($i = 0; $i < $c; $i++) {
                if($dataProvider[$i]["monto6"]>0){
                  $this->Cell(30,8,'',0,0,'C');
                  $this->Cell(80,8,utf8_decode($dataProvider[$i]["campo6"]),1,0,'L',1);
                  $this->Cell(40,8,'',1,0,'C');
                  $this->Cell(10,8,'S/','TBL',0,'R',1);
                  $this->Cell(30,8,number_format($dataProvider[$i]["monto6"], 2, ',', '.'),'TBR',0,'R',1);
                  $this->Ln();
                }
            }
           
            for ($i = 0; $i < $c; $i++) {
                if($dataProvider[$i]["monto7"]>0){
                  $this->Cell(30,8,'',0,0,'C');
                  $this->Cell(80,8,utf8_decode($dataProvider[$i]["campo7"]),1,0,'L',1);
                  $this->Cell(40,8,'',1,0,'C');
                  $this->Cell(10,8,'S/','TBL',0,'R',1);
                  $this->Cell(30,8,number_format($dataProvider[$i]["monto7"], 2, ',', '.'),'TBR',0,'R',1);
                  $this->Ln();
                }
            }
            
            for ($i = 0; $i < $c; $i++) {
                if($dataProvider[$i]["monto8"]>0){
                  $this->Cell(30,8,'',0,0,'C');
                  $this->Cell(80,8,utf8_decode($dataProvider[$i]["campo8"]),1,0,'L',1);
                  $this->Cell(40,8,'',1,0,'C');
                  $this->Cell(10,8,'S/','TBL',0,'R',1);
                  $this->Cell(30,8,number_format($dataProvider[$i]["monto8"], 2, ',', '.'),'TBR',0,'R',1);
                  $this->Ln();
                }
            }
              
            $this->SetFillColor(88, 214, 141);
            $this->Cell(30,8,'',0,0,'C');
            $this->Cell(80,8,'','',0,'L');
            $this->Cell(40,8,'SALDO TOTAL',1,0,'C',1);
            $this->Cell(10,8,'S/','TBL',0,'R',1);
            $this->Cell(30,8,number_format($saldo_total, 2, ',', '.'),'TBR',0,'R',1);
            $this->Ln();
            $this->Cell(30,8,'',0,0,'C');
            $this->Cell(80,8,'','',0,'L');
            $this->Cell(40,8,'EFECTIVO',1,0,'C',1);
            $this->Cell(10,8,'S/','TBL',0,'R',1);
            $this->Cell(30,8,number_format($efectivo, 2, ',', '.'),'TBR',0,'R',1);
            $this->Ln();
            $this->Cell(30,8,'',0,0,'C');
            $this->Cell(80,8,'','',0,'L');
            $this->Cell(40,8,'DEPOSITO',1,0,'C',1);
            $this->Cell(10,8,'S/','TBL',0,'R',1);
            $this->Cell(30,8,number_format($deposito, 2, ',', '.'),'TBR',0,'R',1);
            $this->Ln();
            $this->Cell(30,8,'',0,0,'C');
            $this->Cell(80,8,'','',0,'L');
            $this->Cell(40,8,'TOTAL',1,0,'C',1);
            $this->Cell(10,8,'S/','TBL',0,'R',1);
            $this->Cell(30,8,number_format($total, 2, ',', '.'),'TBR',0,'R',1);
            $this->Ln();
            
        }
        
        
}
            
	
		
?>