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

    File:               SuggestGebouwTerrein.asp
    Description:        Suggereer gebouwen en terreinen
    Parameters:         k             De eerste paar letters
                        filtcode beperk de lijst met getoonde gebouwen en terreinen TO BE DEFINED
                        lockey      : optioneel:
                        autlevel    : optioneel: schrijfrechten op dit niveau nodig
    Globals:
    Context:
    Note:
*/ %>

<%
JSON_Result = true;
%>

<!-- #include file="../../Shared/common.inc" -->
<!-- #include file="./suggest.inc" -->
<!-- #include file="./plaatsFilter.inc" -->
<!-- #include file="../../BEZ/bez.inc" -->

<%
var chars = getQParam("k", "");
var autlevel = getQParamInt("autlevel", -1);
var bAll = getQParamInt("SuggestAll", 0) == 1;
var regkey = getQParamInt("regkey", -1);
var diskey = getQParamInt("diskey", -1);
var lockey = getQParamInt("lockey", -1);
var filtcode = getQParam("filtcode", "");
var terrein = getQParamInt("terrein", 0) == 1;
var act_key = getQParamInt("act_key", -1);
var stdm_key = getQParamInt("stdm_key", -1);
var person_key = getQParamInt("personfor_key", -1);
if (person_key == -1) person_key = getQParamInt("person_key", -1);
var issuer_search = getQParamInt("issuer_search", 0);
var search_person = (person_key > 0 && issuer_search == 1);
if (person_key > 0 && issuer_search == 1) { autlevel = new Perslid(person_key).checkAutorisation("WEB_MLDUSE").ALGwritelevel; }
var scope = getQParamInt("scope", 0);

var params = { stdm_key: stdm_key, terrein: terrein, scope: scope };
if (filtcode == "PRK")
{
  var date_from = getQParamDate("date_from", new Date());
  var date_to = getQParamDate("date_to", new Date());

  // Verplichte parameters
  var params = { date_from : date_from,
                 date_to : date_to,
                 locatiekey : lockey
               };
}
params.act_key = act_key;

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

var filtClauseGebouw = getFiltClauseGebouw(filtcode, params); // Alleen voor gebouw gedeelte
var filtClause = "";

if (lockey > 0)
{ // Locatie (parent) gegeven
  filtClause += " AND l.alg_locatie_key = " + lockey;
}
else if (diskey > 0)
{ // District gegeven
  filtClause += " AND l.alg_locatie_key IN"
                + " (SELECT alg_locatie_key"
                + " FROM alg_v_aanweziglocatie"
                + " WHERE alg_district_key = " + diskey + ")";
}
else if (regkey > 0)
{ // Regio gegeven
  filtClause += " AND l.alg_locatie_key IN"
                + " (SELECT ll.alg_locatie_key"
                + " FROM alg_v_aanweziglocatie ll"
                + ", alg_v_aanwezigdistrict d"
                + " WHERE ll.alg_district_key = d.alg_district_key"
                + " AND d.alg_regio_key = " + regkey + ")";
}

if (autlevel > -1)
{
  var filtClauseTerr =
      filtClause + " AND t.alg_locatie_key IN"
                 + " (SELECT alg_locatie_key FROM fac_v_my_locations fl"
                 + "   WHERE prs_perslid_key = " + (search_person ? person_key : user_key)
                 + "     AND niveau = " + autlevel
                 + "     AND fl.alg_locatie_key = t.alg_locatie_key)";
  filtClause +=" AND g.alg_locatie_key IN"
             + " (SELECT alg_locatie_key FROM fac_v_my_locations fl"
             + "   WHERE prs_perslid_key = " + (search_person ? person_key : user_key)
             + "     AND niveau = " + autlevel
             + "     AND fl.alg_locatie_key = g.alg_locatie_key)";
}
else
  filtClauseTerr = filtClause;

sql = "SELECT g.alg_gebouw_key alg_key"
    + "     , " + S("alg_geb_string") + filtClauseGebouw.code + " code"
    + "     , " + lcl.xsql('sg.alg_srtgebouw_omschrijving', 'sg.alg_srtgebouw_key') + " naam"
    + "     , 'B' extra"
    + "     , 1 niveau"
    + " FROM alg_v_aanweziggebouw g"
    + "    , alg_locatie l"
    + "    , alg_srtgebouw sg"
    + filtClauseGebouw.from
    + " WHERE (UPPER(" + S("alg_geb_string") + ") LIKE " + safe.quoted_sql_wild2(chars)
    + "     OR UPPER(" + lcl.xsql('sg.alg_srtgebouw_omschrijving', 'sg.alg_srtgebouw_key') + ") LIKE " + safe.quoted_sql_wild2(chars) + ")"
    + "   AND l.alg_locatie_key = g.alg_locatie_key"
    + "   AND g.alg_srtgebouw_key = sg.alg_srtgebouw_key(+)"
    + "   AND (l.alg_locatie_vervaldatum IS NULL OR l.alg_locatie_vervaldatum > TRUNC(SYSDATE))"
    + "   AND (g.alg_gebouw_vervaldatum IS NULL OR g.alg_gebouw_vervaldatum > TRUNC(SYSDATE))"
    + filtClauseGebouw.where_geb
    + filtClause;

if (autlevel >= 3)
  sql +=" AND g.alg_gebouw_key IN"
      + " (SELECT alg_gebouw_key FROM fac_v_my_buildings"
      + " WHERE prs_perslid_key = " + (search_person ? person_key : user_key)
      + " AND niveau = " + autlevel + ")";

if (terrein)
  sql += " UNION "
       + " SELECT t.alg_terreinsector_key alg_key"
       + "      , " + S("alg_ter_string") + filtClauseGebouw.code + " code"
       + "      , " + lcl.xsql('st.alg_srtterreinsec_omschrijving', 'st.alg_srtterreinsector_key') + " naam"
       + "      , 'T' extra"
       + "      , 3 niveau"
       + " FROM alg_v_aanwezigterreinsector t"
       + "    , alg_locatie l"
       + "    , alg_srtterreinsector st"
       + filtClauseGebouw.from
       + " WHERE (UPPER(" + S("alg_ter_string") + ") LIKE " + safe.quoted_sql_wild2(chars)
       + "     OR UPPER(" + lcl.xsql('st.alg_srtterreinsec_omschrijving', 'st.alg_srtterreinsector_key') + ") LIKE " + safe.quoted_sql_wild2(chars) + ")"
       + "   AND (l.alg_locatie_vervaldatum IS NULL OR l.alg_locatie_vervaldatum > TRUNC(SYSDATE))"
       + "   AND (t.alg_terreinsector_vervaldatum IS NULL OR t.alg_terreinsector_vervaldatum > TRUNC(SYSDATE))"
       + "   AND l.alg_locatie_key = t.alg_locatie_key"
       + "   AND t.alg_srtterreinsector_key = st.alg_srtterreinsector_key(+)"
       + filtClauseGebouw.where_ter
       + filtClauseTerr;

sql += " ORDER BY 5, 2";

WriteResult2(sql, { bAll: bAll,
                    fldName: "code",
                    fldKey: "alg_key",
                    fldDesc: "naam",
                    fldExtraParam: "extra"
                   });
%>
<% ASPPAGE_END(); %>
