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

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

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

	$func = new functions();

	$dtFec=date("Y-m-d");

	# QUERYS
	function iAutoriza($numauto) { 
	    global $empCod, $usuNom, $dtFec;
	    $desc="Autorizada por: ".$usuNom;
	    return "INSERT INTO autoriza(aut_num, aut_fec,aut_desc) VALUES('$numauto','$dtFec','$desc')";
	}

	function sAutoriza($numauto) { 
	    global $empCod, $usuNom, $dtFec;
	    return "SELECT * FROM autoriza WHERE aut_num='$numauto'";
	}

	# EJECUCION DE QUERYS 
	$db = new dbExec();
	// Para mandar todo a cero
	$flag="S";

	while ($flag=="S") {
		$numauto = mt_rand(1,999999);
	    $select["autoriza"] = $db->query(sAutoriza($numauto));
    	if ($db->numRows($select["autoriza"]) > 0) {
    		continue;
    	}
    	$flag="N";
    	$db->query(iAutoriza($numauto));
	}

	$db->close();
	$resp["numauto"]=$numauto;
	return json_encode($resp);
}
?>