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

     File:         prs_edit_contactpersoon_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"-->

<%
var autfunction = ["WEB_RELUSE"];
if (user.iamContact())
  autfunction.push("WEB_EXTREL");

var authparams = user.checkAutorisation(autfunction);
user.auth_required_or_abort(authparams && authparams.ALGwritelevel < 9);

var bdr_key = getFParamInt("prs_bdrkey");
var cps_key = getQParamInt("cps_key", -1 );

if (cps_key > -1)
{
  var sql = "SELECT c.prs_bedrijf_key"
          + "     , COALESCE(b.prs_bedrijf_intern, 0) intern"
          + "  FROM prs_contactpersoon c, "
          + "       prs_bedrijf b "
          + " WHERE b.prs_bedrijf_key = c.prs_bedrijf_key"
          + "   AND c.prs_contactpersoon_key = " + cps_key;
}
else
{
  var sql = "SELECT b.prs_bedrijf_key"
          + "     , COALESCE(b.prs_bedrijf_intern, 0) intern"
          + "  FROM prs_bedrijf b"
          + " WHERE b.prs_bedrijf_verwijder IS NULL"
          + "   AND b.prs_bedrijf_key = " + bdr_key;
}
var oRs = Oracle.Execute(sql);
var prs_bdrkey = oRs("prs_bedrijf_key").value;
var prs_intern = oRs("intern").value == 1;
oRs.Close();

if (user.iamContact()) // Als ik contactpersoon ben dan moet ik het ook van dit externe uitvoerende bedrijf zijn.
  user.auth_required_or_abort(!prs_intern && user.iamContact(prs_bdrkey));

var isNew = (cps_key<0);

var fields =  [ { dbs: "prs_bedrijf_key",                 typ: "key",      frm: "prs_bdrkey" },
                { dbs: "prs_contactpersoon_aanhef",       typ: "varchar",  frm: "prs_aanh" },
                { dbs: "prs_contactpersoon_voorletters",  typ: "varchar",  frm: "prs_voorl" },
                { dbs: "prs_contactpersoon_naam",         typ: "varchar",  frm: "prs_naam" },
                { dbs: "prs_contactpersoon_voornaam",     typ: "varchar",  frm: "prs_vrnaam" },
                { dbs: "prs_contactpersoon_tussenv",      typ: "varchar",  frm: "prs_tsnvgs" },
                { dbs: "prs_contactpersoon_functie",      typ: "varchar",  frm: "prs_func" },
                { dbs: "prs_contactpersoon_titel",        typ: "varchar",  frm: "prs_titel" },
                { dbs: "prs_contactpersoon_telefoon_1",   typ: "varchar",  frm: "prs_tel1" },
                { dbs: "prs_contactpersoon_telefoon_2",   typ: "varchar",  frm: "prs_tel2" },
                { dbs: "prs_contactpersoon_fax",          typ: "varchar",  frm: "prs_fax" },
                { dbs: "prs_contactpersoon_email",        typ: "varchar",  frm: "prs_email" },
                { dbs: "prs_contactpersoon_opmerking",    typ: "varchar",  frm: "prs_opm" },
                { dbs: "prs_perslid_key",                 typ: "key",      frm: "prs_prskey" },
                { dbs: "prs_contactpersoon_uurloon",      typ: "float",    frm: "prs_loon" },
                { dbs: "prs_contactpersoon_systeem",      typ: "check0",   frm: "prs_systeem" }];

var warning = "";
if (cps_key > 0)
{
    sql = buildUpdate("prs_contactpersoon", fields)
        + "     prs_contactpersoon_key = " + cps_key;

    var err = Oracle.Execute(sql, true);
    if (err.friendlyMsg)
        warning = err.friendlyMsg;
}
else
{
    fields.push({  dbs: "prs_contactpersoon_key",         typ: "key",      seq: "prs_s_prs_contactpersoon_key" });
    var regIns = buildInsert("prs_contactpersoon", fields);
    var cps_key = regIns.sequences["prs_contactpersoon_key"];
    sql = regIns.sql;
    var err = Oracle.Execute(sql, true);
    if (err.friendlyMsg)
        warning = err.friendlyMsg;
}

var result = {success: true,
              cps_key: cps_key,
              warning: warning,
              keepForm: (warning?true:false),
              refresh: true
             };

if (!warning)
{
   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 = " + cps_key;

   saveFlexKenmerken(cps_key, { kenmerkTable: "prs_kenmerklink",
                                kenmerkParentKey : "prs_link_key",
                                kenmerkWaarde: "prs_kenmerklink_waarde",
                                kenmerkKey: "prs_kenmerk_key",
                                kenmerkToonbaar: "prs_kenmerk_toonbaar",
                                currentKenmerkenSQL: currentKenmerkenSQL,
                                requestQF: Request.Form,
                                module: "PRS",
                                moduleName: "prs_kenmerklink_niveau",
                                moduleVal: "C",
                                isNew: isNew,
                                flexPath: "PRS/C"
                          });


    var pResult = new HookResult();
    if (!custfunc.prs_contact_postsave(cps_key, pResult))
    {
      var warning = pResult.errmsg;
      var result = {success: false,
                    cps_key: cps_key,
                    warning: warning,
                    keepForm: false, // Het opslaan van de contactpersoon al succesvol gedaan; dus altijd scherm sluiten.
                    refresh: false
                   };
    }
}

Response.Write(JSON.stringify(result));
%>
<% ASPPAGE_END(); %>
