<%@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="../Shared/json2.js" -->
<!-- #include file="prs.inc"-->
<%

var prs_key = getQParamInt( "prs_key", -1 );
var isNew = (prs_key<0);

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

var fields =  [ { dbs: "prs_srtperslid_key", typ: "key",  frm: "prs_srtkey" },
                { dbs: "prs_afdeling_key", typ: "key",  frm: "prs_afdkey" },
                { dbs: "prs_perslid_naam", typ: "varchar",  frm: "prs_naam" },
                { dbs: "prs_perslid_geslacht", typ: "key",  frm: "prs_geslacht" },
                { dbs: "prs_perslid_voorletters", typ: "varchar",  frm: "prs_vltr" },
                { dbs: "prs_perslid_voornaam", typ: "varchar",  frm: "prs_vrnaam" },
                { dbs: "prs_perslid_titel", typ: "varchar",  frm: "prs_titel" },
                { dbs: "prs_perslid_initialen", typ: "varchar",  frm: "prs_ini" },
                { dbs: "prs_perslid_nr", typ: "varchar",  frm: "prs_nr" },
                { dbs: "prs_perslid_dienstverband", typ: "float",  frm: "prs_dnst" },
                { dbs: "prs_perslid_opp", typ: "float",  frm: "prs_opp" },
                { dbs: "mld_adres_key", typ: "key",  frm: "mld_adrkey" },
                { dbs: "prs_perslid_uurloon", typ: "float",  frm: "prs_uurln" },
                { dbs: "prs_perslid_telefoonnr", typ: "varchar",  frm: "prs_telnr" },
                { dbs: "prs_perslid_ingangsdatum", typ: "date",  frm: "prs_ingdat" },
                { dbs: "prs_perslid_einddatum", typ: "date",  frm: "prs_enddat" },
                { dbs: "prs_perslid_tussenvoegsel", typ: "varchar",  frm: "prs_tussen" },
                { dbs: "fac_profiel_key", typ: "key",  frm: "fac_prkey", track: L("lcl_prs_profile") , foreign: "fac_profiel"},
                { dbs: "prs_perslid_email", typ: "varchar",  frm: "prs_email", track: L("lcl_prs_person_email") },
                { dbs: "prs_perslid_mobiel", typ: "varchar",  frm: "prs_mobiel", track: L("lcl_prs_person_mobile") } ];

if (prsauthparams.writesys || (S("prs_mgt_edit_login")?xfunc.canWrite("WEB_FACMGT"):false))
{
    fields.push(
            { dbs: "prs_perslid_oslogin", typ: "varchar",  frm: "prs_oslog", track: L("lcl_prs_person_login") },
            { dbs: "prs_perslid_oslogin2", typ: "varchar",  frm: "prs_oslog2", track: L("lcl_prs_person_login2") },
            { dbs: "prs_perslid_ingids", typ: "check",  frm: "ingids"}
          )
}
if (prsauthparams.writetab)
{
    fields.push({ dbs: "prs_perslid_apikey", typ: "varchar",  frm: "prs_apikey", track: true });
}

var prs_lang = getFParam("prs_lang", "");
if (prs_lang == -1) prs_lang = "";
fields.push( {dbs: "prs_perslid_lang", typ: "varchar", val: prs_lang });

if (Request.Form("has_" + "noti1").count == 1) // Dan zijn ze er allemaal
{
    var srtnoti_mode = -1;
    if (Request.Form("noti0").count == 1)
      srtnoti_mode = 0; // Geen notificaties
    else
    {
      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 notivicatie vinkje gezet. De initiele waarde van srtnoti_mode was -1 i.p.v. 0. Daarom 1 erbij optellen.
        srtnoti_mode += 1;
    }
    fields.push({ dbs: "prs_perslid_srtnoti_mode", typ: "number",  val: (srtnoti_mode >= 0? srtnoti_mode : "") });
}

if (prs_key > 0)
{
    var 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);

    var oldapi = prsUpd.oldjsvals["prs_perslid_apikey"];
    var newapi = getFParam("prs_apikey", "")
    if (prsauthparams.writetab && oldapi != newapi)
    {
        var txt = L("lcl_prs_api_key_upd");
        if (oldapi && !newapi)
            txt = L("lcl_tracktoempty")
        else if (!oldapi && newapi)
            txt = L("lcl_prs_api_key_set");
        prsUpd.trackarray.push(L("lcl_prs_apiuser")+ " " + txt);
    }

    shared.trackaction("PRSUPD", prs_key, (prsUpd.trackarray.length ? prsUpd.trackarray.join("\n") : null) );
}
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);
}

currentKenmerkenSQL = "SELECT kl.prs_kenmerk_key " +
                         "  , kl.prs_kenmerklink_waarde" +
                         "  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;

saveFlexKenmerken(prs_key, { kenmerkTable: "PRS_KENMERKLINK",
                             kenmerkParentKey : "PRS_LINK_KEY",
                             kenmerkWaarde: "prs_kenmerklink_waarde",
                             kenmerkKey: "PRS_KENMERK_KEY",
                             currentKenmerkenSQL: currentKenmerkenSQL,
                             requestQF: Request.Form,
                             moduleName: "PRS_KENMERKLINK_NIVEAU",
                             moduleVal: "P",
                             isNew: isNew,
                             flexPath: "PRS/P"
                       });

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