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

     File:         job_edit_job_save.asp
     Description:  SUBMIT-form
*/
var JSON_Result = true;
%>

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

<%
var job_key = getQParamInt("job_key", -1);

var isTemplate = getFParamInt("template", 0);
var rtable = getQParamInt("rtable");
var unit = getFParamInt("unit", -1);
if (unit === -1) {
  unit = null;
}
var periode = getFParamInt("periode", 0);
var bits  = getFParamInt("bits", 0);
var rkey = getQParamInt("rkey", -1);
var action = getFParamInt("action", -1);
var mkey = -1;

var rtablename;
switch (rtable)
{
  case 1: rtablename = "bestelling"; break;
  case 2: rtablename = "melding"; break;
  case 3: rtablename = "opdracht"; break;
  case 4: rtablename = "melding"; break;
}
var this_job = job.func_enabled_job(job_key, { "module": rtablename, "template": isTemplate });
user.auth_required_or_abort(this_job.canWrite);

var fields =  [ { dbs: "prs_perslid_key",             typ: "key",     frm: "person",    track: L("lcl_requestor"), foreign: "PRS_PERSLID" },
                { dbs: "fac_activiteit_xmlnode",      typ: "varchar", val: rtablename },
                { dbs: "fac_activiteit_status_key",   typ: "number",  frm: "status",    track: L("lcl_job_status") },
                { dbs: "fac_activiteit_omschrijving", typ: "varchar", frm: "act_descr", track: L("lcl_descr") },
                { dbs: "fac_activiteit_eerste",       typ: "date",    frm: "firstdate", track: L("lcl_job_firstdate") }];

if (isTemplate)
{
    fields.push({ dbs: "fac_activiteit_eenheid",     typ: "number",     val: "5",         track: L("lcl_job_unit") });
    fields.push({ dbs: "fac_activiteit_periode",     typ: "number",     val: "1",         track: L("lcl_job_period") });
}
else
{
  if (action == 1)
  { // eenmalig
    fields.push({ dbs: "fac_activiteit_eenheid",     typ: "number",     val: unit,        track: L("lcl_job_unit") });
    fields.push({ dbs: "fac_activiteit_periode",     typ: "number",     val: "1",         track: L("lcl_job_period") });
    fields.push({ dbs: "fac_activiteit_bits",        typ: "number",     val: "" });
    fields.push({ dbs: "fac_activiteit_einddatum",   typ: "date",       val: "",          track: L("lcl_job_enddate") });
    fields.push({ dbs: "fac_activiteit_aantal",      typ: "number",     val: "1",         track: L("lcl_job_n_times") });
  }
  else if (action == 2)
  { // Tot einddatum
    fields.push({ dbs: "fac_activiteit_eenheid",     typ: "number",     val: unit,        track: L("lcl_job_unit") });
    fields.push({ dbs: "fac_activiteit_periode",     typ: "number",     val: periode,     track: L("lcl_job_period") });
    fields.push({ dbs: "fac_activiteit_bits",        typ: "number",     val: bits });
    fields.push({ dbs: "fac_activiteit_einddatum",   typ: "date",       frm: "enddate",   track: L("lcl_job_enddate") });
    fields.push({ dbs: "fac_activiteit_aantal",      typ: "number",     val: "",          track: L("lcl_job_n_times") });
  }
  else if (action == 3)
  { // Aantal keer
    fields.push({ dbs: "fac_activiteit_eenheid",     typ: "number",     val: unit,        track: L("lcl_job_unit") });
    fields.push({ dbs: "fac_activiteit_periode",     typ: "number",     val: periode,     track: L("lcl_job_period") });
    fields.push({ dbs: "fac_activiteit_bits",        typ: "number",     val: bits });
    fields.push({ dbs: "fac_activiteit_einddatum",   typ: "date",       val: "",          track: L("lcl_job_enddate") });
    fields.push({ dbs: "fac_activiteit_aantal",      typ: "number",     frm: "aantal",    track: L("lcl_job_n_times") });
  }
}

var warning = "";
if (job_key > 0)
{
  var actUpd = buildTrackingUpdate("fac_activiteit", " fac_activiteit_key = " + job_key, fields);
  var err = Oracle.Execute(actUpd.sql, true);

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

  if (actUpd.trackarray.length)
    shared.trackaction("ACTUPD", job_key, actUpd.trackarray.join("\n") );

}
else
{
  fields.push({ dbs: "fac_activiteit_key",         typ: "key",      seq: "fac_s_fac_activiteit_key" });
  fields.push({ dbs: "fac_activiteit_user_key",    typ: "key",      val: user_key });
  var regIns = buildInsert("fac_activiteit", fields);
  var job_key = regIns.sequences["fac_activiteit_key"];
  sql = regIns.sql;
  var err = Oracle.Execute(sql, true);
  if (err.friendlyMsg)
    warning = err.friendlyMsg;
}

var result = {
    success: !warning,
    job_key: job_key,
    rtable: rtable,
    rkey: rkey,
    warning: safe.jsstring(warning),
    keepForm: !!warning
};

Response.Write(JSON.stringify(result));
Response.End();

ASPPAGE_END();
%>
