<% /*
     $Revision$
     $Id$

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

<!-- #include file="../../mld/mld.inc" -->

<%

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;
      case "MG":   // Alleen de vakgroepen waarop gegroepeerd kan worden mag je zien
                   lfiltClause.from += ", mld_disc_params dp";
                   lfiltClause.where += " AND dp.mld_ins_discipline_key = di.ins_discipline_key"
                                      + " AND CASE WHEN dp.mld_disc_params_mldgroup = 0"
                                      + "          THEN BITAND(sd.ins_srtdiscipline_mldgroup, 1)"
                                      + "          ELSE BITAND(dp.mld_disc_params_mldgroup, 1)"
                                      + "     END = 1";
                   break;
      case "WINS": // Alleen disciplines met objecten (INS)
                   lfiltClause.where += " AND sd.ins_srtdiscipline_ins = 1"
                                      + " AND sd.ins_srtdiscipline_key != " + S("cnd_gebreken_srtdisc_key"); // Geen conditie metingen.
                   break;
      default:     __Log("KIES JUISTE FILTERCODE!!!");
                   lfiltClause.where = " AND 1 = 2";
                   break;
    }
  }

  // meldingen die extern worden afgehandeld kunnen nooit worden gekozen.
  lfiltClause.where += "   AND sm.mld_stdmelding_externurl IS NULL";

  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.stdmelding_keys && params.stdmelding_keys.length)
  {
    lfiltClause.where += "   AND sm.mld_stdmelding_key IN (" + params.stdmelding_keys.join(",") + ")"
  }
  else if (params.disc_key && params.disc_key != -1) // parent gegeven
  {
    lfiltClause.where += " AND di.ins_discipline_key = " + params.disc_key;
  }
  else if (params.disc_keys && params.disc_keys.length)
  {
    lfiltClause.where += " AND di.ins_discipline_key IN (" + params.disc_keys.join(",") + ")"
  }
  else if (params.srtdisc_key && params.srtdisc_key != -1) // parent-parent gegeven
  {
    lfiltClause.where += " AND di.ins_srtdiscipline_key = " + params.srtdisc_key;
  }
  else if (params.srtdisc_keys && params.srtdisc_keys.length)
  {
    lfiltClause.where += " AND di.ins_srtdiscipline_key IN (" + params.srtdisc_keys.join(",") + ")"
  }

  if (params.autfunctionKeyArray && params.autfunctionKeyArray[0] > 0) // Efficiente versie van FAC_V_MY_PR**S_AFDELINGEN_READ
  {
    if (!("perslidKey" in params) || user_key == params.perslidKey)
      var same_level = 9;
    else if (params.perslidKey > 0) // aut.fac_get_prs_same_level niet onnodig aanroepen
    {
      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();
    }
    else
    {
      var same_level = 0; // Dit geeft aut.fac_get_prs_same_level bij params.perslidKey == -1.
    }

    lfiltClause.where += " AND di.ins_discipline_key IN"
                        + " (SELECT ins_discipline_key FROM fac_v_webgebruiker fw"
                        + "   WHERE fac_gebruiker_alg_level_write < 9" // extra: Vooral voor MLDUSE gebruikers met alleen PRS-leesrechten
                        + "     AND fac_gebruiker_prs_level_write <= " + same_level
                        + "     AND fac_functie_key IN (" + params.autfunctionKeyArray.join(",") + ")"
                        + "     AND prs_perslid_key = " + user_key
                        + "     AND di.ins_discipline_key = fw.ins_discipline_key)"
  }

  if (params.behandelteam_key > 0 && params.disc_old > 0)
  { // Andere vakgroepen waar hetzelfde behandelteam ingezet kan worden zijn:
    // 1) De vakgroepen met het hetzelfde bahandelteam.
    // 2) De vakgroepen met alle behandelteams (geen enkel mld_discipline_discipline record).
    lfiltClause.where += " AND sm.mld_ins_discipline_key IN"
                       + "     (SELECT mld_discipline_key1 ins_discipline_key"
                       + "        FROM mld_discipline_discipline"
                       + "       WHERE mld_discipline_key2 = " + params.behandelteam_key
                       + "         AND mld_discipline_key1 != " + params.disc_old
                       + "      UNION"
                       + "      SELECT ins_discipline_key"
                       + "        FROM ins_tab_discipline"
                       + "           , mld_disc_params"
                       + "       WHERE ins_tab_discipline.ins_discipline_key = mld_disc_params.mld_ins_discipline_key(+)"
                       + "         AND mld_disc_params.mld_disc_params_srtgroep IN (5, 7)"
                       + "         AND ins_tab_discipline.ins_discipline_module = 'MLD'"
                       + "         AND ins_discipline_verwijder IS NULL"
                       + "         AND NOT EXISTS"
                       + "                 (SELECT mld_discipline_key1"
                       + "                    FROM mld_discipline_discipline"
                       + "                   WHERE mld_discipline_key1 = ins_discipline_key))";
  }

  if (params.ins_key_arr && params.ins_key_arr.length)
  {
     lfiltClause.where += " AND EXISTS"
                            + " (SELECT 1"
                            + "    FROM mld_v_mld_stdmelding_srtinst si"
                            + "   WHERE si.mld_stdmelding_key = sm.mld_stdmelding_key"
                            + "     AND si.ins_deel_key IN (" + params.ins_key_arr.join(",") + ")"
                            + " )";

  }

  if (params.loc_key > 0 && mld.useAlgScope("S")) // 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"
                       + "              )"
                       + "      )";
  }

  if (params.room_key > 0)
  {
    lfiltClause.from += ", mld_stdmelding_srtruimte mss";
    lfiltClause.where += " AND sm.mld_stdmelding_key = mss.mld_stdmelding_key(+)"
                       + " AND (mss.mld_stdmelding_key IS NULL"
                       + "  OR mss.alg_srtruimte_key = (SELECT alg_srtruimte_key"
                       + "                                FROM alg_ruimte"
                       + "                               WHERE alg_ruimte_key = " + params.room_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;
      case "PM": lExtraInf = ", COALESCE(mld_stdmelding_priomatrix, 0) extra";
                 break;
    }
  }
  return lExtraInf;
}
%>
