<%@ LANGUAGE = JavaScript %>
<% /*
    $Revision$
    $Id$

    File:           ins_moveup.asp
    Description:    Doorschuiven van een mjob taak.

    Parameters:     key
    Context:        Vanuit show-schermen en of vanuit overzichten
    Result:         JSON object
    Note:

*/
var JSON_Result = true;
%>

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

<%
protectRequest.validateToken();

var ins_key_arr = getFParamKeyArray("ins_keys", []);
var srtcont_key_arr = getFParamKeyArray("srtcont_key_arr", []);
var dlsrtcont_key_arr = getFParamIntArray("dlsrtcont_key_arr", []); // De eerste "-1" waarde moeten niet verwijderd worden uit de array, daarom getFParamIntArray i.p.v. getFParamKeyArray.
var aantaltaken = getFParamInt("aantaltaken", 0);

var inflatiepct = S("mjb_inflation");
var inflatiefactor = 1 + (inflatiepct / 100);
var mjb_freeze_year = Math.max(S("mjb_freeze_year"), new Date().getFullYear());
var message = "";

var result = {};
var tobemovedup = 0;
var ingesloten = [];
// Bepaal de objecten in de selectie die ook echt doorgeschoven 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],
                                       {srtcont_key: srtcont_key_arr[i],
                                        deelsrtcont_key: dlsrtcont_key_arr[i]
                                       });
  if (this_ins.canInspMoveUp)
  {
    var sql = "SELECT cd.ins_discipline_omschrijving"
            + "  FROM ins_v_defined_inspect_xcp xcp"
            + "     , ctr_v_aanwezigdiscipline cd"
            + " WHERE xcp.ctr_discipline_key = cd.ins_discipline_key"
            + "   AND xcp.ins_deel_key = " + ins_key_arr[i]
            + "   AND xcp.ins_srtcontrole_key = " + srtcont_key_arr[i];
    var oRs = Oracle.Execute(sql);
    var ins_discipline_omschrijving = oRs("ins_discipline_omschrijving").Value;
    oRs.Close();

    ingesloten.push({ ins_key: ins_key_arr[i],
                      srtcont_key: srtcont_key_arr[i],
                      dlsrtcont_key: dlsrtcont_key_arr[i],
                      disc_oms: ins_discipline_omschrijving });
    tobemovedup++;
  }
}
// Hier geen "user.anything_todo_or_abort(tobemovedup > 0);". We geven altijd een melding terug hoeveel taken er terug gezet zijn.

var ins_xcp_active_cost = S("ins_xcp_active_cost");

result.success = true;
for (var i = 0; i < ingesloten.length; i++)
{
  var costarray = [];
  if (ins_xcp_active_cost & 1) costarray.push("COALESCE(ins_srtcontrole_kosten, 0)");
  if (ins_xcp_active_cost & 2) costarray.push("COALESCE(ins_srtcontrole_kosten2, 0)");
  if (ins_xcp_active_cost & 4) costarray.push("COALESCE(ins_srtcontrole_kosten3, 0)");
  if (ins_xcp_active_cost & 8) costarray.push("COALESCE(ins_srtcontrole_materiaal, 0)");
  var sqlcost = "";
  if (costarray.length > 0)
    sqlcost = "(" + costarray.join(" + ") + ") * ";
  var sql = "SELECT (ins_deel_aantal * "
          +         sqlcost
          + "       (COALESCE(ins_srtcontrole_percentage, 100) / 100) * "
          + "       POWER(" + inflatiefactor + " , " + mjb_freeze_year + " - EXTRACT(YEAR FROM SYSDATE))) kosten"
          + "  FROM ins_v_defined_inspect_xcp"
          + " WHERE ins_deel_key = " + ingesloten[i].ins_key
          + "   AND ins_srtcontrole_key = " + ingesloten[i].srtcont_key
          + "   AND ctr_ismjob = 1";
  var oRs = Oracle.Execute(sql);
  var kosten = oRs("kosten").Value;
  oRs.Close();

  var fields =  [ { dbs: "ins_deelsrtcontrole_freezecost", typ: "float",    val: kosten,                          track: L("lcl_ins_controle_freezecost") },
                  { dbs: "ins_deelsrtcontrole_freezedate", typ: "datetime", val: new Date(mjb_freeze_year, 0, 1), track: L("lcl_ins_controle_freezedate") } // Freezedate eerste dag van freeze year.
                ];
  // Inspectie (ins_deelsrtcontrole) record aanpassen.
  var insUpd = buildTrackingUpdate("ins_deelsrtcontrole", " ins_deelsrtcontrole_key = " + ingesloten[i].dlsrtcont_key, fields);

  // De aangepaste freeze velden opslaan.
  var err = Oracle.Execute(insUpd.sql, true);
  if (err.friendlyMsg)
  {
    result.message = err.friendlyMsg;
    result.success = false;
  }

  // Tracking van de wijziging.
  ins.trackinspectupdate(ingesloten[i].dlsrtcont_key, L("lcl_ins_controle_is_upd").format(ingesloten[i].disc_oms) + "\n" + insUpd.trackarray.join("\n"));
}

if (ingesloten.length < aantaltaken)
{ // Niet alle taken zijn doorgeschoven.
  result.message = ingesloten.length == 1? L("lcl_mjb_moveup_mes_1").format(aantaltaken) : L("lcl_mjb_moveup_mes_n").format(ingesloten.length, aantaltaken);
}
else
{ // Alle taken zijn doorgeschoven.
  result.toaster = L("lcl_mjb_moveup_completed");
}
Response.Write(JSON.stringify(result));
%><% ASPPAGE_END(); %>
