 <?php

if (isset($_SERVER['HTTP_ORIGIN'])) {
    header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 86400'); // cache for 1 day
}

error_reporting(E_ERROR | E_PARSE);

$_SESSION['database'] = $_GET['database'];
$_SESSION['suc_id'] = $_GET['suc_id'];
$_SESSION['e_id'] = $_GET['e_id'];

include 'conn.php';
include 'funciones.php';

$suc_id = $_SESSION['suc_id'];
$estacion = $_SESSION['e_id'];

$sqlOrden = "select  max(op_num_corre) as op_num_corre from " . $_GET['database'] . ".operacion  where suc_id = " . $suc_id . " and op_estacion =" . $estacion . " and op_tipo = 2";
$qur = $connection->query($sqlOrden);

//print_r($sqlOrden);

while ($rOrd = mysqli_fetch_assoc($qur)) {

    $op_num_corre = $rOrd['op_num_corre'];

}

$sqlOrden = "select  max(op_num_corre) as op_num_corre from " . $_GET['database'] . ".operacion  where suc_id = " . $suc_id . " and op_estacion =" . $estacion . " and op_tipo = 16";
$qur = $connection->query($sqlOrden);

//print_r($sqlOrden);

while ($rOrd = mysqli_fetch_assoc($qur)) {

    $op_num_corre_d = $rOrd['op_num_corre'];

}

$sqlOrden = "select  max(op_id) as op_id from " . $_GET['database'] . ".operacion  where suc_id = " . $suc_id . " and op_estacion =" . $estacion . "";
$qur = $connection->query($sqlOrden);

//print_r($sqlOrden);

while ($rOrd = mysqli_fetch_assoc($qur)) {

    $op_id = $rOrd['op_id'];

}

$sqlOrden = "select  max(ci_op_num_corre) as ci_op_num_corre from " . $_GET['database'] . ".cierres_gen_v2  where tipo = 'x' and suc_id = " . $suc_id . " and ci_op_estacion =" . $estacion . "";
$qur = $connection->query($sqlOrden);

//print_r($sqlOrden);

while ($rOrd = mysqli_fetch_assoc($qur)) {

    $ci_op_num_corre = $rOrd['ci_op_num_corre'];

}

$ci_op_num_correz = 0;
$sqlOrden = "select  max(ci_op_num_corre) as ci_op_num_corre from " . $_GET['database'] . ".cierres_gen_v2  where tipo = 'z' and suc_id = " . $suc_id . " and ci_op_estacion =" . $estacion . "";
$qur = $connection->query($sqlOrden);

//print_r($sqlOrden);

while ($rOrd = mysqli_fetch_assoc($qur)) {

    $ci_op_num_correz = $rOrd['ci_op_num_corre'];

}

//print_r($sqlOrden);

if ($_SESSION['codigo'] == "99") {

    $json->datos = "false";

} else {

    $json->datos = "true";

    $jsonTempData1 = array();
    $jsonTempData1['op_num_corre'] = $op_num_corre;
    $jsonTempData1['op_num_corre_d'] = $op_num_corre_d;
    $jsonTempData1['op_id'] = substr("$op_id", -6);
    $jsonTempData1['ci_op_num_corre'] = $ci_op_num_corre;
    $jsonTempData1['ci_op_num_correz'] = $ci_op_num_correz;
    $jsonData1[] = $jsonTempData1;
    $json->ordenProc = $jsonData1;

}

print_r(json_encode($json));

$qur = null;
@mysqli_close($connection);
?>