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

    File:        fac_list_worker.asp
    Description: Levert JSON op voor een shared/fullcalendar.asp
                 met mijn lopende zaken
    Parameters:  date-from/date-to
    Context:

*/
var JSON_Result = true;
%>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/json2.js" -->
<%
// fullCalendar werkt een factor 1000 kleiner met hun datums dan wij.
  var date_from = new Date(getQParamInt("start") * 1000);
  var date_to = new Date(getQParamInt("end") * 1000);

  function fnNodeColor(oRs) {
    var lecolor = "#ccc";
    switch (oRs("xmlnode").value)
    {
        // ja, eigenlijk naar css
        case "melding": lecolor = "#CCCCFF"; break;
        case "reservering": lecolor = "#FFE6CF"; break;
        case "bestelling": lecolor = "#CCFFCC"; break;
        case "afspraak": lecolor = "#E7CDE7"; break;
    }
    return lecolor;
 }

  // De query uit de Lopende Zaken (mobile)
  var sql = "SELECT item||' '||detail oms"                          // de externe referentie (meldingnummer)
         + ",  item_key key"                                        // de interne referentie (key)
         + ",  datum1 van"                                          // de datum van de service (varieert)
         + ",  datum2 tot "                                         // de datum van de service (varieert)
         + ",  'default.asp?u='||xmlnode||'&k='||item_key fclturl " // de url naar de details
         + ",  xmlnode "
         + " FROM ("
         + "SELECT isd.ins_srtdiscipline_prefix || TO_CHAR (m.mld_melding_key) item, "
         + "       m.mld_melding_key item_key, "
         + "       'melding' xmlnode, "
         + "       m.mld_melding_datum datum1, "
         + "       m.mld_melding_einddatum datum2, "
         + (S("mld_selector_mode") == 0
           ? "       " + lcl.xsql('id.ins_discipline_omschrijving', 'id.ins_discipline_key') + " detail, "
           : "       " + lcl.xsql('sm.mld_stdmelding_omschrijving', 'sm.mld_stdmelding_key') + " detail, ")
         + "       m.mld_melding_status status_key"
         + "  FROM mld_melding m"
         + "     , ins_srtdiscipline isd"
         + "     , ins_tab_discipline id"
         + "     , mld_stdmelding sm"
         + " WHERE id.ins_srtdiscipline_key = isd.ins_srtdiscipline_key "
         + "   AND sm.mld_ins_discipline_key = id.ins_discipline_key"
         + "   AND m.mld_stdmelding_key = sm.mld_stdmelding_key"
         + "   AND m.fac_activiteit_key IS NULL "
         + "   AND m.mld_melding_einddatum >= " + date_from.toSQL(true)
         + "   AND m.mld_melding_einddatum <= " + date_to.toSQL(true)
         + "   AND (m.mld_workflowstep_key IS NULL OR mld_melding_start_key = mld_melding_key) "
         + "   AND m.mld_melding_status IN (0, 2, 3, 4, 7) "
         + "   AND m.prs_perslid_key = " + user_key
         + "UNION ALL "
         + "SELECT   "+safe.quoted_sql(S("bes_bestelling_prefix"))+"||TO_CHAR (bes_bestelling_key), "
         + "         bes_bestelling_key, "
         + "         'bestelling', "
         + "         b.bes_bestelling_datum datum1, "
         + "         b.bes_bestelling_leverdatum+0.5 datum2, "
         + "         (SELECT   MIN (" + lcl.xsql('ins_discipline_omschrijving', 'ins_discipline_key') + ") "
         + "            FROM   bes_discipline "
         + "           WHERE   ins_discipline_key IN "
         + "                         (SELECT   ins_discipline_key "
         + "                            FROM   bes_srtgroep sg, bes_srtdeel sd, bes_bestelling_item bi "
         + "                           WHERE       sd.bes_srtgroep_key = sg.bes_srtgroep_key "
         + "                                   AND bi.bes_srtdeel_key = sd.bes_srtdeel_key "
         + "                                   AND bi.bes_bestelling_key = b.bes_bestelling_key)) "
         + "            oms, "
         + "         b.bes_bestelling_status"
         + "  FROM   bes_bestelling b "
         + " WHERE   b.bes_bestelling_status IN (5, 4, 3, 2) AND b.fac_activiteit_key IS NULL "
         + "   AND   (b.prs_perslid_key_voor = " + user_key
         + "       OR b.prs_perslid_key = " + user_key + ")"
         + "UNION ALL "
         + "SELECT   'R'||TO_CHAR (rr.res_reservering_key) || '/' || rr.res_rsv_ruimte_volgnr, "
         + "         rr.res_rsv_ruimte_key, "
         + "         'reservering', "
         + "         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, 0, rr.res_status_fo_key, -1) "
         + "  FROM   res_v_aanwezigrsv_ruimte rr "
         + " WHERE   rr.res_status_bo_key IN (4, 3, 2, 7) "
         + "   AND   rr.res_rsv_ruimte_van >= " + 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 " // dirtlevel van de onderliggenden bepalen?
         + "SELECT   'R'||TO_CHAR (rr.res_reservering_key) || '/' || rr.res_rsv_ruimte_volgnr, "
         + "         rr.res_rsv_ruimte_key, "
         + "         'reservering', "
         + "         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_van >= " + 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 " // dirtlevel van de onderliggenden bepalen?
         + "SELECT   'R'||TO_CHAR (rr.res_reservering_key) || '/' || rr.res_rsv_ruimte_volgnr, "
         + "         rr.res_rsv_ruimte_key, "
         + "         'reservering', "
         + "         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_van >= " + 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   'A'||TO_CHAR (b.bez_afspraak_key), "
         + "         b.bez_afspraak_key, "
         + "         'afspraak', "
         + "         a.bez_afspraak_datum datum1, "
         + "         a.bez_afspraak_eind datum2, "
         + "         b.bez_afspraak_naam "
         + "         || (CASE WHEN bez_afspraak_bedrijf IS NOT NULL THEN ' (' || bez_afspraak_bedrijf || ')' END), "
         + "         (CASE WHEN bez_bezoekers_done IS NOT NULL THEN 2 ELSE 1 END) "
         + "  FROM   bez_bezoekers b, bez_afspraak a "
         + " WHERE   b.bez_afspraak_key = a.bez_afspraak_key AND bez_bezoekers_out IS NULL "
         + "   AND   a.bez_afspraak_datum >= " + date_from.toSQL(true)
         + "   AND   a.bez_afspraak_datum <= "+ date_to.toSQL(true)
         + "   AND   (a.bez_afspraak_contact_key = " + user_key
         + "       OR a.bez_afspraak_host_key = " + user_key + ")"
         + ")"
         + " ORDER by  xmlnode, datum1";

    var oRs = Oracle.Execute(sql);

    result = [];
    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,
                      color:     fnNodeColor(oRs),
                      textColor: "#000",
                      allDay: false
                    });

        oRs.MoveNext();
    }
    oRs.Close();
    Response.Write(JSON.stringify(result));
%>
