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

     File:         prs_edit_perslid_save.asp
     Description:  SUBMIT-form
*/
var JSON_Result = true;
%>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../shared/save2db.inc" -->
<!-- #include file="../Shared/kenmerk_common.inc"-->
<!-- #include file="prs.inc"-->
<%

var prs_key = getQParamInt( "prs_key", -1 );
var isNew = (prs_key < 0);
var xfunc = user.func_enabled("*", null, null, prs_key>0?prs_key:null);

var prsauthparams = prs.checkAutorisation(prs_key);
user.auth_required_or_abort(prsauthparams.writeman || prsauthparams.writeuse || prsauthparams.writeself);

var prs_user = new Perslid(prs_key);
user.auth_required_or_abort(!prs_user.has("WEB_FACFAC") || user.has("WEB_FACFAC")); // WEB_FACFAC alleen als je het zelf ook hebt

var prs_tz = getFParam("prs_tz", "");

if (prs_tz != "")
{
    var sql = "SELECT '' FROM V$TIMEZONE_NAMES"
            + " WHERE tzname = " + safe.quoted_sql(prs_tz); // Case-sensitive

    var oRs = Oracle.Execute(sql);
    if (oRs.EoF)
        shared.internal_error("timezone (" + safe.html(prs_tz) + ") not found.");
    oRs.Close();
}

var old_flags = 0;
if (!isNew)
{
    var sql = "SELECT prs_perslid_flags"
            + "  FROM prs_perslid"
            + " WHERE prs_perslid_key = " + prs_key;
    var oRs = Oracle.Execute(sql);
    old_flags = oRs("prs_perslid_flags").Value;
    oRs.Close();
}

var fields =  [ { dbs: "prs_srtperslid_key", typ: "key",  frm: "prs_srtkey", track: L("lcl_prs_person_function"), foreign: "prs_srtperslid" },
                { dbs: "prs_perslid_key_verantw", typ: "key",  frm: "prs_verantwkey", track: L("lcl_prs_person_verantw"), foreign: "prs_perslid" },
                { dbs: "prs_afdeling_key", typ: "key",  frm: "prs_afdkey", track: L("lcl_prs_organisatie"), foreign: "prs_afdeling" },
                { dbs: "prs_kostenplaats_key", typ: "key", frm: "prs_kplkey", track: L("prs_kostenplaats"), foreign: "prs_kostenplaats" },
                { dbs: "prs_perslid_geslacht", typ: "number",  frm: "prs_geslacht", track: L("lcl_prs_person_geslacht") },
                { dbs: "mld_adres_key", typ: "key",  frm: "mld_adrkey", track: L("lcl_delivery_address"), foreign: "mld_adres" },
                { dbs: "prs_perslid_telefoonnr", typ: "varchar",  frm: "prs_telnr", track: L("lcl_prs_person_phone") },
                { dbs: "prs_perslid_ingangsdatum", typ: "date",  frm: "prs_ingdat", track: L("lcl_prs_person_ingangsdatum") },
                { dbs: "prs_perslid_einddatum", typ: "date",  frm: "prs_enddat", track: L("lcl_prs_person_einddatum") },
                { dbs: "fac_profiel_key", typ: "key",  frm: "fac_prkey", track: L("lcl_prs_profile"), foreign: "fac_profiel"},
                { dbs: "prs_perslid_timezone", typ: "varchar",  frm: "prs_tz", track: L("lcl_prs_person_timezone") },
                { dbs: "prs_perslid_lcid", typ: "number",  frm: "prs_lcid", track: L("lcl_prs_person_lcid") },
                { dbs: "prs_perslid_visibility", typ: "number",  frm: "prs_visibility", track: L("lcl_prs_person_visibility") },  // ja, dit is een LOV/reference
                { dbs: "prs_perslid_mobiel", typ: "varchar",  frm: "prs_mobiel", track: L("lcl_prs_person_mobile") } ];

if (prsauthparams.writeman)
{ // Hier de manRWFIELD's
  fields.push({ dbs: "prs_perslid_naam", typ: "varchar",  frm: "prs_naam", track: L("lcl_prs_person_name") },
              { dbs: "prs_perslid_tussenvoegsel", typ: "varchar",  frm: "prs_tussen", track: L("lcl_prs_person_tussen") },
              { dbs: "prs_perslid_voorletters", typ: "varchar",  frm: "prs_vltr", track: L("lcl_prs_person_voorletters") },
              { dbs: "prs_perslid_voornaam", typ: "varchar",  frm: "prs_vrnaam", track: L("lcl_prs_person_voornaam") },
              { dbs: "prs_perslid_titel", typ: "varchar",  frm: "prs_titel", track: L("lcl_prs_person_title") },
              { dbs: "prs_perslid_initialen", typ: "varchar",  frm: "prs_ini", track: L("lcl_prs_person_initials") },
              { dbs: "prs_perslid_email", typ: "varchar",  frm: "prs_email", track: L("lcl_prs_person_email") },
              { dbs: "prs_perslid_nr", typ: "varchar",  frm: "prs_nr", track: L("lcl_prs_person_nr") },
              { dbs: "prs_perslid_dienstverband", typ: "float",  frm: "prs_dnst", track: L("lcl_prs_person_dienstverband") },
              { dbs: "prs_perslid_opp", typ: "float",  frm: "prs_opp", track: L("lcl_prs_person_opp") },
              { dbs: "prs_perslid_uurloon", typ: "float",  frm: "prs_uurln", track: L("lcl_prs_person_uurloon") },
              { dbs: "prs_perslid_inactief", typ: "date", frm: "date_inactief", track: L("prs_perslid_inactief") }
             );
}

if (prsauthparams.writesys || (S("prs_mgt_edit_login")?xfunc.canWrite("WEB_FACMGT"):false))
{
    // Check for uniqueness of OSLOGIN and OSLOGIN2 among existing OSLOGIN and OSLOGIN2
    if (hasFParam("prs_oslog")) // Als je deze hebt, heb je ook prs_oslog2
    {
        var login  = getFParam("prs_oslog");
        var login2 = getFParam("prs_oslog2");
        var logSql = "SELECT CASE"
                   + "           WHEN prs_perslid_oslogin2 = " + safe.quoted_sql_upper(login) + " THEN 1"
                   + "           ELSE 2"
                   + "       END"
                   + "           veld"
                   + "  FROM prs_perslid"
                   + " WHERE (prs_perslid_oslogin2 = " + safe.quoted_sql_upper(login)
                   + "        OR prs_perslid_oslogin = " + safe.quoted_sql_upper(login2) + ")"
                   + (prs_key > 0 ? "   AND prs_perslid_key <> " + prs_key : "");
        var logoRs = Oracle.Execute(logSql);
        if (logoRs.eof)
        {
            fields.push({ dbs: "prs_perslid_oslogin",  typ: "varchar",  frm: "prs_oslog",  track: L("lcl_prs_person_login") });
            fields.push({ dbs: "prs_perslid_oslogin2", typ: "varchar",  frm: "prs_oslog2", track: L("lcl_prs_person_login2") });
        }
        else
        {
            var safe_login = safe.html(logoRs("veld").Value == 1 ? login : login2);
            abort_with_warning(L("lcl_prs_person_login_in_use").format(safe_login));
        }
        logoRs.Close();
    }
    fields.push({ dbs: "prs_perslid_ingids", typ: "check",  frm: "ingids", track: L("lcl_prs_in_guide") });
}
if (prsauthparams.writetab)
{
    fields.push({ dbs: "prs_perslid_systeemadres", typ: "varchar", frm: "prs_systeemadres", track: L("lcl_prs_systeemadres") });

    if ((old_flags&32)!=32)
        fields.push({ dbs: "prs_perslid_flags",    typ: "number",  frm: "prs_flags",        track: L("lcl_prs_flagscode") });
    if (user.has("WEB_FACFAC"))
        fields.push({ dbs: "prs_perslid_loglevel", typ: "check0",  frm: "prs_loglevel",     track: L("prs_perslid_loglevel") });
}
if (prsauthparams.writesys)
{
    fields.push({ dbs: "prs_perslid_errorhandling", typ: "check0",  frm: "prs_errorhandling", track: L("prs_perslid_errorhandling") });
    fields.push({ dbs: "prs_perslid_opmerking",    typ: "varchar", frm: "prs_opm",          track: L("lcl_prs_person_opmerking") });
}
var prs_lang = getFParam("prs_lang", "");
if (prs_lang == -1) prs_lang = "";
fields.push( {dbs: "prs_perslid_lang", typ: "varchar", val: prs_lang, track: L("lcl_lcl_taal") });

if (hasFParam("has_noti0")) // Dan zijn ze er meer
{
    var srtnoti_mode = -1;

    if (Request.Form("noti0").count == 1)
      srtnoti_mode = 0; // Expliciet geen notificaties
    else
    {
      // Merk op dat vinkjes buiten S("prs_personal_noti_options") niet aanwezig waren en terugvallen op 0
      if (Request.Form("noti1").count == 1)
        srtnoti_mode += 1;
      if (Request.Form("noti2").count == 1)
        srtnoti_mode += 2;
      if (Request.Form("noti4").count == 1)
        srtnoti_mode += 4;
      if (Request.Form("noti8").count == 1)
        srtnoti_mode += 8;
      if (srtnoti_mode > -1)
      {
        // Er is een notificatie vinkje gezet. De initiele waarde van srtnoti_mode was -1 i.p.v. 0. Daarom 1 erbij optellen.
        srtnoti_mode += 1;
      }
      else
      {
        // Er is *geen enkel* vinkje gezet. srtnoti_mode blijft -1 en in de volgende fields wordt hij NULL
        // Daarmee valt de gebruiker terug naar de defaults.
      }
    }
    fields.push({ dbs: "prs_perslid_srtnoti_mode", typ: "number",  val: (srtnoti_mode >= 0? srtnoti_mode : ""), track: L("lcl_noti_prsvoorkeur") });
}

var prsUpd;
if (!isNew)
{
    prsUpd = buildTrackingUpdate("prs_perslid", " prs_perslid_key = " + prs_key, fields);
    sql = buildUpdate("prs_perslid", fields)
        + "     prs_perslid_key = " + prs_key;

    var err = Oracle.Execute(sql, true);
    if (err.friendlyMsg)
        abort_with_warning(err.friendlyMsg);


    if (user_key == prs_key)
    {
      Session("time_zone") = getFParam("prs_tz", S("fac_server_timezone"));
      if (getFParamInt("prs_lcid", 0) != 0)
      {
        Session.LCID = getFParamInt("prs_lcid");
      }
    }
}
else
{
    fields.push({  dbs: "prs_perslid_key",         typ: "key",      seq: "prs_s_prs_alluitvoerende_keys" });
    var regIns = buildInsert("prs_perslid", fields);
    var prs_key = regIns.sequences["prs_perslid_key"];
    sql = regIns.sql;
    var err = Oracle.Execute(sql, true);
    if (err.friendlyMsg)
        abort_with_warning(err.friendlyMsg);
    shared.trackaction("PRSNEW", prs_key);
}

var flextrack = [];
currentKenmerkenSQL = "SELECT kl.prs_kenmerk_key"
                    + "     , kl.prs_kenmerklink_waarde"
                    + "     , k.prs_kenmerk_toonbaar"
                    + "     , k.fac_functie_key"
                    + "  FROM prs_kenmerklink kl"
                    + "     , prs_kenmerk k"
                    + " WHERE k.prs_kenmerk_key = kl.prs_kenmerk_key"
                    + "   AND prs_kenmerklink_verwijder IS NULL"
                    + "   AND prs_link_key = " + prs_key;

var allKenmerkenSQL = " SELECT " + lcl.xsql('k.prs_kenmerk_omschrijving', 'k.prs_kenmerk_key') + " kenmerk_omschrijving"
                    + "      , k.prs_kenmerk_kenmerktype    kenmerk_kenmerktype"
                    + "      , k.prs_kenmerk_systeem        kenmerk_systeem"
                    + "      , k.prs_kenmerk_toonbaar       kenmerk_readonly"
                    + "      , k.prs_kenmerk_uniek          kenmerk_uniek"
                    + "      , fac_kenmerkdomein_key"
                    + "   FROM prs_kenmerk k "
                    + "  WHERE k.prs_kenmerk_verwijder IS NULL"
                    + "    AND k.prs_kenmerk_key = "; /* wordt in saveFlexkenmerken uitgebreid */

flextrack = saveFlexKenmerken(prs_key, { kenmerkTable: "prs_kenmerklink",
                                         kenmerkParentKey: "prs_link_key",
                                         kenmerkWaarde: "prs_kenmerklink_waarde",
                                         kenmerkKey: "prs_kenmerk_key",
                                         kenmerkToonbaar: "prs_kenmerk_toonbaar",
                                         currentKenmerkenSQL: currentKenmerkenSQL,
                                         allKenmerkenSQL: allKenmerkenSQL,
                                         requestQF: Request.Form,
                                         module: "PRS",
                                         moduleName: "prs_kenmerklink_niveau",
                                         moduleVal: "P",
                                         isNew: isNew,
                                         tracking: true,
                                         flexPath: "PRS/P"
                                       });

if (!isNew)
{ // Update.
  if (flextrack.length)
  {
    if (prsUpd && prsUpd.trackarray)
      prsUpd.trackarray = prsUpd.trackarray.concat(flextrack);
    else
      prsUpd.trackarray = flextrack;
  }
  if (prsUpd.trackarray.length)
    shared.trackaction("PRSUPD", prs_key, prsUpd.trackarray.join("\n") );
}

// Klantspecifieke check functie (hookfunction) voor de invoer
var pResult = new HookResult();
if (!custfunc.prs_postsave(prs_key, pResult))
{
  abort_with_warning(pResult.errmsg);
}


result = { success: true
         , prs_key: prs_key
         };
Response.Write(JSON.stringify(result));
%>
<% ASPPAGE_END(); %>
