 <?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['po_father'] = $_GET['po_father'];
$_SESSION['mod_id_auto'] = $_GET['mod_id_auto'];
$_SESSION['ind'] = $_GET['ind'];

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

//if ($_SESSION['ind'] == 1){
$sql = "select po_id_prod, mod_id_auto from " . $_SESSION['database'] . ".tmp_producto_op where po_pr_tipo = 5 and po_father = " . $_SESSION['po_father'];

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

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

    //print_r($r['po_id_prod']);
    $_SESSION['pr_id'] = $r['po_id_prod'];
    //$_SESSION['mod_id_auto'] = $r['mod_id_auto'];

}
//}

//print_r($sql);

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

$sql = "select pm.pr_id, pm.mod_id_auto, mp.mod_descripcion, pm.mod_min, " .
    " pm.mod_max, pm.mod_precio, " .
    " mm.pr_id, p.pr_descripcion, p.w_tipo, p.w_btcolor, p.w_bt_prior, pm.ocultar, pm.tipo_prod, pm.mod_cant, pm.mod_del " .
    " from " . $_SESSION['database'] . ".prod_modif pm " .
    " join " . $_SESSION['database'] . ".modif_match mm on pm.mod_id_auto = mm.mod_id_auto " .
    " Join " . $_SESSION['database'] . ".productos p on mm.pr_id = p.pr_id" .
    " Join " . $_SESSION['database'] . ".modif_prod mp on pm.mod_id_auto = mp.mod_cod" .
    " where pm.tipo_prod = 'M' and pm.mod_del = 0 and pm.pr_id = '" . $_SESSION['pr_id'] .
    "'  and pm.mod_id_auto = " . $_SESSION['mod_id_auto'];

//print_r($sql );

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

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_min'] = $r['mod_min'];
        $jsonTempData['mod_max'] = $r['mod_max'];
        $jsonTempData['mod_precio'] = $r['mod_precio'];
        $jsonTempData['pr_id'] = $r['pr_id'];
        $jsonTempData['pr_descripcion'] = utf8_encode($r['pr_descripcion']);
        $jsonTempData['w_tipo'] = $r['w_tipo'];
        $jsonTempData['mod_descripcion'] = utf8_encode($r['mod_descripcion']);
        $jsonTempData['ocultar'] = $r['ocultar'];
        $jsonTempData['tipo_prod'] = utf8_encode($r['tipo_prod']);
        $jsonTempData['mod_cant'] = $r['mod_cant'];
        $jsonTempData['w_btcolor'] = DelphiTColorToRGB($r['w_btcolor']);
        $jsonTempData['w_bt_prior'] = $r['w_bt_prior'];

        $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->Modificadores = $jsonData;

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

    $json->datos = "false";

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

}

$jsonData = null;
$jsonTempData = null;

$sql = "select pm.pr_id, pm.mod_id_auto, mp.mod_descripcion, pm.mod_min, " .
    " pm.mod_max, pm.mod_precio, " .
    " mm.pr_id, p.pr_descripcion, p.w_tipo, p.w_btcolor, p.w_bt_prior, pm.ocultar, pm.tipo_prod, pm.mod_cant, pm.mod_del " .
    " from " . $_SESSION['database'] . ".prod_modif pm " .
    " join " . $_SESSION['database'] . ".modif_match mm on pm.mod_id_auto = mm.mod_id_auto " .
    " Join " . $_SESSION['database'] . ".productos p on mm.pr_id = p.pr_id" .
    " Join " . $_SESSION['database'] . ".modif_prod mp on pm.mod_id_auto = mp.mod_cod" .
    " where pm.tipo_prod = 'P' and pm.mod_del = 0 and pm.pr_id = '" . $_SESSION['pr_id'] .
    "'";

//print_r($sql );

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

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_min'] = $r['mod_min'];
        $jsonTempData['mod_max'] = $r['mod_max'];
        $jsonTempData['mod_precio'] = $r['mod_precio'];
        $jsonTempData['pr_id'] = $r['pr_id'];
        $jsonTempData['pr_descripcion'] = utf8_encode($r['pr_descripcion']);
        $jsonTempData['w_tipo'] = $r['w_tipo'];
        $jsonTempData['mod_descripcion'] = utf8_encode($r['mod_descripcion']);
        $jsonTempData['ocultar'] = $r['ocultar'];
        $jsonTempData['tipo_prod'] = utf8_encode($r['tipo_prod']);
        $jsonTempData['mod_cant'] = $r['mod_cant'];
        $jsonTempData['w_btcolor'] = DelphiTColorToRGB($r['w_btcolor']);
        $jsonTempData['w_bt_prior'] = $r['w_bt_prior'];

        $jsonData[] = $jsonTempData;
    }
}

$json->ModificadoresProd = $jsonData;

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

$sql = "select distinct(pm.mod_id_auto), mp.mod_descripcion" .
    " from " . $_SESSION['database'] . ".prod_modif pm " .
    " join " . $_SESSION['database'] . ".modif_match mm on pm.mod_id_auto = mm.mod_id_auto " .
    " Join " . $_SESSION['database'] . ".productos p on mm.pr_id = p.pr_id" .
    " Join " . $_SESSION['database'] . ".modif_prod mp on pm.mod_id_auto = mp.mod_cod" .
    " where pm.pr_id = '" . $_SESSION['pr_id'] . "'";

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

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_descripcion'] = utf8_encode($r['mod_descripcion']);

        $jsonData[] = $jsonTempData;
    }

}

$json->MaestroModificadores = $jsonData;

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

    $json->datos = "false";

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

}

print_r(json_encode($json));

@mysqli_close($connection);
?>