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

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

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

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

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

}

function consultar() {
	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 consultarSerie() {
	global $empCod;

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

	$func = new functions();

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

	# QUERYS
	$sCorrTipov = "SELECT *
				FROM corrtipov
				WHERE ctv_emp='$empCod' AND ctv_tipo='$txtSerie'";

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

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

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

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

	$func = new functions();

	$txtNumr = utf8_decode($_POST["txtNumr"]);
	$dtFec = $_POST["dtFec"];
	$txtSerie = $_POST["txtSerie"];
	$numCorri = $_POST["numCorri"];
	$numCorrf = $_POST["numCorrf"];
	$dtFecven = $_POST["dtFecven"];
	$stat = $_POST["stat"];
	$fecg = $_POST["fecg"];

	# QUERYS PARA UN REGISTRO
	$iResolu = "INSERT INTO resolu(res_emp, res_num, res_fec, res_tipo, res_serie, res_ini, res_fin, res_stat, res_fecg, res_tipod, res_fecven) VALUES ('$empCod','$txtNumr','$dtFec','$txtSerie','$txtSerie','$numCorri','$numCorrf','$stat','$fecg','FACTURA','$dtFecven')";

	$uResolu = "UPDATE resolu SET res_fec='$dtFec', res_tipo='$txtSerie', res_serie='$txtSerie', res_fin='$numCorrf', res_fecven='$dtFecven' WHERE res_emp='$empCod' AND res_num='$txtNumr'";

	$sTipov = "SELECT * FROM tipov WHERE tv_cod='$txtSerie'";

	$iTipov = "INSERT INTO tipov(tv_cod, tv_nom, tv_impues, tv_pone, tv_abrev, tv_librosn, tv_seriesn, tv_codasiste, tv_fel, tv_soloinv, tv_partidasn) VALUES ('$txtSerie','$txtSerie','N','P','$txtSerie','S','N','FC','N','N','S')";

	$iCorrTipov = "INSERT INTO corrtipov(ctv_emp, ctv_tipo, ctv_num) VALUES ('$empCod','$txtSerie',1)";

	# EJECUCION DE QUERYS 
	$db = new dbExec();
	if ($func->stringToBool($_POST['nuevo'])) {
		$resp = $db->iEx($iResolu);
		$db->iEx($func->iBitacora(utf8_decode("Se creó Resolucion No.  $txtNumr  Usuario: ").$usuNom, $bitMod, $bitForm));
	} else {
		$resp = $db->uEx($uResolu);
		$db->iEx($func->iBitacora(utf8_decode("Se modificaron Resolucion No.  $txtNumr  Usuario: ").$usuNom, $bitMod, $bitForm));
	}

	if ($func->stringToBool($_POST['nuevoserie'])) {
		$resp = $db->iEx($iCorrTipov);
	}

	$select["tipov"] = $db->query($sTipov);
	if ($db->numRows($select["tipov"]) == 0) {
		$db->iEx($iTipov);		
    }

	$db->close();

	return json_encode($resp);
}

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

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

	$func = new functions();

	$txtNumr = $_POST["txtNumr"];

	# QUERYS PARA UN REGISTRO
	$dResolu = "DELETE FROM resolu WHERE res_emp='$empCod' AND res_num='$txtNumr'";

	# EJECUCION DE QUERYS 
	$db = new dbExec();
	$resp = $db->dEx($dResolu);
	$db->iEx($func->iBitacora(utf8_decode("Se Borro Resolucion No.  $txtNumr   Usuario: ").$usuNom, $bitMod, $bitForm));

	$db->close();

	return json_encode($resp);
}

?>