<?php

include('pdf/fpdf.php');
session_cache_limiter('private');
session_start();
$com = "I";
include("DMMYSQLG.php");
include("DSA3000.php");
include("DMMYSQLG.php");
 
class PDF extends FPDF
{
	var $nomemp;
	//Cabecera de Página
	function Header()
	{
	    //Logotipo
	    //Consulta Datos Empresa
		include("DMMYSQLG.php");
		$sql_encabezado = mysqli_query($GLOBALS['con'] ,"SELECT * FROM dxempres where cla_emp = $_SESSION['empresa']");
		$row_encabezado = mysqli_fetch_array($sql_encabezado);
		
		$cvdm = intval($row_encabezado['cve_delmun']);
		$cves = intval($row_encabezado['cve_estado']);
		
		//Consulta Datos Del/Mun
		$sqld_encabezado = mysqli_query($GLOBALS['con'] ,"SELECT * FROM dxdelmun where cve_delmun = $cvdm");
		$rowd_encabezado = mysqli_fetch_array($sqld_encabezado);
		
		//Consulta Datos Estado 
		$sqle_encabezado = mysqli_query($GLOBALS['con'] ,"SELECT * FROM dxestado where cve_estado = $cves");
		$rowe_encabezado = mysqli_fetch_array($sqle_encabezado);
		
		$this->SetFont('Arial','B',8);
		
		//Empresa
		$this->Cell(180,3,'',0,1,'L');
		$this->Cell(190,3,$row_encabezado['nom_emp'],0,1,'C');
		$this->nomemp=$row_encabezado['nom_emp'];
		//Domicilio
		$this->SetFont('Arial','B',5);
		$tamx=40;
		$this->Cell(180,1,'',0,1,'L');
		$this->Cell($tamx,1,'Domicilio.',0,0,'R');
		$this->MultiCell(140,1,' '.$row_encabezado['calle'].' '.$row_encabezado['no_ext'].' '.$row_encabezado['no_int'],0,1,'L');
		$this->Cell(180,1,'',0,1,'L');
		$this->Cell($tamx,1,'Colonia.',0,0,'R');
		$this->MultiCell(140,1,' '.$row_encabezado['des_col'].' Del-Mun. '.$rowd_encabezado['des_delmun'].' Edo. '.$rowe_encabezado['des_estado'],0,1,'L');
		
		//Telefonos
		$this->Cell(180,1,'',0,1,'L');
		$this->Cell($tamx,1,'C.P.',0,0,'R');
		$this->Cell(140,1,' '.$row_encabezado['cod_pos'].' Telefono: '.$row_encabezado['telefono01'].' Fax: '.$row_encabezado['fax01'],0,1,'L');
		
		//Registro Federal de Comtribuyentes
		$this->Cell(180,1,'',0,1,'L');
		$this->Cell($tamx,1,'R.F.C.',0,0,'R');
		$this->Cell(140,1,' '.$row_encabezado['rfc'],0,1,'L');
		$this->Cell($tamx,1,'',0,1,'R');
		$this->Cell($tamx,1,'Fecha:',0,0,'R');
		$this->Cell(25,1,date("d/m/Y"),0,0,'');
		$this->Cell(15,1,'Hora:',0,0,'R');
		$this->Cell(25,1,date('H:i:s'),0,0,'');
		// ---------------IMAGEN--------------- //
		$logo='logos/'.$_SESSION['base'].'.jpg';
		$this->Image($logo,10,13,($tamx-10),15);
		$this->Ln(5);
		//include("DYIM_VER.php");
	    //Arial Bold (14)
		include("DMMYSQLG.php");
	    $this->SetFont('Arial','B',7);
	    //Espacio
		$this->Cell(190,3,'NOMINA GENERAL',0,1,'C');
		$this->SetFillColor(230,230, 230);
		$this->SetFont('Arial','',7);
		$nonom=$_POST['tenonom2'];
		$periodo=$_POST['tefecini'].' al '.$_POST['tefecfin'];
		$sqtipnom=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select * from dntipnom where cve_nomi = '".$_POST['setipnom']."'"));
		$this->Cell(40,3,'',0,0,'L');
		$this->Cell(50,3,'TIPO NOMINA',1,0,'C',1);
		$this->Cell(30,3,'PERIODO',1,0,'C',1);
		$this->Cell(30,3,'FECHA',1,1,'C',1);
		$this->Cell(40,3,'',0,0,'L');
		$this->Cell(50,3,$sqtipnom["des_nomi"],1,0,'C');
		$this->Cell(30,3,$nonom,1,0,'C');
		$this->Cell(30,3,$periodo,1,1,'C');
		$this->SetFont('Arial','',5);
		$this->Cell(15,3,'NO. NOMINA',1,0,'L',1);
		$this->Cell(50,3,'NOMBRE',1,'0','L',1);
		$this->Cell(20,3,'R.F.C.',1,0,'L',1);
		$this->Cell(20,3,'I.M.S.S.',1,'0','L',1);
		$this->Cell(20,3,'SUELDO DIA',1,'0','L',1);
		$this->Cell(35,3,'DEPARTAMENTO',1,0,'L',1);
		$this->Cell(33,3,'PUESTO',1,1,'L',1);
		$this->Cell(88,3,'PERCEPCIONES',1,0,'C',1);
		$this->Cell(88,3,'DEDUCCIONES',1,0,'C',1);
		$this->Cell(17,3,'NETO',1,1,'C',1);
	    //Salto de línea
	    $this->Ln(3);
	}
	//Pie de Página
	function Footer()
	{
	    //Posición: 1,5 cm del Final
	    $this->SetY(-15);
	    //Arial Italic 8
	    $this->SetFont('Arial','I',5);
	    //Número de página
	    $this->Cell(0,0, $this->PageNo(),0,0,'C');
	}
	
	function empresa(){
	return $this->nomemp;
	}
}
include("DN54100_CL.php");
include('DF10000.php');
$arrhoja=array(215,280);
$pdf=new PDF('P','mm',$arrhoja);
$m=substr($_SESSION['m_a'],0,2);
$a=substr($_SESSION['m_a'],2,4);
$nomusu = new clase('dnnomina',$a,$m,$_POST['setipnom'],$_POST['tenonom2'],-1,$_SESSION['num_usu']);
$norec=0;
$pdf->AddPage();
if($_REQUEST['ra_imp']=='emp')
	$result=$nomusu->listaElemEmp();
else
	$result=$nomusu->listaElem();
$cchck=$nomusu->conReg();
$pdf->SetFillColor(230,230, 230);

$pdf->SetFont('Times','',6);
for($dfi=0; $dfi<$cchck; $dfi++) 
{
	$norec++;
	$lab="P";
	$ent=-1;
	$cont=0;
	$cont1=0;
	$cont2=0;
	$totpd=array(0,0);
	//Busquedas de c/trbajador
	$chboxes=mysqli_fetch_array($result);
	$nomusu->modEmp("$chboxes['no_nom']");
	$noEmp = $nomusu->getEmp();
	$sqbusqx=mysqli_query($GLOBALS['con'] ,"select * from dntrab_a where no_trab = '$noEmp'");
	$arbusq=mysqli_fetch_array($sqbusqx);
	$sqbusqxdat=mysqli_query($GLOBALS['con'] ,"select * from dndatnom where no_nom = '$noEmp'");
	$sqbusqxx=mysqli_query($GLOBALS['con'] ,"select * from dntrab_b where no_nom = '$noEmp'");
	$detnom=$nomusu->listaBusq();
	$arbusqxdat=mysqli_fetch_array($sqbusqxdat);
	$arbusqx=mysqli_fetch_array($sqbusqxx);
	$buspue=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select * from dncatpue where no_puesto = '".$arbusqxdat['puesto']."'"));
	$busdep=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select * from dncatdep where no_depto = '".$arbusqxdat['depto']."'"));
	
	$pdf->SetFillColor(230,230, 230);
		
	if($dfi==0) $departamento=$chboxes['depto'];
	if($_POST['ra_imp']=='dep'){
		if($departamento!=$chboxes['depto']){
				$dt_catdep= mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select * from dncatdep where no_depto = '$departamento'"));
			$pdf->Cell(193,3,' TOTAL '.$dt_catdep['des_depto'],0,1,'C',1);
			impTotDet($togendep,$totpddep,$pdf);
			impTot($totpddep,$pdf);
			$togendep=NULL;
			$totpddep=NULL;
			$pdf->Cell(193,1.5,' ',0,1,'C');
			$pdf->Cell(193,0.3,' ',0,1,'C',1);
			$departamento=$chboxes['depto'];
		}
	}
		
	$pdf->Cell(15,3,$arbusq["no_nom"],0,0,'L');
	if($arbusqxdat["status"]=='A') $pdf->Cell(50,3,$arbusq["nom"].' '.$arbusq["ape_pat"].' '.$arbusq["ape_mat"],0,'0','L');
	else
		$pdf->Cell(50,3,$arbusq["nom"].' '.$arbusq["ape_pat"].' '.$arbusq["ape_mat"].'(Inactivo)',0,'0','L');
	$pdf->Cell(20,3,$arbusqx["rfc"],0,0,'L');
	$pdf->Cell(20,3,$arbusqx["seg_soc"],0,'0','L');
	$pdf->Cell(20,3,$arbusqxdat["sal_diario"],0,'0','L');
	$pdf->Cell(35,3,$busdep['des_depto'],0,0,'L');
	$pdf->Cell(33,3,$buspue['des_puesto'],0,1,'L');
	$pdf->SetFillColor(0,0,0);
	while($adetnom=mysqli_fetch_array($detnom)){//Inicia separa detalles de trabajador per/ded
		if($ent==-1) $ent=0;
		if($adetnom["operac"]!=$lab){
			$lab=$adetnom["operac"];
			$ent=1;
		}
		$valgen=$adetnom["cve_mae"];
		totDet($ent,$adetnom,$valgen,$togen);
		sepDet($ent,$adetnom,$cont1,$cont2,$totpd,$perded);
		if($_POST['ra_imp']=='dep'){
				totDet($ent,$adetnom,$valgen,$togendep);
		}
		//Guarda valores parciales
	}//Termina separa detalles de trabajador per/ded
	impSepDet($GLOBALS['con']t1,$cont2,$perded,$pdf);
	impTot($totpd,$pdf);
	$pdf->Cell(193,1,'',0,1,'C');
} 
$pdf->SetFillColor(230,230, 230);
if($_POST['ra_imp']=='dep'){
		$dt_catdep= mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select * from dncatdep where no_depto = '".$chboxes['depto']."'"));
		$pdf->Cell(193,3,' TOTAL '.$dt_catdep['des_depto'],0,1,'C',1);
		impTotDet($togendep,$totpddep,$pdf);
		impTot($totpddep,$pdf);
		$togendep=NULL;
		$totpddep=NULL;
		$departamento=$chboxes['depto'];
}
/*Se añade pagina para imprimir resumen de detalles con valores totales y neto*/
$pdf->AddPage();
$pdf->Cell(190,3,'RESUMEN NOMINA ACUMULADA',0,1,'C');
impTotDet($togen,$totpd,$pdf);
$pdf->Cell(193,0.3,' ',0,1,'C',1);
$pdf->Cell(193,1,' ',0,1,'C');
impTot($totpd,$pdf);
$pdf->Cell(193,1,'',0,1,'C');
$pdf->Output();

//FUNCIONES DE IMPRESION y DATOS
function totDet($ent,$adetnom,$valgen,&$togen){//Guarda totales de detalles encontrados
	$dncapxx=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select * from dncatper where no_pd = '".$adetnom['no_pd']."'  and operac='".$adetnom['operac']."' order by operac desc"));
	//Guarda valores totales
	$togen[$ent][$valgen][0]=$adetnom["no_pd"];
	$togen[$ent][$valgen][1]=$dncapxx["des_perc"];
	$togen[$ent][$valgen][2] +=$adetnom["can_mov"];
	$togen[$ent][$valgen][3] +=$adetnom["imp_mov"];
}

function impTotDet($togen,&$totpd,&$pdf){// Imprime totales detalles encontrados
	$totpd[0]=0.00;
	$totpd[1]=0.00;
	$toper=$togen[0];
	$toded=$togen[1];
	if(($GLOBALS['con']t1=count($toper))>($GLOBALS['con']t2=count($toded))) $cont=$cont1;
	else $cont=$cont2;
	for($i=0; $i<$cont; $i++){
		if($GLOBALS['con']t1>$i){
			list($clave,$des) = each($toper);
			$pdf->Cell(10,3,$des[0],0,0,'C');
			$pdf->Cell(48,3,$des[1],0,0,'L');
			$pdf->Cell(12,3,number_format($des[2],2,'.',','),0,0,'L');
			$pdf->Cell(15,3,number_format($des[3],2,'.',','),0,0,'L');
			$totpd[0] +=$des[3];
		}
		else 
			$pdf->Cell(85,3,'',0,0,'C');
		$pdf->Cell(5,3,'',0,0,'C');
		if($GLOBALS['con']t2>$i){
			list($clave2,$des2) = each($toded);
			$pdf->Cell(10,3,$des[0],0,0,'C');
			$pdf->Cell(48,3,$des2[1],0,0,'L');
			$pdf->Cell(12,3,number_format($des2[2],2,'.',','),0,0,'L');
			$pdf->Cell(15,3,number_format($des2[3],2,'.',','),0,1,'L');
			$totpd[1] +=$des2[3];
		}
		else
			$pdf->Cell(85,3,'',0,1,'C');
	} 
}

function sepDet($ent,$adetnom,&$cont1,&$cont2,&$totpd,&$perded){//Separa Detalles por tipo operacion y actualiza total
	$dncapxx=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select * from dncatper where no_pd = '".$adetnom['no_pd']."'  and operac='".$adetnom['operac']."' order by operac desc"));
	if($ent==0){ 
		$perded[$ent][$cont1][0]=$adetnom["no_pd"];
		$perded[$ent][$cont1][1]=$dncapxx["des_perc"];
		$perded[$ent][$cont1][2]=$adetnom["can_mov"];
		$perded[$ent][$cont1][3]=$adetnom["imp_mov"];
		$totpd[$ent] +=$adetnom["imp_mov"];
		$cont1++;
	}
	elseif($ent==1){ 
		$perded[$ent][$cont2][0]=$adetnom["no_pd"];
		$perded[$ent][$cont2][1]=$dncapxx["des_perc"];
		$perded[$ent][$cont2][2]=$adetnom["can_mov"];
		$perded[$ent][$cont2][3]=$adetnom["imp_mov"];
		$totpd[$ent] +=$adetnom["imp_mov"];
		$cont2++;
	}
}


function impSepDet($GLOBALS['con']t1,$cont2,$perded,&$pdf){//Imprime Detalles Encontrados
	if($GLOBALS['con']t1<=$cont2) $cont=$cont2;
	else $cont=$cont1;
	for($i=0; $i<$cont; $i++){//Inicia impresion de detalles de trabajador
		if($GLOBALS['con']t1>$i){
			$pdf->Cell(10,3,$perded[0][$i][0],0,0,'C');
			$pdf->Cell(48,3,$perded[0][$i][1],0,0,'L');
			$pdf->Cell(12,3,number_format($perded[0][$i][2],2,'.',','),0,0,'L');
			$pdf->Cell(15,3,number_format($perded[0][$i][3],2,'.',','),0,0,'L');
		}
		else 
			$pdf->Cell(85,3,'',0,0,'C');
		$pdf->Cell(5,3,'',0,0,'C');
		if($GLOBALS['con']t2>$i){
			$pdf->Cell(10,3,$perded[1][$i][0],0,0,'C');
			$pdf->Cell(48,3,$perded[1][$i][1],0,0,'L');
			$pdf->Cell(12,3,number_format($perded[1][$i][2],2,'.',','),0,0,'L');
			$pdf->Cell(15,3,number_format($perded[1][$i][3],2,'.',','),0,1,'L');
		}
		else
			$pdf->Cell(85,3,'',0,1,'C');
	} //Termina impresion de detalles de trabajador
}

function impTot($totpd,&$pdf){
	$pdf->Cell(70,3,'TOTAL DE PERCEPCIONES: ',0,0,'R',0);
	$pdf->Cell(15,3,number_format($totpd[0],2,'.',','),0,0,'L');
	$pdf->Cell(5,3,'',0,0,'C');
	$pdf->Cell(70,3,'TOTAL DE DEDUCCIONES: ',0,0,'R',0);		
	$pdf->Cell(15,3,number_format($totpd[1],2,'.',','),0,0,'L');
	$pdf->Cell(5,3,'',0,0,'R',0);	
	$pdf->Cell(5,3,'NETO: ',0,0,'R',0);		
	$pdf->Cell(8,3,number_format(($totpd[0]-$totpd[1]),2,'.',','),0,1,'L');
}
?> 