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

     File:               mld_cancelworkflow_save.asp
     Description:        Zet alle nog actieve meldingen in een workflow op afgewezen
     Parameters:         mld_key    key van de root melding
                         mld_oms    reden van afsluiten vervolgmeldingen

  */ %>
<%
var JSON_Result = true;
%>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="mld.inc" -->
<%
function workflow_tree(mld_key, org_mld_key)
{
    if (!org_mld_key)
        org_mld_key = mld_key;

    var sql = "SELECT mld_melding_key"
            + "     , mld_melding_status"
            + "  FROM mld_melding"
            + " WHERE mld_workflowstep_key IS NOT NULL"
            + "   AND mld_melding_start_key = " + mld_key;
    var oRs = Oracle.Execute(sql);

    while (!oRs.eof)
    {
        var next_mld_key = oRs("mld_melding_key").Value;
        var statusopdr = oRs("mld_melding_status").Value;
        if (inArray(statusopdr, [0,2,3,4,7]))
        {
            // Sluit deze actieve melding
            cancel_workflowstep(next_mld_key, org_mld_key);
        }
        // Als niet zichzelf dan volgend niveau
        if (next_mld_key != mld_key)
        {
            workflow_tree(next_mld_key, org_mld_key);
        }
        oRs.MoveNext();
    }
    oRs.Close();
}

function cancel_workflowstep(next_mld_key, org_mld_key)
{
    var sql_u = "UPDATE mld_melding"
              + "   SET mld_melding_status = 1"
              + " WHERE mld_melding_key = " + next_mld_key;
    Oracle.Execute(sql_u);
    var sql_i = "INSERT INTO mld_melding_note"
              + "( mld_melding_key"
              + ", prs_perslid_key"
              + ", mld_melding_note_omschrijving"
              + ") VALUES"
              + "(" + next_mld_key
              + "," + user_key
              + "," + safe.quoted_sql(mld_opmerking)
              + ")";
    Oracle.Execute(sql_i);

    // tracking: Vervolgmelding {0} is afgesloten.
    var melding_nr_next = mld.mld_prefix(next_mld_key) || next_mld_key;
    var melding_nr_org  = mld.mld_prefix(org_mld_key)  || org_mld_key;
    var msg = ( next_mld_key == org_mld_key ? "" : melding_nr_org);
    shared.trackaction("#MLDAFM", next_mld_key, L("lcl_mld_cancel_workflow_tracking").format(msg) );
}

    protectRequest.validateToken();
    var mld_root_key = getQParamInt("mld_key");
    var mld_opmerking = getFParam("descr");
    var autfunction = "WEB_MLDMSU";
    var mld_info = mld.mld_melding_info(mld_root_key);

    lcl.set_dialect(mld_info.srtdisc_key, "MLD_SRTDISCIPLINE_KEY");
    user.anything_todo_or_abort(mld_info.workflowRoot && user.has(autfunction) && S("mld_msu_can_stop_workflows"));

    workflow_tree(mld_root_key);

    var result = { key: mld_root_key
                 , toaster: L("lcl_mld_cancel_workflow_success")
                 , success: true
                 };
    Response.Write(JSON.stringify(result));
    Response.End;
%>
<% ASPPAGE_END(); %>
