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

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

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

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

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

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

	# QUERYS
	$sTipoac = "SELECT ta.*, TRIM(ta.ta_nom) AS ta_nom,
				TRIM(ta.ta_trela) AS ta_trela,
				TRIM(tv.tv_nom) AS ta_trelanom
				FROM tipoaboc as ta
				LEFT JOIN tipov tv ON ta.ta_trela = tv.tv_cod
				WHERE ta_cod='$txtCod'";

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

	$func = new functions();

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

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

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

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


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

	# QUERYS PARA UN REGISTRO
	$iTipoac = "INSERT INTO tipoaboc(ta_cod, ta_nom, ta_trela, ta_afe, ta_partsn, ta_otros) VALUES ('$txtCod','$txtNom','$txtTrela','$crAfe','$crGenp','$crOtros')";

	$uTipoac = "UPDATE tipoaboc SET ta_nom='$txtNom',ta_trela='$txtTrela',ta_afe='$crAfe',ta_partsn='$crGenp',ta_otros='$crOtros' WHERE ta_cod='$txtCod'";

	# EJECUCION DE QUERYS 
	$db = new dbExec();
	if ($func->stringToBool($_POST['nuevo'])) {
		$resp = $db->iEx($iTipoac);
		$db->iEx($func->iBitacora(utf8_decode("Se creó Tipoac Abono cxc, Codigo: $txtCod   txtNom Usuario: ").$usuNom, $bitMod, $bitForm));
	} else {
		$resp = $db->uEx($uTipoac);
		$db->iEx($func->iBitacora(utf8_decode("Se modificó Tipoac Abono cxc, 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
	$dTipoac = "DELETE FROM tipoaboc WHERE ta_cod='$txtCod'";

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

	return json_encode($resp);
}
?>