 <?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);

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

//$sql= "select  m.puesto, count(o.po_id) as po_id, m.op_id as op_id_puesto from ".$_GET['database'].".mesaspuesto m left outer join ".$_GET['database'].".tmp_producto_op o on m.sala = o.sala and m.mesa = o.mesa and m.puesto = o.ubicacion and m.suc_id = o.suc_id where m.suc_id = "
//                                        .$_SESSION['suc_id']." and m.mesa = ".$_SESSION['mesa']." and m.sala = ".$_SESSION['sala']." group by m.puesto";

//$sql= "select m.puesto, op_id, m.op_id as op_id_puesto  from ".$_GET['database'].".mesaspuesto m  " .
//     " left outer join ".$_GET['database'].".tmp_producto_op o on m.sala = o.sala " .
//    " and m.mesa = o.mesa and m.puesto = o.ubicacion and m.suc_id = o.suc_id where m.suc_id = " .$_SESSION['suc_id'].
//    " and m.mesa = ".$_SESSION['mesa']." and m.sala = ".$_SESSION['sala']." group by m.puesto, op_id";

//echo "aqui".$sql;

//print_r($sql);

$json = $_POST['json'];
//$json = '{"datos":[{"num_orden":"000000016","num_fact":"000000087","terminal":"SUC[1]-ESTACION[1]","usuario":"usuario","fecha":"2017-10-27 15:59:14"}],"productos":[{"descripcion":"ARTICULO DE PRUEBAS","cantidad":1}]}';

$operacion = json_decode($json);
$datos = $operacion->{'datos'};

$op_telf = "";
$c_idfiscal = "";
$op_direc = "";
$op_id_cliente = "";
$op_id = "";
$op_nombre = "";
$ind = 0;
$op_usuario = 1;
$suc_id = -1;
$op_tipo = -1;
$op_estacion = -1;
$ci_op_idx = -1;

foreach ($datos as $valor) {
    $op_telf = $valor->{'op_telf'};
    $c_idfiscal = $valor->{'c_idfiscal'};
    $op_direc = $valor->{'op_direc'};
    $op_id_cliente = $valor->{'op_id_cliente'};
    $op_id = $valor->{'op_id'};
    $op_nombre = $valor->{'op_nombre'};
    $op_usuario = $valor->{'op_usuario'};
    $suc_id = $valor->{'suc_id'};
    $op_tipo = $valor->{'op_tipo'};
    $op_estacion = $valor->{'op_estacion'};
    $ci_op_idx = $valor->{'ci_op_idx'};
}

$sqlOrden = "select null from " . $_POST['database'] . ".tmp_operacion  where op_id = '" . $op_id . "'";
$qur = $connection->query($sqlOrden);

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

    $ind = 1;
}

if ($ind == 1) {
    $sqlOrden = "update " . $_POST['database'] . ".tmp_operacion set op_nombre = '" . $op_nombre .
        "', op_telf = '" . $op_telf .
        "', c_idfiscal2 = '" . $c_idfiscal .
        "', op_direc = '" . $op_direc .
        "', op_id_cliente = '" . $op_id_cliente .
        "' where op_id = '" . $op_id . "'";
} else {
    $sqlOrden = "insert into
                " . $_POST['database'] . ".tmp_operacion
                (op_id,
                op_telf,
                id_status,
                c_idfiscal2,
                op_nombre,
                op_usuario,
                suc_id,
                op_fecha,
                op_id_cliente,
                op_tipo,
                op_num_corre,
                op_estacion,
                op_id_espe,
                op_direc,
                ci_op_idx)" .
        " values ('" . $op_id .
        "', '" . $op_telf .
        "', 2,
                '" . $c_idfiscal .
        "', '" . $op_nombre .
        "', '" . $op_usuario .
        "', " . $suc_id .
        ", '" . $op_fecha .
        "','" . $op_id_cliente .
        "', '" . $op_tipo .
        "', '" . $op_id_espe .
        "', " . $op_estacion .
        ",'" . $op_id_espe .
        "', '" . $op_direc . "', 0)";
}

//print_r($sqlOrden);
$qur = $connection->query($sqlOrden);

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

    $json->datos = "false";

} else {
    $json->datos = "true";

}

print_r(json_encode($json));

@mysqli_close($connection);
?>