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

    File:        res_fe_worker.asp
    Description: Levert JSON op voor een shared/fullcalendar.asp
                 met mijn lopende reserveringen
                 waar ik contactpersoon en/of gastheer/vrouw ben
    Parameters:  date-from/date-to
    Context:

*/
var JSON_Result = true;
%>
<!-- #include file="../Shared/common.inc" -->
<%
// date_from en date_to vanuit res_list.inc, met fallback naar de default start & end vanuit fullcalendar.asp
var date_from = getQParamDate("date_from", new Date(getQParamInt("start") * 1000));
var date_to = getQParamDate("date_to", new Date(getQParamInt("end") * 1000));

var authparams = user.checkAutorisation("WEB_RESUSE");

var result = [];

var sql = "SELECT item || ' ' || detail oms"
        + "     , datum1 van"
        + "     , datum2 tot"
        + "     , 'default.asp?internal=1&u=reservering&k='||item_key fclturl"
        + " FROM ("
        + "   SELECT 'R'||TO_CHAR (rr.res_reservering_key) || '/' || rr.res_rsv_ruimte_volgnr item," // Ruimte reserveringen
        + "          rr.res_rsv_ruimte_key item_key,"
        + "          rr.res_rsv_ruimte_van datum1,"
        + "          rr.res_rsv_ruimte_tot datum2,"
        + "          COALESCE (rr.res_rsv_ruimte_omschrijving, (SELECT "+ lcl.xsql('ra.res_activiteit_omschrijving', 'res_activiteit_key')
        + "                                                       FROM res_activiteit ra"
        + "                                                      WHERE res_activiteit_key = rr.res_activiteit_key)) detail,"
        + "          DECODE (res_rsv_ruimte_dirtlevel, 0, rr.res_status_fo_key, -1)"
        + "     FROM res_v_aanwezigrsv_ruimte rr"
        + "    WHERE rr.res_status_bo_key IN (2)"
        + "      AND rr.res_rsv_ruimte_tot >= " + date_from.toSQL(true)
        + "      AND rr.res_rsv_ruimte_van <= " + date_to.toSQL(true)
        + "      AND (   rr.res_rsv_ruimte_contact_key = " + user_key
        + "           OR rr.res_rsv_ruimte_host_key = " + user_key + ")"
        + "    UNION ALL"
        + "   SELECT 'R'||TO_CHAR (rr.res_reservering_key) || '/' || rr.res_rsv_ruimte_volgnr," // Object reserveringen
        + "          rr.res_rsv_ruimte_key,"
        + "          rr.res_rsv_ruimte_van datum1,"
        + "          rr.res_rsv_ruimte_tot datum2,"
        + "          COALESCE (rr.res_rsv_ruimte_omschrijving, (SELECT "+ lcl.xsql('ra.res_activiteit_omschrijving', 'res_activiteit_key')
        + "                                                       FROM res_activiteit ra"
        + "                                                      WHERE res_activiteit_key = rr.res_activiteit_key)),"
        + "          DECODE (res_rsv_ruimte_dirtlevel + res_rsv_deel_dirtlevel, 0, rr.res_status_fo_key, -1)"
        + "     FROM res_v_aanwezigrsv_ruimte rr, res_v_aanwezigrsv_deel dd"
        + "    WHERE rr.res_status_bo_key IS NULL AND dd.res_rsv_ruimte_key = rr.res_rsv_ruimte_key"
        + "      AND rr.res_rsv_ruimte_tot >= " + date_from.toSQL(true)
        + "      AND rr.res_rsv_ruimte_van <= " + date_to.toSQL(true)
        + "      AND (   rr.res_rsv_ruimte_contact_key = " + user_key
        + "           OR rr.res_rsv_ruimte_host_key = " + user_key + ")"
        + "    UNION ALL"
        + "   SELECT 'R'||TO_CHAR (rr.res_reservering_key) || '/' || rr.res_rsv_ruimte_volgnr," // Artikel reserveringen
        + "          rr.res_rsv_ruimte_key,"
        + "          rr.res_rsv_ruimte_van datum1,"
        + "          rr.res_rsv_ruimte_tot datum2,"
        + "          COALESCE (rr.res_rsv_ruimte_omschrijving, (SELECT "+ lcl.xsql('ra.res_activiteit_omschrijving', 'res_activiteit_key')
        + "                                                       FROM res_activiteit ra "
        + "                                                      WHERE res_activiteit_key = rr.res_activiteit_key)),"
        + "          DECODE (res_rsv_ruimte_dirtlevel + res_rsv_artikel_dirtlevel, 0, rr.res_status_fo_key, -1)"
        + "     FROM res_v_aanwezigrsv_ruimte rr, res_v_aanwezigrsv_artikel dd"
        + "    WHERE rr.res_status_bo_key IS NULL AND dd.res_rsv_ruimte_key = rr.res_rsv_ruimte_key"
        + "      AND rr.res_rsv_ruimte_tot >= " + date_from.toSQL(true)
        + "      AND rr.res_rsv_ruimte_van <= " + date_to.toSQL(true)
        + "      AND (   rr.res_rsv_ruimte_contact_key = " + user_key
        + "           OR rr.res_rsv_ruimte_host_key = " + user_key + ")"
        + ")";
var oRs = Oracle.Execute(sql);

while (!oRs.Eof) {
  result.push({
    title:    oRs("oms").Value || L("lcl_unknown"),
    start:    new Date(oRs("van").Value).getTime() / 1000,
    end:      new Date(oRs("tot").Value).getTime() / 1000,
    fclturl:  oRs("fclturl").Value,
    allDay:   false
  });
  oRs.MoveNext();
}
oRs.Close();

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