<% /*
     $Revision$
     $Id$

    File:               stdmeldingFilter.inc
    Description:        functies voor stdmelding suggest velden
    Globals:
    Context:
    Note:
*/ %>


<%

function getFiltClauseStdmelding(pfiltcode, params, init)
{
  var lfiltClause = {from: "", where: ""};
  if (pfiltcode && pfiltcode != "")
  {
    switch(pfiltcode)
    {
      case "FO":   break;
      case "FE":   lfiltClause.where += " AND sm.mld_stdmelding_notfrontend = 0";
                   break;
      case "VDK":  // Alleen de vakgroepen die gewijzigd mogen worden mag je zien
                   lfiltClause.from += ", mld_disc_params dp";
                   lfiltClause.where += " AND dp.mld_ins_discipline_key = di.ins_discipline_key"
                                      + " AND dp.mld_disc_params_keten = 1";
                   break;
      default:     __Log("KIES JUISTE FILTERCODE!!!");
                   lfiltClause.where = " AND 1 = 2";
                   break;
    }
  }

  if (!params.show_expired && !init)
  { // Alleen bij vinkje "Toon vervallen" en bij init de vervallen stdmeldingen laten zien.
    lfiltClause.where += " AND (sm.mld_stdmelding_vervaldatum IS NULL"
                       + "      OR sm.mld_stdmelding_vervaldatum > SYSDATE)";
  }

  if (params.stdmelding_key && params.stdmelding_key > 0)
  {
    lfiltClause.where += "   AND sm.mld_stdmelding_key = " + params.stdmelding_key
  }
  else
  if (params.disc_key && params.disc_key != -1) // parent gegeven
  {
    lfiltClause.where += " AND di.ins_discipline_key = " + params.disc_key;
  }
  else if (params.srtdisc_key && params.srtdisc_key != -1) // parent-parent gegeven
  {
    lfiltClause.where += " AND di.ins_srtdiscipline_key = " + params.srtdisc_key;
  }

  if (params.autfunctionKey > -1) // Efficiente versie van FAC_V_MY_PR**S_AFDELINGEN_WRITE
  {
    if (user_key == params.perslidKey)
      var same_level = 9;
    else
    {
      var sql = "SELECT aut.fac_get_prs_same_level(" + user_key + ", " + params.perslidKey + ") theLevel FROM DUAL";
      var oRs = Oracle.Execute(sql);
      var same_level = oRs("theLevel").Value;
      oRs.Close();
    }
    lfiltClause.where += " AND di.ins_discipline_key IN"
                       + " (SELECT ins_discipline_key FROM fac_v_webgebruiker fw"
                       + "   WHERE fac_functie_key = " + params.autfunctionKey
                       + "     AND fac_gebruiker_prs_level_write <= " + same_level
                       + "     AND fac_gebruiker_alg_level_write < 9" // extra: Vooral voor MLDUSE gebruikers met alleen PRS-leesrechten
                       + "     AND prs_perslid_key = " + user_key
                       + "     AND di.ins_discipline_key = fw.ins_discipline_key)"
  }

  if (params.ins_key > 0)
  {
     lfiltClause.where += "  AND EXISTS (SELECT 1"
                        + "        FROM ins_srtgroep isg,"
                        + "             ins_srtdeel isd,"
                        + "             ins_deel insd"
                        + "       WHERE insd.ins_deel_key = " + params.ins_key
                        + "         AND isd.ins_srtdeel_key = insd.ins_srtdeel_key"
                        + "         AND isg.ins_srtgroep_key = isd.ins_srtgroep_key"
                        + "         AND (   sm.ins_srtinst_niveau = 'S' AND ins_srtinst_key = insd.ins_srtdeel_key"
                        + "              OR sm.ins_srtinst_niveau = 'G' AND ins_srtinst_key = isg.ins_srtgroep_key"
                        + "              OR sm.ins_srtinst_niveau = 'D' AND ins_srtinst_key = isg.ins_discipline_key"
                        //       + "        OR msm.ins_srtinst_niveau IS NULL" // is wel héél algemeen
                        + "             )"
                        + "     )";

  }

  if (S("mld_edit_layout") == 1 && params.loc_key > 0) // Als de bld_key, flr_key of room_key is meegegeven is ook de loc_key meegegeven.
  { // Plaats gegevens worden voor de discipline/stdmelding gegevens getoond. Discipline en stdmelding afhankelijk van dienstlocatie maken.
    // Als de melding onderdeel is van *een* dienstniveau dan moet bij de ruimte een niveau ingevuld zijn waar deze melding onderdeel van is.
    lfiltClause.where += "  AND ((SELECT COUNT (*) aantal"
                       + "          FROM mld_dienstpakket dp1"
                       + "         WHERE dp1.mld_stdmelding_key = sm.mld_stdmelding_key) = 0"
                       + "     OR"
                       + "       EXISTS (SELECT  COALESCE (NULL"
                       + (params.room_key > 0? "         , ar.mld_dienstniveau_key" : "")
                       + (params.flr_key  > 0? "         , av.mld_dienstniveau_key" : "")
                       + (params.bld_key  > 0? "         , ag.mld_dienstniveau_key" : "")
                       + "                               , al.mld_dienstniveau_key"
                       + "                                ) mld_dienstniveau_key"
                       + "                 FROM"
                       + (params.room_key > 0? " alg_ruimte ar," : "")
                       + (params.flr_key > 0?  " alg_verdieping av," : "")
                       + (params.bld_key > 0?  " alg_gebouw ag," : "")
                       + "                       alg_locatie al,"
                       + "                       mld_dienstpakket dp"
                       + "                WHERE"
                       + (params.room_key > 0? " ar.alg_ruimte_key = " + params.room_key
                       + "                 AND   ar.alg_verdieping_key = av.alg_verdieping_key AND" : "")
                       + (params.flr_key > 0?  " av.alg_verdieping_key = " + params.flr_key
                       + "                 AND   av.alg_gebouw_key = ag.alg_gebouw_key AND" : "")
                       + (params.bld_key > 0?  " ag.alg_gebouw_key = " + params.bld_key
                       + "                 AND   ag.alg_locatie_key = al.alg_locatie_key AND" : "")
                       + "                       al.alg_locatie_key = " + params.loc_key
                       + "                 AND dp.mld_stdmelding_key = sm.mld_stdmelding_key"
                       + "                 AND   COALESCE (NULL"
                       + (params.room_key > 0? "         , ar.mld_dienstniveau_key" : "")
                       + (params.flr_key > 0?  "         , av.mld_dienstniveau_key" : "")
                       + (params.bld_key > 0?  "         , ag.mld_dienstniveau_key" : "")
                       + "                               , al.mld_dienstniveau_key"
                       + "                                ) = dp.mld_dienstniveau_key"
                       + "              )"
                       + "      )";
  }

  return lfiltClause;
}

function getExtraInfStdmelding(pextracode)
{
  var lExtraInf = ", sm.mld_ins_discipline_key extra";
  if (pextracode && pextracode != "")
  {
    switch(pextracode)
    {
      case 'KB': lExtraInf = ", (SELECT COUNT(*)"
                           + "     FROM mld_stdmeldingfaq sf"
                           + "    WHERE (sf.mld_stdmelding_key = sm.mld_stdmelding_key"
                           + "       OR (sf.ins_discipline_key = sm.mld_ins_discipline_key"
                           + "           AND sf.mld_stdmelding_key IS NULL))) extra";
                 break;
    }
  }
  return lExtraInf;
}
%>
