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

    File:               SuggestWerkplek.asp
    Description:        Suggereer werkplekken
    Parameters:         k             De eerste paar letters
                        n             De key van de te tonen werkplek
                        filtcode beperk de lijst met getoonde werkplekken

                        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 lockey = getQParamInt("lockey", -1);
var gebkey = getQParamInt("gebkey", -1);
var verkey = getQParamInt("verkey", -1);
var ruikey = getQParamInt("ruikey", -1);
var filtcode = getQParam("filtcode", "");
var showvirtueel = getQParamInt("showvirtueel", 0) == 1;

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

var filtClause = getFiltClauseWerkplek(filtcode);

if (ruikey > 0)
{ // Ruimte (parent) gegeven
  filtClause += " AND prs_alg_ruimte_key = " + ruikey;
}
else if (verkey > 0)
{ // Verdieping gegeven
  filtClause += " AND alg_ruimte_key IN"
                + " (SELECT r.alg_ruimte_key"
                + " FROM alg_v_aanwezigruimte r"
                + " WHERE alg_verdieping_key = " + verkey + ")";
}
else if (gebkey > 0)
{ // Gebouw gegeven
  filtClause += " AND prs_alg_ruimte_key IN"
                + " (SELECT r.alg_ruimte_key"
                + " FROM alg_v_aanwezigruimte r"
                + ", alg_v_aanwezigverdieping v"
                + " WHERE r.alg_verdieping_key = v.alg_verdieping_key"
                + " AND v.alg_gebouw_key = " + gebkey + ")";
}
else if (lockey > 0)
{ // Locatie gegeven
  filtClause += " AND prs_alg_ruimte_key IN"
                + " (SELECT r.alg_ruimte_key"
                + " FROM alg_v_aanwezigruimte r"
                + ", alg_v_aanwezigverdieping v"
                + ", alg_v_aanweziggebouw g"
                + " WHERE r.alg_verdieping_key = v.alg_verdieping_key"
                + " AND v.alg_gebouw_key = g.alg_gebouw_key"
                + " AND g.alg_locatie_key = " + lockey + ")";
}
else if (diskey > 0)
{ // District gegeven
  filtClause += " AND prs_alg_ruimte_key IN"
                + " (SELECT r.alg_ruimte_key"
                + " FROM alg_v_aanwezigruimte r"
                + ", alg_v_aanwezigverdieping v"
                + ", alg_v_aanweziggebouw g"
                + ", alg_v_aanweziglocatie l"
                + " WHERE r.alg_verdieping_key = v.alg_verdieping_key"
                + " AND v.alg_gebouw_key = g.alg_gebouw_key"
                + " AND g.alg_locatie_key = l.alg_locatie_key"
                + " AND l.alg_district_key = " + diskey + ")";
}
else if (regkey > 0)
{ // Regio gegeven
  filtClause += " AND prs_alg_ruimte_key IN"
                + " (SELECT r.alg_ruimte_key"
                + " FROM alg_v_aanwezigruimte r"
                + ", alg_v_aanwezigverdieping v"
                + ", alg_v_aanweziggebouw g"
                + ", alg_v_aanweziglocatie l"
                + ", alg_v_aanwezigdistrict d"
                + " WHERE r.alg_verdieping_key = v.alg_verdieping_key"
                + " AND v.alg_gebouw_key = g.alg_gebouw_key"
                + " AND g.alg_locatie_key = l.alg_locatie_key"
                + " AND l.alg_district_key = d.alg_district_key"
                + " AND d.alg_regio_key = " + regkey + ")";
}

if (autlevel > -1) // Efficiente versie van FAC_V_MY_PR**S_AFDELINGEN_WRITE
{
//      filtClause +=" AND alg_locatie_key IN "
//                 + "(SELECT alg_locatie_key FROM fac_v_my_locations "
//                 + " WHERE prs_perslid_key = " + user_key
//                 + " AND niveau = " + autlevel + ")";
}

sql = " SELECT DISTINCT prs_werkplek_key"
    + ", prs_werkplek_volgnr"
      + " || ' (' ||"
      + " COALESCE((SELECT SUM (prs_perslidwerkplek_bezetting)"
        + " FROM prs_v_perslidwerkplek_gegevens w2"
        + " WHERE w2.prs_werkplek_key = w1.prs_werkplek_key"
        + " AND prs_perslidwerkplek_key IS NOT NULL), 0)"
      + " || ') ' ||"
      + " COALESCE((SELECT MAX(prs_perslid_naam_full)"
        + " FROM prs_v_perslid_fullnames pf, prs_v_perslidwerkplek_gegevens wpg "
        + " WHERE wpg.prs_perslid_key = pf.prs_perslid_key "
        + " AND wpg.prs_werkplek_key = w1.prs_werkplek_key),'') || "
        + " CASE (SELECT count(prs_perslid_key) "
          + " FROM prs_v_perslidwerkplek_gegevens wpg "
          + " WHERE wpg.prs_werkplek_key = w1.prs_werkplek_key) "
        + " WHEN 1"
        + " THEN ''"
        + " ELSE '...'"
        + " END naam"
    + ", prs_werkplek_volgnr"
    + " FROM prs_v_perslidwerkplek_gegevens w1"
    + " WHERE prs_werkplek_volgnr LIKE " + safe.quoted_sql_wild2(chars)
    + filtClause
    + " ORDER BY 2"


if (ruikey == -1)   // we weten niet in welke ruimte we zitten, dus moeten we meer info tonen
  sql= "  SELECT   w1.prs_werkplek_key, "
     + "           MIN (prs_werkplek_volgnr) prs_werkplek_volgnr, "
     + " MIN(g.alg_gebouw_code || '-' || v.alg_verdieping_code || '-' || r.alg_ruimte_nr)||'-'||"
     + "           MIN (prs_werkplek_omschrijving) ||' ['|| "
     + "           MIN (wb.prs_werkplek_bezetting)||'%]' naam, "
     + "           COALESCE ( "
     + "              (SELECT   MIN (prs_perslid_naam_full) "
     + "                 FROM   prs_v_perslid_fullnames pf, prs_perslidwerkplek wpg "
     + "                WHERE       wpg.prs_perslid_key = pf.prs_perslid_key "
     + "                        AND wpg.prs_werkplek_key = w1.prs_werkplek_key), "
     + "              '' "
     + "           ) "
     + "           || CASE "
     + "                 WHEN (SELECT   COUNT (prs_perslid_key) "
     + "                         FROM   prs_perslidwerkplek wpg "
     + "                        WHERE   wpg.prs_werkplek_key = w1.prs_werkplek_key) > "
     + "                         1 "
     + "                 THEN "
     + "                    '...' "
     + "                 ELSE "
     + "                    '' "
     + "              END "
     + "              code "
     + "    FROM   prs_werkplek w1, prs_v_werkplek_bezetting wb, "
     + "           alg_v_aanwezigruimte r, alg_verdieping v, alg_gebouw g"
     + "   WHERE   g.alg_gebouw_code || '-' || v.alg_verdieping_code || '-' || r.alg_ruimte_nr||'-'||"
     + "           prs_werkplek_omschrijving LIKE " + safe.quoted_sql_wild2(chars)
     + "   AND w1.prs_alg_ruimte_key = r.alg_ruimte_key"
     + "   AND r.alg_verdieping_key = v.alg_verdieping_key"
     + "   AND v.alg_gebouw_key = g.alg_gebouw_key"
     + filtClause
     + "     AND wb.prs_werkplek_key = w1.prs_werkplek_key "
     + " GROUP BY   w1.prs_werkplek_key "
     + " ORDER BY   naam";
//     + "ORDER BY   g.alg_gebouw_code || '-' || v.alg_verdieping_code || '-' || r.alg_ruimte_nr||'-'||prs_werkplek_volgnr "
else    // we weten veel context, we zitten binnen een bekende ruimte
  sql= "  SELECT   w1.prs_werkplek_key, "
     + "           MIN (prs_werkplek_volgnr) prs_werkplek_volgnr, "
     + "           MIN (prs_werkplek_omschrijving) ||' ['"
     + "         || CASE WHEN w1.prs_werkplek_virtueel = 1 THEN " + safe.qL("lcl_estate_wp_virt") + " ELSE MIN (wb.prs_werkplek_bezetting)||'%' END" // Laat onderscheid zien bij virtueel
     //+ "           MIN (wb.prs_werkplek_bezetting)||'%"
     + "           || ']' naam, "
     + "           COALESCE ( "
     + "              (SELECT   MIN (prs_perslid_naam_full) "
     + "                 FROM   prs_v_perslid_fullnames pf, prs_perslidwerkplek wpg "
     + "                WHERE       wpg.prs_perslid_key = pf.prs_perslid_key "
     + "                        AND wpg.prs_werkplek_key = w1.prs_werkplek_key), "
     + "              '' "
     + "           ) "
     + "           || CASE "
     + "                 WHEN (SELECT   COUNT (prs_perslid_key) "
     + "                         FROM   prs_perslidwerkplek wpg "
     + "                        WHERE   wpg.prs_werkplek_key = w1.prs_werkplek_key) > "
     + "                         1 "
     + "                 THEN "
     + "                    '...' "
     + "                 ELSE "
     + "                    '' "
     + "              END "
     + "              code"
     + "    FROM   prs_werkplek w1, prs_v_werkplek_bezetting wb "
     + "   WHERE   prs_werkplek_volgnr LIKE " + safe.quoted_sql_wild2(chars)
     + filtClause
     + "     AND wb.prs_werkplek_key = w1.prs_werkplek_key "
     + (!showvirtueel
       ? "   AND w1.prs_werkplek_virtueel = 0"
       : "")
     + " GROUP BY   w1.prs_werkplek_key , w1.prs_werkplek_virtueel"
     + " ORDER BY   prs_werkplek_volgnr";

    WriteResult(sql, bAll, "naam", "prs_werkplek_key", "code");
%>
<% ASPPAGE_END(); %> 
