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

switch ($_POST['action']) {
	case 'consultarFamii':
		echo consultarFamii();
		break;
	case 'consultarSubFamii':
		echo consultarSubFamii();
		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 consultarSubFamii() {
	global $empCod;
	$txtDep=utf8_decode($_POST['txtDep']);
	$txtCod=utf8_decode($_POST['txtCod']);

	# QUERYS
	$sSubFamii = "SELECT *, TRIM(sdep_nom) AS sdep_nom FROM subdeptosi sdi LEFT JOIN deptosi di ON di.dep_cod=sdi.sdep_dep WHERE sdep_dep='$txtDep' AND sdep_cod='$txtCod'";

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

	$func = new functions();

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


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

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

	$func = new functions();

	$txtDep=$_POST['txtDep'];
	$txtCod=$_POST['txtCod'];
	$txtNom=utf8_decode($_POST['txtNom']);
	$numCorr=(empty($_POST['numCorr'])) ? 0 : $_POST['numCorr'];

	# QUERYS PARA UN REGISTRO
	$iSubFamii = "INSERT INTO subdeptosi(sdep_dep,sdep_cod,sdep_nom,sdep_corr) VALUES ('$txtDep','$txtCod','$txtNom','$numCorr')";

	$uSubFamii = "UPDATE subdeptosi SET sdep_nom='$txtNom', sdep_corr='$numCorr' WHERE sdep_dep='$txtDep' AND sdep_cod='$txtCod'";

	# EJECUCION DE QUERYS 
	$db = new dbExec();
	if ($func->stringToBool($_POST['nuevo'])) {
		$resp = $db->iEx($iSubFamii);
		$db->iEx($func->iBitacora(utf8_decode("Se creó Sub-Familia Inventario, Codigo: $txtCod   txtNom Usuario: ").$usuNom, $bitMod, $bitForm));
	} else {
		$resp = $db->uEx($uSubFamii);
		$db->iEx($func->iBitacora(utf8_decode("Se modificó Sub-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();

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

	# QUERYS
	$dSubFamii = "DELETE FROM subdeptosi WHERE sdep_dep='$txtDep' AND sdep_cod='$txtCod'";

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

	return json_encode($resp);
}
?>