 <?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'];

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

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

/////////INICIO BUSCAR PRODUCTOS Y DETALLES

$montoesql = "select p.pr_tipo, pd.ubic_id, p.autoinc, p.pr_id, p.pr_id_familia,
		replace(p.pr_descripcion,'/','') as pr_descripcion, '5' as pr_imagen, p.w_tipo,
		p.w_fncolor, pd.pr_precio1_imp, pd.pr_costo_prom,
        pd.pr_venta1, pd.pr_venta2, pd.pr_venta3, pd.pr_venta4, pd.pr_venta5, pd.pr_venta6, pd.pr_venta7, p.w_btcolor
       , pd.pr_itbms, p.pr_detalle, p.fab_id, p.id_marca, p.sub_cat_1, p.sub_cat_2, p.sub_cat_3, p.pr_referencia, p.referencia2, p.referencia3, p.w_bt_prior
        from " . $_SESSION['database'] . ".productos p join
		" . $_SESSION['database'] . ".productos_det pd on
        p.pr_id = pd.pr_id
        and pd.pr_activo = 1     and p.pr_descripcion<>''      ";

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

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

    $json->datos = "false";

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

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

        $jsonTempData = array();
        $jsonTempData['autoinc'] = $r['autoinc'];
        $jsonTempData['pr_id'] = utf8_encode($r['pr_id']);
        $jsonTempData['pr_f'] = utf8_encode($r['pr_id_familia']);

        //$jsonTempData['pr_d']        =  utf8_encode($r['pr_descripcion']);
        $jsonTempData['pr_d'] = $r['pr_descripcion'];

        $jsonTempData['pr_imagen'] = utf8_encode($r['pr_imagen']);
        $jsonTempData['w_tipo'] = utf8_encode($r['w_tipo']);
        $jsonTempData['w_fncolor'] = $r['w_fncolor'];
        $jsonTempData['w_btcolor'] = DelphiTColorToRGB($r['w_btcolor']);
        $jsonTempData['w_bt_prior'] = $r['w_bt_prior'];
        //  $jsonTempData['pr_precio1_imp']         = $r['pr_precio1_imp'];
        $jsonTempData['pr_c'] = $r['pr_costo_prom'];
        $jsonTempData['pr_v'] = utf8_encode($r['pr_venta1']);
        $jsonTempData['pr_v2'] = utf8_encode($r['pr_venta2']);
        $jsonTempData['pr_v3'] = utf8_encode($r['pr_venta3']);
        $jsonTempData['pr_v4'] = utf8_encode($r['pr_venta4']);
        $jsonTempData['pr_v5'] = utf8_encode($r['pr_venta5']);
        $jsonTempData['pr_v6'] = utf8_encode($r['pr_venta6']);
        $jsonTempData['pr_v7'] = utf8_encode($r['pr_venta7']);
        $jsonTempData['pr_detalle'] = utf8_encode($r['pr_detalle']);
        $jsonTempData['pr_i'] = $r['pr_itbms'];
        $jsonTempData['fab_id'] = $r['fab_id'];
        $jsonTempData['id_marca'] = $r['id_marca'];
        $jsonTempData['sub_cat_1'] = $r['sub_cat_1'];
        $jsonTempData['sub_cat_2'] = $r['sub_cat_2'];
        $jsonTempData['sub_cat_3'] = $r['sub_cat_3'];
        $jsonTempData['pr_referencia'] = utf8_encode($r['pr_referencia']);
        $jsonTempData['referencia2'] = utf8_encode($r['referencia2']);
        $jsonTempData['referencia3'] = utf8_encode($r['referencia3']);
        $jsonTempData['ubic_id'] = utf8_encode($r['ubic_id']);
        $jsonTempData['pr_tipo'] = $r['pr_tipo'];
        $jsonData[] = $jsonTempData;
    }
}

function DelphiTColorToRGB($color)
{
    $R = $color & 0xFF;
    $G = ($color >> 8) & 0xFF;
    $B = ($color >> 16) & 0xFF;

    return RGBToHex($R, $G, $B);
}

function RGBToHex($r, $g, $b)
{
    //String padding bug found and the solution put forth by Pete Williams (http://snipplr.com/users/PeteW)
    $hex = "#";
    $hex .= str_pad(dechex($r), 2, "0", STR_PAD_LEFT);
    $hex .= str_pad(dechex($g), 2, "0", STR_PAD_LEFT);
    $hex .= str_pad(dechex($b), 2, "0", STR_PAD_LEFT);

    return $hex;
}

$json->Productos = $jsonData;

/////////INICIO BUSCAR MARCA, FABRICANTES Y CATEG
$jsonData = null;
$jsonTempData = null;

$sql_marcas = "select * from " . $_SESSION['database'] . ".marcas";

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

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

    $json->datos = "false";

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

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

        $jsonTempData = array();
        $jsonTempData['id_marca'] = $r['id_marca'];
        $jsonTempData['des_marca'] = utf8_encode($r['des_marca']);
        $jsonTempData['w_bt_prior'] = $r['w_bt_prior'];
        $jsonTempData['w_btcolor'] = DelphiTColorToRGB($r['w_btcolor']);
        $jsonData[] = $jsonTempData;
    }
}

$json->marcas = $jsonData;

$jsonData = null;
$jsonTempData = null;

$sql_fabricantes = "select * from " . $_SESSION['database'] . ".fabricantes";

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

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

    $json->datos = "false";

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

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

        $jsonTempData = array();
        $jsonTempData['fab_id'] = $r['fab_id'];
        $jsonTempData['des_fabricante'] = utf8_encode($r['des_fabricante']);
        $jsonTempData['w_bt_prior'] = $r['w_bt_prior'];
        $jsonTempData['w_btcolor'] = DelphiTColorToRGB($r['w_btcolor']);
        $jsonData[] = $jsonTempData;
    }
}

$json->fabricantes = $jsonData;

$jsonData = null;
$jsonTempData = null;

$sql_cat1 = "select * from " . $_SESSION['database'] . ".sub_categoria_1";

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

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

    $json->datos = "false";

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

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

        $jsonTempData = array();
        $jsonTempData['sub_codigo1'] = $r['sub_codigo1'];
        $jsonTempData['familia_id'] = $r['familia_id'];
        $jsonTempData['descripcion_sub1'] = utf8_encode($r['descripcion_sub1']);
        $jsonData[] = $jsonTempData;
    }
}

$json->sub_categoria_1 = $jsonData;

$jsonData = null;
$jsonTempData = null;

$sql_cat2 = "select * from " . $_SESSION['database'] . ".sub_categoria_2";

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

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

    $json->datos = "false";

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

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

        $jsonTempData = array();
        $jsonTempData['sub_codigo2'] = $r['sub_codigo2'];
        $jsonTempData['familia_id'] = $r['familia_id'];
        $jsonTempData['descripcion_sub2'] = utf8_encode($r['descripcion_sub2']);
        $jsonData[] = $jsonTempData;
    }
}

$json->sub_categoria_2 = $jsonData;

$jsonData = null;
$jsonTempData = null;

$sql_cat3 = "select * from " . $_SESSION['database'] . ".sub_categoria_3";

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

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

    $json->datos = "false";

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

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

        $jsonTempData = array();
        $jsonTempData['sub_codigo3'] = $r['sub_codigo3'];
        $jsonTempData['familia_id'] = $r['familia_id'];
        $jsonTempData['descripcion_sub3'] = utf8_encode($r['descripcion_sub3']);
        $jsonData[] = $jsonTempData;
    }
}

$json->sub_categoria_3 = $jsonData;

/////////INICIO BUSCAR MODIFICADORES prod_modif
$jsonData = null;
$jsonTempData = null;

$sql_modif = "select * from " . $_SESSION['database'] . ".prod_modif where mod_del = 0";

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

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

    $json->datos = "false";

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

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

        $jsonTempData = array();
        $jsonTempData['pr_id'] = $r['pr_id'];
        $jsonTempData['mod_id_auto'] = $r['mod_id_auto'];
        $jsonTempData['mod_min'] = $r['mod_min'];
        $jsonTempData['mod_max'] = $r['mod_max'];
        $jsonTempData['mod_precio'] = $r['mod_precio'];
        $jsonTempData['ocultar'] = $r['ocultar'];
        $jsonTempData['mod_cant'] = $r['mod_cant'];
        $jsonTempData['tipo_prod'] = utf8_encode($r['tipo_prod']);
        $jsonData[] = $jsonTempData;
    }
}

$json->prod_modif = $jsonData;

/////////INICIO BUSCAR MODIFICADORES modif_match
$jsonData = null;
$jsonTempData = null;

$sql_modif = "select * from " . $_SESSION['database'] . ".modif_match";

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

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

    $json->datos = "false";

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

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

        $jsonTempData = array();
        $jsonTempData['mod_autoin'] = $r['mod_autoin'];
        $jsonTempData['mod_id_auto'] = $r['mod_id_auto'];
        $jsonTempData['pr_id'] = $r['pr_id'];
        $jsonTempData['mod_precio'] = $r['mod_precio'];
        $jsonTempData['modt_precio'] = $r['modt_precio'];
        $jsonTempData['mod_itbms'] = $r['mod_itbms'];
        $jsonData[] = $jsonTempData;
    }
}

$json->modif_match = $jsonData;

/////////INICIO BUSCAR MODIFICADORES modif_prod
$jsonData = null;
$jsonTempData = null;

$sql_modif = "select * from " . $_SESSION['database'] . ".modif_prod";

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

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

    $json->datos = "false";

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

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

        $jsonTempData = array();
        $jsonTempData['mod_id_auto'] = $r['mod_id_auto'];
        $jsonTempData['mod_cod'] = $r['mod_cod'];
        $jsonTempData['mod_descripcion'] = utf8_encode($r['mod_descripcion']);
        $jsonData[] = $jsonTempData;
    }
}

$json->modif_prod = $jsonData;

/////////INICIO BUSCAR WIND_POS
$jsonData = null;
$jsonTempData = null;

$montoesql = "select autoin,w_id, w_des_vent,
			SUBSTRING(w_des_tecla,1,20) AS w_des_tecla ,w_relac,w_fncolor,pr_imagen , w_id_order, w_bt_prior,w_btcolor,w_tipo, w_id_order
			from " . $_GET['database'] . ".wind_pos where
			w_tipo in (1,2,6) and w_id = 201";

//print_r($montoesql);

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

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

    $json->datos = "false";

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

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

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

        $jsonTempData['w_relac'] = utf8_encode($r['w_relac']);
        $jsonTempData['w_fncolor'] = $r['w_fncolor'];
        $jsonTempData['pr_imagen'] = utf8_encode($r['pr_imagen']);
        $jsonTempData['w_btcolorletra'] = DelphiTColorToRGB($r['w_btcolor']);
        $jsonTempData['w_bt_prior'] = $r['w_bt_prior'];
        $jsonTempData['w_tipo'] = $r['w_tipo'];
        $jsonTempData['w_id_order'] = $r['w_id_order'];
        $jsonData[] = $jsonTempData;
    }
}

$json->Wind_pos = $jsonData;

/////////INICIO BUSCAR FAMILIAS
$jsonData = null;
$jsonTempData = null;

$montoesql = "select f_id, f_descripcion,f_des_tecla, f_detalle, w_btcolor, w_bt_prior from " . $_SESSION['database'] . ".familia where f_activo = 1";

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

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

    $json->datos = "false";

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

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

        $jsonTempData = array();
        $jsonTempData['f_id'] = $r['f_id'];
        //$jsonTempData['f_descripcion']       = utf8_encode($r['f_descripcion']);

        $jsonTempData['f_descripcion'] = $r['f_descripcion'];

        $jsonTempData['f_des_tecla'] = utf8_encode($r['f_des_tecla']);
        $jsonTempData['f_detalle'] = utf8_encode($r['f_detalle']);
        $jsonTempData['w_btcolor'] = DelphiTColorToRGB($r['w_btcolor']);
        $jsonTempData['w_bt_prior'] = $r['w_bt_prior'];

        $jsonData[] = $jsonTempData;
    }
}

$json->Familias = $jsonData;

/////////INICIO BUSCAR COMPUESTOS
$jsonData = null;
$jsonTempData = null;

$montoesql = "select * from " . $_SESSION['database'] . ".compuestos";

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

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

    $json->datos = "false";

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

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

        $jsonTempData = array();
        $jsonTempData['comp_id'] = $r['comp_id'];
        $jsonTempData['pr_id'] = $r['pr_id'];
        $jsonTempData['cant_equi'] = ($r['cant_equi']);
        $jsonTempData['pr_costo'] = ($r['pr_costo']);
        $jsonTempData['pr_costo_equi'] = ($r['pr_costo_equi']);
        $jsonTempData['suc_id'] = $r['suc_id'];

        $jsonData[] = $jsonTempData;
    }
}

$json->Compuestos = $jsonData;

// fin compuestos

$montoesql2 = "select e_corre_e from " . $_SESSION['database'] . ".estaciones limit 1";

$qur = $connection->query($montoesql2);
$corre = '1';
while ($r = mysqli_fetch_assoc($qur)) {

    $corre = $r['e_corre_e'];
    if ($corre == '0') {
        $corre = '1';
    }

}

$json->correprod = $corre;

print_r(json_encode($json));

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