<?php
 
session_start();
include("DMMYSQLG.php");
//include("DSA1000.php");
include("DMMYSQLG.php");
include("DF30000.inc");
include("DF90000.php");
//   --->  llena matriz de percepciones y deducciones
$mat_pd = array("P04", "P12", "P19", "P20", "Q03", "Q04", "Q06", "Q13", "Q14", "Q15");

//   --->  Saca el axo, mes cve_nomi y no_nomi que depende de la variable de session  $_SESSION['tip_nomina']
$sq_pargen=mysqli_query($con ,"select * from dnpargen");
$dt_pargen=mysqli_fetch_array($sq_pargen);

$sq_tipnom=mysqli_query($con ,"select * from dntipnom where cve_nomi = '".$_SESSION['tip_nomina']."'");
$dt_tipnom=mysqli_fetch_array($sq_tipnom);

$mesproc=$dt_pargen["mes_pro"];
$fecproc=$dt_pargen["axo_pro"].'-'.$mesproc.'-01';

$ssq_cageno="select * from dncageno where cve_nomi = '".$_SESSION['tip_nomina']."' and axo_mes ='$fecproc' and act_nom = 'NO' order by no_nomi";
$sq_cageno=mysqli_query($con ,$ssq_cageno);
$b=0;
	while($dt_cageno=mysqli_fetch_array($sq_cageno)){
		if($dt_cageno['act_nom']=="NO"){
			$b=1;
			break;
		}
	}

$axm = $dt_cageno['axo_mes']; $axo = substr($axm,0,4);$mes = substr($axm,5,2);

$cve_nomi = $dt_cageno['cve_nomi'];// tipo de nomina
$no_nomi = $dt_cageno['no_nomi'];// numero de nomina


if(isset($_REQUEST['Submit6']) and  $_REQUEST['t2']<>""){
	$f1 = $_REQUEST['fec_ini'];
	$f2 = $_REQUEST['fec_fin'];
	include ('DF50000.php');
	if($dias_diferencia < 0){
		?>
			<script language="javascript1.2">
				alert ("Error N°545: Fechas Invalidas");
			</script>
		<? 
	}else{
		//agregar
		$tm="A";
		$no_nomina=$_REQUEST['bot'];
		$operac=$_REQUEST['t1'];
		$no_pd=$_REQUEST['t2'];
		$can_nom=$_REQUEST['t3'];
		if($can_nom=="" or $can_nom==0){
			$f1=$_REQUEST['fec_ini'];
			$f2=$_REQUEST['fec_fin'];
			include('DF50000.php');
			$can_nom=$dias_diferencia + 1;
		}
		$fec = $_REQUEST['fec_ini'];
		include('DF20000.php');
		$fec_ini=$f_m;
		$fec = $_REQUEST['fec_fin'];
		include('DF20000.php');
		$fec_fin=$f_m;
		include ("DN31300_CL.php");
	}
}
if(isset($_REQUEST['boton3'])){
	$tm="E";
	$arrLlaves = array_keys($_POST);
	$TotalRecibidos = count($arrLlaves);
	for ($i=0;$i<$TotalRecibidos;$i++){
		$let = substr($arrLlaves[$i],0,4);
		if($let == "cver"){
			if($_POST[$arrLlaves[$i]] <> ""){
				$n_reg=$_POST[$arrLlaves[$i]];
				include ("DN31300_CL.php");
			}
		}
	}
}
if(isset($_REQUEST['boton4'])){
	//Modificar
	$tm="M";
	
	$no_nomina=$_REQUEST['bot'];
	$n_reg = $_REQUEST['key_inc'];
	$operac=$_REQUEST['t1'];
	$no_pd=$_REQUEST['t2'];
	$can_nom=$_REQUEST['t3'];
	if($can_nom=="" or $can_nom==0){
		$f1=$_REQUEST['fec_ini'];
		$f2=$_REQUEST['fec_fin'];
		include('DF50000.php');
		$can_nom=$dias_diferencia + 1;
	}
	
	$fec = $_REQUEST['fec_ini'];
	include('DF20000.php');
	$fec_ini=$f_m;
	$fec = $_REQUEST['fec_fin'];
	include('DF20000.php');
	$fec_fin=$f_m;
	include ("DN31300_CL.php");
}
if(isset($_REQUEST['boton2']) or ($_REQUEST['ttr']==1 and $tm <> "M")){
	$arrLlaves = array_keys($_POST);
	$TotalRecibidos = count($arrLlaves);
	for ($i=0;$i<$TotalRecibidos;$i++){
		$let = substr($arrLlaves[$i],0,4);
		if($let == "cver"){
			if($_POST[$arrLlaves[$i]] <> ""){
				$n_reg=$_POST[$arrLlaves[$i]];
			}
		}
	}
	$ssss = mysqli_query($con ,"select * from dnincide where n_reg = '$n_reg'");
	$ffr = mysqli_fetch_array($ssss);
}

?>

<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 = 'DN31300A_AG2.php';
include("DYAYUDA.php");
if($b==0){
	echo '<div align="center">PERIODO NO VALIDO</div>';
}
?>
<div align="center">
  <form name="frm1" method="post" action="">
<input name="bot" type="hidden" <?php
 echo 'value = "'.$_POST['bot'].'"'?>>
<input name="key_inc" type="hidden" 
<?php
 
	if ($n_reg<>""){
		echo 'value = "'.$n_reg.'"';
	}else{
		echo 'value = "'.$_POST['key_inc'].'"';
	}
?>>
<br>
    <table   align="center">
      <tr>
        <th colspan="9"><div align="left">
          <input type="submit" name="Submit6" value="Agregar" onClick="openpage('DN31300_PR2.php','_self')"  
		  <?php
 
		  if(isset($_REQUEST['boton2']) or ($_REQUEST['ttr']==1 and $tm <> "M")){
		  	echo ' disabled';
		  }
		  ?>
		  />
          <input name="boton3" type="submit" id="boton3" onClick="openpage('DN31300_PR2.php','_self')" value="Eliminar"
		  <?php
 
		  if(isset($_REQUEST['boton2']) or ($_REQUEST['ttr']==1 and $tm <> "M")){
		  	echo ' disabled';
		  }
		  ?>
		  />
          <input name="boton2" type="submit" id="boton2" onClick="openpage('DN31300_PR2.php','_self')" onMouseMove="val_checks()" value="Modificar"<?php
 
		  if(isset($_REQUEST['boton2']) or ($_REQUEST['ttr']==1 and $tm <> "M")){
		  	echo ' disabled';
		  }
		  ?>
		  />
          <input type="button" name="Submit2" value="Regresar" onClick="openpage('DN31300_PR1.php','_self')"
		  <?php
 
		  if(isset($_REQUEST['boton2'])){
		  	echo ' disabled';
		  }
		  ?>
		  />
          <span class="inputcodigo">
          <?php
 
		if(isset($_REQUEST['boton2']) or ($_REQUEST['ttr']==1 and $tm <> "M")){
			?>
          <input name="ttr" type="hidden" value="1">
          <input name="boton4" type="submit" id="boton4" onClick="openpage('DN31300_PR2.php','_self')" value="Modificar"/>
          <?php

		}
		?>
          </span></div></th>
      </tr>
      <tr>
        <th colspan="7">PERIODO N&deg; <?php
 echo $dt_cageno['no_nomi'] ?> DEL <?php
 echo con_fec($dt_cageno['fec_ini'],2) ?> AL <?php
 echo con_fec($dt_cageno['fec_fin'],2) ?></th>
      </tr>
      <tr>
        <th colspan="7">INCIDENCIAS EMPLEADO :   
		<?php
 
		$no_nomina = $_REQUEST['bot'];
		$bus = mysqli_query($con ,"select * from dntrab_a where no_trab = $no_nomina");
		$row1 = mysqli_fetch_array($bus);
		echo $no_nomina.' '.$row1['ape_pat'].' '.$row1['ape_mat'].' '.$row1['nom'];
		?>		</th>
      </tr>
      
      <tr>
        <td class="inputcodigo"><span class="thcon">
          <input type="checkbox" name="checkbox" value="checkbox" onClick="todos(this)">
        </span></td>
        <td class="inputcodigo">Tipo:  
          <select name="t1" id="t1" onChange="javascript: document.getElementById('frm1').submit()">
		<option></option>
		<?php
 
		if($_REQUEST['t1']=="P" or ($ffr['operac']  == "P" and (isset($_REQUEST['boton2']) or ($_REQUEST['ttr']==1 and $tm <> "M")))){
			echo '<option value="P" selected>(P) PERCEPCIONES</option>';
		}else{
			echo '<option value="P">(P) PERCEPCIONES</option>';
		}
		if($_REQUEST['t1']=="Q" or ($ffr['operac']  == "Q" and (isset($_REQUEST['boton2']) or ($_REQUEST['ttr']==1 and $tm <> "M" and !isset($_REQUEST['t1']))))){
			echo '<option value="Q" selected>(Q) DEDUCCIONES</option>';
		}else{
			echo '<option value="Q">(Q) DEDUCCIONES</option>';
		}
		?>
        </select>        </td>
        <td colspan="2" class="inputcodigo">No.:
          <select name="t2" id="t2" onChange="javascript: document.getElementById('frm1').submit()">
		  <option></option>
          <?php
 
		  if(isset($_REQUEST['boton2'])){
		  	$r = mysqli_query($con ,"SELECT * FROM dncatper where operac = '".$ffr['operac']."'");
		  }elseif($_REQUEST['ttr']==1 and $tm <> "M"){
		  	$r = mysqli_query($con ,"SELECT * FROM dncatper where operac = '".$_REQUEST['t1']."' order by no_pd");
		  }else{
		  	$r = mysqli_query($con ,"SELECT * FROM dncatper where operac = '".$_REQUEST['t1']."' order by no_pd");
		  }
	  	
		while($reg = mysqli_fetch_array($r)){
			if((isset($_REQUEST['boton2']) or ($_REQUEST['ttr']==1 and $tm <> "M")) and $reg['no_pd'] == $ffr['no_pd'] or $_REQUEST['t2'] == $reg['no_pd']){
				echo '<option value="'.$reg['no_pd'].'" selected>'.$reg['no_pd'].' - ' .$reg['des_perc'].'</option>';
			}else{
				echo '<option value="'.$reg['no_pd'].'">'.$reg['no_pd'].' - ' .$reg['des_perc'].'</option>';
			}
			
		}
	  ?>
        </select></td>
        <td class="inputcodigo"> 
		<?php
 
		//   --->  Busco el tipo de percepcion o deduccion
		$sq_catper = mysqli_query($con ,"select * from dncatper where (no_pd = '".$_POST['t2']."' or no_pd = '".$ffr['no_pd']."')and (operac = '".$_POST['t1']."' or operac = '".$ffr['operac']."')");
		$dt_catper = mysqli_fetch_array($sq_catper);
		//$sq_master = mysqli_query($con ,"select * from geoyos_jc.dnmaster where cve_mae = '".$dt_catper['cve_mae']."'");
		//echo 'Valor :'.$dt_catper['cve_mae'].'<br>';
		//$dt_master = mysqli_fetch_array($sq_master);
		$nr = count($mat_pd);
		$y = 0;
		for ($i=0;$i<$nr;$i++){
			if ($mat_pd[$i] == $dt_catper['cve_mae']){
				?>
				Fec/Ini
				<input name="fec_ini" type="text" class="emplea" id="fec_ini" size="15" readonly
				<?php
 
				$fec1 = $ffr['fec_ini'];
				include('DF20000.php');
				echo 'value = "'.$f_f.'"';
				?>
				>		      
		      <input name="reset2" id="bot1" value=" ... " type="image" src="logos/cal4.jpg"/>
		        <script type="text/javascript">
				  	Calendar.setup(
    					{
      						inputField  : "fec_ini",        // ID of the input field
      						ifFormat    : "%d/%m/%Y",    // the date format
      						button      : "bot1"      // ID of the button
    					}
  					);
				</script>
				&nbsp;Fec/Ini
				
				<input name="fec_fin" type="text" class="emplea" id="fec_fin" size="15" readonly 
				<?php
 
				$fec1 = $ffr['fec_fin'];
				include('DF20000.php');
				echo 'value = "'.$f_f.'"';
				?>
				>
		      <input name="reset2" id="bot2" value=" ... " type="image" src="logos/cal4.jpg"/>
				<script type="text/javascript">
				  	Calendar.setup(
    					{
      						inputField  : "fec_fin",        // ID of the input field
      						ifFormat    : "%d/%m/%Y",    // the date format
      						button      : "bot2"      // ID of the button
    					}
  					);
				</script>
				<?		
				$y = 1;
			}
		}
		if ($y <> 1){
		?>
		Cantidad: 
        <input name="t3" type="text" id="t3"
		<?php
 
		if(isset($_REQUEST['boton2']) or ($_REQUEST['ttr']==1 and $tm <> "M")){
			echo ' value="'.$ffr['can_nom'].'"';
		}
		?>
		>
		
		
		<?php
 }?>		</td>
      </tr>
      <tr>
        <th><div align="center">Sel</div></th>
        <th><div align="center">Per/Ded</div></th>
        <th><div align="center">N&deg; </div></th>
        <th><div align="center">Descripcion</div></th>
        <th><div align="center">Cantidad</div></th>
      </tr>
      <?php

	$no_nomina = $_REQUEST['bot'];
	//$axo = substr($axm,0,4);$mes = substr($axm,5,2);

//$cve_nomi = $dt_cageno['cve_nomi'];// tipo de nomina
//$no_nomi = $dt_cageno['no_nomi'];// numero de nomina
	@$sql = mysqli_query($con ,"SELECT * FROM dnincide where axo = '$axo' and mes = '$mes' and cve_nomi='$cve_nomi' and no_nomi = '$no_nomi' and no_nomina = '$no_nomina' order by operac,no_pd");

	echo '<input name="txt_t1" type="hidden" value="'.$_POST['t1'].'">';
	while($reg = mysqli_fetch_array($sql))
	{
		$jj += 1;
	  	echo "<tr>";
		if(isset($_REQUEST['boton2']) or ($_REQUEST['ttr']==1 and $tm <> "M")){
			$se = 'checked';
		}
		echo '<td><div align="center"><input name="cver'.$jj.'" type="checkbox" value="'. $reg['n_reg'].'"></div></td>';
		if($reg['operac']=="P"){
			echo '<td><div align="center">PERCEPCION</div></td>';
		}else{
			echo '<td><div align="center">DEDUCCION</div></td>';
		}
		echo '<td><div align="center">' . $reg['no_pd'] . '&nbsp</div></td>';
		
		@$sss = mysqli_query($con ,"SELECT * FROM dncatper WHERE operac = '".$reg['operac']."' and no_pd = '".$reg['no_pd']."'");
		$rrr = mysqli_fetch_array($sss);
		
		echo '<td><div align="center">' . $rrr['des_perc'] . '&nbsp</div></td>';
		echo '<td><div align="center">' . $reg['can_nom'] . '&nbsp</div></td>';
	}
	mysqli_close($con);
	?>
    </table>
  </form>
</div>
</body>
</html>