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

    File:               SuggestLocatie.asp
    Description:        Suggereer locaties
    Parameters:         k             De eerste paar letters
                        filtcode    : beperk de lijst met getoonde locaties
                        diskey      : optioneel de districtkey waarbinnen de locaties
                        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" -->

<%
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 filtcode = getQParam("filtcode", "");
var extracode = getQParam("extraCode", "");
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 = { act_key: act_key, stdm_key: stdm_key, scope: scope };

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

var filtClause = getFiltClauseLocatie(filtcode, params);

if (diskey > 0)
{ // District (parent) gegeven
  filtClause.where += " AND alg_district_key = " + diskey;
}
else if (regkey > 0)
{ // Regio gegeven
  filtClause.where += " AND alg_district_key IN"
                    + " (SELECT alg_district_key"
                    + "    FROM alg_v_aanwezigdistrict"
                    + "   WHERE alg_regio_key = " + regkey + ")";
}

if (autlevel > -1)
{
   if (autlevel == 0)
   {
     filtClause.where += " AND l.alg_locatie_key IN"
                       + " (SELECT alg_locatie_key"
                       + "    FROM alg_locatie l, alg_district d, alg_v_my_region ro "
                       + "   WHERE l.alg_district_key = d.alg_district_key "
                       + "     AND ro.alg_regio_key = d.alg_regio_key  "
                       + "     AND prs_perslid_key = " + (search_person ? person_key : user_key) + ")";
   }
   else
   {
     filtClause.where += " AND l.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 = l.alg_locatie_key)"; // redundant voor de perfomance
   }
}

sql = "SELECT l.alg_locatie_key"
    + ", " + S("alg_loc_string") + " naam"
    + ",      alg_locatie_plaats plaats"
    + ",      alg_locatie_postcode postcode"
    + "  FROM alg_v_aanweziglocatie l"
    + filtClause.from
    + " WHERE (UPPER(" + S("alg_loc_string") + ") LIKE " + safe.quoted_sql_wild2(chars)
    //+ "       OR UPPER(alg_locatie_code) LIKE " + safe.quoted_sql_wild2(chars) // die zit als relevant ook wel in de string
    + "       OR UPPER(alg_locatie_postcode) LIKE " + safe.quoted_sql_wild2(chars)
    + "       OR UPPER(alg_locatie_plaats) LIKE " + safe.quoted_sql_wild2(chars) + ")"
    + "   AND (l.alg_locatie_vervaldatum IS NULL OR l.alg_locatie_vervaldatum > TRUNC(SYSDATE))"
    + filtClause.where
    + " ORDER BY l.alg_locatie_volgnr, 2";

WriteResult2(sql, { bAll: bAll,
                    fldName: "naam",
                    fldKey: "alg_locatie_key",
                    fldDesc: "plaats",
                    fldExtraParam: "postcode"
                  });
%>
<% ASPPAGE_END(); %>
