 <?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['codigo'] = "";

$_SESSION['costo'] = '0';
$_SESSION['precio1'] = '0';
$_SESSION['precio6'] = '0';

$_SESSION['itbm'] = '0';
$_SESSION['familia'] = '0';

include 'conn.php';

include 'funciones.php';

$montoesql1 = "select productos.pr_id,productos.pr_referencia,pr_nro_parte,productos.pr_descripcion,productos_det.pr_venta1,pr_costo_prom,pr_id_familia,fab_id,id_marca,round(productos_det.pr_itbms,0) as pr_itbms from " . $_SESSION['database'] . ".productos
              left outer join productos_det on productos.pr_id=productos_det.pr_id

             where productos.pr_id='" . $_GET['codigo'] . "' or pr_referencia='" . $_GET['codigo'] . "' or pr_nro_parte='" . $_GET['codigo'] . "' limit 1";

//echo $montoesql1;

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

while ($r1 = mysqli_fetch_assoc($qur1)) {

    $_SESSION['codigo'] = $r1['pr_id'];
    $_SESSION['pr_referencia'] = $r1['pr_referencia'];
    $_SESSION['pr_nro_parte'] = $r1['pr_nro_parte'];
    $_SESSION['pr_descripcion'] = $r1['pr_descripcion'];
    $_SESSION['pr_id_familia'] = $r1['pr_id_familia'];
    $_SESSION['fab_id'] = $r1['fab_id'];
    $_SESSION['id_marca'] = $r1['id_marca'];
    $_SESSION['pr_itbms'] = $r1['pr_itbms'];
    $_SESSION['costo'] = $r1['pr_costo_prom'];
    $_SESSION['precio1'] = $r1['pr_venta1'];
}
if ($_SESSION['codigo'] == "") {
    $json->datos = "false";
} else {

    $json = new StdClass;

    $json->codigo = $_SESSION['codigo'];

    $json->pr_descripcion = utf8_encode($_SESSION['pr_descripcion']);

    $json->pr_id_familia = $_SESSION['pr_id_familia'];
    $json->fab_id = $_SESSION['fab_id'];
    $json->id_marca = $_SESSION['id_marca'];

    $json->pr_referencia = $_SESSION['pr_referencia'];
    $json->pr_nro_parte = $_SESSION['pr_nro_parte'];
    $json->costo = $_SESSION['costo'];
    $json->precio1 = $_SESSION['precio1'];
    $json->pr_itbms = $_SESSION['pr_itbms'];

}

$montoesql3 = "select depositos.dep_descripcion as bodega    ,p_existencia as cant from prod_existencia
              left outer join depositos on prod_existencia.p_existencia_dep_id=depositos.dep_id
              where prod_existencia.pr_id='" . $_SESSION['codigo'] . "' order by depositos.dep_orden asc ";

$qur3 = $connection->query($montoesql3);

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

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

    $json->datos = "false";

} else {
    logs($montoesql3);

    $json->datos = "true";
    while ($r = mysqli_fetch_assoc($qur3)) {

        $jsonTempData = array();
        $jsonTempData['bodega'] = utf8_encode($r['bodega']);
        $jsonTempData['cant'] = $r['cant'];

        $jsonData[] = $jsonTempData;
    }
}

$json->stock = $jsonData;

$montoesql = "SELECT ifnull(DATE_FORMAT(op_fecha,'%m-%d-%Y'),'Ninguno') fecha ,'7' as dias,format(ifnull(sum(producto_op.po_cant),0),0) cant FROM producto_op

left outer join operacion on producto_op.po_op_id=operacion.op_id

WHERE po_id_prod='" . $_SESSION['codigo'] . "'  and   op_fecha>=date_add(NOW(), INTERVAL -7 DAY)

union

SELECT DATE_FORMAT(op_fecha,'%m-%d-%Y') op_fecha ,'30' as dias, format(ifnull(sum(producto_op.po_cant),0),0) cant FROM producto_op

left outer join operacion on producto_op.po_op_id=operacion.op_id

WHERE po_id_prod='" . $_SESSION['codigo'] . "'  and   op_fecha>=date_add(NOW(), INTERVAL -30 DAY)

union

SELECT DATE_FORMAT(op_fecha,'%m-%d-%Y') op_fecha ,'60' as dias, format(ifnull(sum(producto_op.po_cant),0),0) cant FROM producto_op

left outer join operacion on producto_op.po_op_id=operacion.op_id

WHERE po_id_prod='" . $_SESSION['codigo'] . "'  and   op_fecha>=date_add(NOW(), INTERVAL -60 DAY)

union

SELECT DATE_FORMAT(op_fecha,'%m-%d-%Y') op_fecha ,'90' as dias, format(ifnull(sum(producto_op.po_cant),0),0) cant FROM producto_op

left outer join operacion on producto_op.po_op_id=operacion.op_id

WHERE po_id_prod='" . $_SESSION['codigo'] . "'  and   op_fecha>=date_add(NOW(), INTERVAL -90 DAY) ";

$qur4 = $connection->query($montoesql);

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

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

    $json->datos = "false";

} else {
//logs($montoesql);
    $json->datos = "true";

    if (is_object($qur4) && $qur4->num_rows > 0) {

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

            $jsonTempData = array();
            $jsonTempData['dias'] = $r['dias'];
            $jsonTempData['cant'] = (float) $r['cant'];

            $jsonData[] = $jsonTempData;
        }
    }}

$json->ventas = $jsonData;

print_r(json_encode($json));

//echo $_SESSION['codigo']."@".$_SESSION['costo']."@".$_SESSION['precio1']."@".$_SESSION['precio6']."@".$_SESSION['itbm']."@".$_SESSION['familia'];

@mysqli_close($connection);
?>