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

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

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

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

function consultarTipomaq() {
	global $empCod;
	$numCod=utf8_decode($_POST['numCod']);

	# QUERYS
	$sTipomaq = "SELECT *, TRIM(tm_nom) AS tm_nom FROM tipomaquina where tm_emp='$empCod' AND tm_cod='$numCod'";

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

	$func = new functions();

	return $func->jsonEncodeArray($resp, "tipomaquina");
}


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

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

	$func = new functions();

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

	# QUERYS PARA UN REGISTRO
	$iTipomaq = "INSERT INTO tipomaquina(tm_emp,tm_cod, tm_nom) VALUES ('$empCod','$numCod','$txtNom')";

	$uTipomaq = "UPDATE tipomaquina SET tm_nom='$txtNom' WHERE tm_emp='$empCod' AND tm_cod='$numCod'";

	# EJECUCION DE QUERYS 
	$db = new dbExec();
	if ($func->stringToBool($_POST['nuevo'])) {
		$resp = $db->iEx($iTipomaq);
		$db->iEx($func->iBitacora(utf8_decode("Se creó Maquinaria, Codigo: $numCod   txtNom Usuario: ").$usuNom, $bitMod, $bitForm));
	} else {
		$resp = $db->uEx($uTipomaq);
		$db->iEx($func->iBitacora(utf8_decode("Se modificó Maquinaria, Codigo: $numCod   $txtNom Usuario: ").$usuNom, $bitMod, $bitForm));
	}
	
	$db->close();

	return json_encode($resp);
}

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

	$func = new functions();

	$numCod=$_POST['numCod'];
	$txtNom=$_POST['txtNom'];

	# QUERYS
	$dTipomaq = "DELETE FROM tipomaquina WHERE tm_emp='$empCod' AND tm_cod='$numCod'";

	# EJECUCION DE QUERYS 
	$db = new dbExec();
	$resp = $db->dEx($dTipomaq);
	$db->iEx($func->iBitacora(utf8_decode("Se borró Maquinaria, Codigo: $numCod  $txtNom Usuario: ").$usuNom, $bitMod, $bitForm));
	$db->close();

	return json_encode($resp);
}
?>