<?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 = "fel1030";
$bitMod = "fel";
$empCod = $_SESSION['empCod'];

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

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

function consultarFacturas() {
	global $empCod;

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

	$dtFecIni = $_POST["dtFecIni"];
	$dtFecFin = $_POST["dtFecFin"];

	# QUERYS
	$sFacturas = "SELECT *, TRIM(cli.cli_email) AS cli_email
				FROM facturas fac LEFT JOIN cliencc cli ON fac.fact_codp=cli.cli_cod 
				WHERE fact_emp='$empCod' AND fact_fec>='$dtFecIni' AND fact_fec<='$dtFecFin' GROUP BY fact_idinterno ORDER BY fact_idinterno DESC";
	
	# EJECUCION DE QUERYS
	$db = new dbExec();
	$resp["facturas"] = $db->sFetchAll($sFacturas);
	$db->close();

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

function imprimir() {
	global $empCod;

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

	$_SESSION["tblFac"] = $_POST["tblFac"];
	return "";
}
?>