<?php

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

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

    function SetWidths($w){
        $this->widths=$w;
    }

    function SetAligns($a){
        $this->aligns=$a;
    }

    function Rows($data){
        $nb=0;
        for($i=0;$i<count($data);$i++){
            $nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));
        }
        $h=9*$nb;
        $this->CheckPageBreak($h);
        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);
        }
        $this->Ln($h);
    }
    
    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=8*$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
            $this->MultiCell($w,6,$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',10,10,40,25);    
	}

	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  '.date('d/m/Y').' por el Usuario '.$session['login'].'  |  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 encuesta($model,$comercio)
	{ 

            if(!empty($model)){
                 $this->Ln(10);
                $this->SetFont('Arial','B',11);
	
                $this->Cell(180,7,'ENCUESTA FACTURACION CERO CLIENTES BANPLUS',0,0,'C');
                
                $this->Ln(20);
               
                $this->SetFont( 'Arial', '', 9);
                $this->SetFillColor(255, 255, 255 ); 
                $this->SetWidths(array(112,72));
                $this->SetAligns(array('L','L'));
                $this->RowS(array(utf8_decode('NOMBRE DEL COMERCIO: ').utf8_decode($comercio->comercio),
                    'RIF.: '. utf8_decode($comercio->rif)
                ));
                
                $this->SetWidths(array(112,72));
                $this->SetAligns(array('L','L'));
                $this->RowS(array(utf8_decode('RUBRO: ').utf8_decode($model->rubro),
                    'ZONA: '. utf8_decode(Estados::descripcion($comercio->id_estado))
                ));
                
                
                
                $this->SetWidths(array(184));
                $this->SetAligns(array('L'));
                $this->RowS(array(utf8_decode('TECNICO: ').utf8_decode($model->tecnico)
                ));
                
                
                
                
                
                $this->Ln(10);
                $this->SetFont( 'Arial', '', 11);
                $this->Cell(190,7,utf8_decode('A continuación, se presenta el motivo por el cual, el comercio antes mencionado no está realizando'),0,0,'J');
                
                
                $this->Ln(7);
                
                $this->Cell(190,7,utf8_decode('transacciones a través del punto de venta:'),0,0,'J');
                
                $this->Ln(20);
                $this->Cell(40,7,utf8_decode(''),0,0,'J');
                $this->Cell(7,7,utf8_decode('#'),1,0,'C');
                $this->Cell(90,7,utf8_decode('MOTIVO'),1,0,'C');
                $this->Cell(7,7,utf8_decode(''),1,0,'J');
                $this->Ln(7);
                
                //validar si fueron marcados las siguientes preguntas
                
                
                $cerrado=$model->cerrado!='0'?'X':'';
                $imposibilidad=$model->imposibilidad!='0'?'X':'';
                $descontento=$model->descontento!='0'?'X':'';
                $problemas=$model->problemas!='0'?'X':'';
                $equipo=$model->equipo!='0'?'X':'';
                $otros=$model->otros!='0'?'X':'';
                
                
               
                $this->Cell(40,7,utf8_decode(''),0,0,'J');
                $this->Cell(7,7,utf8_decode('1'),1,0,'C');
                $this->Cell(90,7,utf8_decode('Cerrado por Pandemia'),1,0,'J');
                $this->Cell(7,7,utf8_decode($cerrado),1,0,'C');
                $this->Ln(7);
                $this->Cell(40,7,utf8_decode(''),0,0,'J');
                $this->Cell(7,7,utf8_decode('2'),1,0,'C');
                $this->Cell(90,7,utf8_decode('Imposibilidad de pagar la Mensualidad	'),1,0,'J');
                $this->Cell(7,7,utf8_decode($imposibilidad),1,0,'C');
                $this->Ln(7);
                $this->Cell(40,7,utf8_decode(''),0,0,'J');
                $this->Cell(7,7,utf8_decode('3'),1,0,'C');
                $this->Cell(90,7,utf8_decode('Descontento con el Banco Banplus'),1,0,'J');
                $this->Cell(7,7,utf8_decode($descontento),1,0,'C');
                $this->Ln(7);
                $this->Cell(40,7,utf8_decode(''),0,0,'J');
                $this->Cell(7,7,utf8_decode('4'),1,0,'C');
                $this->Cell(90,7,utf8_decode('Problemas de conectividad / Señal en la zona'),1,0,'J');
                $this->Cell(7,7,utf8_decode($problemas),1,0,'C');
                $this->Ln(7);
                $this->Cell(40,7,utf8_decode(''),0,0,'J');
                $this->Cell(7,7,utf8_decode('5'),1,0,'C');
                $this->Cell(90,7,utf8_decode('Equipo dañado'),1,0,'J');
                $this->Cell(7,7,utf8_decode($equipo),1,0,'C');
                 $this->Ln(7);
                $this->Cell(40,7,utf8_decode(''),0,0,'J');
                $this->Cell(7,7,utf8_decode('6'),1,0,'C');
                $this->Cell(90,7,utf8_decode('Otros'),1,0,'J');
                $this->Cell(7,7,utf8_decode($otros),1,0,'C');
                $this->Ln(20);
                
                $this->SetFillColor(255, 255, 255 ); 
                $this->SetWidths(array(180));
                $this->SetAligns(array('J'));
                $this->Row(array(utf8_decode('OBSERVACION: ').utf8_decode($model->observacion)
                ));
                
                
                
                
                
                

                 
        }
}

}
		
	
		
?>