<?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'];
$empre = $_SESSION['database'] . ".empresas";
$suc = $_SESSION['database'] . ".sucursales";

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

//$json->bd = $empre;

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

/////////INICIO
/////////
/////////

$empresasql = "SELECT emp.emp_descripcion, emp.emp_idfiscal, suc.suc_id,suc.suc_codigo,suc.suc_descripcion FROM " . $empre . " emp inner join " . $suc . " suc on emp.emp_id = suc.emp_id where suc.suc_id<>1 ";

//print $empresasql;
///$json->bd = $empresasql;

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

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

    $json->datos = "false";

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

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

        $jsonTempData = array();
        $jsonTempData['nombre_empresa'] = utf8_encode($r['emp_descripcion']);
        $jsonTempData['emp_idfiscal'] = utf8_encode($r['emp_idfiscal']);
        $jsonTempData['suc_id'] = $r['suc_id'];
        $jsonTempData['suc_codigo'] = $r['suc_codigo'];
        $jsonTempData['suc_descripcion'] = utf8_encode($r['suc_descripcion']);

        $jsonData[] = $jsonTempData;
    }
}

$json->sucursales = $jsonData;

/////////
///////// FIN
/////////
/////////

print_r(json_encode($json));

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