<?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 ticket($model,$comercio)
	{ 

            if(!empty($model)){
                
                $this->SetFont('Arial','B',11);
	
                $this->Cell(10,7,'TICKET.:',0,0,'L');
                $this->Cell(40,7,str_pad($model->id_llamada,6,"0",STR_PAD_LEFT),0,0,'C');
              
                $this->Ln();
             
                
                $this->Cell(30,7,'TECNICO RESPONSABLE.:',0,0,'L');
                $this->Cell(100,7,str_pad(utf8_decode(Tecnico::descripcion($model->tecnico)),6,"0",STR_PAD_LEFT),0,0,'C');
              
                $this->Ln();
                
                $this->Ln(10);
                $this->SetFont( 'Arial', 'B', 9);
                $this->SetFillColor(255, 255, 255 ); 
                $this->SetWidths(array(112,72));
                $this->SetAligns(array('L','L'));
                $this->RowS(array(utf8_decode('COMERCIO: ').utf8_decode($comercio->comercio),
                    'AFILIADO.: '. utf8_decode($comercio->afiliado)
                ));
                
                $this->RowS(array(utf8_decode('PERSONA DE CONTACTO: ').utf8_decode($model->persona),
                    'TELEFONO.: '. utf8_decode($model->telefono)
                ));
                
                
                
                $this->RowS(array(utf8_decode('MODELO: ').utf8_decode($comercio->modelo),
                    'TERMINAL.: '.$comercio->terminal
                ));
                
                
                
                $this->RowS(array(utf8_decode('ZONA: ').utf8_decode(Estados::descripcion($comercio->id_estado)),
                    'CIUDAD.: '.  utf8_decode(Ciudades::descripcion($comercio->id_ciudad))
                ));
                
                $this->SetWidths(array(184));
                $this->SetAligns(array('L'));
                
                $this->RowS(array(utf8_decode('DIRECCION: ').utf8_decode($comercio->direccion)
                ));  

                 
        }
}

}
		
	
		
?>