<?php
 
session_start();
include("DMMYSQLG.php");
include("DSA1000.php");
include("DMMYSQLG.php");
include('DF30000.inc');
?>
<?php
 
//busquedas
	if(isset($_POST['setipnom'])){
		//$m=substr($_SESSION['m_a'],0,2);
		$a=$_SESSION['axo_pro'];
		//$fecproc=$arbusproc["axo_pro"].'-'.$arbusproc["mes_pro"].'-01';
		//$busqcal="select * from dncageno where cve_nomi = '".$_POST['setipnom']."' and axo_mes='$fecproc'";
		//$busq="select * from dndatnom where tip_nom ='".$_POST['setipnom']."' and status ='A'";
	}
	else{
		$busq=" ";
	}
	//$sqbusq=mysqli_query($con ,$busq);
	$sqbusqcal=mysqli_query($con ,$busqcal);
	while(@$arbusqw=mysqli_fetch_array($sqbusqcal)) if($arbusqw['act_nom']=="NO") break;
	
	$fec1 = $arbusqw['fec_ini'];
	include('DF20000.php');
	$fecha_inicial = $f_f;
	$fec1 = $arbusqw['fec_fin'];
	include('DF20000.php');
	$fecha_final = $f_f;
	if(!isset($_POST['ra_imp'])) $_REQUEST['ra_imp']='emp';
?>
<html>
<link rel="stylesheet" href="css/estilo1.css" type="text/css"/>
<script type="text/javascript" language="JavaScript1.2" src="DSSCRIPT.js"></script>

<body>
<?php
 
$ayuda = 'DN6200A_PR.php';
include("DYAYUDA.php");
?>
<div align="center">
<form name="form1" id="form1" method="post" action="">
  <table  align="center">
    <tr>
      <th colspan="2">REPORTE NOMINA ACUMULADA  </th>
    </tr>
    <tr>
      <th colspan="2">        Tipo de Nomina</th>
      </tr>
	<tr>
	  <td colspan="2" class="inputcodigo"><div align="center">
	    <select name="setipnom" id="setipnom" onChange="javascript: document.getElementById('form1').submit()">
	      <option value="-1">--Seleccione Nomina--</option>
	      <?php
  
		  $stipnom="select * from dntipnom";
		  $sqtipnom=mysqli_query($con ,$stipnom);
		  while($artipnom=mysqli_fetch_array($sqtipnom)){
		  	if("$_POST['setipnom']"=="$artipnom['cve_nomi']") echo '<option value="'.$artipnom['cve_nomi'].'" selected>'.$artipnom['des_nomi'].'</option>';
		  	else echo '<option value="'.$artipnom['cve_nomi'].'">'.$artipnom['des_nomi'].'</option>';
		  }
		  ?>
		  </select>
	    </div>	    <div align="center">
	      
          </div></td>
	  </tr>
	    <tr>
		  <th colspan="2">Reporte</th>
	  </tr>
		<tr>
          <th> 
            <input name="ra_imp" type="radio" value="emp" onClick="javascript: document.getElementById('form1').submit()" 
	  <?php
 
			if($_REQUEST['ra_imp']=="emp")
				echo 'checked';
		?> >
            Por Empleado </th>
	      <th> 
            <input name="ra_imp" type="radio" value="dep" onClick="javascript: document.getElementById('form1').submit()" 
	  <?php
 
			if($_REQUEST['ra_imp']=="dep")
				echo 'checked';
		?> >
	        Por Departamento </th>
      </tr>
	    <tr>
	      <th colspan="2"><div align="center">A&ntilde;o deNomina </div></th>
      </tr>
	    <tr>
	      <td colspan="2" class="typeagre"><div align="center">
		  <?php
 
		  	echo $_SESSION['axo_pro'];
		  ?>
		  </div></td>
      </tr>
      <tr>
        <td colspan="2" class="inputcodigo">
		    <div align="center">
		      <p>
			  <input name="tenonom" type="hidden" id="tenonom"  readonly value="<?php
 echo $arbusqw['no_nomi'];?>">
			  <input name="tefecini" type="hidden" class="thcons" id="tefecini" size="30"  readonly
			  <?php
 
			  if(isset($_POST['tefecini'])) echo 'value="'.$fecha_inicial.'"'; 
			 ?>
			  >
			  <input name="tefecfin" type="hidden" class="thcons" id="tefecfin" size="30" readonly
				<?php
 
			  if(isset($_POST['tefecfin'])) echo 'value="'.$fecha_final.'"'; 
			  ?>
				>
		      </p>
      </div></td></tr>
      </table>
</form>
<form name="form2" method="post" action="">
</form>
</div>
<div align="center">
  <form name="frm1" id="frm1" method="post" action="" >
   <input type="hidden" name="setipnom" id="setipnom" value="<?php
 echo $_POST['setipnom'];?>">
   <input name="tenonom2" type="hidden" id="tenonom2"   value="<?php
 echo $arbusqw['no_nomi'];?>">
    <input name="ra_imp" type="hidden" id="ra_imp"   value="<?php
 echo $_REQUEST['ra_imp'];?>">
    <input name="tefecfin" type="hidden" class="thcons" id="tefecfin" size="30"
	<?php
 
			  if(isset($_POST['tefecfin'])) echo 'value="'.$fecha_final.'"'; 
	 ?>	>
	 <input name="tefecini" type="hidden" class="thcons" id="tefecini" size="30"
	<?php
 
			  if(isset($_POST['tefecini'])) echo 'value="'.$fecha_inicial.'"'; 
	 ?>>
   <table border='1' >
      <tr>
        <th colspan="7">
		  <div align="left">
            <input name="boimp" type="button" id="boimp" onClick="openpage('DN62000_IM.php','_self')" value="Imprimir"/>
		  </div>		</th>
      </tr>
      <tr>
        <th colspan="7">Concepto -Reporte Nomina Acumulada </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>
	  <tr>      </tr>
<?php

if(isset($_POST['setipnom'])){
	include("DN62000_CL.php");
	include('DF10000.php');
	//$m=substr($_SESSION['m_a'],0,2);
	$a=$_SESSION['axo_pro'];
	$nomusu = new clase('dnnomanu',$a,$_POST['setipnom'],-1,$_SESSION['num_usu']);
	$norec=0;
	$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);
			/*$dncapx=mysqli_query($con ,"select * from dncatper where no_pd = '".$adetnom['no_pd']."'  and operac='".$adetnom['operac']."' order by operac desc");
			$dncapxx=mysqli_fetch_array($dncapx);
			//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"];*/
			sepDet($ent,$adetnom,$cont1,$cont2,$totpd,$perded);
			if($_REQUEST['ra_imp']=='dep'){
				totDet($ent,$adetnom,$valgen,$togendep);
			}
			/*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++;
			}*/
		}//Termina separa detalles de trabajador per/ded
		/*if($cont1<=$cont2) $cont=$cont2;
		else $cont=$cont1;
		for($i=0; $i<$cont; $i++){//Inicia impresion de detalles de trabajador
			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">'.$perded[0][$i][2].'&nbsp;</div></td>';
				echo '<td><div align="center">'.$perded[0][$i][3].'&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">'.$perded[1][$i][2].'&nbsp;</div></td>';
				echo '<td><div align="center">'.$perded[1][$i][3].'&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>';
		} //Termina impresion de detalles de trabajador*/
		impSepDet($cont1,$cont2,$perded);
		/*echo '<tr>';
		echo '<td colspan="3"><div align="center">'.'TOTAL DE PERCEPCIONES: '.''.number_format($totpd[0],2,'.',',').'&nbsp;</div></td>';
		echo '<td colspan="3"><div align="center">'.'TOTAL DE DEDUCCIONES: '.''.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>';*/
		 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ņ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>';
	/*$totpd[0]=0.00;
	$totpd[1]=0.00;
	$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">'.$clave.' | '.$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">'.$clave2.' | '.$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>';
	} */
	impTotDet($togen,$totpd2);//imprime totales detalles encontrados y guarda valores netos en $totpd2
	impTot($totpd2);
	/*echo '<tr>';
	echo '<td colspan="3"><div align="center">'.'TOTAL DE PERCEPCIONES: '.''.number_format($totpd[0],2,'.',',').'&nbsp;</div></td>';
	echo '<td colspan="3"><div align="center">'.'TOTAL DE DEDUCCIONES: '.''.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>';*/
	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">'.$perded[0][$i][2].'&nbsp;</div></td>';
			echo '<td><div align="center">'.$perded[0][$i][3].'&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">'.$perded[1][$i][2].'&nbsp;</div></td>';
			echo '<td><div align="center">'.$perded[1][$i][3].'&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>
  </form>
</div>
</body>
</html>
