<?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');
include_once('../../../php/maxExecTime.php');

$bitForm = "ban1030";
$bitMod = "ban";
$empCod = $_SESSION['empCod'];
$usuCod = utf8_decode($_SESSION['usuCod']);
$usuNom = utf8_decode($_SESSION['usuNom']);

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

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

}

function consultar() {
	global $empCod, $usuNom;

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

	$tipoCod="004";
	$numNum=$_POST['numNum'];
	$selBanCod=$_POST['selBanCod'];

	# QUERYS
	$sCheques = "SELECT * FROM cheques WHERE ch_emp='$empCod' AND ch_ban='$selBanCod' AND ch_tipo='$tipoCod' AND ch_num='$numNum'";

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

	$func = new functions();
	return $func->jsonEncodeArray($resp, 'cheques');
}

function imprimir() {
	global $empCod, $usuNom;

	$numChi=$_POST["numChi"];
	$numChf=$_POST["numChf"];
	$selBanCod=$_POST["selBanCod"];

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

	# QUERYS
	$sCheques = "SELECT *, TRIM(m.mes_nom) AS mes_nom 
		FROM cheques ch 
		LEFT JOIN bancos ban ON ban.ban_emp=ch.ch_emp AND ban.ban_num=ch.ch_ban 
		LEFT JOIN meses m ON month(ch.ch_fec)=m.mes_cod 
		WHERE ch_emp='$empCod' AND ch_ban='$selBanCod' AND ch_num>=$numChi AND ch_num<=$numChf";

	$sPoli_det = "SELECT *, TRIM(pold_cta) AS pold_cta FROM poli_det 
		WHERE pold_emp='$empCod' AND pold_ban='$selBanCod' AND pold_tipo='004' AND pold_num>=$numChi AND pold_num<=$numChf";

	# EJECUCION DE QUERYS
	$db = new dbExec();
	$resp["cheques"] = $db->sFetchAll($sCheques);
	$resp["poli_det"] = $db->sFetchAll($sPoli_det);
	$db->close();

	$resp["asdf"] = $sCheques;

	$func = new functions();
	return $func->jsonEncodeArray($resp, 'cheques');
}
?>