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

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

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/save2db.inc" -->
<!-- #include file="../API2/api2.inc" -->

<%
var bedrijf_key = getQParamInt("bedrijf_key");
var badr_key = getQParamInt("badr_key", -1);
var kanaal = getFParam("prs_type") // type is verplicht
var autfunction = 'WEB_PRSMGT';
var authparams = user.checkAutorisation(autfunction);
user.auth_required_or_abort(authparams && authparams.PRSwritelevel < 9);

var isDeleted = false;
var warning = "";

if ((badr_key > 0) && (kanaal == "X"))
{
    sql = "DELETE FROM prs_bedrijfadres"
        + " WHERE prs_bedrijf_key = " + bedrijf_key
        + "   AND prs_bedrijfadres_key = " + badr_key;
    isDeleted = true;

    var err = Oracle.Execute(sql, true);
    if (err.friendlyMsg)
        warning = err.friendlyMsg;
}
else
{
    var typeopdr = -1;
    if (kanaal == "O")
        typeopdr = getFParamInt("prs_typeopdr", -1);

    var isNew = (badr_key < 0);

    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var xsl = getFParam("prs_xsl", "");
    if (xsl && !fso.FileExists(Server.MapPath(custpath) + "/" + xsl)) // Zo doet puo_xmltools/ SafeLoadFileXSL dat
        abort_with_warning(xsl + " not found");

    var protocol = getFParam("protocol");
    var url = getFParam("prs_url");

    var fields =  [ { dbs: "prs_bedrijfadres_type",         typ: "varchar",  frm: "prs_type"       }
                  , { dbs: "mld_typeopdr_key",              typ: "key",      val: typeopdr,        track: L("lcl_mld_typeopdr"), foreign: "mld_typeopdr" }
                  , { dbs: "prs_bedrijf_key2",              typ: "key",      frm: "prs_bdrkey2",   track: L("prs_bedrijf_naam1"), foreign: "prs_bedrijf" }
                  , { dbs: "alg_district_key",              typ: "key",      frm: "districtkey",   track: L("lcl_district"), foreign: "alg_district" }
                  , { dbs: "alg_locatie_key",               typ: "key",      frm: "locatiekey",    track: L("lcl_location"), foreign: "alg_locatie" }
                  , { dbs: "prs_bedrijfadres_url",          typ: "varchar",  val: protocol + url,  track: L("lcl_prs_bedrijfadres_url") }
                  , { dbs: "prs_bedrijfadres_sender",       typ: "varchar",  frm: "prs_sender",    track: L("lcl_prs_bedrijfadres_afzender") }
                  , { dbs: "prs_bedrijfadres_replyaddress", typ: "varchar",  frm: "prs_reply",     track: L("lcl_prs_bedrijfadres_replyadres") }
                  , { dbs: "prs_bedrijfadres_username",     typ: "varchar",  frm: "prs_username",  track: L("lcl_prs_bedrijfadres_username") }
                  , { dbs: "prs_bedrijfadres_password",     typ: "varchar",  frm: "prs_password",  track: L("lcl_prs_bedrijfadres_wachtwoord") }
                  , { dbs: "prs_bedrijfadres_authmethod",   typ: "number",   frm: "authmethod",    track: L("lcl_prs_bedrijfadres_authmethod") }
                  , { dbs: "prs_bedrijfadres_oa_token_url", typ: "varchar",  frm: "token_url",     track: L("lcl_prs_bedrijfadres_oa_token_url")}
                  , { dbs: "prs_bedrijfadres_oa_scope",     typ: "varchar",  frm: "oa_scope",      track: L("lcl_prs_bedrijfadres_oa_scope")}
                  , { dbs: "prs_bedrijfadres_oa_resource",  typ: "varchar",  frm: "oa_resource",   track: L("lcl_prs_bedrijfadres_oa_resource")}
                  , { dbs: "prs_bedrijfadres_ordermode",    typ: "number",   frm: "prs_ordermode", track: L("lcl_prs_companies_order_mode") }
                  , { dbs: "prs_bedrijfadres_soapversion",  typ: "varchar",  frm: "soapversion",   track: L("lcl_prs_bedrijfadres_soapversion") }
                  , { dbs: "prs_bedrijfadres_soapaction",   typ: "varchar",  frm: "soapaction",    track: L("lcl_prs_bedrijfadres_soapaction") }
                  , { dbs: "prs_bedrijfadres_certificate",  typ: "varchar",  frm: "prs_cert",      track: L("lcl_prs_companies_order_certificate") }
                  , { dbs: "prs_bedrijfadres_xsl",          typ: "varchar",  frm: "prs_xsl",       track: L("lcl_prs_companies_xsl") }
                  , { dbs: "prs_bedrijfadres_error_handler",typ: "check0",   frm: "prs_err",       track: L("lcl_prs_companies_error_handler") }
                  , { dbs: "prs_bedrijfadres_ext",          typ: "varchar",  frm: "prs_ext",       track: L("lcl_prs_bedrijfadres_ext") }
                  , { dbs: "prs_bedrijfadres_lockuser_key", typ: "key",      frm: "lockuser_key",  track: L("lcl_prs_bedrijfadres_lockuser"), foreign: "prs_perslid" }
                  , { dbs: "prs_bedrijfadres_locksecret",   typ: "varchar",  frm: "locksecret",    track: L("lcl_prs_bedrijfadres_locksecret") }
                  , { dbs: "prs_bedrijfadres_lockexpire" ,  typ: "number",   frm: "lockexpire",    track: L("lcl_prs_bedrijfadres_lockexpire") }
                  , { dbs: "prs_bedrijfadres_loglevel",     typ: "number",   frm: "loglevel",      track: L("lcl_prs_bedrijfadres_loglevel") }
                  , { dbs: "prs_bedrijfadres_attachfile",   typ: "varchar",  frm: "bijlage",       track: L("lcl_prs_bedrijfadres_bijlage") }
                  , { dbs: "prs_bedrijfadres_flexfiles" ,   typ: "number",   frm: "flexfiles",     track: L("lcl_puo_order_flexfiles") }
                  , { dbs: "prs_bedrijfadres_encoding",     typ: "number",   frm: "encoding",      track: L("lcl_prs_bedrijfadres_encoding")}
                  , { dbs: "prs_bedrijfadres_charset",      typ: "varchar",  frm: "charset",       track: L("lcl_prs_bedrijfadres_charset") }
                  , { dbs: "prs_bedrijfadres_plugin",       typ: "varchar",  frm: "plugin",        track: L("prs_bedrijfadres_plugin") }
                  , { dbs: "prs_bedrijfadres_plugindata",   typ: "varchar",  frm: "plugindata"     }
                  , { dbs: "prs_bedrijfadres_startdatum",   typ: "datetime", frm: "startdate",     track: L("prs_bedrijf_startdatum") }
                  ];

    var isDeleted = false;
    if (badr_key > 0)
    {
        var prsUpd = buildTrackingUpdate("prs_bedrijfadres", " prs_bedrijfadres_key = " + badr_key, fields);

        // JGL: Volgens mij is onderstaand complexer dan nodig.
        //      Als hierboven bij bijvoorbeeld prs_bedrijfadres_type een track: true wordt meegegeven dan
        //      bevat prsUpd.oldjsvals volautomagisch de oude waarden en kun je daar je tracking op baseren
        //      Dat gebeurt bijvoorbeeld volop in mld_edit_melding_save.asp

        // tracking van velden met listboxen zonder typ=key
        // Eerst de oude waarden ophalen
        var def = " (" + (S("puo_order_flexfiles")?L("lcl_Yes"):L("lcl_No")) + ")";
        var sql_old = "WITH "
                    + "     kanaal (kan_chr, kan_txt)"
                    + "     AS (      SELECT 'B', " + safe.quoted_sql(L("lcl_prs_bedrijfadres_type_b"))+" FROM DUAL"
                            + " UNION SELECT 'O', " + safe.quoted_sql(L("lcl_prs_bedrijfadres_type_o"))+" FROM DUAL"
                            + " UNION SELECT 'C', " + safe.quoted_sql(L("lcl_prs_bedrijfadres_type_c"))+" FROM DUAL"
                            + " UNION SELECT 'V', " + safe.quoted_sql(L("lcl_prs_bedrijfadres_type_v"))+" FROM DUAL"
                            + " UNION SELECT 'N', " + safe.quoted_sql(L("lcl_prs_bedrijfadres_type_n"))+" FROM DUAL"
                            + " UNION SELECT 'I', " + safe.quoted_sql(L("lcl_prs_bedrijfadres_type_i"))+" FROM DUAL"
                            + " UNION SELECT 'R', " + safe.quoted_sql(L("lcl_prs_bedrijfadres_type_r"))+" FROM DUAL"
                            + ")"
                    + "   , authmethod (aut_key, aut_txt, aut_nr)"
                    + "     AS ( " + api2.splitLOV2sql(L("lcl_prs_bedrijfadres_authmethodLOV"))
                            + ")"
                    + "   , flexfiles (flx_key, flx_txt)"

                    + (getFParam("prs_type") == "B" ? " AS (       SELECT  0, " + safe.quoted_sql(L("lcl_no"))  + " FROM DUAL"
                                           + "  UNION SELECT  1, " + safe.quoted_sql(L("lcl_Yes")) + " FROM DUAL"
                                           + ")"
                                       : " AS (      SELECT 0, " + safe.quoted_sql(L("lcl_default") + def) + " FROM DUAL"
                                           + " UNION SELECT 1, " + safe.quoted_sql(L("lcl_Yes"))           + " FROM DUAL"
                                           + " UNION SELECT 2, " + safe.quoted_sql(L("lcl_No"))            + " FROM DUAL"
                                           + ")")
                    + "   , encoding (enc_key, enc_txt)"
                    + "     AS (      SELECT 0, " + safe.quoted_sql(L("lcl_encoding_text_xml"))         + " FROM DUAL"
                            + " UNION SELECT 1, " + safe.quoted_sql(L("lcl_encoding_multipart_form"))   + " FROM DUAL"
                            + " UNION SELECT 2, " + safe.quoted_sql(L("lcl_encoding_application_json")) + " FROM DUAL"
                            + ")"
                    + "SELECT (select kan_txt from kanaal     where kan_chr = prs_bedrijfadres_type)                 old_kan"
                    + "     , (select kan_txt from kanaal     where kan_chr = " + safe.quoted_sql(getFParam("prs_type", "B")) + ") new_kan"
                    + "     , (select aut_txt from authmethod where aut_key = prs_bedrijfadres_authmethod)           old_aut"
                    + "     , (select aut_txt from authmethod where aut_key = " + getFParamInt("authmethod", 0) + ") new_aut"
                    + "     , (select flx_txt from flexfiles  where flx_key = prs_bedrijfadres_flexfiles)            old_flx"
                    + "     , (select flx_txt from flexfiles  where flx_key = " + getFParamInt("flexfiles",  0) + ") new_flx"
                    + "     , (select enc_txt from encoding   where enc_key = prs_bedrijfadres_encoding)             old_enc"
                    + "     , (select enc_txt from encoding   where enc_key = " + getFParamInt("encoding",   0) + ") new_enc"
                    + "  FROM prs_bedrijfadres"
                    + " WHERE prs_bedrijfadres_key = " + badr_key;
        var oRs_old = Oracle.Execute(sql_old);
        if (!oRs_old.eof)
        {
            var old_kan = oRs_old("old_kan").Value;
            var old_aut = oRs_old("old_aut").Value;
            var old_flx = oRs_old("old_flx").Value;
            var old_enc = oRs_old("old_enc").Value;
            var new_kan = oRs_old("new_kan").Value;
            var new_aut = oRs_old("new_aut").Value;
            var new_flx = oRs_old("new_flx").Value;
            var new_enc = oRs_old("new_enc").Value;

            if (old_kan != new_kan)
                prsUpd.trackarray.push(L("lcl_prs_bedrijfadres_type")       + ": " + buildTrackText("varchar", old_kan, new_kan, { nodiff: true }));
            if (old_aut != new_aut)
                prsUpd.trackarray.push(L("lcl_prs_bedrijfadres_authmethod") + ": " + buildTrackText("varchar", old_aut, new_aut, { nodiff: true }));
            if (old_flx != new_flx)
                prsUpd.trackarray.push(L("lcl_puo_order_flexfiles")         + ": " + buildTrackText("varchar", old_flx, new_flx, { nodiff: true }));
            if (old_enc != new_enc)
                prsUpd.trackarray.push(L("lcl_prs_bedrijfadres_encoding")   + ": " + buildTrackText("varchar", old_enc, new_enc, { nodiff: true }));
        }
        oRs_old.Close();

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

        if (prsUpd.trackarray.length)
            shared.trackaction("PRSUPT", badr_key, prsUpd.trackarray.join("\n") );
    }
    else
    {
        fields.push( { dbs: "prs_bedrijf_key", typ: "key", val: bedrijf_key } );
        fields.push( { dbs: "prs_bedrijfadres_key", typ: "key", seq: "prs_s_prs_bedrijfadres_key" });
        var regIns = buildInsert("prs_bedrijfadres", fields);
        var badr_key = regIns.sequences["prs_bedrijfadres_key"];
        sql = regIns.sql;
        var err = Oracle.Execute(sql, true);
        if (err.friendlyMsg)
            warning = err.friendlyMsg;
        // Geen trackaction voor nieuwe bedrijfadressen.
    }
}

result = { success: true
         , bedrijf_key: bedrijf_key
         , badr_key: badr_key
         , warning: warning
         , keepForm: (warning?true:false)
         , refresh: true
         };
Response.Write(JSON.stringify(result));
%>
<% ASPPAGE_END(); %>
