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

    File:           prs_show_perslid.asp
    Description:
    Parameters:     prs_key (required)

    Context:
    Note:

*/ %>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="prs_flexkenmerk.inc" -->
<!-- #include file="prs.inc" -->
<%
FCLTHeader.Requires({plugins:["jQuery"]})

var prs_key  = getQParamInt("prs_key");

// TODO: eigenlijk de ruimste van deze autorisaties nemen
var prsauthparams = prs.checkAutorisation(prs_key);
user.auth_required_or_abort(prsauthparams.readman || prsauthparams.readuse);

var sql = "SELECT p.prs_perslid_key, "
        + "       p.prs_srtperslid_key, "
        +         lcl.xsqla("sp.prs_srtperslid_omschrijving", "sp.prs_srtperslid_key") + ", "
        + "       p.prs_afdeling_key, "
        + "       prs_perslid_naam, "
        + "       prs_perslid_geslacht, "
        + "       prs_perslid_voorletters, "
        + "       prs_perslid_voornaam, "
        + "       prs_perslid_titel, "
        + "       prs_perslid_initialen, "
        + "       prs_perslid_nr, "
        + "       prs_perslid_dienstverband, "
        + "       COALESCE(prs_perslid_opp, sp.prs_srtperslid_opp) prs_perslid_opp, "
        + "       a.mld_adres_naam, "
        + "       prs_perslid_uurloon, "
        + "       prs_perslid_telefoonnr, "
        + "       prs_perslid_lang, "
        + "       prs_perslid_oslogin, "
        + "       prs_perslid_oslogin2, "
        + "       prs_perslid_apikey, "
        + "       prs_perslid_ingangsdatum, "
        + "       prs_perslid_einddatum, "
        + "       d.prs_bedrijf_key, "
        + "       prs_perslid_tussenvoegsel, "
        + "       prs_perslid_email, "
        + "       prs_perslid_mobiel, "
        + "       k.prs_kostenplaats_nr, "
        +         S("prs_kpn_string") + " kpl, "
        +         S("prs_dep_string") + " afdeling, "
        + "       fac_profiel_limiet, "
        + "       fac_profiel_omschrijving,"
        + "       p.fac_profiel_key,"
        + "       prs_perslid_ingids,"
        + "       (SELECT COUNT(pk.prs_perslidkostenplaats_key)"
        + "          FROM prs_perslidkostenplaats pk"
        + "         WHERE pk.prs_perslid_key = p.prs_perslid_key) aantal_pk,"
        + "       prs_perslid_systeemadres, "
        + "       d.prs_kostenplaats_key kpl_key "
        + "  FROM prs_perslid p, "
        + "       prs_v_afdeling d, "
        + "       prs_srtperslid sp, "
        + "       fac_profiel f, "
        + "       mld_adres a, "
        + "       prs_kostenplaats k "
        + " WHERE p.prs_perslid_key = " + prs_key
        + "   AND p.prs_afdeling_key = d.prs_afdeling_key "
        + "   AND p.prs_srtperslid_key = sp.prs_srtperslid_key "
        + "   AND p.fac_profiel_key = f.fac_profiel_key(+)"
        + "   AND p.mld_adres_key = a.mld_adres_key(+)"
        + "   AND k.prs_kostenplaats_key(+) = d.prs_kostenplaats_key ";
var oRs = Oracle.Execute(sql);

var prs_srtkey = oRs("prs_srtperslid_key").value;
var prs_srtoms = oRs("prs_srtperslid_omschrijving").value;
var prs_afdkey = oRs("prs_afdeling_key").value;
var prs_naam   = oRs("prs_perslid_naam").value;
var prs_geslacht = oRs("prs_perslid_geslacht").value;
var prs_vltr   = oRs("prs_perslid_voorletters").value;
var prs_vrnaam = oRs("prs_perslid_voornaam").value;
var prs_titel  = oRs("prs_perslid_titel").value;
var prs_ini    = oRs("prs_perslid_initialen").value;
var prs_nr     = oRs("prs_perslid_nr").value;
var prs_dnst   = oRs("prs_perslid_dienstverband").value;
var prs_opp    = oRs("prs_perslid_opp").value;
var mld_adres  = oRs("mld_adres_naam").value;
var prs_uurln  = oRs("prs_perslid_uurloon").value;
var prs_telnr  = oRs("prs_perslid_telefoonnr").value;
var prs_lang   = oRs("prs_perslid_lang").value;
var prs_oslog  = oRs("prs_perslid_oslogin").value;
var prs_oslog2 = oRs("prs_perslid_oslogin2").value;
var prs_apikey = oRs("prs_perslid_apikey").value;
var prs_ingdat = new Date(oRs("prs_perslid_ingangsdatum").value);
var prs_enddat = oRs("prs_perslid_einddatum").value!=null?new Date(oRs("prs_perslid_einddatum").value):null;
var prs_bdrkey = oRs("prs_bedrijf_key").value;
var prs_tussen = oRs("prs_perslid_tussenvoegsel").value;
var prs_email  = oRs("prs_perslid_email").value;
var kpl_nr     = oRs("prs_kostenplaats_nr").value;
var kpl        = oRs("kpl").value;
var kpl_key    = oRs("kpl_key").value;
var afdeling   = oRs("afdeling").value;
var prs_mobiel = oRs("prs_perslid_mobiel").value;
var prs_bdrkey = oRs("prs_bedrijf_key").value;
var fac_prolim = oRs("fac_profiel_limiet").value;
var fac_proms  = oRs("fac_profiel_omschrijving").value;
var fac_prokey = oRs("fac_profiel_key").value;
var ingids     = oRs("prs_perslid_ingids").value == 1;
var aantal_pk  = oRs("aantal_pk").value;
var systeemadres = oRs("prs_perslid_systeemadres").value;  /* show-only */
oRs.Close();

var thisPrs = prs.prs_perslid(prs_key, { withWL: true, withPhoto: true }); // geeft ook alle informatie
var prs_user = new Perslid(prs_key);
%>
<html>
  <head>
<%  FCLTHeader.Generate();
    var xfunc = user.func_enabled("*", null, null, prs_key);
%>
    <script  type="text/javascript" >
      function prs_change()
      {
          if (FcltMgr.startEdit(window))
              window.location.href = "prs_edit_perslid.asp?prs_key=<%=prs_key%>"
      }
      function prs_delete()
      {
          if (confirm(L("lcl_prs_del_txt_perslid")))
          {
            var data = { key: <%=prs_key%>
                       , level: "P"
                       };
            <% protectRequest.dataToken("data"); %>
            $.post("prs_delete.asp",
                   data,
                   FcltCallbackClose,
                   "json");
          }
      }
      function prs_substitute()
      {
          var url = "../prs/prs_perslid_substitutes.asp?pkey=<%=prs_key%>";
          FcltMgr.openModalDetail(url, "<%=L("lcl_prs_frame_substitutes")%>");
      }
      function prs_changepwd()
      {
          var url = "../prs/pchange.asp?prs_key=<%=prs_key%>";
          FcltMgr.openModalDetail(url, "<%=L("lcl_password_link")%>");
      }

      function prs_approve()
      {
          if (confirm(L("lcl_prs_approve")))
          {
            var data = { prs_key: <%=prs_key%> };
            protectRequest.dataToken(data);
            $.post("prs_approve.asp",
                   data,
                   FcltCallbackRefresh,
                   "json");
           }
      }

      function prs_gotoautgroup()
      {
          var url = "appl/facmgtVB/fac_persoongroeprecord.asp?prs_perslid_key=<%=prs_key%>";
          FcltMgr.openDetail(url, "<%=L("lcl_mgt_aut_group")%>");
      }
      function impersCallback(json, textStatus)
      {
          window.top.location.href = "<%=rooturl%>/";
      };
      function prs_impersonate()
      {
          if (confirm("<%=safe.jsstring(L("lcl_impersonate_start").format(prs_vrnaam + " " + prs_naam))%>"))
          {
              var data = { prs_key: <%=prs_key%> };
              <% protectRequest.dataToken("data"); %>
              $.post("prs_impersonate.asp",
                     data,
                     FcltCallbackAndThen(impersCallback),
                     "json");
          }
      }
    </script>
  </head>

  <body id="showbody">
<%

    var buttons = [];
    if (prsauthparams.writeman || prsauthparams.writeuse) {
      buttons =  [ {title: L("lcl_change"), action:"prs_change()", icon: "wijzigen.png" } ];
    }

    if (prsauthparams.writeman) {
      buttons.push( {title: L("lcl_delete"), action:"prs_delete()", icon: "delete.png" });
    }
    // Bepaalde functies zijn alleen zinvol voor personen die users zijn
    if (prs_oslog)
    {
      if (S("prs_collegas_used") < 9 && (prsauthparams.writeman || prsauthparams.writeuse)) {
        buttons.push( {title: L("lcl_prs_frame_substitutes"), icon: "group.png", action: "prs_substitute()"});
      }

      if (prsauthparams.writesys || xfunc.canWrite("WEB_FACMGT"))
      {
        if (prs_user.isUnapprovedUser())
        buttons.push( { title: L("lcl_prs_approve"), icon: "accept.png", action: "prs_approve()" });
        buttons.push( { title: L("lcl_password_title"), icon: "wall_brick.png", action: "prs_changepwd()" });
      }

      if (prsauthparams.writesys || xfunc.canWrite("WEB_PRSMSU")) {
        buttons.push( { title: L("lcl_menu_fac_autorisaties"), icon: "autorisatie.png", action: "prs_gotoautgroup()" });
      }

      var canImpers = prsauthparams.writetab || (S("prs_allow_impersonate") && prsauthparams.writesys)
      if (canImpers && prs_key != user_key && typeof Session("org_user_key") == "undefined") {
        buttons.push({ title: L("lcl_impersonate"), icon: "impersonate.png", action: "prs_impersonate()", id: "bimpers" });
      }
    }
    IFRAMER_HEADER(L("lcl_prs_persoon_frame"), buttons);
%>
    <div id="show">
      <form name=u2>
<%      BLOCK_START("prsPerslid", L("lcl_prs_basisblok"));

%>      <tr valign="center"><td colspan="2"></td><td rowspan="20">
            <img id='photo' src="<%=thisPrs.photopaththumb %>">
        </td></tr>
<%      var params = { infoPointer: { Url: "appl/shared/status_info.asp?prs_key=" + prs_key,
                          Title: L("lcl_status_details") + " " + thisPrs.naam }};
        ROFIELDTR("fld", L("lcl_prs_person_name"), prs_naam, params);
        ROFIELDTR("fld", L("lcl_prs_person_geslacht"), { 0: L("lcl_prs_person_geslachtV"), 1: L("lcl_prs_person_geslachtM") }[prs_geslacht] , {suppressEmpty: true});
        ROFIELDTR("fld", L("lcl_prs_person_tussen"), prs_tussen, {suppressEmpty: true});
        ROFIELDTR("fld", L("lcl_prs_person_voorletters"), prs_vltr, {suppressEmpty: true});
        ROFIELDTR("fld", L("lcl_prs_person_voornaam"), prs_vrnaam, {suppressEmpty: true});
        ROFIELDTR("fld", L("lcl_prs_person_title"), prs_titel, {suppressEmpty: true});
        ROFIELDTR("fld", L("lcl_prs_person_initials"), prs_ini, {suppressEmpty: true});
        ROFIELDTR("fld", L("lcl_lcl_taal"), prs_lang, {suppressEmpty: true});
        ROFIELDTR("fld", L("lcl_prs_person_phone"), prs_telnr, {suppressEmpty: true});
        ROFIELDTR("fld", L("lcl_prs_person_mobile"), prs_mobiel, {suppressEmpty: true});
        AFIELDTR('fldmailto details', L("lcl_prs_person_email"), "mailto:" + prs_email, prs_email, { suppressEmpty: true });
        if (prsauthparams.writesys || xfunc.canWrite("WEB_FACMGT"))
        {
          ROFIELDTR("fld", "System URL", systeemadres, {suppressEmpty: true});
          ROFIELDTR("fld", L("lcl_prs_person_login"), prs_oslog, {suppressEmpty: true});
          ROFIELDTR("fld", L("lcl_prs_person_login2"), prs_oslog2, {suppressEmpty: true});
          if (prs_apikey)
          {
              var showkey = prs_apikey;
              if (!prsauthparams.writetab)
                  var showkey = showkey.substr(0,3) + "..." + showkey.substr(showkey.length-3);
              ROFIELDTR("fld", L("lcl_prs_apiuser"), showkey, {suppressEmpty: true});
          }
        }

        var wl = thisPrs.werklocatie.alg_locatie_key? thisPrs.werklocatie.aanduiding : "";
        ROFIELDTR('fld', L("lcl_prs_work_location"), wl, {suppressEmpty: true});

        if (prsauthparams.writesys || xfunc.canWrite("WEB_FACMGT")) // Die kunnen ook approve'n
        {
            var flagtxt = [];
            if (prs_user.isSystemUser())     flagtxt.push(L("lcl_prs_flags_system"));
            if (prs_user.isUnapprovedUser()) flagtxt.push(L("lcl_prs_flags_unapproved"));
            if (prs_user.isGroupedUser())    flagtxt.push(L("lcl_prs_flags_grouped"));
            if (prs_user.isBlockedUser())    flagtxt.push(L("lcl_prs_flags_blocked"));
            if (flagtxt.length)
                ROFIELDTR("fld", L("lcl_prs_flags"), flagtxt.join("\n"));
        }
        BLOCK_END();

        BLOCK_START("prsPerslid2", L("lcl_prs_organisatieblok"));
        if (prsauthparams.writesys)
          CHECKBOXTR(L("lcl_prs_in_guide"), "fldnotingids", "not_ingids", ingids, { html: " disabled" });
        ROFIELDTR("fld", L("lcl_prs_person_nr"), prs_nr, {suppressEmpty: true});
        var params_afd = { infoPointer: { Url: "appl/prs/prs_afdeling.asp?key=" + prs_afdkey
                                        }
                         };
        ROFIELDTR("fld", L("lcl_prs_organisatie"), afdeling, params_afd);
        ROFIELDTR("fld", L("lcl_prs_person_function"), prs_srtoms);
        ROFIELDTR("fld", L("lcl_prs_person_opp"), prs_opp, {suppressEmpty: true});
        ROFIELDTR("fld", L("lcl_prs_person_dienstverband_perc"), prs_dnst, {suppressEmpty: prs_dnst!==0});
        if (S("prs_perslid_datums") == 1)
        {
          ROFIELDTR("fld", L("lcl_prs_person_ingangsdatum"), toDateString(prs_ingdat), {suppressEmpty: true});
          ROFIELDTR("fld", L("lcl_prs_person_einddatum"), toDateString(prs_enddat), {suppressEmpty: true});
        }

        if (kpl_key > 0)
        {
            var params_kpl = { suppressEmpty: true
                             , infoPointer: { Url: "appl/prs/kpn_kpn.asp?key=" + kpl_key
                                            }
                             };
            ROFIELDTR("fld", L("lcl_account"), kpl, params_kpl);
        }

        if (fac_prokey)
        { // Er is een profiel
          if (fac_prolim)
            ROFIELDTR("fld", L("lcl_prs_profile"), fac_proms + " (" + S("currency_pref") + ' ' + safe.curr(fac_prolim) + ")", {suppressEmpty: true});
          else
          { // Zijn er profielwaarden?
            sql = "SELECT COUNT(fac_profiel_key) aantalpw"
                + " FROM fac_profielwaarde"
                + " WHERE fac_profiel_key = " + fac_prokey;
            oRs = Oracle.Execute(sql);
            var aantalpw = oRs("aantalpw").value;

            if (aantalpw > 0)
              ROFIELDTR("fld",
                        L("lcl_prs_profile"),
                        fac_proms,
                        {infoPointer: {Url: "appl/prs/profielwaarden_info.asp?fac_prokey=" + fac_prokey + "&autosearch=1", Title: L("lcl_menu_fin_mandatering")},
                         suppressEmpty: true });
          }
        }
        if (aantal_pk > 0)
        {
          sql = "SELECT pk.prs_perslidkostenplaats_key"
              + ", k.prs_kostenplaats_key"
              + ", k.prs_kostenplaats_nr || ' ' || k.prs_kostenplaats_omschrijving kostenplaats"
              + " FROM prs_perslidkostenplaats pk"
              + ", prs_kostenplaats k"
              + " WHERE k.prs_kostenplaats_key(+) = pk.prs_kostenplaats_key"
              + " AND pk.prs_perslid_key = " + prs_key
              + " ORDER BY kostenplaats"
          oRs = Oracle.Execute(sql);

          if (aantal_pk == 1)
            ROFIELDTR("fld", L("lcl_prs_mandate_for"), (oRs("prs_kostenplaats_key").value? oRs("kostenplaats").value : L("lcl_prs_all_accounts")), {suppressEmpty: true});
          else
            ROFIELDTR("fld", L("lcl_prs_mandate_for"), oRs("kostenplaats").value + "...",
                             {infoPointer: {Url: "appl/prs/kpn_mandate_search.asp?prs_key=" + prs_key + "&autosearch=1", Title: L("lcl_menu_fin_mandatering")},
                              suppressEmpty: true });
        }
        ROFIELDTR("fld", L("lcl_delivery_address"), mld_adres, {suppressEmpty: true});
        ROFIELDTR("fld", L("lcl_prs_person_uurloon"), prs_uurln, {suppressEmpty: true});

        // Is persoon een contactpersoon, en zo ja van welk bedrijf.
        sql = "SELECT b.prs_bedrijf_key"
            + "     , b.prs_bedrijf_naam"
            + "  FROM prs_contactpersoon p"
            + "     , prs_bedrijf b"
            + " WHERE p.prs_bedrijf_key = b.prs_bedrijf_key"
            + "   AND prs_contactpersoon_verwijder IS NULL"
            + "   AND prs_perslid_key = " + prs_key;
        oRs = Oracle.Execute(sql);
        while (!oRs.eof)
        {
          var cnt_key = oRs("prs_bedrijf_key").value;
          var cnt_str = oRs("prs_bedrijf_naam").value;
          var params_cnt = { suppressEmpty: true
                           , infoPointer: { Url: "appl/prs/prs_bedrijf.asp?bedrijf_key=" + cnt_key
                                          }
                           };
          ROFIELDTR("fld", L("lcl_prs_perslid_contactpersoon"), cnt_str, params_cnt);
          oRs.MoveNext();
        }

        BLOCK_END();

        // Kan de gebruiker uberhaubt *iets* overrulen? Zo nee dan ook geen vinkjes tonen
        sql = "SELECT count(*) FROM fac_srtnotificatie"
            + " WHERE fac_srtnotificatie_mode IS NOT NULL"
            + "   AND fac_srtnotificatie_mode > 0"
            + "   AND fac_srtnotificatie_usermode IS NOT NULL"
            + "   AND fac_srtnotificatie_usermode > 0"
        oRs = Oracle.Execute(sql);
        var anyOverrule = oRs(0).Value > 0;
        oRs.Close();

        if ((prs_key == user_key || prsauthparams.writesys || prsauthparams.writeman) && anyOverrule)
        {
          BLOCK_START("prsNoti", L("lcl_noti_prsvoorkeur"));
          if (prs_user.noti_niks() == 1 || prs_user.noti_portal() == 1 || prs_user.noti_email() == 1 || prs_user.noti_sms() == 1 || prs_user.noti_popup() == 1)
          {
            CHECKBOXTR(L("lcl_noti_niks"), "fldfacnoti", "noti0", prs_user.noti_niks(), { html: " disabled" });
            CHECKBOXTR(L("lcl_noti_portal"), "fldfacnoti", "noti1", prs_user.noti_portal(), { html: " disabled" });
            CHECKBOXTR(L("lcl_noti_email"), "fldfacnoti", "noti2", prs_user.noti_email(), { html: " disabled" });
            CHECKBOXTR(L("lcl_noti_sms"), "fldfacnoti", "noti4", prs_user.noti_sms(), { html: " disabled" });
            CHECKBOXTR(L("lcl_noti_popup"), "fldfacnoti", "noti8", prs_user.noti_popup(), { html: " disabled" });
          }
          else
          {%>
            <tr id="noNotipref">
              <td class="label" colspan="2">
                <label><span><%=L("lcl_no_noti_prsvoorkeur")%></span></label>
              </td>
            </tr>
          <%}
          BLOCK_END();
        }

        BLOCK_START("prsFlex"+(S("prs_flexcolumns")!=1?"2":""), L("lcl_prs_flexblok"));
          generateFlexKenmerkCode ({
             link_key     : prs_key,
             srt_key      : prs_srtkey,
             niveau       : "P",
             reado        : true,
             flexcolumns  : S("prs_flexcolumns"),
             prsauthparams: prsauthparams
        });

        BLOCK_END();
%>
      </form>
    </div>
  </body>
</html>
