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

    File:               deelwerk_materiaal_save.asp
    Description:        Sla de wijzigingen van formulier splace_activities.asp op.
                        Opslaan urenregels van de haltewerkzaamheden.
    Parameters:
        day
        place
        stopplace       ...and all other splace_activities.asp form u2 fields
    Context:            POST formulier vanuit splace_activities.asp
    Note:
*/ %>

<!-- #include file="../../../appl/shared/common.inc" -->

<% FCLTHeader.Set({root: "../../../appl/"}); %>

<!-- #include file="../../../appl/Shared/iface.inc" -->
<!-- #include file="../../../appl/Shared/json2.js" -->

<%
FCLTHeader.Requires({ plugins: [],
                      js:[]
                    })

// AUTORISATIEPARAMETERS
var urole = getFParam("urole", "fe");
var fronto = urole == "fo";
var backo  = urole == "bo";
var minfo  = urole == "mi";
var frontend = (!fronto & !backo & !minfo);

var weekdate = getFParamInt("deelwerkdate");
wdate = new Date(weekdate);
var ora_date = wdate.beginToSQL();
var prs_key = getFParamInt("person");

var activity = getFParamInt("activity");
var stopplace = getFParamInt("stopplace");
var spendtime = getFParam("spendtime");
spendtime = spendtime.split(":");
var hour = spendtime[0];
var minutes = spendtime[1];
var principal = getFParamInt("principal");
var km = getFParamInt("km", 0);
var opmerk = getFParam("opmerk", "");

var materialkeystring = getFParam("matkeystr");
var materialkeystr = materialkeystring.split(",");

// Opslaan urenlijst van deze dag
oRs = Oracle.Execute("SELECT mld_s_mld_deelwerk_key.nextval FROM DUAL");
deelwerk_key = parseInt(oRs(0).value,10);
oRs.close();

sql = "INSERT INTO mld_deelwerk ("
    + "mld_deelwerk_key"
    + ", prs_perslid_key"
    + ", mld_deelwerk_datum"
    + ", mld_deelwerk_duur"
    + ", ins_deel_key"
    + ", fac_usrdata_key_activiteit"
    + ", fac_usrdata_key_opdrachtgever"
    + ", mld_deelwerk_km"
    + ", mld_deelwerk_opmerking"
    + ") VALUES ("
    + deelwerk_key
    + ", " + prs_key
    + ", " + ora_date
    + ", " + (parseInt(hour, 10) * 60 + parseInt(minutes, 10)) // save in minutes
    + ", " + stopplace
    + ", " + activity
    + ", " + principal
    + ", " + km
    + ", " + safe.quoted_sql(opmerk, 100)
    + ")";

oRs = Oracle.Execute(sql);

for (var j = 0; j < materialkeystr.length; j++)
{
  if (materialkeystr[j] != "")
  {
    sql = "INSERT INTO mld_deelwerkmateriaal ("
        + "mld_deelwerk_key"
        + ", fac_usrdata_key_materiaal"
        + ", mld_deelwerkmateriaal_aantal"
        + ") VALUES ("
        + deelwerk_key
        + ", " + materialkeystr[j]
        + ", " + getFParamInt("amount" + materialkeystr[j]) // materialamountstr[j]
        + ")";
    Oracle.Execute(sql);
  }
}

var doneParams = { prs_key : prs_key, deelwerkdate: weekdate, warning: "" };
%>

<html>
  <head>
    <%  FCLTHeader.Generate() %>
    <script>
      var doneParams = eval('(<%=safe.jsstring(JSON.stringify(doneParams))%>)')
      $(document).ready(function() { FcltMgr.closeDetail(window, doneParams) } );
    </script>
  </head>
  <body>
  </body>
</html>
