<% /*
     $Revision$
     $Id$

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


<%

function getFiltClauseDiscipline(pfiltcode, params)
{
  var lfiltClause = {from: "", where: ""};
  if (params.module == "BES")
  {
    lfiltClause.where = " AND td.ins_discipline_module = 'BES'";
  }
  else if (params.module == "INS")
  {
    lfiltClause.from = ", ins_srtgroep sg";
    lfiltClause.where = " AND sg.ins_discipline_key = td.ins_discipline_key"
                      + " AND td.ins_discipline_module = 'INS'"
                      + " AND sg.ins_srtgroep_key NOT IN (" + S("sle_ins_srt_groep_key") + ", " + S("cil_ins_srt_groep_key") + ")";
  }
  else if (params.module == "SLE")
  {
    lfiltClause.from = ", ins_srtgroep sg";
    lfiltClause.where = " AND sg.ins_discipline_key = td.ins_discipline_key"
                      + " AND td.ins_discipline_module = 'INS'"
                      + " AND sg.ins_srtgroep_key IN (" + S("sle_ins_srt_groep_key") + ", " + S("cil_ins_srt_groep_key") + ")";
  }
  else if (params.module == "MLD" || params.module == "MLDA")
  {
    if (params.module == "MLD") lfiltClause.from = ", mld_stdmelding sm";
    lfiltClause.where = " AND td.ins_discipline_module = 'MLD'"
                       // Standaard alleen vakgroepen tonen met stdmeldingen.
                      + (params.module == "MLD"
                        ? " AND td.ins_discipline_key = sm.mld_ins_discipline_key"
                          + " AND sm.mld_stdmelding_verwijder IS NULL"
                        : "");
  }

  if (pfiltcode && pfiltcode != "")
  {
    switch(pfiltcode)
    { // ins_srtgroep sg is er bij INS altijd
      case "I":    lfiltClause.where += " AND sg.ins_srtgroep_key IN"
                                      + " (SELECT sd1.ins_srtgroep_key"
                                      + "    FROM ins_srtcontrole isc"
                                      + "       , ins_srtdeel sd1"
                                      + "   WHERE sd1.ins_srtgroep_key = sg.ins_srtgroep_key"
                                      + "    AND (    isc.ins_srtcontrole_niveau = 'S' AND isc.ins_srtinstallatie_key = sd1.ins_srtdeel_key"
                                      + "          OR isc.ins_srtcontrole_niveau = 'G' AND isc.ins_srtinstallatie_key = sg.ins_srtgroep_key"
                                      + "          OR isc.ins_srtcontrole_niveau = 'D' AND isc.ins_srtinstallatie_key = sg.ins_discipline_key"
                                      + "        ))";
                   break;
      case "FE":   lfiltClause.where += " AND sm.mld_stdmelding_notfrontend = 0"
                                      + " AND ((sm.mld_stdmelding_vervaldatum is null) "
                                      + "  OR  (sm.mld_stdmelding_vervaldatum > sysdate))";
                   break;
      case "VDK":  // Niet vervallen, binnen keten. Alleen de vakgroepen die gewijzigd mogen worden mag je zien
                   lfiltClause.from += ", mld_disc_params dp";
                   lfiltClause.where += " AND dp.mld_ins_discipline_key = td.ins_discipline_key"
                                      + " AND dp.mld_disc_params_keten = 1";
                   // Geen break, doorvallen naar VD
      case "VD":   // Niet vervallen. Alleen de vakgroepen die gewijzigd mogen worden mag je zien
                   lfiltClause.where += " AND ((sm.mld_stdmelding_vervaldatum is null) "
                                      + "  OR (sm.mld_stdmelding_vervaldatum > sysdate))";
                   break;
      case "K":    // Alleen de vakgroepen die gewijzigd mogen worden mag je zien
                   lfiltClause.from += ", mld_disc_params dp";
                   lfiltClause.where += " AND dp.mld_ins_discipline_key = td.ins_discipline_key"
                                      + " AND dp.mld_disc_params_keten = 1";
                   break;
      default:     __Log("KIES JUISTE FILTERCODE!!!");
                   lfiltClause.where += " AND 1 = 2";
                   break;
    }
  }

  if (params.srtdisc_key && params.srtdisc_key != -1) // parent gegeven
    lfiltClause.where += " AND td.ins_srtdiscipline_key = " + params.srtdisc_key;

  if (params.disc_key && params.disc_key > 0)
    lfiltClause.where += " AND td.ins_discipline_key = " + params.disc_key;

  if (params.autfunctionKey > -1) // Efficiente versie van FAC_V_MY_PR**S_AFDELINGEN_READ
  {
    var authparams = user.checkAutorisation(params.autfunctionKey);
    if (authparams.fac_functie_discipline == 1)
    {
        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;
        }

        lfiltClause.where+= " AND td.ins_discipline_key IN"
                          + "     (SELECT ins_discipline_key"
                          + "        FROM fac_v_webgebruiker fw"
                          + "       WHERE fac_functie_key = "+ params.autfunctionKey
                          + "         AND fac_gebruiker_prs_level_" + (params.autRW == "R" || params.readonly ? "read" : "write") + " <= " + same_level
                          + "         AND fac_gebruiker_alg_level_" + (params.autRW == "R" || params.readonly ? "read" : "write") + " < 9" // extra: Vooral voor MLDUSE gebruikers met alleen PRS-leesrechten
                          + "         AND prs_perslid_key=" + user_key
                          + "         AND fw.ins_discipline_key= td.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 = " + 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 getExtraInfDiscipline(pextracode)
{
  //var lExtraInf = ", td.ins_srtdiscipline_key extra";
  var lExtraInf = { selectstr: ", td.ins_srtdiscipline_key extra", fromstr: "", wherestr: "" };
  if (pextracode && pextracode != "")
  {
    switch(pextracode)
    {
      case 'A': lExtraInf.selectstr = ", idp.ins_disc_params_autonum extra";
                lExtraInf.fromstr = ", ins_disc_params idp"
                lExtraInf.wherestr = " AND td.ins_discipline_key = idp.ins_discipline_key"
                break;
      case 'K': lExtraInf.selectstr = ", (SELECT dp.mld_disc_params_eigen_kp FROM mld_disc_params dp WHERE dp.mld_ins_discipline_key = td.ins_discipline_key) extra";
                break;
    }
  }
  return lExtraInf;
}
%>
