<?php
include_once('../../../php/sessionStart.php');
include_once('../../../php/db/dbExec.php');
include_once('../../../php/functions.php');
include_once('../../../php/timezone.php');
include_once('../../../php/privileges.php');

$bitForm = "nom4190";
$bitMod = "nom";
$empCod = $_SESSION['empCod'];
$usuCod = utf8_decode($_SESSION['usuCod']);
$usuNom = utf8_decode($_SESSION['usuNom']);

switch ($_POST['action']) {
	case 'consultar':
		echo consultar();
		break;

	case 'consultarSuspen':
		echo consultarSuspen();
		break;

	case 'guardar':
		echo guardar();
		break;

	case 'borrar':
		echo borrar();
		break;

}

function consultar() {
	global $empCod;

	$priv = new privileges();
	if (!$priv->consult($_SESSION['usuPriv'])) {
		return json_encode($priv->rErrorConsult());
	}

	$func = new functions();

	$numCod=utf8_decode($_POST['numCod']);

	# QUERYS
	$sMaestro = "SELECT m.*, 
				TRIM(m.mae_nom) AS mae_nom, 
				TRIM(m.mae_ape) AS mae_ape
				FROM maestro m
				WHERE m.mae_emp='$empCod' AND  m.mae_cod='$numCod'";

	
	# EJECUCION DE QUERYS
	$db = new dbExec();
	$codigo=$numCod;
    $_SESSION['numCodsus'] = $numCod;
	$resp = $db->sEx($sMaestro, "maestro");
	$db->close();

	$func = new functions();
	return $func->jsonEncodeArray($resp, "maestro");
}

function consultarSuspen() {
	global $empCod;

	$priv = new privileges();
	if (!$priv->consult($_SESSION['usuPriv'])) {
		return json_encode($priv->rErrorConsult());
	}

	$func = new functions();

	$numCod=utf8_decode($_POST['numCod']);
	$dtDel=utf8_decode($_POST['dtDel']);
	//$dtAl=utf8_decode($_POST['dtAl']);

	# QUERYS
	$sSuspen = "SELECT *, 
				TRIM(sus_obs) AS sus_obs 
				FROM suspen 
				WHERE sus_emp='$empCod' AND sus_cod='$numCod' AND sus_del='$dtDel'";

	
	# EJECUCION DE QUERYS
	$db = new dbExec();
	$resp = $db->sEx($sSuspen, "suspen");
	$db->close();

	$func = new functions();
	return $func->jsonEncodeArray($resp, "suspen");
}

function guardar() {
	global $empCod, $usuNom, $bitMod, $bitForm;

	$priv = new privileges();
	if (!$priv->modify($_SESSION['usuPriv'])) {
		return json_encode($priv->rErrorModify());
	}

	$func = new functions();

	$numCod = utf8_decode($_POST["numCod"]);
	$dtDel = utf8_decode($_POST["dtDel"]);
	$dtAl = utf8_decode($_POST["dtAl"]);
	$crTipo = utf8_decode($_POST["crTipo"]);
	$crGocesn = "N";
	//utf8_decode($_POST["crGocesn"]);
	$txtObs = utf8_decode($_POST["txtObs"]);

	# QUERYS PARA UN REGISTRO
	$iSuspen = "INSERT INTO suspen(sus_emp, sus_cod, sus_del, sus_al, sus_tipo, sus_gocesn, sus_obs, sus_usuario) VALUES ('$empCod','$numCod','$dtDel','$dtAl','$crTipo','$crGocesn','$txtObs','$usuNom')";

	$uSuspen = "UPDATE suspen SET sus_al='$dtAl',sus_tipo='$crTipo',sus_gocesn='$crGocesn',sus_obs='$txtObs',sus_usuario='$usuNom' WHERE sus_emp='$empCod' AND sus_cod='$numCod' AND sus_del='$dtDel'";

	# EJECUCION DE QUERYS 
	$db = new dbExec();
	if ($func->stringToBool($_POST['nuevo'])) {
		$resp = $db->iEx($iSuspen);
		$db->iEx($func->iBitacora(utf8_decode("Se creó suspension empleado, Código: $numCod  del $dtDel  al $dtAl  Usuario: ").$usuNom, $bitMod, $bitForm));
	} else {
		$resp = $db->uEx($uSuspen);
		$db->iEx($func->iBitacora(utf8_decode("Se modificaron suspension empleado, Código: $numCod  del $dtDel  al $dtAl Usuario: ").$usuNom, $bitMod, $bitForm));
	}

	$db->close();

	return json_encode($resp);
}

function borrar() {
	global $empCod, $usuNom, $bitMod, $bitForm, $numCod, $dtDel;

	$priv = new privileges();
	if (!$priv->modify($_SESSION['usuPriv'])) {
		return json_encode($priv->rErrorModify());
	}

	$func = new functions();

	$numCod = utf8_decode($_POST["numCod"]);
	$dtDel = utf8_decode($_POST["dtDel"]);

	# QUERYS PARA UN REGISTRO
	$dSuspen = "DELETE FROM suspen WHERE sus_emp='$empCod' AND sus_cod='$numCod' AND sus_del='$dtDel'";

	# EJECUCION DE QUERYS 
	$db = new dbExec();
	$resp = $db->dEx($dSuspen);
	$db->iEx($func->iBitacora(utf8_decode("Se Borro Suspension empleado, Código: $numCod  del $dtDel Usuario: ").$usuNom, $bitMod, $bitForm));

	$db->close();

	return json_encode($resp);
}

?>