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

    File:               job_save.asp
    Description:

    Parameters:
    Context:            POST formulier vanuit job.asp
    Note:
        TODO: omschrijving saven (HOUT#15592]


*/ %>

<!--#include file="../Shared/common.inc"-->
<!--#include file="../Shared/iface.inc"-->
<!--#include file="../Shared/calendar.inc" -->

<%
protectRequest.validateToken();
FCLTHeader.Requires({ plugins:["suggest"] });
var urole = "fo"; // todo complete auth aanmaken voor de job schermen.

var act_key = getFParamInt("act_key",-1);

var act_key_new  = -1;
var activity  = getFParam("activity", "?");
var rkey      = getFParamInt("rkey", "-1");
var rtable    = getFParamInt("rtable", "-1");
var rtablename= '';
if (rtable == 1) rtablename = 'bestelling';
if (rtable == 2) rtablename = 'melding';
if (rtable == 3) rtablename = 'opdracht';
var mkey      = getFParamInt("mkey", "-1");
var dosave    = getFParamInt("dosave", "1");
var btn_ok    = getFParamInt("btn_ok", "0");
var sdisc     = getFParamInt("sdisc", "-1");

var person    = getFParamInt("person", "-1");
var status    = getFParamInt("status", "-1");
var interval  = getFParamInt("interval", "1");
if (isNaN(interval)) {
  interval = 0;
}
var aantal    = getFParamInt("aantal", 1);

var firstdate = getFParamDate("firstdate", null);
var enddate   = getFParamDate("enddate", null);
var ora_date1 = null;
var ora_date2 = null;

if (firstdate != null)
  ora_date1 = firstdate.toSQL(true);

if (enddate != null)
  ora_date2 = enddate.toSQL(true);

if (act_key != -1) {
  if (dosave == '1') {
    sql =   "UPDATE   fac_activiteit "
          + " SET     prs_perslid_key           = " + person  //1
          + "        ,fac_activiteit_xmlnode    = " + safe.quoted_sql(rtablename) //2
          + "        ,fac_activiteit_status_key = " + status //3
          + "        ,fac_activiteit_interval   = " + interval * 24 //4
          + "        ,fac_activiteit_eerste     = " + ora_date1 //5
          + "        ,fac_activiteit_einddatum  = " + ora_date2 //6
          + (!isNaN(aantal)? ",fac_activiteit_aantal = " + aantal : ",fac_activiteit_aantal = NULL ") //7
          + " WHERE   fac_activiteit_key        = " + act_key
          + " AND     fac_activiteit_volgende   IS NULL ";
    oRs = Oracle.Execute(sql);
  }
} else {
  sql = "SELECT fac_s_fac_activiteit_key.nextval FROM DUAL ";
  oRs = Oracle.Execute(sql);
  act_key_new = oRs(0).value;
  sql =   "INSERT INTO fac_activiteit ("
        + " fac_activiteit_key"  // x
        + ", fac_activiteit_xmlnode" // 2
        + ", fac_activiteit_interval" // 4
        + ", prs_perslid_key" // 1
        + ", fac_activiteit_status_key" // 3
        + (!isNaN(aantal)? ", fac_activiteit_aantal" : "") // 7
        + ", fac_activiteit_eerste" // 5
        + ", fac_activiteit_einddatum" // 6
        + ", fac_activiteit_user_key"
        + ") values ("
        + " " + act_key_new
        + ", " + safe.quoted_sql(rtablename)
        + ", " + interval*24
        + ", " + person
        + ", " + status
        + (!isNaN(aantal)? ", " + aantal : "")
        + ", " + ora_date1
        + ", " + ora_date2
        + ", " + user_key + ")";

  oRs = Oracle.Execute(sql);
}

%>
<html>
  <head>
<%  FCLTHeader.Generate() %>

    <script type="text/javascript">

      function onLoadAction()
      {
<%      if (dosave == '1') { %>
<%        if (act_key != -1) { %> // MOD
<%        } else { %> // ADD
            alert(L("lcl_job_nr") + ' ' + <%=act_key_new%> + ' is aangemaakt');
<%        } %>
<%      } %>

<%      if (btn_ok == '0') { // Alleen bewaren job
%>
<%        if (rkey >= 0) { // Aan de job is reeds een activiteit gekoppeld, toon deze activiteit
%>
          if ("<%=rtable%>" == "1")
            window.location.href = '../BES/bes.asp?urole=<%urole%>&RFO_key=<%=rkey%>&details=1&fronto=1&act_key=0';
          if ("<%=rtable%>" == "2")
            window.location.href = '../MLD/mld_edit_opdr.asp?urole=<%urole%>&backo=&mld_key=<%=rkey%>';
          if ("<%=rtable%>" == "3")
            window.location.href = '../MLD/mld_edit_opdr.asp?urole=<%urole%>&opdr=<%=rkey%>&backo=1';
<%        } else { // Aan de job is nog geen activiteit gekoppeld, maak deze nu aan
%>
          parent.setBtnAddOk('OK');
          if ("<%=rtable%>" == "1")
            window.location.href = '../BES/bes.asp?urole=<%urole%>&fronto=1&act_key=<%=(act_key_new == -1? act_key : act_key_new)%>';
          if ("<%=rtable%>" == "2")
            window.location.href = '../MLD/mld_edit.asp?urole=<%urole%>&srtdisc=<%=sdisc%>&fronto=1&act_key=<%=(act_key_new == -1? act_key : act_key_new)%>';
          if ("<%=rtable%>" == "3")
            window.location.href = '../MLD/mld_edit_opdr.asp?urole=<%urole%>&backo=1&add_pr=f&mldnr=<%=mkey%>&act_key=<%=(act_key_new == -1? act_key : act_key_new)%>';
<%        } %>
<%      } %>
      }
    </script>
  </head>

  <body onload="onLoadAction();">
  </body>
</html>
