<% /*
     $Revision$
     $Id$

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

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

<%

function getFiltClauseDiscipline(pfiltcode, params)
{
  var lfiltClause = {from: "", where: ""};
  if (params.module == "BES")
  {
    lfiltClause.where = " AND td.ins_discipline_module = 'BES'";
  }
  else if (params.module == "FAQ")
  {
    lfiltClause.where = " AND td.ins_discipline_module = 'FAQ'";
  }
  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") + ")";
  }
  /* MLD-module codes;
  MLDA  = (A)lle disciplines
  MLD   = Alleen disciplines met stdmeldingen
  MLDP  = Alleen (P)rimaire vakgroepen met stdmeldingen
  MLDAP = (A)lle (P)rimaire vakgroepen
  MLDB  = Alleen (B)ehandelteams
  */
  else if (params.module == "MLD" || params.module == "MLDA" || params.module == "MLDP" || params.module == "MLDAP")
  {
    // Deze heb je bij MLD bijna altijd nodig
    lfiltClause.from = ", mld_disc_params dp";
    lfiltClause.where = " AND dp.mld_ins_discipline_key = td.ins_discipline_key"
                      + " AND sd.ins_srtdiscipline_verwijder IS NULL";

    if (params.module == "MLD" || params.module == "MLDP")
      lfiltClause.from += ", mld_stdmelding sm";

    lfiltClause.where += " AND td.ins_discipline_module = 'MLD'"
                       // Standaard alleen vakgroepen tonen met stdmeldingen.
                      + (params.stdmelding_keys && params.stdmelding_keys.length
                        ? " AND sm.mld_stdmelding_key IN (" + params.stdmelding_keys.join(",") + ")"
                        : "")
                      + (params.module == "MLD" || params.module == "MLDP"
                        ? " AND td.ins_discipline_key = sm.mld_ins_discipline_key"
                          + " AND sm.mld_stdmelding_verwijder IS NULL"
                        : "")
                      + (params.ref_type == "C" // issue initiated from contract
                        ? "  AND BITAND(sm.mld_stdmelding_kanverwijzen,32) = 32"
                        : "");

    if (params.module == "MLDP" || params.module == "MLDAP")
    {
      lfiltClause.where += " AND BITAND(dp.mld_disc_params_srtgroep, 1) = 1";
    }
  }
  else if (params.module == "MLDB")
  {
    lfiltClause.from = ", mld_disc_params dp";
    lfiltClause.where = " AND dp.mld_ins_discipline_key = td.ins_discipline_key"
                      + " AND BITAND(dp.mld_disc_params_srtgroep, 2) = 2";

    // Determine if category can hold processing teams
    var filterMldDiscs = "";
    var disc_oRs = Oracle.Execute("SELECT count(*) aantal "
                                + " FROM ins_tab_discipline td, mld_disc_params dp "
                                + "WHERE td.ins_discipline_key = dp.mld_ins_discipline_key"
                                + "  AND td.ins_discipline_key = " + params.disc
                                + "  AND BITAND(dp.mld_disc_params_srtgroep, 5) = 5");
    aantal = disc_oRs("aantal").Value;
    disc_oRs.Close();
    // category can hold processing teams
    // determine if a filter must be aplied or to show all the processing teams
    if (aantal > 0) {
      filterMldDiscs = " SELECT mld_discipline_key2 "
                    + " FROM mld_discipline_discipline "
                    + "WHERE mld_discipline_key1 = " + params.disc;
      disc_oRs = Oracle.Execute(filterMldDiscs);
      if (!disc_oRs.Eof) {
        lfiltClause.where += " AND ins_discipline_key in (" + filterMldDiscs + ")";
      }
    disc_oRs.Close();
    }
  }

  if (pfiltcode && pfiltcode != "")
  {
    switch(pfiltcode) // Let op dat sommige filtercodes over INS gaan en andere over MLD
    { // ins_srtgroep sg is er bij INS altijd
      case "I":
      case "IB2":  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"
                                      + "            ))"
                                      + (pfiltcode == "IB2" // Interne uitvoerder, alleen de taken van de eigen bedrijfdienstlocaties en daar de ins_disciplines van.
                                      ? " AND td.ins_discipline_key IN"
                                        + "  (SELECT isi.ins_discipline_key"
                                        + "     FROM ins_srtcontrole isc"
                                        + "        , ins_v_allsrtinstallatie isi"
                                        + "    WHERE isc.ins_srtinstallatie_key = isi.ins_srtinstallatie_key"
                                        + "      AND isc.ins_srtcontrole_niveau = isi.niveau"
                                        + "      AND isc.prs_dienst_key IN (SELECT DISTINCT d.prs_dienst_key"
                                        + "                                   FROM prs_bedrijfdienstlocatie bdl"
                                        + "                                      , prs_dienst d"
                                        + "                                  WHERE bdl.prs_dienst_key = d.prs_dienst_key"
                                        + "                                    AND d.prs_dienst_key = isc.prs_dienst_key"
                                        + "                                    AND bdl.prs_bedrijf_key IN (SELECT cp.prs_bedrijf_key"
                                        + "                                                                  FROM prs_contactpersoon cp"
                                        + "                                                                 WHERE cp.prs_perslid_key = " + user_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.where += " 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.where += " AND dp.mld_ins_discipline_key = td.ins_discipline_key";
                   lfiltClause.where += " AND dp.mld_disc_params_keten = 1";
                   break;
      case "MG":   // (is inclusief 'VD') Alleen de vakgroepen waarop gegroepeerd kan worden mag je zien
                   lfiltClause.where += " AND (   (sm.mld_stdmelding_vervaldatum IS NULL) "
                                      + "      OR (sm.mld_stdmelding_vervaldatum > SYSDATE))"
                                      + " 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;
    }
  }

  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.autfunctionKeyArray && params.autfunctionKeyArray[0] > 0) // Efficiente versie van FAC_V_MY_PR**S_AFDELINGEN_READ
  {
    var authparams = user.checkAutorisation(params.autfunctionKeyArray);
    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;
            oRs.Close();
        }

        lfiltClause.where+= " AND td.ins_discipline_key IN"
                          + "     (SELECT ins_discipline_key"
                          + "        FROM fac_v_webgebruiker fw"
                          + "       WHERE fac_functie_key IN (" + params.autfunctionKeyArray.join(",") + ")"
                          + "         AND fac_gebruiker_prs_level_" + (params.autRW == "R"? "read" : "write") + " <= " + same_level
                          + "         AND fac_gebruiker_alg_level_" + (params.autRW == "R"? "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.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 td.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("D")) // 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 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 mdp.mld_disc_params_eigen_kp"
                                    + "     FROM mld_disc_params mdp"
                                    + "    WHERE mdp.mld_ins_discipline_key = td.ins_discipline_key) extra";
                break;
    }
  }
  return lExtraInf;
}
%>
