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

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

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

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

function consultarFamii() {
	global $empCod;
	$txtCod=utf8_decode($_POST['txtCod']);

	# QUERYS
	$sFamii = "SELECT *, TRIM(dep_nom) AS dep_nom FROM deptosi where dep_cod='$txtCod'";

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

	$func = new functions();

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


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

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

	$func = new functions();

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

	# QUERYS PARA UN REGISTRO
	$iFamii = "INSERT INTO deptosi(dep_cod, dep_nom) VALUES ('$txtCod','$txtNom')";

	$uFamii = "UPDATE deptosi SET dep_nom='$txtNom' WHERE dep_cod='$txtCod'";

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

	return json_encode($resp);
}

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

	$func = new functions();

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

	# QUERYS
	$dFamii = "DELETE FROM deptosi WHERE dep_cod='$txtCod'";

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

	return json_encode($resp);
}
?>