<%@language = "javascript" %>
<% Server.ScriptTimeout=6000; %>
<% /*
     $Revision$
     $Id$

    File:           ins_srtcontrole_pc_save.asp
    Description:    Het indexeren van het periodieke taken prijzenboek.
    Parameters:     verschillende kosten en ctrdiscipline, discipline, srtgroup en srtdeel.
         costs           Kosten 1
         costs2          Kosten 2
         costs2          Kosten 3
         material        Materiaalkosten
         ctrdisc_key     Taakcategorie
         disc_key        Hoofdrecept
         srtgroup_key    Groeprecept
         srtdeel_key     Subrecept

    Context:        Vanuit ins_srtcontrole.js
    Note:           Resultaat van een submit is een json object

*/ %>

<%
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 ins_xcp_active_cost = S("ins_xcp_active_cost");
user.auth_required_or_abort(ins_xcp_active_cost > 0 && ins_xcp_active_cost < 16);

var pc_costs    = ins_xcp_active_cost & 1? getFParamFloat("costs", 0)    : 0; // Prijswijziging kosten.
var pc_costs2   = ins_xcp_active_cost & 2? getFParamFloat("costs2", 0)   : 0; // Prijswijziging kosten2.
var pc_costs3   = ins_xcp_active_cost & 4? getFParamFloat("costs3", 0)   : 0; // Prijswijziging kosten3.
var pc_material = ins_xcp_active_cost & 8? getFParamFloat("material", 0) : 0; // Prijswijziging materiaal.
var pc_adjust_xcp = getFParamInt("adjust_xcp", 0) == 1; // De afwijkingen van de standaardkosten (xcp) ook aanpassen?

var ctrdisc_key = getFParamInt("ctrdisc_key", -1); // Taakcategorie.
var disc_key = getFParamInt("disc_key", -1); // Hoofdrecept.
var srtgroup_key = getFParamInt("srtgroup_key", -1); // Groeprecept.
var srtdeel_key = getFParamInt("srtdeel_key", -1); // Subrecept.

// Dit scherm wordt aangeroepen vanuit het periodieke taken overzicht om de kosten van de taken aan te passen (in procenten).
// Bij periodiek taken wordt de kosten in de ins_srtcontrole tabel aangepast.
// Om periodieke taken te kunnen wijzigen heb je WEB_INSMGT rechten nodig.
var authparams = user.checkAutorisation("WEB_INSMGT");

if (pc_adjust_xcp)
{
  var authparamsINSUSE = user.checkAutorisation("WEB_INSUSE", true);
  var authparamsCTRUSE = user.checkAutorisation("WEB_CTRUSE", true);
  var authparamsINSMAN = user.checkAutorisation("WEB_INSMAN", true);
  var hasWriteINSUSE = authparamsINSUSE && authparamsINSUSE.ALGwritelevel < 9 && authparamsINSUSE.PRSwritelevel < 9;
  var hasWriteCTRUSE = authparamsCTRUSE && authparamsCTRUSE.ALGwritelevel < 9 && authparamsCTRUSE.PRSwritelevel < 9;
  var hasWriteINSMAN = authparamsINSMAN && authparamsINSMAN.ALGwritelevel < 9 && authparamsINSMAN.PRSwritelevel < 9;
  var hasWrite_MJOB = (hasWriteINSUSE || (S("ins_can_edit_own_objects") && objectBeheerder)) && hasWriteCTRUSE && hasWriteINSMAN;
}

var result =  { ctrdisc_key: ctrdisc_key, disc_key: disc_key, srtgroup_key: srtgroup_key, srtdeel_key: srtdeel_key };

if (pc_costs != 0 || pc_costs2 != 0 || pc_costs3 != 0 || pc_material != 0)
{
  var sql = "SELECT ins_srtcontrole_key"
          + "     , ins_srtcontrole_omschrijving"
          + "     , ins_srtcontrole_kosten"
          + "     , ins_srtcontrole_kosten2"
          + "     , ins_srtcontrole_kosten3"
          + "     , ins_srtcontrole_materiaal"
          + "  FROM ins_srtcontrole"
          + " WHERE ins_srtcontrole_key IN (SELECT ins_srtcontrole.ins_srtcontrole_key"
          + "                                 FROM ins_srtcontrole"
          + "                                    , ins_v_allsrtinstallatie"
          + "                                WHERE ins_srtcontrole.ins_srtinstallatie_key = ins_v_allsrtinstallatie.ins_srtinstallatie_key"
          + "                                  AND ins_srtcontrole.ins_srtcontrole_niveau = ins_v_allsrtinstallatie.niveau"
          + (ctrdisc_key > 0
            ? "                                AND ins_srtcontrole.ctr_discipline_key = " + ctrdisc_key
            : "")
          + (disc_key > 0
            ? "                                AND ins_v_allsrtinstallatie.ins_discipline_key = " + disc_key
            : "")
          + (srtgroup_key > 0
            ? "                                AND ins_v_allsrtinstallatie.ins_srtgroep_key = " + srtgroup_key
            : "")
          + (srtdeel_key > 0
            ? "                                AND ins_v_allsrtinstallatie.ins_srtdeel_key = " + srtdeel_key
            : "")
          + "                              )";
  var oRs = Oracle.Execute(sql);
  while (!oRs.eof)
  { // Periodieke taak indexeren.
    var srtcontrole_key = oRs("ins_srtcontrole_key").Value;
    var srtcontrole_oms = oRs("ins_srtcontrole_omschrijving").Value;
    var srtcontrole_kosten = oRs("ins_srtcontrole_kosten").Value;
    var srtcontrole_kosten2 = oRs("ins_srtcontrole_kosten2").Value;
    var srtcontrole_kosten3 = oRs("ins_srtcontrole_kosten3").Value;
    var srtcontrole_materiaal = oRs("ins_srtcontrole_materiaal").Value;

    var haveToUpdate = ((pc_costs != 0 && srtcontrole_kosten != null) ||
                        (pc_costs2 != 0 && srtcontrole_kosten2 != null) ||
                        (pc_costs3 != 0 && srtcontrole_kosten3 != null) ||
                        (pc_material != 0 && srtcontrole_materiaal != null));
    if (haveToUpdate)
    { // Huidige kosten zijn ingevuld en er is een indexatie ingevuld.
      // Als het veld NULL is blijft het veld deze waarde behouden (NULL * waarde = NULL).
      var kosten_val = (srtcontrole_kosten == null? null : Math.round(srtcontrole_kosten * (1 + (pc_costs/100)) * 100) / 100);
      var kosten_track = (Math.round(pc_costs * 100) / 100);
      var kosten2_val = (srtcontrole_kosten2 == null? null : Math.round(srtcontrole_kosten2 * (1 + (pc_costs2/100)) * 100) / 100);
      var kosten2_track = (Math.round(pc_costs2 * 100) / 100);
      var kosten3_val = (srtcontrole_kosten3 == null? null : Math.round(srtcontrole_kosten3 * (1 + (pc_costs3/100)) * 100) / 100);
      var kosten3_track = (Math.round(pc_costs3 * 100) / 100);
      var material_val = (srtcontrole_materiaal == null? null : Math.round(srtcontrole_materiaal * (1 + (pc_material/100)) * 100) / 100);
      var material_track = (Math.round(pc_material * 100) / 100);
      var fields = [ { dbs: "ins_srtcontrole_kosten",    typ: "float", val: kosten_val,
                                                                       track: L("ins_srtcontrole_kosten") + " (" + kosten_track + "%)" },
                     { dbs: "ins_srtcontrole_kosten2",   typ: "float", val: kosten2_val,
                                                                       track: L("ins_srtcontrole_kosten2") + " (" + kosten2_track + "%)" },
                     { dbs: "ins_srtcontrole_kosten3",   typ: "float", val: kosten3_val,
                                                                       track: L("ins_srtcontrole_kosten3") + " (" + kosten3_track + "%)" },
                     { dbs: "ins_srtcontrole_materiaal", typ: "float", val: material_val,
                                                                       track: L("ins_srtcontrole_materiaal") + " (" + material_track + "%)" }
                   ];

      var insUpd = buildTrackingUpdate("ins_srtcontrole", " ins_srtcontrole_key = " + srtcontrole_key, fields);
      var err = Oracle.Execute(insUpd.sql, true);

      if (err.friendlyMsg)
          abort_with_warning(err.friendlyMsg);

      if (insUpd && insUpd.trackarray.length)
        shared.trackaction("#CTRSUP", srtcontrole_key, L("lcl_ins_indexation_costs")
                                    + "\n" + insUpd.trackarray.join("\n"));

      if (pc_adjust_xcp && hasWrite_MJOB)
      { // De afwijkingen t.o.v. de standaard (srtcontrole_key) aanpassen.
        var sql_xcp = "SELECT xcp.ins_srtcontroledl_xcp_key"
                    + "     , xcp.ins_deel_key"
                    + "     , cd.ins_discipline_omschrijving"
                    + "     , isc.ins_srtcontrole_omschrijving"
                    + "     , xcp.ins_srtcontroledl_xcp_kosten"
                    + "     , xcp.ins_srtcontroledl_xcp_kosten2"
                    + "     , xcp.ins_srtcontroledl_xcp_kosten3"
                    + "     , xcp.ins_srtcontroledl_xcp_materia"
                    + "  FROM ins_srtcontroledl_xcp xcp"
                    + "     , ins_srtcontrole isc"
                    + "     , ctr_v_aanwezigdiscipline cd"
                    + " WHERE xcp.ins_srtcontrole_key = isc.ins_srtcontrole_key"
                    + "   AND isc.ctr_discipline_key = cd.ins_discipline_key"
                    + "   AND xcp.ins_srtcontrole_key = " + srtcontrole_key
                    + "   AND xcp.ins_scenario_key = 1"
                    + "   AND (xcp.ins_srtcontroledl_xcp_startdat IS NULL OR xcp.ins_srtcontroledl_xcp_startdat < SYSDATE)"
                    + "   AND (xcp.ins_srtcontroledl_xcp_eind IS NULL OR xcp.ins_srtcontroledl_xcp_eind > SYSDATE)";
        var oRs_xcp = Oracle.Execute(sql_xcp);
        while (!oRs_xcp.eof)
        { // De afwijkingen t.o.v. de standaard indexeren.
          // Het is een bestaand xcp record.
          var kosten   = ins_xcp_active_cost & 1? oRs_xcp("ins_srtcontroledl_xcp_kosten").Value  : 0; // Huidige kosten.
          var kosten2  = ins_xcp_active_cost & 2? oRs_xcp("ins_srtcontroledl_xcp_kosten2").Value : 0; // Huidige kosten2.
          var kosten3  = ins_xcp_active_cost & 4? oRs_xcp("ins_srtcontroledl_xcp_kosten3").Value : 0; // Huidige kosten3.
          var material = ins_xcp_active_cost & 8? oRs_xcp("ins_srtcontroledl_xcp_materia").Value : 0; // Huidige materiaal.

          // De afwijkingen van de standaardkosten (xcp).
          // Als het veld NULL is blijft het veld deze waarde behouden (NULL * waarde = NULL).
          kosten_val = (kosten == null? null : Math.round(kosten * (1 + (pc_costs/100)) * 100) / 100);
          kosten_track = (Math.round(pc_costs * 100) / 100);
          kosten2_val = (kosten2 == null? null : Math.round(kosten2 * (1 + (pc_costs2/100)) * 100) / 100);
          kosten2_track = (Math.round(pc_costs2 * 100) / 100);
          kosten3_val = (kosten3 == null? null : Math.round(kosten3 * (1 + (pc_costs3/100)) * 100) / 100);
          kosten3_track = (Math.round(pc_costs3 * 100) / 100);
          material_val = (material == null? null : Math.round(material * (1 + (pc_material/100)) * 100) / 100);
          material_track = (Math.round(pc_material * 100) / 100);
          fields = [ { dbs: "ins_srtcontroledl_xcp_kosten",    typ: "float", val: kosten_val,
                                                                             track: L("ins_srtcontrole_kosten") + " (" + kosten_track + "%)" },
                     { dbs: "ins_srtcontroledl_xcp_kosten2",   typ: "float", val: kosten2_val,
                                                                             track: L("ins_srtcontrole_kosten2") + " (" + kosten2_track + "%)" },
                     { dbs: "ins_srtcontroledl_xcp_kosten3",   typ: "float", val: kosten3_val,
                                                                             track: L("ins_srtcontrole_kosten3") + " (" + kosten3_track + "%)" },
                     { dbs: "ins_srtcontroledl_xcp_materia",   typ: "float", val: material_val,
                                                                             track: L("ins_srtcontrole_materiaal") + " (" + material_track + "%)" }
                   ];

          var xcp_key = oRs_xcp("ins_srtcontroledl_xcp_key").Value;
          var ins_key = oRs_xcp("ins_deel_key").Value;
          var ctrdisc_oms = oRs_xcp("ins_discipline_omschrijving").Value;
          var srtcontrole_oms = oRs_xcp("ins_srtcontrole_omschrijving").Value;
          var insUpd = buildTrackingUpdate("ins_srtcontroledl_xcp", " ins_srtcontroledl_xcp_key = " + xcp_key, fields);
          var err = Oracle.Execute(insUpd.sql, true);

          if (insUpd && insUpd.trackarray.length)
          {
            ins.trackdeelupdate(ins_key, L("lcl_ins_controle_obj_upd").format(ctrdisc_oms, srtcontrole_oms)
                                  + "\n" + insUpd.trackarray.join("\n"),
                                true); // Geen notificatie.
          }
          oRs_xcp.MoveNext();
        }
      }
    }
    oRs.MoveNext();
  }
  oRs.Close();
}
result.success = true;
result.toaster = L("lcl_mjb_price_change_completed");

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