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

     File:         ins_move_deel_multi_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="./ins.inc" -->

<%
protectRequest.validateToken();
var urole  = getQParamSafe("urole");
var fronto = urole == "fo";
var backo  = urole == "bo";
var minfo  = urole == "mi";
var frontend = (!fronto && !backo && !minfo);
var autfunction = urole == "fo"? "WEB_INSFOF" : "WEB_INSMAN";
var authparams = user.checkAutorisation(autfunction); // Nog nodig voor FCLTplaatsselector

var ins_key_arr = getQParamIntArray("ins_keys");
var tobemoved = 0;
var ingesloten = [];
// Bepaal de objecten in de selectie die ook echt gewijzigd kunnen of mogen worden.
for (var i = 0; i < ins_key_arr.length; i++)
{
    var this_ins = ins.func_enabled_deel(ins_key_arr[i]);
    if (this_ins.canChange)
    {
        ingesloten.push(ins_key_arr[i]);
        tobemoved++;
    }
}
user.anything_todo_or_abort(tobemoved > 0); // We klagen niet over enkele wel en enkele niet

// Multi binding wijzgingen kan alleen als de objecten overeenkomstige binding(s) hebben.
var binding = { srtdeel: 0xFF  // Veronderstel nog even dat alles mag
              , codes: []
              , names: []
              };
var sql = "SELECT DISTINCT sd.ins_srtdeel_binding"
        + "  FROM ins_srtdeel sd"
        + "     , ins_deel d"
        + " WHERE d.ins_srtdeel_key = sd.ins_srtdeel_key"
        + "   AND d.ins_deel_key IN (" + ingesloten.join(",") + ")";
var oRs = Oracle.Execute(sql);
while (!oRs.eof)
{   // bitwise and om alleen de overeenkomstige bindings over te houden.
    binding.srtdeel = binding.srtdeel & oRs("ins_srtdeel_binding").value;
    oRs.MoveNext();
}
oRs.Close();

for (var i=0; i<5; i++)
{
        switch (binding.srtdeel & Math.pow(2,i))
        {
            case BIND_RUIMTE: binding.codes.push("R"); binding.names.push(L("lcl_obj_bind_r")); break;
            case BIND_WERKPL: binding.codes.push("W"); binding.names.push(L("lcl_obj_bind_w")); break;
            case BIND_TERREI: binding.codes.push("T"); binding.names.push(L("lcl_obj_bind_t")); break;
            case BIND_AFDELI: binding.codes.push("A"); binding.names.push(L("lcl_obj_bind_a")); break;
            case BIND_PERSOO: binding.codes.push("P"); binding.names.push(L("lcl_obj_bind_p")); break;
        }
}

// Filter nu alle objecten eruit die de benodigde binding(s) niet hebben.
var upd_delen = [];
var upd_onderdelen = [];
var upd_delenenonderdelen = [];
var sql = "SELECT d.ins_deel_key"
        + "     , d.ins_deel_parent_key"
        + "  FROM ins_deel d"
        + "     , ins_srtdeel sd"
        + " WHERE d.ins_srtdeel_key = sd.ins_srtdeel_key"
        + "   AND BITAND(sd.ins_srtdeel_binding, " + binding.srtdeel + ") > 0"
        + "   AND d.ins_deel_key IN (" + ingesloten.join(",") + ")"
        + " ORDER BY d.ins_deel_key";
var oRs = Oracle.Execute(sql);
while (!oRs.eof)
{
  upd_delenenonderdelen.push(oRs("ins_deel_key").Value);
  if (oRs("ins_deel_parent_key").Value) {
    upd_onderdelen.push(oRs("ins_deel_key").Value);
  } else {
    upd_delen.push(oRs("ins_deel_key").Value);
  }
  oRs.MoveNext();
}
oRs.Close();

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

function getPlaceholder(ins_deel_key_arr)
{
    var oms = [];
    var sql = "SELECT alg_plaatsaanduiding"
            + "  FROM ins_v_deelenonderdeel_gegevens"
            + " WHERE ins_deel_key IN (" + ins_deel_key_arr.join(",") + ")"
            + " ORDER BY ins_deel_key";
    var oRs = Oracle.Execute(sql);
    while (!oRs.eof)
    {
        oms.push(oRs("alg_plaatsaanduiding").Value);
        oRs.MoveNext();
    }
    oRs.Close();
    return  oms;
}

if (upd_delenenonderdelen.length)
{
    /***** Get form parameters *****/
    var w_key = getFParamInt("werkplekkey");
    var r_key = getFParamInt("ruimtekey");
    var v_key = getFParamInt("verdiepingkey");
    var g_key = getFParamInt("gebouwkey");
    var l_key = getFParamInt("locatiekey");
    var a_key = getFParamInt("afdelingkey");
    var p_key = getFParamInt("persoonkey");
    var btype = getFParam("bttype","");
    var wis_grafisch = getFParam("wis_grafisch","off") == "on";
    var isPlaats = (w_key > -1 || r_key > -1 || v_key > -1 || g_key > -1 || l_key > -1);

    if (isPlaats)
    {
        if      (w_key > -1) binding.multi = {typ: "W", key: w_key}
        else if (r_key > -1) binding.multi = {typ: "R", key: r_key}
        else if (g_key > -1) binding.multi = {typ: "T", key: g_key}
        else                 binding.multi = {typ: "_", key: -1};
    }
    else if (a_key > -1)
    {
        binding.multi = {typ: "A", key: a_key};
    }
    else if (p_key > -1)
    {
        binding.multi = {typ: "P", key: p_key};
    }

    if (binding.srtdeel == 0 || binding.names.length == 0 || !inArray(binding.multi.typ, binding.codes))
    {
        abort_with_warning( L("lcl_obj_bind_mandatory") + "\n"
                          + ( binding.names.length
                            ? binding.names.join(" "+L("lcl_fac_api_of")+" ") // geen binding kunnen koppelen aan alg key
                            : " <" + L("lcl_ins_unknown") + ">"               // bindingBits bevat geen geldige bind-bit
                            )
                          );
    }

    var oms_o = getPlaceholder(upd_delenenonderdelen);
    var sql_wis_dwgxy = ", ins_deel_dwgx = null"
                      + ", ins_deel_dwgy = null"
                      + ", ins_deel_dwgz = null"
    // Ik mag de objecten wijzigen dus voer wijzigingen uit.
    // Eerste de objecten die geen onderdeel zijn.
    if (upd_delen.length)
    {
      var sql_upd = "UPDATE ins_deel"
                  + "   SET ins_alg_ruimte_key = " + binding.multi.key
                  + "     , ins_alg_ruimte_type = " + safe.quoted_sql(binding.multi.typ)
                  + (wis_grafisch ? sql_wis_dwgxy : "")
                  + " WHERE ins_deel_key IN (" + upd_delen.join(",") + ")";
      var err = Oracle.Execute(sql_upd, true);
      if (err.friendlyMsg)
      {
          result.message = err.friendlyMsg;
      }
    }
    // Nu de onderdelen.
    if (upd_onderdelen.length)
    {
      // Als de nieuwe ins_alg_ruimte waarden gelijk zijn aan die van de parent dan NULL, anders de nieuwe waarden meegeven.
      sql_upd = "UPDATE ins_deel d1"
              + "   SET d1.ins_alg_ruimte_key = CASE"
              + "                               WHEN (SELECT d2.ins_alg_ruimte_key"
              + "                                       FROM ins_deel d2"
              + "                                      WHERE d2.ins_deel_key = d1.ins_deel_parent_key) = " + binding.multi.key
              + "                                AND (SELECT d3.ins_alg_ruimte_type"
              + "                                       FROM ins_deel d3"
              + "                                      WHERE d3.ins_deel_key = d1.ins_deel_parent_key) = " + safe.quoted_sql(binding.multi.typ)
              + "                               THEN NULL"
              + "                               ELSE " + binding.multi.key
              + "                               END"
              + "     , ins_alg_ruimte_type = CASE"
              + "                               WHEN (SELECT d2.ins_alg_ruimte_key"
              + "                                       FROM ins_deel d2"
              + "                                      WHERE d2.ins_deel_key = d1.ins_deel_parent_key) = " + binding.multi.key
              + "                                AND (SELECT d3.ins_alg_ruimte_type"
              + "                                       FROM ins_deel d3"
              + "                                      WHERE d3.ins_deel_key = d1.ins_deel_parent_key) = " + safe.quoted_sql(binding.multi.typ)
              + "                               THEN NULL"
              + "                               ELSE " + safe.quoted_sql(binding.multi.typ)
              + "                               END"
              + (wis_grafisch ? sql_wis_dwgxy : "")
              + " WHERE ins_deel_key IN (" + upd_onderdelen.join(",") + ")";
      err = Oracle.Execute(sql_upd, true);
      if (err.friendlyMsg)
      {
          result.message = err.friendlyMsg;
      }
    }

    var oms_n = getPlaceholder(upd_delenenonderdelen);
    for (var i=0; i<upd_delenenonderdelen.length; i++)
    {
        var oms = "verplaatst: " + oms_o[i] + " --> " + oms_n[i];
        ins.trackdeelupdate(upd_delenenonderdelen[i], oms);
    }
}

result.success = !result.message;
Response.Write(JSON.stringify(result));
%><% ASPPAGE_END(); %>
