 <?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['sala'] = $_GET['sala'];
$_SESSION['mesa'] = $_GET['mesa'];
$_SESSION['puesto'] = $_GET['puesto'];
$_SESSION['suc_id'] = $_GET['suc_id'];
$_SESSION['opcion'] = $_GET['opcion'];

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

$jsonData = array();
$jsonTempData = array();

//CONSULTAR
if ($_SESSION['opcion'] == "1") {

    $sql = "select * from " . $_GET['database'] . ".tmp_producto_op" .
        " where sala = " . $_SESSION['sala'] .
        " and mesa = " . $_SESSION['mesa'] . " and ubicacion = " . $_SESSION['puesto'] .
        " and suc_id = " . $_SESSION['suc_id'];
}

if ($_SESSION['opcion'] == "2") {

    $sql = "select * from " . $_GET['database'] . ".tmp_producto_op" .
        " where sala = " . $_SESSION['sala'] .
        " and mesa = " . $_SESSION['mesa'] .
        " and suc_id = " . $_SESSION['suc_id'];
}

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

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

    $json->datos = "false";

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

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

        $jsonTempData = array();
        $jsonTempData['autoinc'] = $r['po_id'];
        $jsonTempData['po_op_id'] = $r['po_op_id'];
        $jsonTempData['pr_id'] = $r['po_id_prod'];
        $jsonTempData['cantidad'] = $r['po_cant'];
        $jsonTempData['ord_precio'] = $r['po_precio'];
        $jsonTempData['porcdesc'] = $r['porcdesc'];
        $jsonTempData['porcdesc_general'] = $r['porcdesc_general'];
        $jsonTempData['puesto'] = $r['ubicacion'];
        $jsonData[] = $jsonTempData;
    }
    $json->jsonordenes = $jsonData;
}

//print_r(json_encode($sql));

print_r(json_encode($json));

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