<%@ language = "JavaScript" %>
<% /*
    $Revision$
    $Id$
    File:               mld_statusupdate.asp
    Description:        A deeplink call from Topdesk is handled to update the status of a complain.
    Parameters:
        opdrachtkey     Key of the mld_opdr                     -> Used by FACILITOR (rapport)
        opdrachtnr      Id of the mld_opdr (melding/volgnummer) -> Used by Topdesk/Deprecated?
        meldingnr       Id of the mld_melding (= key)           -> Used by Topdesk/Deprecated?
        externnr        Id used by Topdesk                      -> Used by Topdesk/Deprecated?
        mld_info        Logged in the complain (as opmerking)   -> Used by Topdesk/Deprecated?
                        or the order (as opmerking_fiat)        -> Used by FACILITOR (rapport)
        mld_einddatum   When complain is planned to be solved   -> Used by Topdesk/Deprecated?
        mld_status      Key of mld_statusses                    -> Used by both (a bit dirty)!
    Context:            Client specific file to handle calls from Topdesk or FACILITOR itself.
    Note:
*/ %>
<%
DOCTYPE_Disable = 1;
%>
<!--#include file="../../appl/Shared/common.inc"-->
<!--#include file="../../appl/Shared/json2.js"-->
<!--#include file="../../appl/Shared/funcodes.inc"-->
<%
  message = "";
  var externnr_kenmerk_key = 1577;
  var mld_opdr_key = -1;
  var mld_melding_key = -1;
  var opdrachtkey = getQParamInt("opdrachtkey", -1);
  var opdrachtnr = getQParam("opdrachtnr","");
  var meldingnr = getQParamInt("meldingnr", -1);
  var externnr = getQParam("externnr","");
  var mld_info = getQParam("mld_info","");
  var mld_einddatum = getQParam("mld_einddatum","");
  var mld_status = getQParamInt("mld_status", -1);

  if (opdrachtkey != "") {
    sql = " SELECT mld_melding_key FROM mld_opdr "
        + "  WHERE mld_opdr_key = " + opdrachtkey;
  }
  else if (opdrachtnr != "") { // DEPRECATED!
    if (externnr != "") {
      // vul kenmerk bij opdracht
      // bestaat het kenmerk?
      sql = " SELECT mld_kenmerkopdr_key FROM mld_kenmerkopdr ko, mld_opdr o "
          + "  WHERE ko.mld_kenmerk_key = " + externnr_kenmerk_key
          + "    AND ko.mld_opdr_key = o.mld_opdr_key "
          + "    AND o.mld_melding_key || '/' || o.mld_opdr_bedrijfopdr_volgnr = " + safe.quoted_sql(opdrachtnr);

      oRs = Oracle.Execute(sql);
      if (oRs.eof) {
        sql = " INSERT INTO mld_kenmerkopdr (mld_kenmerkopdr_waarde, mld_kenmerk_key, mld_opdr_key) "
            + " SELECT " + safe.quoted_sql(externnr) + ", " + externnr_kenmerk_key + ", mld_opdr_key FROM mld_opdr "
            + "  WHERE mld_melding_key || '/' || mld_opdr_bedrijfopdr_volgnr = " + safe.quoted_sql(opdrachtnr);
        Oracle.Execute(sql);
      }
    }
    sql = " SELECT mld_melding_key FROM mld_opdr "
        + "  WHERE mld_melding_key || '/' || mld_opdr_bedrijfopdr_volgnr = " + safe.quoted_sql(opdrachtnr);
  }
  else if (meldingnr != "") {  // DEPRECATED!
    sql = " SELECT mld_melding_key FROM mld_melding WHERE mld_melding_key = " + meldingnr;
  }

  oRs = Oracle.Execute(sql);
  if (!oRs.eof) {
    // meldingnummer is gevonden
    mld_melding_key = oRs(0).value;
  }

  // DEPRECATED!
  if (opdrachtkey == -1 && mld_info != "") {
    sql = " UPDATE mld_melding "
        + "    SET mld_melding_opmerking = DECODE (mld_melding_opmerking, NULL, " + safe.quoted_sql(mld_info) + ", " + safe.quoted_sql(mld_info) + " || '\n' || SUBSTR (mld_melding_opmerking , 1, (4000 - " + mld_info.length + "))) "
        + "  WHERE mld_melding_key = " + mld_melding_key
        + "    AND mld_melding_status IN (2, 3, 4, 7) ";
    Oracle.Execute(sql);
  }

  // DEPRECATED!
  if (opdrachtkey == -1 && mld_einddatum != "") {
    sql = " UPDATE mld_melding SET mld_melding_einddatum = fac.safe_to_date(" + safe.quoted_sql(mld_einddatum) + ", 'dd-mm-yyyy') "
        + "  WHERE mld_melding_key = " + mld_melding_key
        + "    AND mld_melding_status IN (4, 7) "
        + "    AND fac.safe_to_date (" + safe.quoted_sql(mld_einddatum) + ", 'dd-mm-yyyy') > mld_melding_einddatum";
    Oracle.Execute(sql);
  }

  if (opdrachtnr != "" && mld_status == 6) { // DEPRECATED!
    // meld opdracht af; de bovenliggende melding wordt afgemeld tijdens de db-procedure: rwsn_export_did_opdr!
    sql = " SELECT mld_opdr_key FROM mld_opdr "
        + "  WHERE mld_melding_key || '/' || mld_opdr_bedrijfopdr_volgnr = " + safe.quoted_sql(opdrachtnr);

    oRs = Oracle.Execute(sql);
    if (!oRs.eof) {
      // opdrachtnummer is gevonden
      mld_opdr_key = oRs(0).value;

      sql = "BEGIN mld.setopdrachtstatus("+ mld_opdr_key +", 6 , " + user_key + "); END;";
      Oracle.Execute(sql);
    }
  }
  else if (opdrachtkey != "" && mld_status == 1) {
    // melding afwijzen; ALLEEN door geautoriseerde medewerkers!
    if (func_enabled["WEB_USER15"]) {
    if (mld_info == "") {
      message = 'Bij afwijzen is het invoeren van een reden verplicht.';
    }
    else {
      //message = ' Applicatie aanvraag afgewezen door Beheerder.';

      //sql = " UPDATE mld_melding "
      //    + "    SET mld_melding_opmerking = DECODE (mld_melding_opmerking "
      //    + "                              , NULL "
      //    + "                              , TO_CHAR (SYSDATE, 'dd-mm-yyyy hh24:mi:ss') || " + safe.quoted_sql(message)
      //    + "                              , TO_CHAR (SYSDATE, 'dd-mm-yyyy hh24:mi:ss') || " + safe.quoted_sql(message) + " || '\n' || SUBSTR (mld_melding_opmerking , 1, (4000 - " + message.length + "))) "
      //    + "  WHERE mld_melding_key = " + mld_melding_key;
      sql = " UPDATE mld_opdr "
          + "    SET mld_opdr_opmerking_fiat = DECODE (mld_opdr_opmerking_fiat, NULL, " + safe.quoted_sql(mld_info) + ", " + safe.quoted_sql(mld_info) + " || '\n' || SUBSTR (mld_opdr_opmerking_fiat , 1, (1024 - " + mld_info.length + "))) "
          + "  WHERE mld_opdr_key = " + opdrachtkey;
      Oracle.Execute(sql);

      //sql = "BEGIN mld.setmeldingstatus("+ mld_melding_key +", 1, " + user_key + "); END;";
      //Oracle.Execute(sql);
      sql = "BEGIN mld.setopdrachtstatus("+ opdrachtkey +", 2, " + user_key + "); END;";
      Oracle.Execute(sql);

      sql = "BEGIN mld.setmeldingstatus("+ mld_melding_key +", 0, " + user_key + "); END;";
      Oracle.Execute(sql);

      message = 'Applicatie aanvraag afgewezen.';
    }
    }
    else {
      message = L("lcl_no_auth");
    }

    result = { success : true, message : message }

    Response.Write(JSON.stringify(result));
  }
  else if (opdrachtkey != "" && mld_status == 4) {
    // melding accepteren; ALLEEN door geautoriseerde medewerkers!
    if (func_enabled["WEB_USER15"]) {
      //message = ' Applicatie aanvraag goedgekeurd door Beheerder.';

      //sql = " UPDATE mld_melding "
      //    + "    SET mld_melding_opmerking = DECODE (mld_melding_opmerking "
      //    + "                              , NULL "
      //    + "                              , TO_CHAR (SYSDATE, 'dd-mm-yyyy hh24:mi:ss') || " + safe.quoted_sql(message)
      //    + "                              , TO_CHAR (SYSDATE, 'dd-mm-yyyy hh24:mi:ss') || " + safe.quoted_sql(message) + " || '\n' || SUBSTR (mld_melding_opmerking , 1, (4000 - " + message.length + "))) "
      //    + "  WHERE mld_melding_key = " + mld_melding_key;
      //Oracle.Execute(sql);

      //sql = "BEGIN mld.setmeldingstatus("+ mld_melding_key +", 4, " + user_key + "); END;";
      //Oracle.Execute(sql);
      sql = "BEGIN mld.setopdrachtstatus("+ opdrachtkey +", 5, " + user_key + "); END;";
      Oracle.Execute(sql);

      sql = "BEGIN mld.setmeldingstatus("+ mld_melding_key +", 0, " + user_key + "); END;";
      Oracle.Execute(sql);

      message = 'Applicatie aanvraag goedgekeurd.';
    }

    result = { success : true, message : message }

    Response.Write(JSON.stringify(result));
  }
  oRs.Close();
%>
