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

    File:               ins_edit_deel_multi_save.asp
    Description:        save script van ins_edit_deel_multi.asp
    Globalen:
    Context:
    Note:
*/
var JSON_Result = true;
%>

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

<%
protectRequest.validateToken();
var urole = getQParamSafe("urole");
var ins_key_arr = getQParamIntArray("ins_keys");
var warning = "";
var result = {};
var fields = [];

if (Request.Form("vervaldatum").Count == 1 && getFParam("check_vervaldatum", "off") == "on")
    fields.push({ dbs: "ins_deel_vervaldatum", typ: "date",  frm: "vervaldatum",  track: L("lcl_ins_vervaldatum") });
if (getFParam("aanmaakdatum"))
    fields.push({ dbs: "ins_deel_aanmaak",      typ: "date",  frm: "aanmaakdatum", track: L("lcl_last_action") });
if (Request.Form("ins_manager").Count == 1 && getFParam("check_ins_manager", "off") == "on")
    fields.push({ dbs: "prs_perslid_key_beh",  typ: "key",   frm: "ins_manager",  track: L("lcl_ins_manager"), foreign: "prs_perslid" });
if (Request.Form("count").Count == 1 && getFParam("check_count", "off") == "on")
    fields.push({ dbs: "ins_deel_aantal",      typ: "float", frm: "count",        track: L("lcl_count"), relativeValuesAllowed: true });

for (var i = 0; i < ins_key_arr.length; i++)
{   // Ik mag het object wijzigen dus voer wijziging uit
    var ins_key = ins_key_arr[i];
    var trackarray = [];

    var this_ins = ins.func_enabled_deel(ins_key_arr[i], {urole: urole});
    if (this_ins.canChange)
    {
        if (fields.length)
        {
            var insUpd = buildTrackingUpdate("ins_deel", " ins_deel_key = " + ins_key_arr[i], fields);
            trackarray = insUpd.trackarray;
            // Submit the changed data
            var err = Oracle.Execute(insUpd.sql, true);
            if (err.friendlyMsg)
                warning = err.friendlyMsg;
        }

        var currentKenmerkenSQL = "SELECT kd.ins_kenmerk_key"
                                + "     , kd.ins_kenmerkdeel_waarde"
                                + "     , k.ins_kenmerk_toonbaar"
                                + "     , k.fac_functie_key"
                                + "  FROM ins_kenmerkdeel kd"
                                + "     , ins_kenmerk k"
                                + " WHERE k.ins_kenmerk_key = kd.ins_kenmerk_key"
                                + "   AND kd.ins_kenmerkdeel_verwijder IS NULL"
                                + "   AND kd.ins_deel_key = " + ins_key;

        var allKenmerkenSQL = " SELECT COALESCE(" + lcl.xsql("k.ins_kenmerk_omschrijving", "k.ins_kenmerk_key")
                            + "                ," + lcl.xsql("t.ins_srtkenmerk_omschrijving", "t.ins_srtkenmerk_key")
                            + "                ) kenmerk_omschrijving"
                            + "      , t.ins_srtkenmerk_kenmerktype  kenmerk_kenmerktype"
                            + "      , t.ins_srtkenmerk_systeem      kenmerk_systeem"
                            + "      , k.ins_kenmerk_toonbaar        kenmerk_readonly"
                            + "      , k.ins_kenmerk_uniek           kenmerk_uniek"
                            + "      , t.fac_kenmerkdomein_key"
                            + "   FROM ins_kenmerk k "
                            + "      , ins_srtkenmerk t"
                            + "  WHERE k.ins_srtkenmerk_key = t.ins_srtkenmerk_key"
                            + "    AND k.ins_kenmerk_verwijder IS NULL"
                            + "    AND k.ins_kenmerk_key = "; /* wordt in saveFlexkenmerken uitgebreid */

        var flextrack = saveFlexKenmerken(ins_key, { kenmerkTable: "ins_kenmerkdeel",
                                                     kenmerkParentKey: "ins_deel_key",
                                                     kenmerkWaarde: "ins_kenmerkdeel_waarde",
                                                     kenmerkKey: "ins_kenmerk_key",
                                                     kenmerkToonbaar: "ins_kenmerk_toonbaar",
                                                     currentKenmerkenSQL: currentKenmerkenSQL,
                                                     allKenmerkenSQL: allKenmerkenSQL,
                                                     requestQF: Request.Form,
                                                     module: "INS",
                                                     isNew: false,
                                                     multiMode: true,
                                                     relativeValuesAllowed: true,
                                                     tracking: true,
                                                     flexPath: "INS/I"
                                                   });

        if (trackarray.length || (flextrack && flextrack.length))
        {
            var update_string = L("lcl_ins_is_insupd") + "\n" + trackarray.join("\n") + (trackarray.length ? "\n" : "");
            if (flextrack && flextrack.length)
                update_string += flextrack.join("\n");
            shared.trackaction("INSUPD", ins_key, update_string);
        }


//        /* flextrackarray = */ ins.updateKenmerkdeel(ins_key_arr[i], {multimode: true});
//        var update_string = L("lcl_ins_is_insupd") + "\n" + insUpd.trackarray.join("\n");
//        ins.trackdeelupdate(ins_key_arr[i], update_string);
    }
}

result = { success : true, refresh: true };

Response.Write(JSON.stringify(result));

ASPPAGE_END();
%>
