<?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';
            //Save the current position
            $x=$this->GetX();
            $y=$this->GetY();
            //Draw the border
            $this->Rect($x,$y,$w,$h,'DF');
            //Print the text
            
            if($i==0){
             $this->SetFillColor(255,255,255); 
             $this->SetFont('Arial','',14);  
            
             }elseif ($i==1){
             //DIA
             $this->SetFillColor(249, 231, 159);
             $this->SetFont('Arial','',14);  
             
             }elseif ($i==2){
             //ZONA
             $this->SetFillColor(169, 223, 191);
             $this->SetFont('Arial','',14);  
             
             }elseif ($i==3){
             //ESTATUS
             $this->SetFillColor(255,255,255); 
             $this->SetFont('Arial','B',16);  
             }elseif ($i==4){
                 
                 
             //NOMBRES
             $this->SetFillColor(245, 203, 167 ); 
    
             $this->SetFont('Arial','',14);  
             }elseif ($i==5){
             //APELLIDOS
             $this->SetFillColor(245, 203, 167 ); 
             
          
             $this->SetFont('Arial','B',16);  
             }elseif ($i==6){
             //CELULAR
             $this->SetFillColor(213, 245, 227);
             $this->SetFont('Arial','B',16);
             
             }elseif ($i==7){
                 
             //DURACION
            $this->SetFillColor(248, 216, 221); 
             $this->SetFont('Arial','B',16);  
             
             }elseif ($i==8){
             //PAGOS
             $this->SetFillColor(213, 245, 227); 
             $this->SetFont('Arial','B',16);  
             }elseif ($i==9){
                 
             //SOLES
             $this->SetFillColor(169, 204, 227); 
             $this->SetFont('Arial','B',16); 
             
             }elseif ($i==10){
             //ATRAZOS    
             $this->SetFillColor(248, 216, 221); 
             $this->SetFont('Arial','B',16);  
             }elseif ($i==11){  
             //CREDITO
             $this->SetFillColor(249, 231, 159 ); 
             $this->SetFont('Arial','B',16);  
             }elseif ($i==12){
             //CUOTA 
             $this->SetFillColor(169, 204, 227); 
             $this->SetFont('Arial','B',16);  
             }elseif ($i==13){
              //PAGAR
             $this->SetFillColor(213, 245, 227);
             $this->SetFont('Arial','B',16);   
             }elseif ($i==14){
       
              //DEBE
             $this->SetFillColor(247, 220, 111);  
   
             $this->SetFont('Arial','B',16);   
             }elseif ($i==15){
                 
              //FECHA DE PAGO  
             $this->SetFillColor(255,255,255);  
              $this->SetFont('Arial','B',16);  
             }elseif ($i==16){
                 
              //SALDO  
            $this->SetFillColor(255,255,255);  
               $this->SetFont('Arial','',16);  
             }elseif ($i==17){
              //MODALIDAD  
             $this->SetFillColor(255,255,255); 
             $this->SetFont('Arial','',16);  
             }elseif ($i==18){
                 
               //DIRECCION 
              $this->SetFillColor(214, 234, 248); 
               $this->SetFont('Arial','',16);  
             }elseif ($i==19){
              //MZ 
               $this->SetFillColor(249, 231, 159 );
               $this->SetFont('Arial','',16);  
             }elseif ($i==20){
              //LT  
               $this->SetFillColor(248, 216, 221);
               $this->SetFont('Arial','',16);  
             }elseif ($i==21){
              //REFERENCIA 
               $this->SetFillColor(255,255,255); 
               $this->SetFont('Arial','',16);  
             }elseif ($i==22){
              //AVAL 
               $this->SetFillColor(255,255,255); 
               $this->SetFont('Arial','',16);  
             }
            
            
             
            $this->MultiCell($w,8,$data[$i],0,$a,0);
            //Put the position to the right of the cell
            $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');
            //Print the text
          
             if($i==0){
             $this->SetFont('Arial','B',16);  
             }elseif ($i==1){
             $this->SetFont('Arial','B',16);  
             }elseif ($i==2){
             $this->SetFont('Arial','B',16);  
             }elseif ($i==3){
             $this->SetFont('Arial','B',16);  
             }elseif ($i==4){
             $this->SetFont('Arial','B',16);  
             }elseif ($i==5){
 
             $this->SetFont('Arial','B',16);  
             }elseif ($i==6){

             $this->SetFont('Arial','B',16);  
             }elseif ($i==7){
            
             $this->SetFont('Arial','B',16);  
             }elseif ($i==8){
           
             $this->SetFont('Arial','B',16);  
             }elseif ($i==9){
          
             $this->SetFont('Arial','B',16);  
             }elseif ($i==10){
         
             $this->SetFont('Arial','B',16);  
             }elseif ($i==11){
             
             $this->SetFont('Arial','B',16);  
             }elseif ($i==12){
             
             $this->SetFont('Arial','B',16);  
             }elseif ($i==13){
             
             $this->SetFont('Arial','B',16);  
             }elseif ($i==14){
             
             $this->SetFont('Arial','B',16);  
             }elseif ($i==15){
             
             $this->SetFont('Arial','B',16);  
             }elseif ($i==16){
             
             $this->SetFont('Arial','B',16);  
             }elseif ($i==17){
             
             $this->SetFont('Arial','B',16);  
             }
             
            $this->MultiCell($w,8,$data[$i],0,$a,0);
            //Put the position to the right of the cell
            $this->SetXY($x+$w,$y);
        }
        //Go to the next line
        $this->Ln($h);
    }
    function CheckPageBreak($h){
        //If the height h would cause an overflow, add a new page immediately
        if($this->GetY()+$h>$this->PageBreakTrigger){
            $this->AddPage($this->CurOrientation);
        }
    }

    function NbLines($w,$txt){
        //Computes the number of lines a MultiCell of width w will take
        $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->Image('public/images/logo.jpeg',12,3,30,15);
		$this->SetFont('Arial','B',25);
		$this->SetX(30);
		$this->Cell(120,10,'CARTERA POR GESTOR CANCELADOS',0,0,'C');
		$this->Ln(20);
		$this->SetFont('Arial','B',14);
		$this->SetFillColor(254,249,10);
		$this->SetTextColor(0);
		$this->SetDrawColor(0);
		$this->SetLineWidth(.3);       
 
		$this->SetWidths(array(60,35,40,60,80,80,40,40,35,30,38,30,30,30,30,35,40,120,25,25,120,80,60));
                $this->SetAligns(array('C', 'C','C', 'C', 'C','C', 'C','C','C', 'C', 'C', 'C', 'C', 'C','C', 'C', 'C', 'C', 'C', 'C','C','C','C','C','C'));
   
		$this->Row2(array(  'GESTOR',
                                    'DIA',
                                    'ZONA',
                                    'ESTATUS',
                                    'NOMBRES',
                                    'APELLIDOS',
                                    'CELULAR',
                                    'DURACION',
                                     utf8_decode('Nº PAGOS'),
                                    'SOLES',
                                     utf8_decode('Nº ATRAZOS'),
                                    'CREDITO',
                                    'CUOTA',
                                    'PAGAR',
                                    'DEBE',
                                    'F.PAGO',
                                    'SALDO', 
                                    'DIRECCION', 
                                    'MZ',
                                    'LT',
                                    'NUM.RENOVACION'
                    
                    ));


               
	}

	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 cartera($dataProvider)
	{
            set_time_limit(0); 
            $this->SetTextColor(0);
            $this->SetWidths(array(60,35,40,60,80,80,40,40,35,30,38,30,30,30,30,35,40,120,25,25,120,80,60));
            $this->SetAligns(array('C', 'C', 'C','C','C','C', 'C','C', 'C','C', 'C','C','C','C', 'C','C', 'C', 'C', 'C', 'C', 'C','C','C','C','C'));
            
                  
            $c=count($dataProvider);   
           
            for ($i = 0; $i < $c; $i++) {
                $this->SetFillColor(255,255,255); 
                
           
                
                $this->Row(array(  
       
                utf8_decode($dataProvider[$i]["gestor"]),    
                utf8_decode($dataProvider[$i]["dia_prestamo"]), 
                utf8_decode($dataProvider[$i]["zona"]),  
                utf8_decode($dataProvider[$i]["estatus"]),      
                utf8_decode($dataProvider[$i]["nombre"]),
                utf8_decode($dataProvider[$i]["apellido"]),
                utf8_decode($dataProvider[$i]["celular"]),
                utf8_decode($dataProvider[$i]["id_duracion"]),    
                utf8_decode($dataProvider[$i]["cuotas_pagadas"]),  
                utf8_decode($dataProvider[$i]["total"]),     
                utf8_decode($dataProvider[$i]["atrasos"]),      
                utf8_decode($dataProvider[$i]["id_monto"]), 
                utf8_decode($dataProvider[$i]["cuota"]),    
                utf8_decode($dataProvider[$i]["pagar"]), 
                utf8_decode($dataProvider[$i]["debe"]),      
                $dataProvider[$i]["fecha_ultimo_pago"]=="1970-01-01"?null:Utility::fecha($dataProvider[$i]["fecha_ultimo_pago"]),    
                utf8_decode($dataProvider[$i]["monto_ultimo_pago"]),
                utf8_decode($dataProvider[$i]["direccion"]),    
                utf8_decode($dataProvider[$i]["mz"]),    
                utf8_decode($dataProvider[$i]["lt"]),
                utf8_decode($dataProvider[$i]["num"]),     
                ));
            }        
        }
}

	
		
?>