<?php
function PDF_TIT($_ID,$_DAT){
	ejt("INSERT INTO reportes VALUES ($_ID,'T','$_DAT',0)");
}
function PDF_SQL($_ID,$_DAT){
	$_DAT=str_replace("'","*@*",$_DAT);
	$s="INSERT INTO reportes VALUES ($_ID,'S','$_DAT',0)";
	ejt($s);	
}
function PDF_CAM($_ID,$_DAT){
	ejt("INSERT INTO reportes VALUES ($_ID,'C','$_DAT',0)");
}
function PDF_ESP($_ID,$_DAT){
	ejt("INSERT INTO reportes VALUES ($_ID,'E','$_DAT',0)");
}
function PDF_ANC($_ID,$_DAT){
	ejt("INSERT INTO reportes VALUES ($_ID,'A','$_DAT',0)");
}

function PDF_CREATE($_ID){
		$TC=0;
		$_CxL=90;
		$TI=gs("valor","reportes","id=$_ID AND dato='T'");
		$s=gs("valor","reportes","id=$_ID AND dato='S'");
		$s=str_replace("*@*","'",$s);
		$T=gs("valor","reportes","id=$_ID AND dato='C'");
		$E=gs("valor","reportes","id=$_ID AND dato='E'");
		$A=gs("valor","reportes","id=$_ID AND dato='A'");		
				
		$T=explode(",",$T);
		$E=explode(",",$E);
		$A=explode(",",$A);
		$TT=$A;
		$TC=count($T);
		//
		for($f=0;$f<$TC;$f++){
			$TT[$f]=0;
			if($A[$f]==1 || $A[$f]==2)
				$T[$f]=vbr(vbfill(" ",$E[$f]).$T[$f],$E[$f]);
			else
				$T[$f]=vbl($T[$f].vbfill(" ",$E[$f]),$E[$f]);			
		}
		$Z="  ";$StrCAB="";
		for($f=0;$f<$TC;$f++){
			$StrCAB=$StrCAB.$T[$f].$Z;
		}
		//		
		$pdf = new PDF('P','mm',A4);
		$pdf->AddPage();$pdf->SetFont('courier','',10);
		//CEBECERA
		$pdf->Cell(0,4,$TI,0,1);
		$pdf->Cell(0,4,vbfill("=",strlen($TI)),0,1);			
		$pdf->Cell(0,4,"",0,1);	
		$pdf->Cell(0,4,$StrCAB,0,1);	
		$pdf->Cell(0,4,vbfill("-",$_CxL),0,1);	
		$l=5;
		//		
		$result = grs($s);
		while ($row = $result->fetch_row()){
			//
			$l++;$StrLIN="";
			for($f=0;$f<$TC;$f++){			
				if($A[$f]>0){
					$StrLIN=$StrLIN.vbr(vbfill(" ",$E[$f]).$row[$f],$E[$f]).$Z;
					$TT[$f]=$TT[$f] + $row[$f];
				}else{
					$StrLIN=$StrLIN.vbl($row[$f].vbfill(" ",$E[$f]),$E[$f]).$Z;								
				}
			}
			$pdf->Cell(0,4,$StrLIN,0,1);	
			if($l==65){				
				$pdf->Cell(0,4,vbfill("-",$_CxL),0,1);
				//CEBECERA
				$pdf->Cell(0,4,$TI,0,1);
				$pdf->Cell(0,4,vbfill("=",strlen($TI)),0,1);			
				$pdf->Cell(0,4,"",0,1);	
				$pdf->Cell(0,4,$StrCAB,0,1);	
				$pdf->Cell(0,4,vbfill("-",$_CxL),0,1);	
				$l=5;
				//
			}
			//
		}
		if($l!=5){
			$pdf->Cell(0,4,vbfill("-",$_CxL),0,1);	
		}
		//Imprimir totales.
		for($f=0;$f<$TC;$f++){
			$tt=$A[$f];
			if($tt==2){
				$ss=vbl("TOTAL ".trim($T[$f]).vbfill(" ",35),35)
					." : "." ".vbr(vbfill(" ",12).number_format($TT[$f],1,'.',','),12);
				$pdf->Cell(0,4, vbr(vbfill(" ",$_CxL-strlen($ss)).$ss,$_CxL) ,0,1);	
			}
		}
		//
		ejt("DELETE FROM reportes WHERE id<$_ID");
		//
		$pdf->Output("Reporte.pdf");		
		return "Reporte.pdf";
		//
}
?>
