<%@language = "javascript" %>
<% /*
     $Revision$
     $Id$

    File:           SuggestSrtdeel.asp
    Description:    Suggereer Srtdelen
    Parameters:     k             De eerste paar letters
                    filtcode beperk de lijst
                    autfunction : verplicht:
    Globals:
    Context:
    Note:           LET OP: Dit bestand wordt zowel voor BES_SRTDEEL als INS_SRTDEEL gebruikt!

*/ %>

<%
JSON_Result = true;
%>

<!-- #include file="../../Shared/common.inc" -->
<!-- #include file="./suggest.inc" -->
<!-- #include file="./SrtdeelFilter.inc" -->

<%
var chars = getQParam("k", "");
var autfunction = getQParam("autfunction");
var bAll = getQParamInt("SuggestAll", 0) == 1;
var disc_key = getQParamInt("disc_key", -1);
var srtgroep_key = getQParamInt("srtgroep_key", -1);
var orgmodule = getQParamSafe("module", "INS");
var extracode = getQParam("extracode", "");
var filtcode = getQParam("filtcode", "");
var binding = getQParamInt("binding", -1);
var module = (orgmodule == "SLE" ? "INS" : orgmodule);

if (orgmodule != "INS" && orgmodule != "BES" && orgmodule != "SLE")
  shared.internal_error(L("lcl_obj_bad_value") + " module={0}.".format(orgmodule));

var chars = chars.replace(/\*/g,"%");

var params = {  autfunction: autfunction,
                disc_key: disc_key,
                srtgroep_key: srtgroep_key,
                module: orgmodule,
                binding: binding };

var filtClause = getFiltClauseSrtdeel(filtcode, params);
var extraInf = getExtraInfSrtdeel(extracode, module);

var sql = "SELECT sd." + module + "_srtdeel_key srtdeel_key"
        + ",  CASE"
          + " WHEN sd." + module + "_srtdeel_nr IS NULL"
          + " THEN ''"
          + " ELSE sd." + module + "_srtdeel_nr || ' '"
          + " END ||"
          +   lcl.xsql("sd." + module + "_srtdeel_omschrijving",  "sd." + module + "_srtdeel_key");
    if (module == "INS")
    {
        sql += " ||CASE"
             + " WHEN sd." + module + "_srtdeel_code IS NULL"
             + " THEN ''"
             + " ELSE ' ('||sd." + module + "_srtdeel_code || ') '"
             + " END ";
    }

    var koppelteken = ((S("ins_srtdeelsuggest_info") == 3) ? "||' - '||" : "");
    sql += " srtdeel_omschrijving"
         + ", " + (S("ins_srtdeelsuggest_info") & 2  ? lcl.xsql("id." + module + "_discipline_omschrijving", "id." + module + "_discipline_key") + koppelteken : "")
                + (S("ins_srtdeelsuggest_info") & 1  ? lcl.xsql("sg." + module + "_srtgroep_omschrijving",   "sg." + module + "_srtgroep_key") : "")
                + " srtgroep_omschrijving"
         + extraInf
         + " FROM " + module + "_srtdeel sd"
         + ", "     + module + "_v_aanwezigsrtgroep sg"
         + filtClause.from
         + " WHERE sd." + module + "_srtgroep_key = sg." + module + "_srtgroep_key"
         + " AND (" + module + "_srtdeel_vervaldatum IS NULL"
           + " OR " + module + "_srtdeel_vervaldatum > SYSDATE)"
         + " AND (UPPER(" + lcl.xsql("sd." + module + "_srtdeel_omschrijving",  "sd." + module + "_srtdeel_key")  + ") LIKE " + safe.quoted_sql_wild2(chars)
           + " OR UPPER(" + lcl.xsql("sg." + module + "_srtgroep_omschrijving", "sg." + module + "_srtgroep_key") + ") LIKE " + safe.quoted_sql_wild2(chars)
           + " OR UPPER(sd." + module + "_srtdeel_nr) LIKE " + safe.quoted_sql_wild2(chars)
           + (module == "INS"?" OR UPPER(sd." + module + "_srtdeel_code) LIKE " + safe.quoted_sql_wild2(chars):"")
           + ")"
         + filtClause.where
         + " ORDER BY sd." + module + "_srtdeel_nr"
         + ", UPPER(" + lcl.xsql("sd." + module + "_srtdeel_omschrijving", "sd." + module + "_srtdeel_key") + ")";

WriteResult(sql, bAll, "srtdeel_omschrijving", "srtdeel_key", "srtgroep_omschrijving", "extra");
%>
<% ASPPAGE_END(); %> 
