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

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

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

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

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

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

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

	# QUERYS
	$sTipodoci = "SELECT td.*, TRIM(td.td_nom) AS td_nom,
				TRIM(td.td_relatv) AS td_relatv,
				TRIM(tv.tv_nom) AS td_relatvnom,
				TRIM(td.td_relatpol) AS td_relatpol,
				TRIM(tpol.tp_nom) AS td_relatpolnom
				FROM tipodoci as td
				LEFT JOIN tipov tv ON td.td_relatv = tv.tv_cod
				LEFT JOIN tipopol tpol ON td.td_relatpol = tpol.tp_cod
				WHERE td_cod='$txtCod'";

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

	$func = new functions();

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

function consultarTrela() {
	global $empCod;
	$txtTrela=utf8_decode($_POST['txtTrela']);

	# QUERYS
	$sTrela = "SELECT *, TRIM(tv_nom) AS tv_nom
				FROM tipov WHERE tv_cod='$txtTrela'";
	
	# EJECUCION DE QUERYS
	$db = new dbExec();
	$resp = $db->sEx($sTrela, "tipov");
	$db->close();

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

function consultarTrelapol() {
	global $empCod;
	$txtTrelapol=utf8_decode($_POST['txtTrelapol']);

	# QUERYS
	$sTrelapol = "SELECT *, TRIM(tp_nom) AS tp_nom
				FROM tipop WHERE tp_cod='$txtTrelapol'";
	
	# EJECUCION DE QUERYS
	$db = new dbExec();
	$resp = $db->sEx($sTrelapol, "tipopol");
	$db->close();

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

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']);
	$txtTrela=utf8_decode($_POST['txtTrela']);
	$txtTrelapol=utf8_decode($_POST['txtTrelapol']);
	$crEoS=$_POST['crEoS'];
	$crAfecos=$_POST['crAfecos'];
	$crMovbod=$_POST['crMovbod'];
	$crRepven=$_POST['crRepven'];
	$crGenp=$_POST['crGenp'];
	$crInveini=$_POST['crInveini'];

	# QUERYS PARA UN REGISTRO
	$iTipodoci = "INSERT INTO tipodoci(td_cod, td_nom, td_eos, td_afecos, td_movbod, td_repven, td_relatv, td_partsn, td_relatpol, td_inveini) VALUES ('$txtCod','$txtNom','$crEoS','$crAfecos','$crMovbod','$crRepven','$txtTrela','$crGenp','$txtTrelapol','$crInveini')";

	$uTipodoci = "UPDATE tipodoci SET td_nom='$txtNom',td_relatv='$txtTrela',td_relatpol='$txtTrelapol',td_eos='$crEoS',td_afecos='$crAfecos',td_movbod='$crMovbod',td_repven='$crRepven',td_partsn='$crGenp',td_inveini='$crInveini' WHERE td_cod='$txtCod'";

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

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

	return json_encode($resp);
}
?>