<?php
include 'conn.php';
//Select data from database

include 'funciones.php';

session_start();
$_SESSION['database'] = $_POST['database'];
$_SESSION['dep_id'] = $_POST['dep_id'];
$_SESSION['fecha1'] = $_POST['fecha1'];

$_SESSION['fecha2'] = $_POST['fecha2'];

$hoynow = getdate();

$montoesql = "SELECT pr.*,
     us.u_usuario,
     '0'  AS CNTP,
  '0'     AS SUMP
   FROM " . $_SESSION['database'] . ".plantilla_productos pr
   LEFT JOIN " . $_SESSION['database'] . ".plantilla_productos_det pd ON pr.autoin= pd.plantilla_id
   LEFT JOIN " . $_SESSION['database'] . ".usuarios                us ON pr.u_id = us.u_id
   WHERE ( pr.fecha >= '" . $_SESSION['fecha1'] . "' AND pr.fecha <= '" . $_SESSION['fecha2'] . "')
   and pr.cantidad_productos>0


     AND  pr.nombre LIKE '%%' AND ((TRIM(pd.prom_codigo) = '' ) OR (TRIM(pd.prom_codigo) = '0') OR pd.prom_codigo IS NULL )


     AND pd.dep_id='" . $_SESSION['dep_id'] . "'  GROUP BY pr.autoin     ORDER BY pr.fecha DESC, pr.nombre   LIMIT 10";

//echo "sql:".$montoesql;

$msg = array("autoin" => "0", "nombre" => "0", "fecha" => "0");

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

//$_SESSION['succes'] ='false';

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

//$msg = array("autoin" => $r['autoin'],  "nombre" => $r['nombre'], "fecha" => $r['fecha']);

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

    $jsonTempData['nombre'] = utf8_encode($r['nombre']);
    $jsonTempData['fecha'] = utf8_encode($r['fecha']);

    $jsonTempData['u_id'] = utf8_encode($r['u_id']);
    $jsonTempData['status'] = utf8_encode($r['status']);

    $jsonTempData['cantidad_productos'] = utf8_encode($r['cantidad_productos']);
    $jsonTempData['u_usuario'] = utf8_encode($r['u_usuario']);

    $jsonTempData['CNTP'] = utf8_encode($r['CNTP']);
    $jsonTempData['SUMP'] = utf8_encode($r['SUMP']);

    $jsonData[] = $jsonTempData;
}

$json = $jsonData;
header('content-type: application/json');

echo json_encode($json);

@mysqli_close($connection);
?>
