<?php
include('../../../php/sessionStart.php');
include('../../../php/db/dbExec.php');
include('../../../php/functions.php');

echo buscar();

function buscar() {
	$dtFecIni=utf8_decode(TRIM($_POST['dtFecIni']));
	$dtFecFin=utf8_decode(TRIM($_POST['dtFecFin']));
	$numValor=$_POST['numValor'];
	$txtNom=utf8_decode(TRIM($_POST['txtNom']));
	$empCod=$_SESSION['empCod'];

	# QUERYS
	if ($numValor == 0.00 && !empty($txtNom)) {
		$sCheques = "SELECT *, 
				TRIM(ch.ch_bene) AS ch_bene,
				TRIM(ch.ch_con)  AS ch_con,
				TRIM(ban.ban_nom) AS ban_nom
				FROM cheques ch LEFT JOIN bancos ban ON ch.ch_emp=ban.ban_emp AND ch.ch_ban=ban.ban_num
				WHERE ch_emp='$empCod' AND ch.ch_fec>='$dtFecIni' AND ch.ch_fec<='$dtFecFin' AND ch_bene LIKE '%$txtNom%'";
	}
	if ($numValor != 0.00 && empty($txtNom)) {
		$sCheques = "SELECT *, 
				TRIM(ch.ch_bene) AS ch_bene,
				TRIM(ch.ch_con)  AS ch_con,
				TRIM(ban.ban_nom) AS ban_nom
				FROM cheques ch LEFT JOIN bancos ban ON ch.ch_emp=ban.ban_emp AND ch.ch_ban=ban.ban_num
				WHERE ch_emp='$empCod' AND ch.ch_fec>='$dtFecIni' AND ch.ch_fec<='$dtFecFin' AND ch_tot=$numValor";
	}
	if ($numValor != 0.00 && !empty($txtNom)) {
		$sCheques = "SELECT *, 
				TRIM(ch.ch_bene) AS ch_bene,
				TRIM(ch.ch_con)  AS ch_con,
				TRIM(ban.ban_nom) AS ban_nom
				FROM cheques ch LEFT JOIN bancos ban ON ch.ch_emp=ban.ban_emp AND ch.ch_ban=ban.ban_num
				WHERE ch_emp='$empCod' AND ch.ch_fec>='$dtFecIni' AND ch.ch_fec<='$dtFecFin' AND ch_tot=$numValor AND ch_bene LIKE '%$txtNom%'";
	}
	if ($numValor == 0.00 && empty($txtNom)) {
		$sCheques = "SELECT *, 
				TRIM(ch.ch_bene) AS ch_bene,
				TRIM(ch.ch_con)  AS ch_con,
				TRIM(ban.ban_nom) AS ban_nom
				FROM cheques ch LEFT JOIN bancos ban ON ch.ch_emp=ban.ban_emp AND ch.ch_ban=ban.ban_num
				WHERE ch_emp='$empCod' AND ch.ch_fec>='$dtFecIni' AND ch.ch_fec<='$dtFecFin'";
	}

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

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