<html>
<head>
<link rel="stylesheet" type="text/css" href="../../styles/Style.css">
<script language="JavaScript" type="text/javascript" src="../ajax.js"></script>
</head>
<body>
<?php
//header("Cache-Control: no-store, no-cache, must-revalidate");
include("../../bd/sql_variables.php");
$db_conexion= mysql_connect("$sql_host", "$sql_usuario", "$sql_pass") or die(header ("No Disponible Servidor de Base de Datos"));
mysql_select_db("$sql_db");


$idj=$_POST['idj'];
$ids=$_POST['ids'];
$anno=$_POST['anno'];
$mes=$_POST['mes'];
$idtp=$_POST['idtp'];
$ugid=$_POST['ugid'];

$arrcodper = unserialize($_POST['arrcodper']); 
$arrdia = unserialize($_POST['arrdia']); 
$arrcodprog = unserialize($_POST['arrcodprog']); 
$arrhorario = unserialize($_POST['arrhorario']); 
$arrarea = unserialize($_POST['arrarea']); 
$arrgarea = unserialize($_POST['arrgarea']); 
$arrtiporeg = unserialize($_POST['arrtiporeg']); 
$arrformareg = unserialize($_POST['arrformareg']); 





echo "<input type='hidden' id='idj' name='idj' value='".$idj."'>";
echo "<input type='hidden' id='ids' name='ids' value='".$ids."'>";
echo "<input type='hidden' id='anno' name='anno' value='".$anno."'>";
echo "<input type='hidden' id='mes' name='mes' value='".$mes."'>";
echo "<input type='hidden' id='idtp' name='idtp' value='".$idtp."'>";
echo "<input type='hidden' id='ugid' name='ugid' value='".$ugid."'>";

for ($i=0;$i<count($arrcodper);$i++){
//busca codigo del personal y el servicio
$sqlask ="SELECT ug_id as cod, s_id as ids FROM tb_usutrab where ut_codprog='$arrcodprog[$i]' and  ug_id='$arrcodper[$i]' and tp_id='$idtp' and uap_anno='$anno' and uap_mes='$mes' and j_id='$idj' and ut_idserva='$ids'";
//echo $sqlask."<br>"; 
$resultask = mysql_query($sqlask);
if(mysql_num_rows($resultask)!=0){
while($rowask = mysql_fetch_array($resultask)){
$idso = $rowask['ids'];
}}
if($arrformareg[$i]==1){
$sqlrepro = "select * from tb_reprohrs where rh_dia='$arrdia[$i]' and h_id='$arrhorario[$i]' and a_id='$arrarea[$i]' and ga_id='$arrgarea[$i]' and tp_id='$idtp'"; 
$sqlrepro = $sqlrepro." and ug_id='$arrcodper[$i]' and uap_anno='$anno' and uap_mes='$mes' and s_id='$idso' and rh_idserva='$ids' and j_id='$idj' and rh_tipo='$arrtiporeg[$i]'";
//echo $sqlrepro."<br>";
$resultrepro = mysql_query($sqlrepro);
if(mysql_num_rows($resultrepro)==0){
$sqlinsert = "insert into tb_reprohrs values ('$arrdia[$i]','$arrcodprog[$i]','$arrhorario[$i]','$arrarea[$i]','$arrgarea[$i]','$idtp','$arrcodper[$i]','$anno','$mes','$idso','$idj','$ids','$arrtiporeg[$i]','$ugid',now())";
//echo $sqlinsert."<br>";
$resultinsert = mysql_query($sqlinsert);
}
}else{
$sqldel= "delete from tb_reprohrs where h_id='$arrhorario[$i]' and rh_dia='$arrdia[$i]' and a_id='$arrarea[$i]' and ga_id='$arrgarea[$i]' and tp_id='$idtp'"; 
$sqldel= $sqldel." and ug_id='$arrcodper[$i]' and uap_anno='$anno' and uap_mes='$mes' and s_id='$idso' and rh_idserva='$ids' and j_id='$idj' and rh_tipo='$arrtiporeg[$i]'";
$resultdel = mysql_query($sqldel);
}}


$arrcodpertxt = unserialize($_POST['arrcodpertxt']); 
$arrtxtobs = unserialize($_POST['arrtxtobs']); 
//print_r ($arrcodpertxt);
//print_r ($arrtxtobs);
for ($j=0;$j<count($arrcodpertxt);$j++){

//busca codigo del personal y el servicio
$sqlask ="SELECT ug_id as cod, s_id as ids FROM tb_usutrab where ug_id='$arrcodpertxt[$j]' and tp_id='$idtp' and uap_anno='$anno' and uap_mes='$mes' and j_id='$idj' and ut_idserva='$ids'";
//echo $sqlask."-".$arrtxtobs[$j]."<br>"; 
$resultask = mysql_query($sqlask);
if(mysql_num_rows($resultask)!=0){
while($rowask = mysql_fetch_array($resultask)){
$idso = $rowask['ids'];
}}
//echo "<br>".$arrtxtobs[$j]."<br>";
if($arrtxtobs[$j] != "-"){
$sqlask2 ="select * from tb_obsreprog where j_id='$idj' and ug_id='$arrcodpertxt[$j]' and s_id='$idso' and uap_anno='$anno' and uap_mes='$mes' and or_idserva='$ids'";
$resultask2 = mysql_query($sqlask2);
//echo $sqlask2."<br>";
if(mysql_num_rows($resultask2)==0){
$sqlinserttxt = "insert into tb_obsreprog values ('$idj','$idso','$arrcodpertxt[$j]','$anno','$mes','$ids',upper('$arrtxtobs[$j]'))";
$resultinsert = mysql_query($sqlinserttxt);
//echo $sqlinserttxt."<br>";
}else{
$sqlup="update tb_obsreprog set or_obs=upper('$arrtxtobs[$j]') where ug_id='$arrcodpertxt[$j]' and j_id='$idj' and s_id='$idso' and uap_anno='$anno' and uap_mes='$mes' and or_idserva='$ids'";
//echo $sqlup."<br>";
$resultup = mysql_query($sqlup);
}
}else{
$sqldel="delete from tb_obsreprog where j_id='$idj' and s_id='$idso' and ug_id='$arrcodpertxt[$j]' and uap_anno='$anno' and uap_mes='$mes' and or_idserva='$ids'";
//echo $sqldel."<br>";
$resultdel = mysql_query($sqldel);
}
}
//echo "<meta http-equiv='refresh' content='60'>";
$anno= $anno;
$mes=$mes;
$idj=$idj;
$ids=$ids;
$idtp=$idtp;
$ugid=$ugid;
include("reprogramacionper.php");

?>
