<%@ language = "JavaScript" %>
<%
 /*
    $Revision$
    $Id$

    File:               get_res_info_ajax.asp
    Description:        Ophalen van allerlei info om client-side data te hebben
    Parameters:         req_info:   "res_ruimte"    --> requires "res_ruimte_key"
    Context:
    Note:               Wordt misbruikt vanuit ../fac/permon.asp als simpele request

*/ %>
<%
if (Request.QueryString("perfmontest").Count == 0)
    var JSON_Result = true;
%>

<!--#include file="../Shared/common.inc"-->
<!--#include file="./res.inc" -->

<%
var req_info = getQParam("req_info");
var result = {};
switch (req_info)
{
    case "res_ruimte":
    {
        var res_ruimte_key = getQParamInt("res_ruimte_key");
        result = res.res_ruimte_info(res_ruimte_key);
        var params = { rsv_van_datum: getQParamDate("rsv_van", null)
                     , rsv_tot_datum: getQParamDate("rsv_tot", null)
                     , rsv_extern: getQParamInt("extern", 0)
                     };
        if (params.rsv_van_datum)
        {
            result.ruimte_prijs = res.res_getruimteprijs(res_ruimte_key, params);
        }
        break;
    }
    case "res_nr_periods":
    {
        var params = { urole: getQParam("urole"),
                       np: getQParamInt("np"),
                       nextDate: getQParamInt("nextDate"),
                       lastDate: getQParamInt("lastDate"),
                       bits: getQParamInt("bits")
                     }
        result = res.res_getnrperiods(params);
        break;
    }
    case "res_new_dates":
    {
        var params = { urole: getQParam("urole"),
                       chk_lastmin: getQParamInt("chk_lastmin") == 1,
                       np: getQParamInt("np"),
                       nextDate: getQParamInt("nextDate"),
                       bits: getQParamInt("bits"),
                       rsv_ruimte_van: getQParamInt("rsv_ruimte_van"),
                       ruimte_startdatum: getQParamInt("ruimte_startdatum"),
                       ruimte_vervaldatum: getQParamInt("ruimte_vervaldatum"),
                       earliest_expire_change: getQParamInt("earliest_expire_change"),
                       act_effectief_limiet: getQParamInt("act_effectief_limiet")
                     }
        result = res.res_getnewdates(params);
        break;
    }
    case "rsv_opstelling_fit":
    {
        var params = { res_ruimte_key: getQParamInt("res_ruimte_key"),
                       res_opstel_key: getQParamInt("res_opstel_key"),
                       rsv_van: getQParamInt("rsv_van"),
                       rsv_tot: getQParamInt("rsv_tot")
                     }
        result = res.rsv_opstelling_fit(params);
        break;
    }
    case "res_pers":
    {
        var prs_key = getFParamInt("prs_key");
        var res_disc_key = getFParamInt("discipline");
        // var authRES = user.func_enabled2("RES", { ins_discipline_key: res_disc_key });
        var authparams = user.checkAutorisation("WEB_RESFOF", false, res_disc_key);
        if (authparams.PRSwritelevel == -1) {
            /* Alles mag */
        } else {
            if (authparams.PRSwritelevel == 0) {
                var sql = "SELECT 'x'"
                        + "  FROM prs_perslid p"
                        + " WHERE p.prs_perslid_key = " + prs_key
                        + "   AND p.prs_afdeling_key IN"
                        + "    (SELECT a.prs_afdeling_key FROM prs_v_afdeling a"
                        + "      WHERE a.prs_bedrijf_key = " + user.afdeling().prs_bedrijf_key() + " )";
            } else if (authparams.PRSwritelevel > 0) {
                var sql = "SELECT 'x'"
                        + "  FROM prs_perslid p"
                        + " WHERE p.prs_perslid_key = " + prs_key
                        + "   AND p.prs_afdeling_key IN"
                        + "    (SELECT prs_afdeling_key FROM prs_v_afdeling_familie a"
                        + "      WHERE a.prs_afdeling_elder_key IN"
                        + "        (SELECT aa.prs_afdeling_elder_key"
                        + "           FROM prs_v_afdeling_familie aa"
                        + "          WHERE aa.prs_afdeling_key = " + user.prs_afdeling_key()
                        + "            AND aa.niveau = " + authparams.PRSwritelevel + "))";
            }
            var oRs = Oracle.Execute(sql);
            user.auth_required_or_abort(!oRs.EoF);
            oRs.Close();
        }

        var res_van = getFParamDate("res_van", null);
        var sql = "SELECT rm.res_reservering_key,"
                + "       rm.res_rsv_ruimte_volgnr,"
                + "       rm.res_rsv_ruimte_key,"
                + "       res_rsv_ruimte_van,"
                + "       res_rsv_ruimte_tot,"
                + "       (SELECT prs_perslid_naam_friendly FROM prs_v_perslid_fullnames pf WHERE pf.prs_perslid_key = rm.res_rsv_ruimte_host_key) host,"
                + "       rm.res_rsv_ruimte_host_key host_key,"
                + "       rd.res_deel_key"
                + "  FROM res_rsv_ruimte rm,"
                + "       res_rsv_deel rd,"
                + "       res_deel d"
                + " WHERE res_rsv_ruimte_van < {0}".format(res_van.endToSQL())
                + "   AND res_rsv_ruimte_tot > {0}".format(res_van.beginToSQL())
                + "   AND rd.res_deel_key = d.res_deel_key"
                + "   AND d.res_discipline_key = " + res_disc_key
                + "   AND rd.res_rsv_ruimte_key = rm.res_rsv_ruimte_key"
                + "   AND rm.res_rsv_ruimte_host_key = " + prs_key
                + "   AND rm.res_rsv_ruimte_verwijder IS NULL"
                + "   AND rd.res_rsv_deel_verwijder IS NULL"
                + "   AND rd.res_rsv_deel_dirtlevel = 0"
                + " ORDER BY rm.res_rsv_ruimte_van";
        result.success = true;
        result.rsv = [];
        var oRs = Oracle.Execute(sql);
        while (!oRs.EoF) {
            result.rsv.push({
                bezet:              oRs("host").Value,
                bezet_key:          oRs("host_key").Value,
                res_deel_key:       oRs("res_deel_key").Value,
                res_rsv_ruimte_key: oRs("res_rsv_ruimte_key").Value,
                res_reservering_nr: oRs("res_reservering_key").Value + "/" + oRs("res_rsv_ruimte_volgnr").Value,
                resvan:             new Date(oRs("res_rsv_ruimte_van").Value).getTime(),
                restot:             new Date(oRs("res_rsv_ruimte_tot").Value).getTime()
            });
            oRs.MoveNext();
        }
        oRs.Close();
        break;
    }
    case "alg_gebouw":
    {
        var bld_key = getQParamInt("bld_key");
        var result = { vrije_dagen: {id: ""}};
        var vrije_dagen_alt = [];
        var sql = "SELECT ag.mld_vrije_dagen_id"
                + "     , mv.mld_vrije_dagen_datum"
                + "  FROM alg_gebouw ag"
                + "     , mld_vrije_dagen mv"
                + " WHERE ag.mld_vrije_dagen_id = mv.mld_vrije_dagen_id"
                + "   AND ag.alg_gebouw_key = " + bld_key;
        var oRs = Oracle.Execute(sql);
        while (!oRs.eof)
        {
            result.vrije_dagen.id = oRs("mld_vrije_dagen_id").Value;
            vrije_dagen_alt.push((new Date(oRs("mld_vrije_dagen_datum").Value)).getTime());
            oRs.MoveNext();
        }
        oRs.Close();
        if (vrije_dagen_alt.length && result.vrije_dagen.id != "Default")
            result.vrije_dagen.alt = vrije_dagen_alt;
        break;
    }
}

Response.Write(JSON.stringify(result));
 %>
<% ASPPAGE_END(); %>
