<?php
 
session_start();
include("DMMYSQLG.php");
include("DSA1000.php");
include("DMMYSQLG.php");
include('DF30000.inc');
?>
<?php
 
//busquedas
	if(isset($_POST['setipnom']) || $_POST['ra_imp']){
		$m=substr($_SESSION['m_a'],0,2);
		$a=substr($_SESSION['m_a'],2,4);
		$fecproc=$a.'-'.$m.'-01';
		$busqcal="select * from dncageno where cve_nomi = '".$_POST['setipnom']."' and axo_mes='$fecproc'";
		$busq="select * from dndatnom where tip_nom ='".$_POST['setipnom']."'";
		$ct_pargen=mysqli_fetch_array(mysqli_query($con ,"select * from dnpargen"));
	}
	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']="gen";
?>
<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 = 'DN5500A_AG.php';
include("DYAYUDA.php");
?>
<div align="center">
<form name="form1" id="form1" method="post" action="">
  <table  align="center">
    <tr>
      <th colspan="2">% IMPUESTO SOBRE NOMINA  </th>
    </tr>
    <tr>
      <th colspan="2"> Nomina </th>
      </tr>
	    <tr class="inputcodigo">
	      <td colspan="2"><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>
	        &nbsp;</div></td>
      </tr>
	    <tr>
	      <th colspan="2">Reporte</th>
      </tr>
	    <tr>
          <th> 
            <input name="ra_imp" type="radio" value="gen" onClick="javascript: document.getElementById('form1').submit()" 
	  <?php
 
			if($_REQUEST['ra_imp']=="gen")
				echo 'checked';
		?> >
            General</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">Periodo de  Nomina</div></th>
      </tr>
	    <tr>
	      <td colspan="2" class="typeagre"><div align="center">
		  <?php
 
		  	echo 'MES - A&Ntilde;O ('.$_SESSION['m_a'].')';
		  ?>
		  </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']) || isset($_POST['ra_imp'])) echo 'value="'.$fecha_inicial.'"'; 
			 ?>
			  >
			  <input name="tefecfin" type="hidden" class="thcons" id="tefecfin" size="30" readonly
				<?php
 
			  if(isset($_POST['tefecfin']) || isset($_POST['ra_imp'])) echo 'value="'.$fecha_final.'"'; 
			  ?>
				>
		      </p>
    </div></td></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 type="hidden" name="seperded" id="seperded" value="<?php
 echo $_POST['seperded'];?>">
   <input  type="hidden" name="secatpd" id="secatpd"   value="<?php
 echo  $_POST['secatpd'];?>">
    <input  type="hidden" name="ra_imp" id="ra_imp"   value="<?php
 echo  $_POST['ra_imp'];?>">
    <input name="tefecfin" type="hidden" class="thcons" id="tefecfin" size="30"
	<?php

			  if(isset($_POST['tefecfin']) || isset($_POST['ra_imp'])) echo 'value="'.$fecha_final.'"'; 
	 ?>	>
	 <input name="tefecini" type="hidden" class="thcons" id="tefecini" size="30"
	<?php

			  if(isset($_POST['tefecini']) || isset($_POST['ra_imp'])) echo 'value="'.$fecha_inicial.'"';
	 ?>>
   <table border='1'>
      <tr>
        <th colspan="6">
		  <div align="left">
            <input name="boimp" type="button" id="boimp" onClick="openpage('DN55000_IM.php','_self')" value="Imprimir"/>
		  </div>		</th>
      </tr>
      <tr>
        <th colspan="6">Concepto -% Impuesto Sobre Nomina Acumulada </th>
      </tr>
	 <tr>
		  <td class="mentab"><div align="center">No.|Des.</div></td>
		   <!--<td  class="mentab"><div align="center">Cantidad Movimiento</div></td>-->
		    <td  class="mentab"><div align="center">Importe Movimiento</div></td>
			 <td class="mentab"><div align="center">Importe Grab./ISPT</div></td>
			  <td  class="mentab"><div align="center">Importe Exe./ISPT</div></td>
			   <td  class="mentab"><div align="center">Importe Grab./IMSS</div></td>
			    <td  class="mentab"><div align="center">Importe Exe./IMSS</div></td>
	  </tr>
	  
<?php

if(isset($_POST['setipnom'])){
	include("DN55000_CL.php");
	include('DF10000.php');
	$busproc=mysqli_query($con ,"select * from dnpargen");
	$arbusproc=mysqli_fetch_array($busproc);
	$nomusu = new clase('dnnommen',$a,$m,$_POST['setipnom'],-1,'P',$_POST['secatpd'],$_SESSION['num_usu']);
	$norec=0;
	$result=$nomusu->listaElem();
	$cchck=$nomusu->conReg();
	$topd=NULL;
	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($ent==-1) $ent=0;
		
		if($_REQUEST['ra_imp']=="dep"){
			if($chboxes['depto']!=$departamento){
				$dt_catdep= mysqli_fetch_array(mysqli_query($con ,"select * from dncatdep where no_depto = '$departamento'"));
				$toperdep=$togendep[$ent];
				echo '<tr class="inputcodigo"><td colspan = "6"><div align="center">'.$dt_catdep['no_depto'].'|'.$dt_catdep['des_depto'].'</div></td></tr>';
				impDet($toperdep);
				impTotNet('TOTAL DEPARTAMENTO: ',$ent,$topddep);
				porimp($ent,$topddep);
				$togendep=NULL;
				$topddep=NULL;
				$departamento=$chboxes['depto'];
			}
		}
		
		while(@$adetnom=mysqli_fetch_array($detnom)){//Inicia separa detalles de trabajador per/ded
			$valgen=$adetnom["cve_mae"];
			if($_REQUEST['ra_imp']=="dep"){
				$togendep=totDet($ent,$valgen,$adetnom,$togendep);
				$topddep = totNet($ent,$adetnom,$topddep);
			}
			$togen =  totDet($ent,$valgen,$adetnom,$togen);
			if($valgen!="P33") $topd = totNet($ent,$adetnom,$topd);
		}//Termina separa detalles de trabajador per/ded
	}
	if($_REQUEST['ra_imp']=="dep"){
		$dt_catdep= mysqli_fetch_array(mysqli_query($con ,"select * from dncatdep where no_depto = '".$chboxes['depto']."'"));
		$toperdep=$togendep[$ent];
		echo '<tr class="inputcodigo"><td colspan = "6"><div align="center">'.$dt_catdep['no_depto'].'|'.$dt_catdep['des_depto'].'</div></td></tr>';
		impDet($toperdep);
		impTotNet('TOTAL DEPARTAMENTO: ',$ent,$topddep);
		porimp($ent,$topddep);
		$togendep=NULL;
		$topddep=NULL;
		$departamento=$chboxes['depto'];
	}
	echo '<tr ><td class = "inputcodigo" colspan="6"><div align="center">------------TOTALES GENERALES---------------</div></td></tr>';
	//Se aņade pagina para imprimir resumen de detalles con valores totales y neto
	$toper=$togen[$ent];
	impDet($toper);
	impTotNet('TOTAL GENERAL: ',$ent,$topd);
	porimp($ent,$topd);
}

function porimp($ent,$topd){
	$ct_pargen=mysqli_fetch_array(mysqli_query($con ,"select * from dnpargen"));
	echo '<tr>';
	echo '<td colspan="7"><div align="center">'.'% IMPUESTO SOBRE NOMINAS: '.''.number_format($ct_pargen["apl_por_snom"]/100,4,'.',',').'&nbsp;</div></td></tr>';
	echo '<tr>';
	echo '<td colspan="6"><div align="center">'.'TOTAL IMPUESTO SOBRE NOMINAS : '.''.number_format(($topd[$ent][1]*$ct_pargen["apl_por_snom"]/100),2,'.',',').'&nbsp;</div></td>';
	echo '</tr>';
}
function totNet($ent,$adetnom,$totales){
	$totales[$ent][0] +=$adetnom["can_mov"];
	$totales[$ent][1] +=$adetnom["imp_mov"];
	$totales[$ent][2] +=$adetnom["imp_gra_isp"];
	$totales[$ent][3] +=$adetnom["imp_exe_isp"];
	$totales[$ent][4] +=$adetnom["imp_gra_ims"];
	$totales[$ent][5] +=$adetnom["imp_exe_ims"];
	return ($totales);
}
function impTotNet($mensaje,$ent,$topd){
	echo '<tr>';
	echo '<td colspan="1"><div align="center">'.$mensaje.'&nbsp;</div></td>';
	echo '<td><div align="center">'.number_format($topd[$ent][1],2,'.',',').'&nbsp;</div></td>';
	echo '<td><div align="center">'.number_format($topd[$ent][2],2,'.',',').'&nbsp;</div></td>';
	echo '<td><div align="center">'.number_format($topd[$ent][3],2,'.',',').'&nbsp;</div></td>';
	echo '<td><div align="center">'.number_format($topd[$ent][4],2,'.',',').'&nbsp;</div></td>';
	echo '<td><div align="center">'.number_format($topd[$ent][5],2,'.',',').'&nbsp;</div></td>';
	echo '</tr>';
}

function impDet($toper){
	$cont=count($toper);
	for($i=0; $i<$cont; $i++){
		echo '<tr>';
		list($clave,$des) = each($toper);
		echo '<td colspan = "1"><div align="left">'.$des[0].' | '.$des[1].'&nbsp;</div></td>';	
		echo '<td ><div align="center">'.number_format($des[3],2,'.',',').'&nbsp;</div></td>';	
		echo '<td ><div align="center">'.number_format($des[4],2,'.',',').'&nbsp;</div></td>';	
		echo '<td ><div align="center">'.number_format($des[5],2,'.',',').'&nbsp;</div></td>';	
		echo '<td ><div align="center">'.number_format($des[6],2,'.',',').'&nbsp;</div></td>';
		echo '<td ><div align="center">'.number_format($des[7],2,'.',',').'&nbsp;</div></td>';		
		echo '</tr>';
	} 
}
function totDet($ent,$valgen,$adetnom,$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);
	$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"];
	$togen[$ent][$valgen][4] +=$adetnom["imp_gra_isp"];
	$togen[$ent][$valgen][5] +=$adetnom["imp_exe_isp"];
	$togen[$ent][$valgen][6] +=$adetnom["imp_gra_ims"];
	$togen[$ent][$valgen][7] +=$adetnom["imp_exe_ims"];
	return ($togen);
}
mysqli_close($con);
?>
    </table>
  </form>
</div>
</body>
</html>
