 <?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['op_num_nco'] = $_GET['op_num_nco'];
$_SESSION['op_num_ncf'] = $_GET['op_num_ncf'];
$_SESSION['po_op_id'] = $_GET['po_op_id'];

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

$op_num_ncf = -1;
$sql = "select op_num_ncf from " . $_SESSION['database'] . ".tmp_operacion " .
    " where op_num_ncf = '" . $_SESSION['op_num_ncf'] . "'";

$qur = $connection->query($sql);

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

    if ($r['op_num_ncf'] != null) {
        $op_num_ncf = $r['op_num_ncf'];
    }

}

if ($op_num_ncf == -1) {
    $sql = "select op_num_ncf from " . $_SESSION['database'] . ".operacion " .
        " where op_num_ncf = '" . $_SESSION['op_num_ncf'] . "'";

    $qur = $connection->query($sql);

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

        if ($r['op_num_ncf'] != null) {
            $op_num_ncf = $r['op_num_ncf'];
        }

    }
}

if ($op_num_ncf == -1) {
    $sql = "update " . $_SESSION['database'] . ".tmp_operacion set op_num_ncf = '" . $_SESSION['op_num_ncf'] . "', op_num_nco = '" . $_SESSION['op_num_nco'] .
        "' where op_id = '" . $_SESSION['po_op_id'] . "'";

    $qur = $connection->query($sql);
}

$json->op_num_ncf = $op_num_ncf;

//print_r($sql);

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

    $json->datos = "false";

} else {

    $json->datos = "true";

}

print_r(json_encode($json));

@mysqli_close($connection);
?>