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

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

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

	case 'consultarCtadif':
		echo consultarCtadif();
		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();

	$numNumb=$_POST['numNumb'];

	# QUERYS
	$sBancos = "SELECT ban.*,
				TRIM(ban.ban_nom) AS ban_nom, 
				TRIM(ban.ban_ctab) AS ban_ctab,
				TRIM(ban.ban_ctac) AS ban_ctac,
				TRIM(ban.ban_ctadif) AS ban_ctadif,
				TRIM(cta.cta_nom) AS ban_ctacnom,
				TRIM(dcta.cta_nom) AS ban_ctadnom 
				FROM bancos AS ban
				LEFT JOIN cuentas cta ON ban.ban_emp = cta.cta_emp AND ban.ban_ctac=cta.cta
				LEFT JOIN cuentas dcta ON ban.ban_emp = dcta.cta_emp AND ban.ban_ctadif=dcta.cta
				WHERE ban.ban_emp='$empCod' AND ban.ban_num='$numNumb'";
	
	# EJECUCION DE QUERYS
	$db = new dbExec();
	$resp = $db->sEx($sBancos, "bancos");
	$db->close();

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

function consultarCtac() {
	global $empCod;
	$txtCtac=utf8_decode($_POST['txtCtac']);

	# QUERYS
	$sCuentas = "SELECT *, TRIM(cta) AS cta, TRIM(cta_nom) AS cta_nom
				FROM cuentas  WHERE cta='$txtCtac' AND cta_emp='$empCod'";
	
	# EJECUCION DE QUERYS
	$db = new dbExec();
	$resp = $db->sEx($sCuentas, "cuentas");
	$db->close();

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

function consultarCtadif() {
	global $empCod;
	$txtCtadif=utf8_decode($_POST['txtCtadif']);

	# QUERYS
	$sCuentas = "SELECT *, TRIM(cta) AS cta, TRIM(cta_nom) AS cta_nom
				FROM cuentas  WHERE cta='$txtCtadif' AND cta_emp='$empCod'";
	
	# EJECUCION DE QUERYS
	$db = new dbExec();
	$resp = $db->sEx($sCuentas, "cuentas");
	$db->close();

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

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

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

	$func = new functions();

	$numNumb=$_POST['numNumb'];
	$txtNom=utf8_decode($_POST['txtNom']);
	$txtCtab=utf8_decode($_POST['txtCtab']);
	$txtCtac=utf8_decode($_POST['txtCtac']);
	$txtCtadif=utf8_decode($_POST['txtCtadif']);
	$numNumi=utf8_decode($_POST['numNumi']);
	$numNumf=utf8_decode($_POST['numNumf']);
	$numCorr=utf8_decode($_POST['numCorr']);
	$selForc=$_POST['selForc'];
	$selMon=$_POST['selMon'];

	# QUERYS PARA UN REGISTRO
	$iBancos = "INSERT INTO bancos(ban_emp, ban_num, ban_nom, ban_ctab, ban_ctac, ban_ctadif, ban_numi, ban_numf, ban_corr, ban_forma, ban_mon, ban_ulcam) VALUES ('$empCod','$numNumb','$txtNom','$txtCtab','$txtCtac','$txtCtadif','$numNumi','$numNumf','$numCorr','$selForc','$selMon','$usuNom')";

	$uBancos = "UPDATE bancos SET ban_nom='$txtNom',ban_ctab='$txtCtab',ban_ctac='$txtCtac',ban_ctadif='$txtCtadif',ban_numi='$numNumi',ban_numf='$numNumf',ban_corr='$numCorr',ban_forma='$selForc',ban_mon='$selMon',ban_ulcam='$usuNom' WHERE ban_emp='$empCod' AND ban_num='$numNumb'";

	# EJECUCION DE QUERYS 
	$db = new dbExec();
	if ($func->stringToBool($_POST['nuevo'])) {
		$resp = $db->iEx($iBancos);
		$db->iEx($func->iBitacora(utf8_decode("Se creó Banco, Numero: $numNumb Usuario: ").$usuNom, $bitMod, $bitForm));
	} else {
		$resp = $db->uEx($uBancos);
		$db->iEx($func->iBitacora(utf8_decode("Se modificó Banco, Numero: $numNumb Usuario: ").$usuNom, $bitMod, $bitForm));
	}
	
	$db->close();

	return json_encode($resp);
}

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

	$func = new functions();

	$numNumb=$_POST['numNumb'];

	# QUERYS
	$dBancos = "DELETE FROM bancos WHERE ban_emp='$empCod' AND ban_num='$numNumb'";

	# EJECUCION DE QUERYS 
	$db = new dbExec();
	$resp = $db->dEx($dBancos);
	$db->iEx($func->iBitacora(utf8_decode("Se borró Banco, Numero: $numNumb Usuario: ").$usuNom, $bitMod, $bitForm));
	$db->close();

	return json_encode($resp);
}
?>