<% /*
     $Revision$
     $Id$

    File:               prs_perslid_list.inc
    Description:        Show an overview of persons in list-form,
                        which meets with the given requirements
    Parameters:
    Context:            Search action from prs_perslid_search.asp form
    Note:               - Check de sopes nog eens goed?
                        - De kolom WP is soms leeg terwijl er wel een WP is. Virtueel of zo? TODO
*/ %>

<!-- #include file="../Shared/json2.js" -->
<!-- #include file="../Shared/resultset_table_v2.inc" -->
<!-- #include file="../Shared/getkenmerksql.inc" -->

<%
FCLTHeader.Requires({ plugins:["jQuery"] })

function fnrowData(oRs)
{
  var key = oRs("prs_perslid_key").value;
  var data = {key: key};
  return JSON.stringify(data);
}
%>
<script type="text/javascript">
  var autosearch = 1; // automatisch laden van resultaat op search pagina

  function persoonEdit(row)
  {
    var algData = eval('(' + row.getAttribute("ROWDATA") + ')');
    var url = "appl/prs/prs_perslid.asp?mode=viewUpdate&key="+algData.key;
    FcltMgr.openDetail(url, "<%=L("lcl_person")%>");
  }

  function doDelete(rowArray, isMulti)
  {
    FcltMgr.confirm(L("lcl_alg_del_txt_ruimte"), { autoconfirm: isMulti }, function() {
        var prsKeyString = getKeyString(rowArray);
        var data = {
            key: prsKeyString,
            level: "P"
        };
        <% protectRequest.dataToken("data"); %>
        $.post("prs_delete.asp",
            data,
            FcltCallbackRefresh,
            "json");
    });
  }
</script>
<%

function perslid_list(pautfunction, params)
{
  // De functie checkAutorisation(autfunction) geeft een Response.End indien er geen autorisatie voor autfunction wordt gevonden
  var addButton = [];

  var autfunction = "WEB_PRSUSE";
  var authparams = user.checkAutorisation(autfunction);

  var authparamsMAN = user.checkAutorisation("WEB_PRSMAN", true);
  var canAdd = (authparamsMAN && authparamsMAN.ALGwritelevel < 9 && authparamsMAN.PRSwritelevel < 9);

  // Voor resulttable de globalen zetten; zou ng anders moeten.
  var outputmode = params.outputmode; // print to 0 = screen, 1 = printer, 2 = excel, etc.
  var showall = params.showall;

  var dis_key = params.dis_key;
  var loc_key = params.loc_key;
  var bld_key = params.bld_key;

  var prsName = params.prsName;
  var prsNR =  params.prsNR;
  var prsLogin = params.prsLogin;
  var profiel_key = params.profiel_key;
  var func = params.func;
  var dept = params.dept;
  var bdr_key = params.bdr_key;
  var altloc = params.altloc;

  function fnrowActionEnabler(oRs)
  {
    /*  var eDelete = false;
      if (alg.canWriteRuimte(oRs("alg_ruimte_key").value, authparams.mALGwritelevel)) */
      eDelete = true;
      return ({eDelete: eDelete})
  }
%>

  <html>
    <head>
      <% FCLTHeader.Generate({outputmode: outputmode}) %>
    </head>
    <body id="listbody">
<%
    // Zijn er profielen aanwezig?
    sql = "SELECT count(*) aantal"
        + "  FROM fac_profiel";
    oRs = Oracle.Execute(sql);
    var hasProfiel = (parseInt(oRs('aantal').value) > 0);
    oRs.Close();

    // is distinct echt vereist? dan zeggen waarom: ik denk omdat afdeling_boom ervoor zorgt dat een persoon
    // meerdere keren in het resultaat komt, en de distinct dus nodig is.
      var sqln = "SELECT DISTINCT p.prs_perslid_key"
               + "     , pf.prs_perslid_naam_full"
               + "     , p.prs_perslid_nr"
               + "     , " + S("prs_dep_string") + " organisatie"
               + "     , COALESCE (p.prs_perslid_telefoonnr, ' ') telefoon"
               + "     , COALESCE (" + lcl.xsql("srt_p.prs_srtperslid_omschrijving", "srt_p.prs_srtperslid_key") + ", ' ') functie"
               + "     , p.prs_perslid_upper"
               + "     , wp.alg_ruimte_aanduiding"
                 + (altloc
                   ? ""
                   : " || DECODE (hwp.aantal,1,'','...') alg_ruimte_aanduiding")
               + "     , wp.prs_werkplek_aanduiding"
                 + (altloc
                   ? ""
                   : " || DECODE (hwp.aantal,1,'','...') prs_werkplek_aanduiding")
               + "     , alg_locatie_omschrijving"
               + "     , p.prs_perslid_mobiel"
               + "     , p.prs_perslid_email"
               + "     , p.prs_perslid_titel"
               + "     , fp.fac_profiel_omschrijving"
               + "  FROM prs_v_aanwezigperslid p"
               + "     , prs_v_afdeling_familie bm"
               + "     , prs_v_perslid_fullnames pf"
               + "     , prs_v_afdeling d"
               + "     , prs_srtperslid srt_p"
               + (altloc
                 ? "   , prs_perslidwerkplek hwp"
                 : "   , prs_v_hoofdperslidwerkplek hwp")
               + "     , prs_v_werkplek_gegevens wp"
               + "     , alg_locatie l"
               + "     , fac_profiel fp"
               + " WHERE p.prs_afdeling_key = bm.prs_afdeling_key"
               + "   AND d.prs_afdeling_key = p.prs_afdeling_key"
               + "   AND srt_p.prs_srtperslid_key = p.prs_srtperslid_key"
               + "   AND pf.prs_perslid_key = p.prs_perslid_key"
               + "   AND l.alg_locatie_key(+) = wp.alg_locatie_key"
               + "   AND wp.prs_werkplek_key(+) = hwp.prs_werkplek_key"
               + "   AND hwp.prs_perslid_key(+) = p.prs_perslid_key"
               + "   AND p.prs_perslid_verwijder IS NULL"
               + "   AND p.fac_profiel_key = fp.fac_profiel_key (+)"

      if (authparams.ALGreadlevel > -1)
      {  // Er is een scope-beperking van kracht
        if (loc_key)
        {
          sqln += " AND l.ALG_DISTRICT_KEY IN "
               +  " (SELECT alg_district_key FROM fac_v_my_districts "
               +   "WHERE prs_perslid_key="+user_key
               +    " AND niveau=" + authparams.ALGreadlevel + ")";
        }

        if (bld_key)
        {
          sqln += " AND l.ALG_LOCATIE_KEY IN "
               +  " (SELECT alg_locatie_key FROM fac_v_my_locations "
               +  " WHERE prs_perslid_key="+user_key
               +  " AND niveau=" + authparams.ALGreadlevel + ")";
        }

        sqln += " AND wp.ALG_GEBOUW_KEY IN "
             +  " (SELECT alg_gebouw_key FROM fac_v_my_buildings "
  	         +  " WHERE prs_perslid_key="+user_key
  	         +  " AND niveau=" + authparams.ALGreadlevel + ")";
      }

      if (authparams.PRSreadlevel > 0)
      {
        sqln += " AND d.prs_afdeling_key IN "
             +  "    (SELECT prs_afdeling_key FROM prs_v_afdeling_familie a"
             +  "      WHERE a.prs_afdeling_elder_key IN"
             +  "        (SELECT aa.prs_afdeling_elder_key"
             +  "           FROM prs_v_afdeling_familie aa"
             +  "          WHERE aa.prs_afdeling_key = " + user.prs_afdeling_key()
             +  "            AND aa.niveau = " + authparams.PRSreadlevel + "))";
      }

      if (authparams.PRSreadlevel == 0) // (PRSreadlevel == 0) ==> prs_bedrijf
      {
        sqln += " AND d.prs_afdeling_key IN "
             +  " (SELECT a.prs_afdeling_key FROM prs_v_afdeling a "
  	         +  " WHERE a.prs_bedrijf_key = " + user.afdeling().prs_bedrijf_key() + ")";
      }

      sqln += getKenmerkSql("PRS", "p.prs_perslid_key");

      if (bld_key)
      {
        sqln += " AND wp.alg_gebouw_key = " + bld_key;
      }
      else if (loc_key)
      {
        sqln += " AND l.alg_locatie_key = " + loc_key;
      }
      else if (dis_key)
      {
        sqln += " AND l.alg_district_key = " + dis_key;
      }

      if (prsName)
      {
        Oracle.Execute("alter session set nls_comp = 'LINGUISTIC' NLS_SORT = 'BINARY_AI'"); // Zodat müller gevonden wordt
        sqln += " AND p.prs_perslid_upper LIKE " + safe.quoted_sql_wild(prsName + "%");
      }

      if (prsLogin)
      {
        sqln += " AND (UPPER(p.prs_perslid_oslogin) LIKE " + safe.quoted_sql_wild(prsLogin + "%")
             + "   OR (UPPER(p.prs_perslid_oslogin2) LIKE " + safe.quoted_sql_wild(prsLogin + "%")
             + "       AND NOT EXISTS (SELECT p2.prs_perslid_oslogin "
             + "                         FROM prs_perslid p2 "
             + "                        WHERE p2.prs_perslid_oslogin = p.prs_perslid_oslogin2))) ";
      }

      if (func)
      {
        sqln += " AND p.prs_srtperslid_key = " + func;
      }

      if (prsNR)
      {
        sqln += " AND UPPER(p.PRS_PERSLID_NR) LIKE " + safe.quoted_sql_wild(prsNR + "%")
      }

      if (dept)
      {
        sqln += " AND bm.prs_afdeling_elder_key = " + dept;
      }

      if (bdr_key)
      {
        sqln += " AND d.prs_bedrijf_key = " + bdr_key;
      }

      if (profiel_key)
      {
        sqln += " AND fp.fac_profiel_key = " + profiel_key;
      }

      sqln += " ORDER BY pf.prs_perslid_naam_full"


      var addurl = "appl/prs/prs_perslid.asp";
      if (canAdd)
      {
        addButton = [{ icon: "plus.png", title: L("lcl_add"), action: "FcltMgr.openDetail('" + addurl + "', '" + L("lcl_add") + "')" }]
      }

      var rst = new ResultsetTable({sql: sqln,
                                     keyColumn: "prs_perslid_key",
                                     ID: "prstable",
                                     showAll: showall,
                                     rowData: fnrowData,
                                     rowActionEnabler: fnrowActionEnabler,
                                     outputmode: outputmode,
                                     title: L("lcl_search_pers"),
                                     flexModule: "PRS",
                                     flexId: "prs_perslid_key",
                                     filterParams: params,
                                     buttons: addButton
                                    });

      rst.addColumn(new Column({caption: L("lcl_prs_person_name"), content: "prs_perslid_naam_full"}));
      rst.addColumn(new Column({caption: L("lcl_prs_person_nr"), content: "prs_perslid_nr"}));
      rst.addColumn(new Column({caption: L("lcl_prs_organisatie"), content: "organisatie"}));
      rst.addColumn(new Column({caption: L("lcl_prs_person_function"), content: "functie"}));
      if (hasProfiel)
      {
        rst.addColumn(new Column({caption: L("lcl_prs_profile"), content: "fac_profiel_omschrijving", combine:true}));
      }
      if (S("prs_hide_fixed_fields") == 0) {
        rst.addColumn(new Column({caption: L("lcl_prs_person_phone"), content: "telefoon"}));
      }
      if (outputmode != 0)
        rst.addColumn(new Column({caption: L("lcl_location"), content: "alg_locatie_omschrijving"}));
      rst.addColumn(new Column({caption: ( S("prs_werkplek_implicit")==1 ? L("lcl_workplace") : L("lcl_workplace")),
                                    content: ( S("prs_werkplek_implicit")==1 ? "alg_ruimte_aanduiding": "prs_werkplek_aanduiding")}));
      rst.addColumn(new Column({caption: L("lcl_prs_person_mobile"), content: "prs_perslid_mobiel", purpose: PRINTING_ONLY}));
      rst.addColumn(new Column({caption: L("lcl_prs_person_email"), content: "prs_perslid_email", purpose: PRINTING_ONLY}));
      rst.addColumn(new Column({caption: L("lcl_prs_person_title"), content: "prs_perslid_titel", purpose: PRINTING_ONLY}));
      rst.addAction({ action: "persoonEdit", caption: L("lcl_edit"), isDefault: true});
      if (canAdd)
          rst.addAction({ action: "doDelete", caption: L("lcl_delete"), enabler: "eDelete", single: false, multi: true, multiOnce: true});

      var cnt = rst.processResultset();
%>
    </body>
  </html>
<%
}
%>

