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

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


function cerrar() {
	global $empCod, $usuNom, $bitMod, $bitForm;

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

	$func = new functions();

	$jsonPlani=json_decode($_POST['tblPlani']);

	# QUERY
	$sPlani = "SELECT * 
              FROM ctrpla  
              WHERE ctrp_emp='$empCod' AND ctrp_stat!='C' AND ctrp_stat!='B' ORDER BY ctrp_num";

	# QUERY PARA MARCAR COMO AUTORIZADA LA PLANILLA
	function uPlani($num) { 
		global $empCod, $usuNom;
		return "UPDATE ctrpla SET ctrp_stat='C' WHERE ctrp_emp='$empCod' AND ctrp_num='$num'";
	}

	# EJECUCION DE QUERYS 
	$db = new dbExec();
	// PARA ACTUALIZAR EL STATUS
	foreach ($jsonPlani as $obj) {
		$numero = $obj->num;
		$db->query(uPlani($numero));
		$db->iEx($func->iBitacora(utf8_decode("Se Cerro Planilla No.: $numero ").$usuNom, $bitMod, $bitForm));
	}

	$resp["ctrpla"] = $db->sFetchAll($sPlani);
	$db->close();

	return $func->jsonEncodeArray($resp, 'ctrpla');
//	return json_encode($resp);	
}
?>