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

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

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

if ($ind == 1) {
    //$sqlpo_op_id = "select po_op_id from ".$_SESSION['database'].".tmp_producto_op ".
    //            " where po_visible_grid = 'y' and  sala = ".$sala." and mesa = ".$mesa." and suc_id = ".$suc_id." and ubicacion = ".$puesto." limit 1";

    //print_r($sqlpo_op_id);
    // $qur_po_op_id = $connection->query($sqlpo_op_id);

    //$_SESSION['po_op_id'] =0;

    // while($r = mysqli_fetch_assoc($qur_po_op_id)){

    //   $_SESSION['po_op_id'] =  $r['po_op_id'];

    //}
}

if ($cuenta_mesa == 0) {
    $sql = "select op_fecha, op_subtotal, op_total, op_itbms, op_desc " .
        "from " . $_SESSION['database'] . ".tmp_operacion " .
        " where op_id = " . $_SESSION['po_op_id'];

    $sql1 = "select * " .
        "from " . $_SESSION['database'] . ".tmp_producto_op " .
        " where po_visible_grid = 'y' and po_op_id = " . $_SESSION['po_op_id'] . " and ubic_enviada = 1 order by ubicacion ";

    $sql2 = "select sum(po_precio * po_cant) as precio, po_itbms, porcdesc_general, (sum((po_precio * po_cant - ((po_precio * po_cant) * porcdesc)/100)-((po_precio * po_cant - ((po_precio * po_cant) * porcdesc)/100) * porcdesc_general)/100) * po_itbms)/100 as imp   " .
        "from " . $_SESSION['database'] . ".tmp_producto_op  where  po_visible_grid = 'y' and  po_itbms > 0 and  ubic_enviada = 1 and po_op_id = " . $_SESSION['po_op_id'] . " group by po_itbms  order by ubicacion ";
} else {

    $sql = "select op_fecha, sum(op_subtotal) as op_subtotal, sum(op_total) as op_total, sum(op_itbms) as op_itbms, sum(op_desc) as op_desc from " .
        "(select distinct o.op_fecha, o.op_subtotal, o.op_total, o.op_itbms, o.op_desc " .
        "from " . $_SESSION['database'] . ".tmp_operacion o  join " . $_SESSION['database'] . ".tmp_producto_op p on o.op_id = p.po_op_id " .
        " where  p.po_visible_grid = 'y' and  p.sala = " . $sala . " and p.mesa = " . $mesa . " and p.suc_id = " . $suc_id . " and p.ubic_enviada = 1)a";

    //print_r($sql);

    $sql1 = "select * " .
        "from " . $_SESSION['database'] . ".tmp_producto_op " .
        " where po_visible_grid = 'y' and  sala = " . $sala . " and mesa = " . $mesa . " and suc_id = " . $suc_id . " and ubic_enviada = 1";

    $sql2 = "select sum(po_precio * po_cant) as precio, po_itbms, porcdesc_general, (sum((po_precio * po_cant - ((po_precio * po_cant) * porcdesc)/100)-((po_precio * po_cant - ((po_precio * po_cant) * porcdesc)/100) * porcdesc_general)/100) * po_itbms)/100 as imp   " .
        "from " . $_SESSION['database'] . ".tmp_producto_op  where po_visible_grid = 'y' and   po_itbms > 0 and  ubic_enviada = 1 and sala = " . $sala . " and mesa = " . $mesa . " and suc_id = " . $suc_id . " group by po_itbms";

}

//print_r($sql);
//print_r($sql1);
//print_r($sql2);

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

$qur1 = $connection->query($sql1);

$qur2 = $connection->query($sql2);

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

    $json->datos = "false";

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

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

        $jsonTempData['op_fecha'] = $r['op_fecha'];
        $jsonTempData['op_subtotal'] = $r['op_subtotal'];
        $jsonTempData['op_total'] = $r['op_total'];
        $jsonTempData['op_desc'] = $r['op_desc'];
        $jsonTempData['operacion'] = $_SESSION['po_op_id'];
        $jsonTempData['op_itbms'] = $r['op_itbms'];

        $jsonData[] = $jsonTempData;
    }
    $json->jsonoperacion = $jsonData;

    $jsonData = array();
    $jsonTempData = array();
    while ($r = mysqli_fetch_assoc($qur2)) {

        $jsonTempData = array();
        $jsonTempData['po_itbms'] = $r['po_itbms'];
        $jsonTempData['imp'] = $r['imp'];
        $jsonTempData['porcdesc_general'] = $r['porcdesc_general'];
        $jsonData[] = $jsonTempData;
    }

    $json->jsonbases = $jsonData;

    $jsonData = array();
    $jsonTempData = array();
    while ($r = mysqli_fetch_assoc($qur1)) {

        $jsonTempData = array();
        $jsonTempData['po_id_prod'] = $r['po_id_prod'];
        $jsonTempData['po_cant'] = $r['po_cant'];
        $jsonTempData['po_descripcion'] = utf8_encode($r['po_descripcion']);
        $jsonTempData['po_precio'] = $r['po_precio'];
        $jsonTempData['po_itbms'] = $r['po_itbms'];
        $jsonTempData['porcdesc'] = $r['porcdesc'];
        $jsonTempData['porcdesc_general'] = number_format($r['porcdesc_general'], 2, '.', ',');
        $jsonData[] = $jsonTempData;
    }

    $json->jsonproductos = $jsonData;

}

//print_r(json_encode($sql));

print_r(json_encode($json));

@mysqli_close($connection);
?>