<?php

$pdf=new PDF('P','mm','letter');
$pdf->SetMargins(6,0,12);
$pdf->SetAutoPageBreak(true,5);
$pdf->AddFont('verdana');
$pdf->SetFont('verdana','',7);
$pdf->AddPage();

$expFecFac=explode('/',chfecha($dt_encfac['fecFac'],'d/m/Y','d/M/Y'));
$dt_encfac['fecFacDay']=$expFecFac[0];
$dt_encfac['fecFacMonth']=$expFecFac[1];
$dt_encfac['fecFacYear']=$expFecFac[2];

/*Ajustes Y y X*/
$yAdj=-3;
$xAdj=1;
/***************/

/*Datos Factura*/
$posData['tipFac']['print']=false;
$posData['tipFac']['x']=$xAdj;           $posData['tipFac']['y']=$yAdj;
$posData['numFac']['print']=true;
$posData['numFac']['x']=$xAdj+190;       $posData['numFac']['y']     =$yAdj+49;
$posData['fecFac']['x']=$xAdj;           $posData['fecFac']['y']     =$yAdj;
$posData['fecFac']['D']['x']=$xAdj+100;  $posData['fecFac']['D']['y']=$yAdj+57;
$posData['fecFac']['M']['x']=$xAdj+125;  $posData['fecFac']['M']['y']=$yAdj+57;
$posData['fecFac']['Y']['x']=$xAdj+187;  $posData['fecFac']['Y']['y']=$yAdj+57;
/************************/
/*Datos Cliente*/
$posData['idCli']['print']=false;
$posData['idCli']['x']    =$xAdj;     $posData['idCli']['y']    =$yAdj;
$posData['nomCli']['x']   =$xAdj+35;  $posData['nomCli']['y']   =$yAdj+63;
$posData['calleCli']['x'] =$xAdj+35;  $posData['calleCli']['y'] =$yAdj+69;//Desgloce en calle, Num. Ext y Num. Int.
$posData['dirCli']['x']   =$xAdj+35;  $posData['dirCli']['y']   =$yAdj+72;//Desgloce en colonia, c. p. y del/municipio
$posData['desEdoCli']['x']=$xAdj+35;  $posData['desEdoCli']['y']=$yAdj+79;
$posData['rfcCli']['x']   =$xAdj+130; $posData['rfcCli']['y']   =$yAdj+79;
/************************/
/*Detalle Producto*/
$posData['canProd']['x']=$xAdj+12;  $posData['canProd']['y']=$yAdj+97;
$posData['desProd']['x']=$xAdj+33;  $posData['desProd']['y']=$yAdj+97;
$posData['preUni']['x'] =$xAdj+150; $posData['preUni']['y'] =$yAdj+97;
$posData['impProd']['x']=$xAdj+174; $posData['impProd']['y']=$yAdj+97;
/************************/
/*Datos de Totales*/
$posData['subFac']['x']   =$xAdj+173; $posData['subFac']['y']   =$yAdj+224;
$posData['ivaFac']['x']   =$xAdj+173; $posData['ivaFac']['y']   =$yAdj+236;
$posData['totFac']['x']   =$xAdj+173; $posData['totFac']['y']   =$yAdj+243;
$posData['totLetFac']['x']=$xAdj+15;  $posData['totLetFac']['y']=$yAdj+230;
/***********************/

/*Impresion Datos Factura*/
if($posData['tipFac']['print']){
	$pdf->SetXY($posData['tipFac']['x'],$posData['tipFac']['y']);
	$pdf->Cell(5,4,$dt_encfac['tipFac'],0,0,'L',0);
}
if($posData['numFac']['print']){
	$pdf->SetXY($posData['numFac']['x'],$posData['numFac']['y']);
	$pdf->Cell(17,4,$dt_encfac['numFac'],0,0,'L',0);
}
$pdf->SetXY($posData['fecFac']['D']['x'],$posData['fecFac']['D']['y']);
$pdf->Cell(25,4,$dt_encfac['fecFacDay'],0,0,'L',0);
$pdf->SetXY($posData['fecFac']['M']['x'],$posData['fecFac']['M']['y']);
$pdf->Cell(25,4,$dt_encfac['fecFacMonth'],0,0,'L',0);
$pdf->SetXY($posData['fecFac']['Y']['x'],$posData['fecFac']['Y']['y']);
$pdf->Cell(25,4,$dt_encfac['fecFacYear'],0,0,'L',0);
/**************************/

/*Impresion Datos Cliente*/
if($posData['idCli']['print']){
	$pdf->SetXY($posData['idCli']['x'],$posData['idCli']['y']);
	$pdf->Cell(25,4,$dt_encfac['idCli'],0,0,'L',0);
}
$pdf->SetXY($posData['nomCli']['x'],$posData['nomCli']['y']);
$pdf->Cell(150,4,trim($dt_encfac['nomCli']),0,0,'L',0);
$pdf->SetXY($posData['calleCli']['x'],$posData['calleCli']['y']);
$pdf->Cell(150,4,$dt_encfac['calleCli'].' '.trim($dt_encfac['numExtCli'].' '.$dt_encfac['numIntCli']),0,0,'L',0);
$pdf->SetXY($posData['dirCli']['x'],$posData['dirCli']['y']);
$pdf->Cell(150,4,$dt_encfac['desColCli'].' C.P. '.$dt_encfac['codPosCli'].' '.$dt_encfac['desMunCli'],0,0,'L',0);
$pdf->SetXY($posData['desEdoCli']['x'],$posData['desEdoCli']['y']);
$pdf->Cell(70,4,$dt_encfac['desEdoCli'],0,0,'L',0);
$pdf->SetXY($posData['rfcCli']['x'],$posData['rfcCli']['y']);
$pdf->Cell(70,4,$dt_encfac['rfcCli'],0,0,'L',0);
/************************/

/*Impresion Detalle*/
$tamXcanProd=$posData['desProd']['x']-$posData['canProd']['x'];
$tamXdesProd=$posData['preUni']['x']-$posData['desProd']['x'];
$tamXpreUni=$posData['impProd']['x']-$posData['preUni']['x'];
$tamXimpProd=$pdf->GetPageWidth()-$posData['impProd']['x']-$pdf->GetRMargin();
$pdf->SetXY($posData['canProd']['x'],$posData['canProd']['y']);
$pdf->SetWidths(array($tamXcanProd,$tamXdesProd,$tamXpreUni,$tamXimpProd));
$pdf->SetDrawColor(255);
$pdf->SetAligns(array('R','L','R','R'));
while($dt_detfac=mysqli_fetch_array($sq_detfac)){
	$pdf->Row(array(number_format($dt_detfac['canDet'],2,'.',','),$dt_detfac['desTipNot'],number_format($dt_detfac['preUni'],2,'.',','),number_format($dt_detfac['subDet'],2,'.',',')));
	if(trim($dt_detfac['obsOpe'])!=""){
		$pdf->SetX($posData['desProd']['x']);
		$pdf->MultiCell($tamXdesProd,3,$dt_detfac['obsOpe'],0,'J');
	}
	$pdf->SetX($posData['canProd']['x']);
	$subFac +=$dt_detfac['subDet'];
	$ivaFac +=$dt_detfac['ivaDet'];
	$totFac +=$dt_detfac['totDet'];
	
}
/******************/


/*Impresion Totales Factura*/
include("DF10000.php");
$can=number_format($totFac,2,'.','');
$parTot=explode(".","$can");
if($parTot[1]=="0" || $parTot[1]==''){
	$parTot[1]='00';
}
$pre_let=num2letras(number_format($parTot[0], 2, '.', ''));
$pdf->SetXY($posData['totLetFac']['x'],$posData['totLetFac']['y']);
$pdf->Cell(140,5,$pre_let.' PESOS '."$parTot[1]".'/100 M.N.',0,0,'L');

$pdf->SetXY($posData['subFac']['x'],$posData['subFac']['y']);
$pdf->Cell(30,4,number_format($subFac,2,'.',','),0,0,'R',0);
$pdf->SetXY($posData['ivaFac']['x'],$posData['ivaFac']['y']);
$pdf->Cell(30,4,number_format($ivaFac,2,'.',','),0,0,'R',0);
$pdf->SetXY($posData['totFac']['x'],$posData['totFac']['y']);
$pdf->Cell(30,4,number_format($totFac,2,'.',','),0,0,'R',0);
/******************/

//if(!$_POST['imp_fac_cop'])
	//$pdf->AutoPrint(true);
$pdf->Output("fac"."$dt_encfac['numFac'].pdf",'D');
?>