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

     File:         ins_edit_srtdeel_save.asp
     Description:  SUBMIT-form

     TODO: tracking van sensortype-wijziging in tekst
*/ %>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="./ins.inc" -->
<!-- #include file="../shared/save2db.inc" -->
<!-- #include file="../Shared/kenmerk_common.inc" -->
<%
FCLTHeader.Requires({ plugins:["jQuery"],
                      js: []
                    });

var authparams = user.checkAutorisation("WEB_INSMGT");
user.auth_required_or_abort(authparams.ALGwritelevel < 9 && authparams.PRSwritelevel < 9);

var ins_score_enabled = S("ins_score_enabled") == 1;
var srtdeel_key = getQParamInt("srtdeel_key", -1);
var isNew = (srtdeel_key < 0);

var binding = (getFParam("bindR", "off") == "on" ? BIND_RUIMTE : 0) +
              (getFParam("bindW", "off") == "on" ? BIND_WERKPL : 0) +
              (getFParam("bindT", "off") == "on" ? BIND_TERREI : 0) +
              (getFParam("bindA", "off") == "on" ? BIND_AFDELI : 0) +
              (getFParam("bindP", "off") == "on" ? BIND_PERSOO : 0) +
              (getFParam("bindI", "off") == "on" ? BIND_INSDEE : 0);

var fields =  [ { dbs: "ins_srtgroep_key",            typ: "key",          frm: "srtgroep",            track: L("lcl_obj_group"),       foreign: "ins_srtgroep" },
                { dbs: "ins_srtdeel_code",            typ: "varchar",      frm: "srtcode",             track: L("lcl_obj_srtdeel_code") },
                { dbs: "ins_srtdeel_omschrijving",    typ: "varchar",      frm: "srtomschr",           track: L("lcl_descr") },
                { dbs: "ins_srtdeel_volgnr",          typ: "number",       frm: "srtdeel_volgnr",      track: L("lcl_ins_volgnr") },
                { dbs: "ins_srtdeel_eenheid",         typ: "varchar",      frm: "srtdeel_eenheid",     track: L("lcl_obj_unit") },
                //{ dbs: "ins_srtdeel_dimensie",        typ: "varchar",      frm: "srtdeel_dimensie",    track: L("lcl_ins_dimensie") },
                { dbs: "ins_srtdeel_acadsymbol",      typ: "varchar",      frm: "srtdeel_acadsymbol",  track: L("lcl_obj_symbol") },
                { dbs: "ins_srtdeel_acadsymbol_dwgx", typ: "float",        frm: "srtdeel_acadsymbol_dwgx", track: L("lcl_obj_symbol_dwgx") },
                { dbs: "ins_srtdeel_acadsymbol_dwgy", typ: "float",        frm: "srtdeel_acadsymbol_dwgy", track: L("lcl_obj_symbol_dwgy") },
                { dbs: "ins_srtdeel_acadlabelsize",   typ: "number",       frm: "srtdeel_acadlabelsize", track: L("lcl_srtdeel_acadlabelsize") },
                { dbs: "ins_srtdeel_acadlabelpos",    typ: "number",       frm: "srtdeel_acadlabelpos", track: L("lcl_srtdeel_acadlabelpos") },
                { dbs: "ins_srtdeel_uitleenbaar",     typ: "check",        frm: "srtdeel_uitleenbaar", track: L("lcl_ins_uitleenbaar") },
                { dbs: "ins_srtdeel_nr",              typ: "varchar",      frm: "srtdeel_nr",          track: L("lcl_ins_srtartikelnr") },
                { dbs: "prs_bedrijf_key",             typ: "key",          frm: "prs_bedrijf_key",     track: L("lcl_ins_leverancier"), foreign: "prs_bedrijf" },
                { dbs: "ins_srtdeel_vervaldatum",     typ: "date",         frm: "vervaldatum",         track: L("lcl_ins_vervaldatum") },
                { dbs: "ins_srtdeel_image",           typ: "varchar",      frm: "srtdeel_image",       track: L("lcl_ins_image") },
                { dbs: "ins_srtdeel_sensortype",      typ: "number",       frm: "srtdeel_sensortype",  track: L("lcl_ins_sensortype") },
                { dbs: "ins_srtdeel_statethreshold",  typ: "varchar",      frm: "statethreshold",      track: L("lcl_ins_srtdeel_statethreshold") },
                { dbs: "ins_srtdeel_statestepsize",   typ: "float",        frm: "statestepsize",       track: L("lcl_ins_srtdeel_statestepsize") },
                { dbs: "ins_srtdeel_stateanchor",     typ: "float",        frm: "stateanchor",         track: L("lcl_ins_srtdeel_stateanchor") },
                { dbs: "mld_stdmelding_key",          typ: "key",          frm: "stdm",                track: L("lcl_ins_srtdeel_statethresholdmld"), foreign: "mld_stdmelding" },
                { dbs: "ins_srtdeel_module",          typ: "varchar",      val: "INS" },
                { dbs: "ins_srtdeel_binding",         typ: "number",       val: binding,               track: L("lcl_obj_bind") }];

if (ins_score_enabled)
    fields.push({ dbs: "ins_srtdeel_mjb_grensscore1", typ: "float",        frm: "srtdeel_grensscore1", track: L("ins_srtdeel_mjb_grensscore1") });

var warning = "";
var insUpd = { trackarray: [] };
if (srtdeel_key > 0)
{
  insUpd = buildTrackingUpdate("ins_srtdeel", "ins_srtdeel_key = " + srtdeel_key, fields);
  var err = Oracle.Execute(insUpd.sql, true);
  if (err.friendlyMsg)
    warning = err.friendlyMsg;
}
else
{ // nieuw objectsoort
  fields.push({ dbs: "ins_srtdeel_key",            typ: "key",          seq: "ins_s_ins_srtinst_keys" });
  var regIns = buildInsert("ins_srtdeel", fields);
  srtdeel_key = regIns.sequences["ins_srtdeel_key"];
  var err = Oracle.Execute(regIns.sql, true);

  if (err.friendlyMsg)
    warning = err.friendlyMsg;

  ins.tracksrtdeelupdate(srtdeel_key, L("lcl_obj_sort_add"));
}

if (!warning)
{
  var currentKenmerkenSQL = "SELECT v.ins_kenmerk_key"
                          + "     , v.ins_kenmerkdeel_waarde"
                          + "     , ik.ins_kenmerk_toonbaar"
                          + "     , ik.fac_functie_key"
                          + "  FROM ins_kenmerkdeel v"
                          + "     , ins_kenmerk ik"
                          + " WHERE v.ins_kenmerkdeel_verwijder IS NULL"
                          + "   AND v.ins_kenmerk_key = ik.ins_kenmerk_key"
                          + "   AND ik.ins_kenmerk_bewerkniveau = 'S'"
                          + "   AND v.ins_deel_key = " + srtdeel_key; // herge/misbruik ins_deel_key

  var flextrack = saveFlexKenmerken(srtdeel_key,
                                    { kenmerkTable: "ins_kenmerkdeel",
                                      kenmerkParentKey : "ins_deel_key", // zal in de praktijk srtdeel_key zijn
                                      kenmerkWaarde: "ins_kenmerkdeel_waarde",
                                      kenmerkKey: "ins_kenmerk_key",
                                      kenmerkToonbaar: "ins_kenmerk_toonbaar",
                                      currentKenmerkenSQL: currentKenmerkenSQL,
                                      noFlexCleanup: true,
                                      requestQF: Request.Form,
                                      isNew: isNew,
                                      flexPath: "INS/S",
                                      module: "INS"
                                    });
}
else
    flextrack = [];

if ((insUpd.trackarray.length || flextrack.length))
{
  ins.tracksrtdeelupdate(srtdeel_key, L("lcl_obj_is_inssuptrack").format(srtdeel_key) + "\n" + insUpd.trackarray.concat(flextrack).join("\n"));
}
%>
<html>
  <head>
    <%  FCLTHeader.Generate() %>
    <script>
        $(function()
        {
          FcltMgr.closeDetail(window, { srtdeel_key: <%=srtdeel_key%>,
                                       warning: "<%=safe.jsstring(warning)%>",
                                       keepForm: <%=warning != ""? "true" : "false"%>,
                                       refresh: true
                                      });
        });
    </script>
  </head>
  <body>
  </body>
</html>
<% ASPPAGE_END(); %>
