<% /*
     $Revision$
     $Id$

    File:               job_list.inc
    Description:
    Parameters:
    Context:
    Note:
*/ %>

<!-- #include file="../Shared/json2.js" -->
<!-- #include file="../Shared/resultset_table_v2.inc" -->

<%
FCLTHeader.Requires({ plugins:["jQuery"],
                      js: ["./job_list.js"]
                    })

 function fnrowData(oRs)
        {
          var job_key = oRs("fac_activiteit_key").value;

          var data = {job_key: job_key};

          return JSON.stringify(data);
        }
%>
<script type="text/javascript">
  var autosearch = 1; // automatisch laden van resultaat op search pagina
</script>
<%

function job_list(pautfunction, params)
{
  // De functie checkAutorisation(autfunction) geeft een Response.End indien er geen autorisatie voor autfunction wordt gevonden
  var addButton = [];

  authparams = user.checkAutorisation("WEB_MLDBOF", true);

  var MLDBOF_read  = authparams && (authparams.PRSreadlevel < 9 || authparams.ALGreadlevel < 9);
  var MLDBOF_write = authparams && (authparams.PRSwritelevel < 9 || authparams.ALGwritelevel < 9);

  authparams = user.checkAutorisation("WEB_MLDFOF", true);

  var MLDFOF_read  = authparams && (authparams.PRSreadlevel < 9 || authparams.ALGreadlevel < 9);
  var MLDFOF_write = authparams && (authparams.PRSwritelevel < 9 || authparams.ALGwritelevel < 9);

  authparams = user.checkAutorisation("WEB_BESFOF", true);

  var BESFOF_read  = authparams && (authparams.PRSreadlevel < 9 || authparams.ALGreadlevel < 9);
  var BESFOF_write = authparams && (authparams.PRSwritelevel < 9 || authparams.ALGwritelevel < 9);

  if (!(MLDFOF_read || MLDFOF_write || BESFOF_read || BESFOF_write || MLDBOF_read || MLDBOF_write))
  {
    shared.simpel_page(L("lcl_no_auth"));
  }

  // Voor resulttable de globalen zetten; zou ng anders moeten.
  var outputmode = params.outputmode; // print to 0 = screen, 1 = printer, 2 = excel, etc.
  var showall = params.showall;

  var act_key = params.act_key
  var prs_prskey = params.prs_prskey
  var rtype_key = params.rtype_key
  var cat_key = params.cat_key
  var prod_key = params.prod_key
  var stat_key = params.stat_key

  function fnrowActionEnabler(oRs)
  {
    /*  var eDelete = false;
    if (alg.canWriteRuimte(oRs("alg_ruimte_key").value, authparams.mALGwritelevel)) */
    eDelete = true;
    return ({eDelete: eDelete})
  }
%>

<html>
  <head>
    <% FCLTHeader.Generate({outputmode:outputmode}) %>
  </head>
  <body id="listbody">
<%  // is distinct echt vereist? dan zeggen waarom: ik denk omdat afdeling_boom ervoor zorgt dat een persoon
    // meerdere keren in het resultaat komt, en de distinct dus nodig is.

    var sqln = "SELECT   fa.fac_activiteit_key, "
             + "         TO_CHAR (fa.fac_activiteit_eerste, 'DD-MM-YYYY') "
             + "                                                        fac_activiteit_eerste, "
             + "         TO_CHAR (fa.fac_activiteit_laatste, 'DD-MM-YYYY HH24:MI') "
             + "                                                       fac_activiteit_laatste, "
             + "         fa.days, "
             + "         TO_CHAR (fa.fac_activiteit_einde, 'DD-MM-YYYY') fac_activiteit_einde, "
             + "         fa.aanvrager, "
             + "         fa.fac_activiteit_xmlnode, "
             + "         NVL ("+ lcl.xsql('vp.ins_discipline_omschrijving','vp.ins_discipline_key') +", '') ins_discipline_omschrijving, "
             + "         fa.stat_omschrijving, "
             + "         fa.fac_activiteit_aantal, "
             + "         vp.rkey, "
             + "         vp.rtable, "
             + "         vp.read_write, "
             + "         fac_activiteit_omschrijving "

    var activiteit_sql = "(SELECT fac_activiteit_key"              // 0 (id)
                       + "       ,fac_activiteit_eerste"           // 1 (Eerste keer)
                       + "       ,fac_activiteit_laatste"          // 2 (Laatste keer)
                       + "       ,TRUNC(fac_activiteit_interval/24,0) days"        // 3 (Periode)
                       + "       ,fac_activiteit_einddatum fac_activiteit_einde"   // 4 (Eind)
                       + "       ,"+S("prs_pers_string")+" aanvrager "  // 5 (Aanvrager)
                       + "       ,fac_activiteit_xmlnode "         // 6 (Type)
                       + "       ,'Product' type_omschrijving "    // 7 (Product)
                       + "       ,stat.stat_omschrijving "         // 8 (Status oms)
                       + "       ,stat.stat_key "                  // 9 (Status key)
                       + "       ,'' "                             // 10 (Free)
                       + "       ,'' "                             // 11 (Free)
                       + "       ,p.prs_afdeling_key "             // 12 (Afdeling key)
                       + "       ,fac_activiteit_aantal"           // 13 (Aantal keer)
                       + "       ,p.prs_perslid_key"
                       + "       ,fac_activiteit_omschrijving"
                       + " FROM FAC_ACTIVITEIT act "
                       + "    ,(SELECT 1 stat_key, '"+L("lcl_job_stat_1_inactief")+"' stat_omschrijving "
                       + "	     FROM dual "
                       + "	     UNION ALL "
                       + "	     SELECT 2, '"+L("lcl_job_stat_2_actief")+"' "
                       + "	     FROM dual "
                       + "	     UNION ALL "
                       + "	     SELECT 3, '"+L("lcl_job_stat_3_1xoverslaan")+"' "
                       + "	     FROM dual) stat "
                       + "     ,prs_perslid p "
                       + " WHERE	act.fac_activiteit_verwijder IS NULL "
                       + " AND   act.fac_activiteit_status_key = stat.stat_key(+) "
                       + " AND   act.prs_perslid_key           = p.prs_perslid_key) ";

    var no_vakgroep_product_sql = " SELECT fa.fac_activiteit_key "
                                + "      , -1 rkey "
                                + "      , -1 ins_discipline_key "
                                + "      , '' ins_discipline_omschrijving "
                                + "      , 0 rtable "
                                + "      , read_write "
                                + "FROM FAC_ACTIVITEIT fa, "
                                + "( SELECT fac_activiteit_key "
                                  + "     , -1 rkey "
                                  + "     , -1 "
                                  + "     , '' "
                                  + "     , 0 rtable "
                                  + "     , 0 read_write "
                                  + "   FROM BES_BESTELLING WHERE FAC_ACTIVITEIT_KEY IS NOT NULL "
                                  + " UNION ALL "
                                  + " SELECT fac_activiteit_key "
                                  + "      , -1 rkey "
                                  + "      , -1 "
                                  + "      , '' "
                                  + "      , 0 rtable "
                                  + "      , 0 read_write "
                                  + "  FROM MLD_MELDING WHERE FAC_ACTIVITEIT_KEY IS NOT NULL "
                                  + " UNION ALL "
                                  + " SELECT fac_activiteit_key "
                                  + "      , -1 rkey "
                                  + "      , -1 "
                                  + "      , '' "
                                  + "      , 0 rtable "
                                  + "      , 0 read_write "
                                  + "  FROM MLD_OPDR WHERE FAC_ACTIVITEIT_KEY IS NOT NULL "
                                  + "  GROUP BY FAC_ACTIVITEIT_KEY) fa2 "
                                + " WHERE fa.fac_activiteit_key =  fa2.fac_activiteit_key(+) "
                                + "   AND fa2.fac_activiteit_key IS NULL ";

    // rooster_table:
    //  0 = nothing
    //  1 = bes_bestelling
    //  2 = mld_melding
    //  3 = mld_opdr
    var vakgroep_product_sql = "("+no_vakgroep_product_sql;

    // Product
    sql_prod_aut = "(SELECT m1.ins_discipline_key, "+ lcl.xsqla('m1.ins_discipline_omschrijving','m1.ins_discipline_key')
                 + " FROM BES_v_aanwezigdiscipline m1 "
                 + " WHERE m1.ins_discipline_key IN ( "
                   + "SELECT ins_discipline_key FROM fac_v_my_disciplines "
                   + "WHERE fac_functie_code ='"+"WEB_BESFOF"+"' "
                   + "AND prs_perslid_key="+user_key+" "
                   + ") "
                 + " ORDER BY 2) ";

    if (BESFOF_read && ((rtype_key == 1) || (rtype_key == -1)))
    {
      vakgroep_product_sql = vakgroep_product_sql
                           + " UNION ALL "
                           + " SELECT bes.fac_activiteit_key "
                           + "      , bes.bes_bestelling_key rkey "
                           + "      , dis.ins_discipline_key "
                           + "      , "+ lcl.xsqla('dis.ins_discipline_omschrijving','dis.ins_discipline_key')
                           + "      , 1 rtable "
                           + "      ,"+(BESFOF_write?"1":"0")+" read_write "
                           + " FROM BES_BESTELLING_ITEM bi "
                           + "    , BES_BESTELLING bes "
                           + "    , bes_srtDEEL sd "
                           + "    , bes_srtGROEP sg "
                           + "    , INS_TAB_DISCIPLINE dis "
                           + "    ,"+sql_prod_aut+" dis_aut "
                           + " WHERE bes.fac_activiteit_key IS NOT NULL "
                           + " AND bes.bes_bestelling_key    = bi.bes_bestelling_key "
                           + " AND bi.bes_srtdeel_key        = sd.bes_srtdeel_key "
                           + " AND sd.bes_srtgroep_key       = sg.bes_srtgroep_key "
                           + " AND sg.ins_discipline_key     = dis.ins_discipline_key "
                           + " AND sg.ins_discipline_key     = dis_aut.ins_discipline_key "
                           + " GROUP BY bes.fac_activiteit_key, bes.bes_bestelling_key, dis.ins_discipline_key, "+ lcl.xsql('dis.ins_discipline_omschrijving','dis.ins_discipline_key');
    }

    // Categorie
    var sql_dis_aut = ""
                    + "(SELECT m1.ins_discipline_key, "+ lcl.xsqla('m1.ins_discipline_omschrijving','m1.ins_discipline_key')
                    + " FROM mld_v_aanwezigdiscipline m1 "
                    + " WHERE EXISTS (SELECT 'x' "
                    +                 "FROM mld_stdmelding ms "
                    +                 "WHERE M1.ins_discipline_key = ms.mld_ins_discipline_key) "
                    + " AND m1.ins_discipline_key IN ( "
                      + "SELECT ins_discipline_key FROM fac_v_my_disciplines "
                      + "WHERE fac_functie_code in ('WEB_MLDFOF','WEB_OPDBOF') "
                      + "AND prs_perslid_key="+user_key+" "
                      + ") "
                    + " ORDER BY 2) ";

    if (MLDFOF_read && ((rtype_key == 2) || (rtype_key == -1)))
    {
      vakgroep_product_sql = vakgroep_product_sql
                           + " UNION ALL "
                           + " SELECT  m.fac_activiteit_key "
                           + "       , m.mld_melding_key rkey "
                           + "       , dis.ins_discipline_key "
                           + "       , "+ lcl.xsqla('dis.ins_discipline_omschrijving','dis.ins_discipline_key')
                           + "      , 2 rtable "
                           + "      ,"+(MLDFOF_write?"1":"0")+" read_write "
                           + " FROM mld_melding m "
                           + "    , mld_stdmelding sm"
                           + "    , ins_tab_discipline dis "
                           + "    ," + sql_dis_aut + " dis_aut "
                           + " WHERE m.fac_activiteit_key IS NOT NULL "
                           + " AND m.mld_stdmelding_key = sm.mld_stdmelding_key"
                           + " AND sm.mld_ins_discipline_key = dis.ins_discipline_key "
                           + " AND sm.mld_ins_discipline_key = dis_aut.ins_discipline_key "
                           + " GROUP BY m.fac_activiteit_key, m.mld_melding_key, dis.ins_discipline_key, "+ lcl.xsql('dis.ins_discipline_omschrijving','dis.ins_discipline_key');
    }

    if (MLDBOF_read && ((rtype_key == 3) || (rtype_key == -1)))
    {
      vakgroep_product_sql = vakgroep_product_sql
                           + " UNION ALL "
                           + " SELECT o.fac_activiteit_key "
                           + "      , o.mld_opdr_key rkey "
                           + "      , dis.ins_discipline_key "
                           + "      , "+ lcl.xsqla('dis.ins_discipline_omschrijving','dis.ins_discipline_key')
                           + "      , 3 rtable "
                           + "      ,"+(MLDBOF_write?"1":"0")+" read_write "
                           + " FROM mld_melding r "
                           + "    , mld_stdmelding sm"
                           + "    , ins_tab_discipline dis "
                           + "    , mld_opdr o "
                           + "    , "+sql_dis_aut+" dis_aut "
                           + " WHERE o.fac_activiteit_key IS NOT NULL "
                           + " AND r.mld_stdmelding_key = sm.mld_stdmelding_key"
                           + " AND r.mld_melding_key = o.mld_melding_key "
                           + " AND sm.mld_ins_discipline_key = dis.ins_discipline_key "
                           + " AND sm.mld_ins_discipline_key = dis_aut.ins_discipline_key "
                           + " GROUP BY o.fac_activiteit_key, o.mld_opdr_key, dis.ins_discipline_key, "+ lcl.xsql('dis.ins_discipline_omschrijving','dis.ins_discipline_key');
    }

    sqln += "  FROM " + activiteit_sql + " FA, "
          + " "+vakgroep_product_sql+ ") VP "
          + " WHERE fa.fac_activiteit_key = vp.fac_activiteit_key "

    if (rtype_key > -1) {
      sqln += "   AND vp.rtable = " + rtype_key
    }

    if (prs_prskey > -1) {
      sqln += "   AND fa.prs_perslid_key = " + prs_prskey;
    }

    if (act_key > -1) {
      sqln += "   AND fa.fac_activiteit_key = " + act_key
    }

    if (cat_key > -1) {
      sqln += "   AND vp.ins_discipline_key = " + cat_key
    }

    if (prod_key > -1)  {
      sqln += "   AND vp.ins_discipline_key = " + prod_key
    }

    if (stat_key > -1)  {
      sqln += "   AND fa.stat_key = " + stat_key
    }

    sqln += " ORDER BY fa.fac_activiteit_key DESC ";


    var addurl = "appl/fac/job.asp";
    if (MLDFOF_write)
    {
      addButton = [{ icon: "plus.png", title: L("lcl_add"), action: "FcltMgr.openDetail('" + addurl + "', '" + L("lcl_add") + "')" }]
    }

    var rst = new ResultsetTable({ sql: sqln,
                                   keyColumn: "fac_activiteit_key",
                                   ID: "workTable",
                                   outputmode: outputmode,
                                   showAll: showall,
                                   rowData: fnrowData,
                                   rowActionEnabler: fnrowActionEnabler,
                                   EmptySetString: "Geen berichten weer te geven",
                                   buttons: addButton
                                  });

    rst.addColumn(new Column({caption: L("lcl_job_nr"), content: "fac_activiteit_key"}));
    rst.addColumn(new Column({caption: L("lcl_descr"), content: "fac_activiteit_omschrijving", hasActions: true}));
    rst.addColumn(new Column({caption: L("lcl_job_status"), content: "stat_omschrijving"}));
    rst.addColumn(new Column({caption: L("lcl_job_firstdate"), content: "fac_activiteit_eerste"}));
    rst.addColumn(new Column({caption: L("lcl_job_n_times"), content: "fac_activiteit_aantal", Align: "center"}));
    rst.addColumn(new Column({caption: L("lcl_job_period"), content: "days", Align: "center"}));
    rst.addColumn(new Column({caption: L("lcl_job_end"), content: "fac_activiteit_einde"}));
    rst.addColumn(new Column({caption: L("lcl_job_requestor"), content: "aanvrager"}));
    rst.addColumn(new Column({caption: L("lcl_job_category")+'/'+L("lcl_job_product"), content: "ins_discipline_omschrijving"}));
    rst.addColumn(new Column({caption: L("lcl_job_lastdate"), content: "fac_activiteit_laatste"}));

    rst.addAction({ action: "jobEdit", caption: L("lcl_details"), isDefault:  true});
    rst.addAction({ action: "jobDelete", caption: L("lcl_delete"), multi: true, single: false});

    var cnt = rst.processResultset();
%>
  </body>
</html>
<%}%>