<?php
require_once("../class/class.conectar.php");
require_once("../class/class.personal.php");
require_once("../class/class.jefaturas.php");
require_once("../class/class.tipopersonal.php");
require_once("../class/class.horaprog.php");
require_once("../class/class.horareprog.php");
require_once("../class/class.horaeje.php");
require_once("../class/class.usudep.php");


$Objcon = new Conectar();
$conex = $Objcon->con();

$idj=$_GET['idj'];
$ids=$_GET['ids'];
$ugid=$_GET['ugid'];


/*
$Objtp = new TipoPersonal();
$res_tp = array();
$res_tp = $Objtp->buscar_tipoper_detalle($conex,$ugid);
$idtp = $res_tp[0]['id'];
*/

//BUSCAR EL CODIGO DEL TIPIO DEL PERSONAL/////

if(isset($_GET['tmov'])){
$fecha=$_GET['fecha'];

$idj=$_GET['jefatura'];
$ids=$_GET['servicio'];
$ugid=$_GET['ugid'];
$idjold=$_GET['depaold'];
$idsold=$_GET['servold'];

$Objp=new UsuarioDependencia();
$res_ut=array();
$res_ut=$Objp->listar_det_trab_per($conex,$ugid);
$udid=$res_ut[0]['id'];
$idtp=$res_ut[0]['idtp'];
$idesp=$res_ut[0]['idesp'];
$estado=$res_ut[0]['estado'];
$fechaing=$res_ut[0]['fechaing'];
$idcont=$res_ut[0]['idcont'];
$idc=$res_ut[0]['idc'];
$udidn = $udid + 1;
	$uno=substr($fecha, 0, 2);
	$dos=substr($fecha, 3, 2);
	$tres=substr($fecha, 6, 4);
	$fecha = ($tres."-".$dos."-".$uno);
$Objp->registrar_det_trab_per($conex,$idtp,$ugid,$ids,$idj,$udidn,$fechaing,'0000-00-00',$idesp,$estado,$idcont,$idc);
$Objp->modificar_fecha($conex,$ugid,$udid,$fecha);
$Objp->finalizar_trab_dep_per($conex,$ugid,$udid,0);
// para eliminar los turnos programados a posterior

//$Objphr = new HoraProgramacion();
//$Objphr->elimina_hrprog_mayor_fecha($ugid,$fecha);

//$Objrphr = new HoraReprogramacion();
//$Objrphr->elimina_hrreprog_mayor_fecha($ugid,$fecha);
//$Objrphr->elimina_hrrereprog_mayor_fecha($ugid,$fecha);

//$Objehr = new HoraEjecucion();
//$Objehr->elimina_hreje_mayor_fecha($ugid,$fecha);

echo "<script type='text/javascript'>window.opener.location.reload(); window.close();</script>";
}

	
$Objj = new Jefaturas();
$res_jef = array();
$res_jef = $Objj->listar_jefatura($conex);
$cadenaj="<select id='jefatura' name='jefatura' onChange='actualizaservicio()'>";
for($i=0;$i<sizeof($res_jef);$i++){
$cadenaj=$cadenaj."<option value='".$res_jef[$i]['cod']."'";
if ($res_jef[$i]['cod']==$idj){
$cadenaj=$cadenaj." selected ";
}
$cadenaj=$cadenaj.">".$res_jef[$i]['descripcion']."</option>";
}
$cadenaj=$cadenaj."</select>";


//seleccionando al comnbo servicio
$res_jefs = array();
$res_jefs = $Objj->listar_jefatura_servicio($conex,$idj);
$cadenaserv="<select id='servicio' name='servicio'>";
$cadenaserv=$cadenaserv."<option value='0'";
if($ids==0){  $cadenaserv=$cadenaserv." selected "; }
$cadenaserv=$cadenaserv.">DEPENDENCIA DIRECTA</option>";
for($i=0;$i<sizeof($res_jefs);$i++){
$cadenaserv=$cadenaserv."<option value='".$res_jefs[$i]['ids']."'";
if ($res_jefs[$i]['ids']==$ids){
$cadenaserv=$cadenaserv." selected ";
}
$cadenaserv=$cadenaserv.">".$res_jefs[$i]['nom']."</option>";
}
$cadenaserv=$cadenaserv."</select>";


$Objp = new Personal();
$res_per=array();
$res_per=$Objp->buscar_nombre($conex,$ugid);
$nombre = $res_per[0]['nombre'];


?>
<html>
<head>
<title>TRANSFERENCIA DEL PERSONAL ASISTENCIAL</title>
<link rel="stylesheet" type="text/css" href="styles/Style.css">

<link type="text/css" rel="stylesheet" href="src/css/jscal2.css" />
    <link type="text/css" rel="stylesheet" href="src/css/border-radius.css" />
    <!-- <link type="text/css" rel="stylesheet" href="src/css/reduce-spacing.css" /> -->

    <link id="skin-win2k" title="Win 2K" type="text/css" rel="alternate stylesheet" href="src/css/win2k/win2k.css" />
    <link id="skin-steel" title="Steel" type="text/css" rel="alternate stylesheet" href="src/css/steel/steel.css" />
    <link id="skin-gold" title="Gold" type="text/css" rel="alternate stylesheet" href="src/css/gold/gold.css" />
    <link id="skin-matrix" title="Matrix" type="text/css" rel="alternate stylesheet" href="src/css/matrix/matrix.css" />

    <link id="skinhelper-compact" type="text/css" rel="alternate stylesheet" href="src/css/reduce-spacing.css" />

    <script src="src/js/jscal2.js"></script>
	<script src="src/js/lang/en.js"></script>
    <!-- you actually only need to load one of these; we put them all here for demo purposes -->
     <script src="src/js/lang/es.js"></script>
    
    <link type="text/css" rel="stylesheet" href="demopage.css" />
	
<script language="JavaScript" type="text/javascript" src="ajax.js"></script>
<script type="text/javascript"> 

function ventanaClose(){ 
      window.close();
} 
function validaFechaDDMMAAAA(fecha){    
var dtCh= "/";    
var minYear=1900;
var maxYear=2100;
        function isInteger(s){
        var i;     
		for (i = 0; i < s.length; i++){ 
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
        }
        return true;
		}
		function stripCharsInBag(s, bag){
        var i;
        var returnString = ""; 
		for (i = 0; i < s.length; i++){ 
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;        }
        return returnString;    }
		function daysInFebruary (year){
        return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );    }
		function DaysArray(n) {
        for (var i = 1; i <= n; i++) { 
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}        
		} 
		return this
		}    
		function isDate(dtStr){
        var daysInMonth = DaysArray(12) 
		var pos1=dtStr.indexOf(dtCh) 
		var pos2=dtStr.indexOf(dtCh,pos1+1) 
		var strDay=dtStr.substring(0,pos1)
        var strMonth=dtStr.substring(pos1+1,pos2)
        var strYear=dtStr.substring(pos2+1)
        strYr=strYear
        if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)        
		if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)        
		for (var i = 1; i <= 3; i++) {            
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
        }        
		month=parseInt(strMonth)        
		day=parseInt(strDay)        
		year=parseInt(strYr)        
		if (pos1==-1 || pos2==-1){            
		return false        }        
		if (strMonth.length<1 || month<1 || month>12){            
		return false        }       
		if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
		{            return false        }        
		if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){ 
		return false        }        
		if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)
		{            return false        }        
		return true    }    
		if(isDate(fecha))
		{        return true;    }
		else{        return false;    }
}
function TransferirPer(){
	var idj = document.getElementById("jefatura").value;
	var ids = document.getElementById("servicio").value;
	var ugid = document.getElementById("ugid").value;
	var idjold = document.getElementById("depaold").value;
	var idsold = document.getElementById("servold").value;
	var fecha = document.getElementById("fecha").value;
	
	if(fecha == ''){
		alert("Fecha no Ingresado");
		return false;
	}
	if(validaFechaDDMMAAAA(fecha) == false){
		alert("Fecha No Esta Escrita Correctamente");
		return false;
	}
	
	if(idj==idjold && ids==idsold){
		alert("Usted No ha Realizado Ningun Cambiado al Lugar de Transferencia");
		return false;
	}
	
	document.location= "transferirpersonal.php?jefatura="+idj+"&servicio="+ids+"&ugid="+ugid+"&depaold="+idjold+"&servold="+idsold+"&fecha="+fecha+"&tmov=1";
	
}
</script> 
</head>
<body>
<table align='center'>
<tr><td>
<div id='transfpersonal'>
<?php
//echo "<input type='hidden' id='tp' name='tp' value='".$idtp."'>";
echo "<input type='hidden' id='ugid' name='ugid' value='".$ugid."'>";
echo "<input type='hidden' id='depaold' name='depaold' value='".$idj."'>";
echo "<input type='hidden' id='servold' name='servold' value='".$ids."'>";

?>
<fieldset>
	<legend align='left'><font color='blue' size='15'>Seleccione la Jefatura a Transferir </font></legend>
	<br>
		<table align='center' class='FacetFormTABLE' width='600'>
		<tr><td class='FacetFieldCaptionTDT' colspan='3'><center>TRANSFERENCIA DEL PERSONAL ASISTENCIAL</center></td></tr>
		<tr><td colspan='3'><center><?php if(isset($error)){ if($error=='0'){ echo "REVISE LOS DATOS, PERSONAL NO REGISTRADO";}elseif($error=='1'){ echo "PERSONAL REGISTRADO";}}?></center></td></tr>
		<tr><td align='right' class='FacetFieldCaptionTD' >PERSONAL </td><td  width='500'><?php echo $nombre; ?></td></tr>
		<tr><td align='right' class='FacetFieldCaptionTD'>AREA / DPTO</td><td><?php echo $cadenaj; ?></td></tr>
		<tr><td align='right' class='FacetFieldCaptionTD'>SERVICIO</td><td><div id='divserv'><?php echo $cadenaserv; ?></div></td></tr>
		<tr><td align='right' class='FacetFieldCaptionTD'>FECHA DE TRANSFERENCIA</td><td><input type='text' size='15' id='fecha' name='fecha'>
		<button id="f_rangeStart_trigger"><img src="../img/b_calendar.png" ></button>
                <button id="f_clearRangeStart" onclick="clearRangeStart()">clear</button>
                <script type="text/javascript">
				  var LEFT_CAL = Calendar.setup({
                    //cont: "cont",
                    //weekNumbers: true,
                    //selectionType: Calendar.SEL_MULTIPLE,
                    //showTime: 12
                    // titleFormat: "%B %Y"
					})
                  new Calendar({
                          inputField: "fecha",
                          dateFormat: "%d/%m/%Y",
                          trigger: "f_rangeStart_trigger",
                          bottomBar: false,
                          onSelect: function() {
                                  var date = Calendar.intToDate(this.selection.get());
								  LEFT_CAL.setLanguage('es');
                                  LEFT_CAL.args.min = date;
                                  LEFT_CAL.redraw();
                                  this.hide();
                          }
                  });
                  function clearRangeStart() {
                          document.getElementById("fecha").value = "";
                          LEFT_CAL.args.min = null;
                          LEFT_CAL.redraw();
                  };
                </script>
		</td></tr>
		<tr><td align='center' colspan='2'><input type='button' id='pprog' style='width:150px' class='FacetButton'  value='Transferir' onClick='TransferirPer()'>
		<button type='button' id='pprog' style='width:150px' class='FacetButton' onClick='ventanaClose();' >Cancelar</button>
		</td></tr></table>
	  <br>
	</fieldset>
</div>
</td><tr>
</table>
</body>
</html>