<?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 = "pov4030";
$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 'consultarResolu':
		echo consultarResolu();
		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_resolu) AS caj_resolu
				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 consultarResolu() {
	global $empCod;

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

	$func = new functions();

	$txtNumr=utf8_decode($_POST['txtNumr']);

	# QUERYS
	$sResolu = "SELECT *, TRIM(r.res_serie) AS res_serie, TRIM(r.res_stat) AS res_stat
				FROM resolu r
				WHERE res_emp='$empCod' AND res_num='$txtNumr'";

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

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

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

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

	$func = new functions();

	$txtNumr = utf8_decode($_POST["txtNumr"]);
	$tipo = utf8_decode($_POST["tipo"]);
	$serie = utf8_decode($_POST["serie"]);

	# QUERYS PARA UN REGISTRO

	$uCaja = "UPDATE caja SET caj_tipofac='$tipo', caj_seriefac='$serie',caj_resolu='$txtNumr' 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 Asigno resolucion:  $txtNumr  a la Caja: $pCaja Usuario: ").$usuNom, $bitMod, $bitForm));
	$db->close();
	return json_encode($resp);
}

?>