<?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 = "nom4030";
$bitMod = "nom";
$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();

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

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

	
	# EJECUCION DE QUERYS
	$db = new dbExec();
	$codigo=$numCod;
	$resp = $db->sEx($sMaestro, "maestro");
	//$select["maestro"] = $db->query($sMaestro);
	$resp["imgArtic"] = "imgEmpleados/$empCod"."_".$func->imgCharacters($codigo).".jpeg?".rand(1,9999);
	$db->close();

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

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"]);
//	$selTsal = utf8_decode($_POST["selTsal"]);
	$numSueldo = (!empty($_POST["numSueldo"])) ? $_POST["numSueldo"] : 0;
	$numBonil = (!empty($_POST["numBonil"])) ? $_POST["numBonil"] : 0;
	$numBonii = (!empty($_POST["numBonii"])) ? $_POST["numBonii"] : 0;
	$numBonoe = (!empty($_POST["numBonoe"])) ? $_POST["numBonoe"] : 0;
	$numViati = (!empty($_POST["numViati"])) ? $_POST["numViati"] : 0;
	$numDepre = (!empty($_POST["numDepre"])) ? $_POST["numDepre"] : 0;

	# QUERYS PARA UN REGISTRO
	$uMaestro = "UPDATE maestro SET mae_sueldo='$numSueldo',mae_bonil='$numBonil',mae_bonii='$numBonii',mae_bonoe='$numBonoe',mae_viati='$numViati',mae_depre='$numDepre' WHERE mae_emp='$empCod' AND mae_cod='$numCod'";

	# EJECUCION DE QUERYS 
	$db = new dbExec();
	$resp = $db->uEx($uMaestro);
	$db->iEx($func->iBitacora(utf8_decode("Se modificaron Ingresos Fijos del empleado, Código: $numCod Usuario: ").$usuNom, $bitMod, $bitForm));
	
	$db->close();

	if (isset($_FILES['file'])) {
		$func->uploadEmpCod($empCod, $numCod, $_FILES['file'], '../imgEmpleados');
	}

	return json_encode($resp);
}

?>