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

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

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

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

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

	# QUERYS
	$sBod = "SELECT *, TRIM(bod_nom) AS bod_nom FROM bodegas where bod_cod='$txtCod'";

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

	$func = new functions();

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


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']);
	$crTienda=$_POST['crTienda'];	

	# QUERYS PARA UN REGISTRO
	$iBod = "INSERT INTO bodegas(bod_cod, bod_nom, bod_tienda) VALUES ('$txtCod','$txtNom','$crTienda')";

	$uBod = "UPDATE bodegas SET bod_nom='$txtNom',bod_tienda='$crTienda' WHERE bod_cod='$txtCod'";

	# EJECUCION DE QUERYS 
	$db = new dbExec();
	if ($func->stringToBool($_POST['nuevo'])) {
		$resp = $db->iEx($iBod);
		$db->iEx($func->iBitacora(utf8_decode("Se creó Bodega, Codigo: $txtCod   txtNom Usuario: ").$usuNom, $bitMod, $bitForm));
	} else {
		$resp = $db->uEx($uBod);
		$db->iEx($func->iBitacora(utf8_decode("Se modificó Bodega, 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
	$dBod = "DELETE FROM bodegas WHERE bod_cod='$txtCod'";

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

	return json_encode($resp);
}
?>