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

switch ($_POST['action']) {
	case 'consultar':
		echo consultar();
		break;
	case 'guardar':
		echo guardar();
		break;
}

function consultar() {
	global $empCod, $pCaja;

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

	$func = new functions();

	# QUERYS
	$sCaja = "SELECT c.*, 
				TRIM(c.caj_nomt) AS caj_nomt, 
				TRIM(c.caj_dirt) AS caj_dirt,
				TRIM(c.caj_telt) AS caj_telt,
				TRIM(c.caj_mailt) AS caj_mailt,
				TRIM(c.caj_leyen1) AS caj_leyen1,
				TRIM(c.caj_leyen2) AS caj_leyen2

				FROM caja c
				WHERE caj_emp='$empCod' AND caj_cod='$pCaja'";

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

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


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

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

	$func = new functions();

	$txtNomt = utf8_decode($_POST["txtNomt"]);
	$txtDirt = utf8_decode($_POST["txtDirt"]);
	$txtTelt = utf8_decode($_POST["txtTelt"]);
	$txtMailt = utf8_decode($_POST["txtMailt"]);
	$txtLeyen1 = utf8_decode($_POST["txtLeyen1"]);
	$txtLeyen2 = utf8_decode($_POST["txtLeyen2"]);

	# QUERYS PARA UN REGISTRO

	$uCaja = "UPDATE caja SET caj_nomt='$txtNomt', caj_dirt='$txtDirt',caj_telt='$txtTelt',caj_mailt='$txtMailt',caj_leyen1='$txtLeyen1',caj_leyen2='$txtLeyen2' WHERE caj_emp='$empCod' AND caj_cod='$pCaja'";

	# EJECUCION DE QUERYS 
	$db = new dbExec();
	$resp = $db->uEx($uCaja);
	$db->iEx($func->iBitacora(utf8_decode("Se modificaron Datos Generales del Establecimiento Caja: $pCaja Usuario: ").$usuNom, $bitMod, $bitForm));
	$db->close();
	return json_encode($resp);
}

?>