<?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'];
$dep = $_SESSION['database'] . ".depositos";
$pers = $_SESSION['database'] . ".personal";
$clie = $_SESSION['database'] . ".clientes";
$estac = $_SESSION['database'] . ".estaciones";

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

//$json->bd = $bd;

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

/////////INICIO depositos
/////////
/////////

$depositossql = "SELECT dep_id,dep_descripcion,dep_activo,suc_id FROM " . $dep . " where suc_id = " . $_GET['suc_id'] . " ";

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

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

    $json->datos = "false";

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

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

        $jsonTempData = array();
        $jsonTempData['dep_id'] = $r['dep_id'];
        $jsonTempData['dep_activo'] = $r['dep_activo'];
        $jsonTempData['suc_id'] = $r['suc_id'];
        $jsonTempData['dep_descripcion'] = utf8_encode($r['dep_descripcion']);

        $jsonData[] = $jsonTempData;
    }
}

$json->depositos = $jsonData;

/////////
///////// FIN depositos
/////////
/////////

$jsonData1 = array();
$jsonTempData1 = array();

/////////INICIO personal
/////////
/////////

$personalsql = "SELECT pers_id,pers_codigo,pers_user_id,pers_descripcion,pers_activo,suc_id FROM " . $pers . " where pers_codigo > 0";

//$json->bd = $personalsql;

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

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

    $json->datos = "false";

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

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

        $jsonTempData1 = array();
        $jsonTempData1['pers_id'] = $r['pers_id'];
        $jsonTempData1['pers_codigo'] = utf8_encode($r['pers_codigo']);
        $jsonTempData1['pers_descripcion'] = utf8_encode($r['pers_descripcion']);
        $jsonTempData1['pers_activo'] = $r['pers_activo'];
        $jsonTempData1['suc_id'] = $r['suc_id'];
        $jsonTempData1['pers_user_id'] = $r['pers_user_id'];

        $jsonData1[] = $jsonTempData1;
    }
}

$json->personal = $jsonData1;

/////////
///////// FIN personal
/////////
/////////

/////////INICIO clientes
/////////
/////////

$jsonData2 = array();
$jsonTempData2 = array();

$clientesql = "SELECT * FROM " . $clie . "";

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

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

    $json->datos = "false";

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

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

        $jsonTempData2 = array();
        $jsonTempData2['c_id'] = $r['c_id'];
        $jsonTempData2['c_activo'] = $r['c_activo'];
        $jsonTempData2['suc_id'] = $r['suc_id'];
        $jsonTempData2['c_descripcion'] = utf8_encode($r['c_descripcion']);
        $jsonTempData2['c_telefono'] = utf8_encode($r['c_telefono']);
        $jsonTempData2['c_direccion'] = utf8_encode($r['c_direccion']);
        $jsonTempData2['c_dire_fiscal'] = utf8_encode($r['c_dire_fiscal']);
        $jsonTempData2['c_correo'] = utf8_encode($r['c_correo']);
        $jsonTempData2['c_idfiscal'] = utf8_encode($r['c_idfiscal']);
        $jsonTempData2['id_contribuyente'] = utf8_encode($r['id_contribuyente']);

        $jsonData2[] = $jsonTempData2;
    }
}

$json->clientes = $jsonData2;

/////////
///////// FIN clientes
/////////
/////////

/////////INICIO estaciones
/////////
/////////

$jsonData3 = array();
$jsonTempData3 = array();

$estacionsql = "SELECT e_id,suc_id,e_codigo,e_descripcion FROM " . $estac . " where e_id > 1 and e_activo = 1 and uso_tablet = 0 and suc_id = " . $_GET['suc_id'] . " ";

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

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

    $json->datos = "false";

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

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

        $jsonTempData3 = array();
        $jsonTempData3['e_id'] = $r['e_id'];
        $jsonTempData3['e_codigo'] = $r['e_codigo'];
        $jsonTempData3['suc_id'] = $r['suc_id'];
        $jsonTempData3['e_descripcion'] = utf8_encode($r['e_descripcion']);

        $jsonData3[] = $jsonTempData3;
    }
}

$json->estaciones = $jsonData3;

/////////
///////// FIN estaciones
/////////
/////////

print_r(json_encode($json));

@mysqli_close($connection);
?>