<?php

session_cache_limiter('private');
session_start();
$com = "I";
include("DMMYSQLG.php");
include("DSA3000.php");
include("DMMYSQLG.php");
 include('DF80000.php');
$name="Nomina General.xls"; 
if(isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
	header('Content-Type: application/force-download');
else
	header('Content-Type: application/octet-stream');
header('Content-disposition: attachment; filename="'.$name.'"');
?> 
<table width="945"  align="center">
  <tr>
    <th colspan="7">Nomina General</th>
  </tr>
  <tr>
    <td class="tabagre"><div align="center">NO. NOMINA</div></td>
    <td class="tabagre"><div align="center">NOMBRE</div></td>
    <td class="tabagre"><div align="center">R.F.C.</div></td>
    <td class="tabagre"><div align="center">I.M.S.S.</div></td>
    <td class="tabagre"><div align="center">SUELDO DIA</div></td>
    <td class="tabagre"><div align="center">DEPARTAMENTO</div></td>
    <td class="tabagre"><div align="center">PUESTO</div></td>
  </tr>
  <tr>
    <td colspan="3" class="mentab"><div align="center">PERCEPCIONES</div></td>
    <td colspan="3" class="mentab"><div align="center">DEDUCCIONES</div></td>
    <td class="mentab"><div align="center">NETO</div></td>
  </tr>
  <?php

include("DN34100_CL.php");
include('DF10000.php');
$busproc=mysqli_query($con ,"select * from dnpargen");
$arbusproc=mysqli_fetch_array($busproc);
$nomusu = new clase('dnnomina',$arbusproc["axo_pro"],$arbusproc["mes_pro"],$_POST['setipnom'],$_POST['tenonom2'],-1,$_SESSION['num_usu']);
$norec=0;
if($_REQUEST['ra_imp']=='emp')
	$result=$nomusu->listaElemEmp();
else
	$result=$nomusu->listaElem();
$cchck=$nomusu->conReg();
	for($dfi=0; $dfi<$cchck; $dfi++) 
	{
		$norec++;
		$lab="P";
		$ent=-1;
		$cont=0;
		$cont1=0;
		$cont2=0;
		$totpd=array(0,0);
		//Busquedas de trabajador
		$chboxes=mysqli_fetch_array($result);
		$nomusu->modEmp("$chboxes['no_nom']");
		$noEmp = $nomusu->getEmp();
		$sqbusqx=mysqli_query($con ,"select * from dntrab_a where no_trab = '$noEmp'");
		$arbusq=mysqli_fetch_array($sqbusqx);
		$sqbusqxdat=mysqli_query($con ,"select * from dndatnom where no_nom = '$noEmp'");
		$sqbusqxx=mysqli_query($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($con ,"select * from dncatpue where no_puesto = '".$arbusqxdat['puesto']."'"));
		$busdep=mysqli_fetch_array(mysqli_query($con ,"select * from dncatdep where no_depto = '".$arbusqxdat['depto']."'"));
		
		if($dfi==0) $departamento=$chboxes['depto'];
		if($_REQUEST['ra_imp']=='dep'){
			if($departamento!=$chboxes['depto']){
					$dt_catdep= mysqli_fetch_array(mysqli_query($con ,"select * from dncatdep where no_depto = '$departamento'"));
					echo '<tr ><td colspan = "7"><div align="center"> TOTAL X DEPARTAMENTO </div></td></tr>';
				impTotDet($togendep,$totpddep);
				impTot($totpddep);
				$togendep=NULL;
				$totpddep=NULL;
				echo '<tr class="typeagre"><td colspan = "7"><div align="center">&nbsp;</div></td></tr>';
				$departamento=$chboxes['depto'];
			}
		}
		
		echo '<tr>';
		echo '<td><div align="center">'.$arbusq["no_nom"].'&nbsp;</div></td>';
		if($arbusqxdat['status']=='A') echo '<td><div align="center">'.$arbusq["nom"].' '.$arbusq["ape_pat"].' '.$arbusq["ape_mat"].'&nbsp;</div></td>';
		else echo '<td><div align="center">'.$arbusq["nom"].' '.$arbusq["ape_pat"].' '.$arbusq["ape_mat"].'(Inactivo) &nbsp;</div></td>';
		echo '<td><div align="center">'.$arbusqx["rfc"].'&nbsp;</div></td>';
		echo '<td><div align="center">'.$arbusqx["seg_soc"].'&nbsp;</div></td>';
		echo '<td><div align="center">'.$arbusqxdat["sal_diario"].'&nbsp;</div></td>';
		echo '<td><div align="center">'.$busdep['des_depto'].'&nbsp;</div></dh>';
		echo '<td><div align="center">'.$buspue['des_puesto'].'&nbsp;</div></td>';
		echo '</tr>';
		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"];
			//Guarda Detalles Encontrados y Acumulacion en $togen
			totDet($ent,$adetnom,$valgen,$togen);
						sepDet($ent,$adetnom,$cont1,$cont2,$totpd,$perded);
			if($_REQUEST['ra_imp']=='dep'){
				totDet($ent,$adetnom,$valgen,$togendep);
			}
		}//Termina separa detalles de trabajador per/ded
		impSepDet($cont1,$cont2,$perded);
		impTot($totpd);
		 $perded=NULL;
		 $totpd=NULL;
		echo '<tr><td colspan="7" class="inputcodigo"><div align="center">&nbsp;</div></td></tr>';
	} 
	if($_REQUEST['ra_imp']=='dep'){
		$dt_catdep= mysqli_fetch_array(mysqli_query($con ,"select * from dncatdep where no_depto = '".$chboxes['depto']."'"));
		echo '<tr><td colspan = "7"><div align="center">TOTAL X DEPARTAMENTO</div></td></tr>';
		impTotDet($togendep,$totpddep);
		impTot($totpddep);
		$togendep=NULL;
		$totpddep=NULL;
		echo '<tr class="typeagre"><td colspan = "7"><div align="center">&nbsp;</div></td></tr>';
	}
	
	/*Se a&ntilde;ade pagina para imprimir resumen de detalles con valores totales y neto*/
	echo '<tr><th  colspan="7" ><div align="center">'.'Resumen'.'&nbsp;</div></th></tr>';
	impTotDet($togen,$totpd2);//imprime totales detalles encontrados y guarda valores netos en $totpd2
	impTot($totpd2);
	echo '<tr><td colspan="7" class="inputcodigo"><div align="center">&nbsp;</div></td></tr>';


//FUNCIONES DE IMPRESION y DATOS
function totDet($ent,$adetnom,$valgen,&$togen){//Guarda totales de detalles encontrados
	$dncapxx=mysqli_fetch_array(mysqli_query($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){// Imprime totales detalles encontrados
	$toper=$togen[0];
	$toded=$togen[1];
	if(($cont1=count($toper))>($cont2=count($toded))) $cont=$cont1;
	else $cont=$cont2;
	for($i=0; $i<$cont; $i++){
		echo '<tr>';
		if($cont1>$i){
			list($clave,$des) = each($toper);
			echo '<td><div align="left">'.$des[0].' | '.$des[1].'&nbsp;</div></td>';	
			echo '<td><div align="center">'.number_format($des[2],2,'.',',').'&nbsp;</div></td>';	
			echo '<td><div align="center">'.number_format($des[3],2,'.',',').'&nbsp;</div></td>';	
			$totpd[0] +=$des[3];
		}
		else 
			echo '<td colspan="3"><div align="center">&nbsp;</div></td>';
		if($cont2>$i){
			list($clave2,$des2) = each($toded);
			echo '<td><div align="left">'.$des2[0].' | '.$des2[1].'&nbsp;</div></td>';	
			echo '<td><div align="center">'.number_format($des2[2],2,'.',',').'&nbsp;</div></td>';	
			echo '<td><div align="center">'.number_format($des2[3],2,'.',',').'&nbsp;</div></td>';	
			$totpd[1] +=$des2[3];
		}
		else
			echo '<td colspan="3"><div align="center">&nbsp;</div></td>';
		echo '<td class="inputcodigo"><div align="center">&nbsp;</div></td>';
		echo '</tr>';
	} 
}

function sepDet($ent,$adetnom,&$cont1,&$cont2,&$totpd,&$perded){//Separa Detalles por tipo operacion y actualiza total
	$dncapxx=mysqli_fetch_array(mysqli_query($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($cont1,$cont2,$perded){//Imprime Detalles Encontrados
	if($cont1<=$cont2) $cont=$cont2;
	else $cont=$cont1;
	for($i=0; $i<$cont; $i++){
		echo '<tr>';
		if($cont1>$i){
			echo '<td><div align="left">'.$perded[0][$i][0].' | '.$perded[0][$i][1].'&nbsp;</div></td>';
			echo '<td><div align="center">'.number_format($perded[0][$i][2],2,'.',',').'&nbsp;</div></td>';
			echo '<td><div align="center">'.number_format($perded[0][$i][3],2,'.',',').'&nbsp;</div></td>';
		}
		else 
			echo '<td colspan="3"><div align="center">&nbsp;</div></td>';
		if($cont2>$i){
			echo '<td><div align="left">'.$perded[1][$i][0].' | '.$perded[1][$i][1].'&nbsp;</div></td>';
			echo '<td><div align="center">'.number_format($perded[1][$i][2],2,'.',',').'&nbsp;</div></td>';
			echo '<td><div align="center">'.number_format($perded[1][$i][3],2,'.',',').'&nbsp;</div></td>';
		}
		else
			echo '<td colspan="3"><div align="center">&nbsp;</div></td>';
		echo '<td class="inputcodigo"><div align="center">&nbsp;</div></td>';
		echo'</tr>';
	} 
}

function impTot($totpd){
		echo '<tr>';
		echo '<td colspan="2"><div align="center">'.'TOTAL DE PERCEPCIONES: '.'&nbsp;</div></td>';
		echo '<td><div align="center">'.number_format($totpd[0],2,'.',',').'&nbsp;</div></td>';
		echo '<td colspan="2"><div align="center">'.'TOTAL DE DEDUCCIONES: '.'&nbsp;</div></td>';
		echo '<td><div align="center">'.number_format($totpd[1],2,'.',',').'&nbsp;</div></td>';
		echo '<td><div align="center">'.'NETO: '.''.number_format(($totpd[0]-$totpd[1]),2,'.',',').'&nbsp;</div></td>';		
		echo '</tr>';
}
mysqli_close($con);
?>
</table>
