<?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 = "cnt4040";
$bitMod = "cnt";
$empCod = $_SESSION['empCod'];
$usuCod = utf8_decode($_SESSION['usuCod']);
$usuNom = utf8_decode($_SESSION['usuNom']);

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

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

function consultar() {
	global $empCod;

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

	$func = new functions();

	$selMesCod=$_POST['selMesCod'];
	$numAno=$_POST['numAno'];

	# QUERYS
	$sCiemes = "SELECT * FROM ciemes WHERE ciem_emp='$empCod' AND ciem_mes='$selMesCod' AND ciem_ano='$numAno'";
	
	# EJECUCION DE QUERYS
	$db = new dbExec();
	$resp = $db->sEx($sCiemes, "ciemes");
	$db->close();

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


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

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

	$func = new functions();

	$selMesCod=$_POST['selMesCod'];
	$selMesNom=$_POST['selMesNom'];	
	$numAno=$_POST['numAno'];
	$txtStat="A";

	# QUERYS PARA UN REGISTRO
	$uCiemes = "UPDATE ciemes SET ciem_stat='$txtStat' WHERE ciem_emp='$empCod' AND ciem_mes='$selMesCod' AND ciem_ano='$numAno'";
	
	# EJECUCION DE QUERYS 
	$db = new dbExec();

	///if ($func->stringToBool($_POST['nuevo'])) {
	///} else {
		$resp = $db->uEx($uCiemes);
		$db->iEx($func->iBitacora(utf8_decode("Se Abrio el mes $selMesNom del $numAno Usuario: ").$usuNom, $bitMod, $bitForm));
	///}
	
	$db->close();

	return json_encode($resp);
}

?>