<% /*
     $Revision$
     $Id$

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


<%

function getFiltClauseSrtcontrole(pfiltcode, params, init)
{
  var lfiltClause = {select: "", from: "", where: ""};
  if (pfiltcode && pfiltcode != "")
  {
    switch(pfiltcode)
    {
      case "MJOB": // MJOB: Alleen de MJOB taken mag je zien.
                   lfiltClause.select += ", " + lcl.xsqla("ins_srtcontrole_omschrijving", "isc.ins_srtcontrole_key")
                   lfiltClause.where += " AND cdp.ctr_disc_params_ismjob = 1"
                                      + " AND (ins_srtcontrole_eind IS NULL OR ins_srtcontrole_eind > SYSDATE)";
                   break;
      case "PI":   // Alleen de Periodieke Inspecties (PI) mag je zien.
                   if (params.ctrdisc_key_arr && params.ctrdisc_key_arr.length > 0)
                     var ctr_disc_sql = params.ctrdisc_key_arr.join(",");
                   else
                     var ctr_disc_sql = "SELECT w.ins_discipline_key"
                                      + "  FROM fac_v_webgebruiker w"
                                      + "     , fac_functie f"
                                      + " WHERE w.fac_functie_key = f.fac_functie_key "
                                      + "   AND f.fac_functie_code = 'WEB_CTRUSE'"
                                      + "   AND w.fac_gebruiker_prs_level_read < 9"
                                      + "   AND w.fac_gebruiker_alg_level_read < 9"
                                      + "   AND w.prs_perslid_key = " + user_key;
                   lfiltClause.select += ", " + lcl.xsql("ins_srtcontrole_omschrijving", "isc.ins_srtcontrole_key")
                                       + "   || CASE"
                                       + "      WHEN ins_srtcontrole_niveau = 'S'"
                                       + "      THEN ' ' || asi.srtdeel_oms"
                                       + "      WHEN ins_srtcontrole_niveau = 'G'"
                                       + "      THEN ' ' || asi.srtgroep_oms"
                                       + "      WHEN ins_srtcontrole_niveau = 'D'"
                                       + "      THEN ' ' || asi.discipline_oms"
                                       + "      ELSE ''"
                                       + "      END ins_srtcontrole_omschrijving"
                   lfiltClause.from += ", ins_v_allsrtinstallatie asi";
                   lfiltClause.where += " AND asi.ins_srtinstallatie_key = isc.ins_srtinstallatie_key"
                                      + " AND asi.niveau = isc.ins_srtcontrole_niveau"
                                      + " AND ctr_discipline_key IN (" + ctr_disc_sql + ")"
                                      + (params.intern_uitv // Interne uitvoerder, alleen diensten van eigen bedrijf.
                                        ? " 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;
      default:     __Log("KIES JUISTE FILTERCODE!!!");
                   lfiltClause.where = " AND 1 = 2";
                   break;
    }
    if (params.srtcontrole_key && params.srtcontrole_key > 0)
    {
      lfiltClause.where += "   AND isc.ins_srtcontrole_key = " + params.srtcontrole_key
    }
  }

  if (params.ctrdisc_key_arr && params.ctrdisc_key_arr.length > 0) // parent gegeven
  {
    lfiltClause.where += " AND isc.ctr_discipline_key IN (" + params.ctrdisc_key_arr.join(",") + ")";
  }

  return lfiltClause;
}
%>
