<?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');

$pCaja = $_SESSION['pCaja'];

$bitForm = "pov1035";
$bitMod = "pov";
$empCod = $_SESSION['empCod'];

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

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

function consultarFacturas() {
	global $empCod,$pCaja;

	$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, trim(tv.tv_nom) AS tv_nom
				FROM facturas fac LEFT JOIN clien cli ON fac.fact_nit=cli.cli_nit LEFT JOIN tipov tv ON fac.fact_tipo=tv.tv_cod
				WHERE fact_emp='$empCod' AND fact_fec>='$dtFecIni' AND fact_fec<='$dtFecFin' AND fact_caja='$pCaja' AND fact_documentguid='' 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 "";
}
?>