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

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

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

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

	$func = new functions();

	$selTipoCod=$_POST['selTipoCod'];
	$selBanCod=$_POST['selBanCod'];
	$dtFecI=$_POST['dtFecI'];
	$dtFecF=$_POST['dtFecF'];
	$crCP=$_POST['crCP'];

	# QUERYS
	$sMov_Acuc = "SELECT *, trim(ma.ma_bene) AS ma_bene, trim(ma.ma_con) AS ma_con FROM mov_acu ma 
          WHERE ma_emp='$empCod' AND ma_tipo='$selTipoCod' AND ma_ban='$selBanCod' AND ma_fec>='$dtFecI' AND ma_fec<='$dtFecF' AND ma_stat='C' ORDER BY ma_emp,ma_tipo,ma_num";

	$sMov_Acup = "SELECT *, trim(ma.ma_bene) AS ma_bene, trim(ma.ma_con) AS ma_con FROM mov_acu ma 
          WHERE ma_emp='$empCod' AND ma_tipo='$selTipoCod' AND ma_ban='$selBanCod' AND ma_fec>='$dtFecI' AND ma_fec<='$dtFecF' AND ma_stat!='C' ORDER BY ma_emp,ma_tipo,ma_num";

	# EJECUCION DE QUERYS
	$db = new dbExec();
	if ($crCP == "C") {
  	  //$resp = $db->sEx($sMov_Acuc, "mov_acu");
	  $resp["mov_acu"] = $db->sFetchAll($sMov_Acuc);
	} else {
  	  //$resp = $db->sEx($sMov_Acup, "mov_acu");
	  $resp["mov_acu"] = $db->sFetchAll($sMov_Acup);
	}
	$db->close();
	$func = new functions();
	return $func->jsonEncodeArray($resp, 'mov_acu');
}

function procesar() {
	global $empCod, $usuNom, $selBanCod, $selTipoCod, $nuevostat;

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

	$func = new functions();

	$selTipoCod=$_POST['selTipoCod'];
	$selBanCod=$_POST['selBanCod'];
	$dtFecI=$_POST['dtFecI'];
	$dtFecF=$_POST['dtFecF'];
	$jsonMova=json_decode($_POST['tblMa']);
	$crCP=$_POST['crCP'];

	if ($crCP == "C") {
	  $nuevostat="P";
	} else {
	  $nuevostat="C";
	}
    
	# QUERYS
	function uMova($numd) { 
		global $empCod, $usuNom, $selBanCod, $selTipoCod, $nuevostat;
		return "UPDATE mov_acu SET ma_stat='$nuevostat' WHERE ma_emp='$empCod' AND ma_tipo='$selTipoCod' AND ma_ban='$selBanCod' AND ma_num=$numd";
	}

	# EJECUCION DE QUERYS
	$db = new dbExec();
	// PARA ACTUALIZAR STATUS DE DOCUMENTOS
	foreach ($jsonMova as $obj) {
		$numd = $obj->numero;
		$db->query(uMova($numd));
	}
	$db->close();
	return json_encode($numd);
}

?>