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

    File:           order_new.asp
    Description:    Maak een nieuwe opdracht aan bij een melding
    Parameters:     mld_key
*/ %>

<% REGISTERMENU_Disable = true; %>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../mld/mld.inc" -->
<!-- #include file="../Shared/Suggest/stdmeldingFilter.inc" -->
<!-- #include file="../Shared/Suggest/uitvoerendeFilter.inc" -->
<!-- #include file="./resultset_table.inc" -->
<!-- #include file="./mobile.inc" -->
<!-- #include file="./iface.inc" -->

<%
var mld_key    = getQParamInt("mld_key");
var opdr_key   = getQParamInt("opdr_key",-1);  // -1 betekent nieuwe
var opdr_type  = getQParamInt("opdr_type", -1);
var dienst_key = getQParamInt("dienst_key", -1);
var uitv_key   = getQParamInt("uitv_key", -1);
var qrc        = getQParamInt("qrc", 0) != 0;

autfunction = ("WEB_MLDBOF");
var authparams = user.checkAutorisation(autfunction);

var transitParam = buildTransitParam(["mld_key", "opdr_key", "opdr_type", "dienst_key", "uitv_key"]);
//
// Bepaal eerst disckey en srtdisckey
var sql = "SELECT s.mld_ins_discipline_key"
        + "     , md.ins_srtdiscipline_key"
        + "     , s.mld_typeopdr_key"
        + "     , s.mld_stdmelding_opdrtypevast"
        + "," + lcl.xsqla("t.mld_typeopdr_omschrijving", "t.mld_typeopdr_key")
        + "  FROM mld_melding m"
        + "     , mld_discipline md"
        + "     , mld_stdmelding s"
        + "     , mld_typeopdr t"
        + " WHERE s.mld_ins_discipline_key = md.ins_discipline_key"
        + "   AND s.mld_typeopdr_key = t.mld_typeopdr_key (+)"
        + "   AND m.mld_melding_key = " + mld_key
        + "   AND m.mld_stdmelding_key = s.mld_stdmelding_key";
var oRs = Oracle.Execute(sql);
var disckey       = oRs("mld_ins_discipline_key").Value;
var srtdisckey    = oRs("ins_srtdiscipline_key").Value;
var opdr_type_def = oRs("mld_typeopdr_key").Value || -1; // eventuele default
var opdr_type_oms = oRs("mld_typeopdr_omschrijving").Value;
var stdmld_vast   = oRs("mld_stdmelding_opdrtypevast").Value;
oRs.Close();
//
// Wat is de plaats waarop de melding betrekking heeft?
var sql = "SELECT mld_alg_locatie_key"
        + "     , mld_alg_onroerendgoed_keys"
        + "  FROM mld_melding"
        + " WHERE mld_melding_key = " + mld_key;
oRs = Oracle.Execute(sql);
var loc_key = oRs("mld_alg_locatie_key").Value || -1;
var onrgoed_key = oRs("mld_alg_onroerendgoed_keys").Value || -1;
oRs.Close();

// Haal gerelateerde melding gegevens op
      sql = "SELECT s.mld_stdmelding_key"
          + ",      s.prs_dienst_key"
          + ",      s.mld_ins_discipline_key"
          + ",      md.ins_srtdiscipline_key"
          + ",      m.mld_melding_ordernr"
          + "  FROM mld_melding m"
          + ",      mld_stdmelding s"
          + ",      mld_discipline md"
          + " WHERE m.mld_melding_key = " + mld_key
          + "   AND s.mld_ins_discipline_key = md.ins_discipline_key"
          + "   AND m.mld_stdmelding_key = s.mld_stdmelding_key";
oRs = Oracle.Execute(sql);
dienst_key = oRs("prs_dienst_key").Value || dienst_key; // default volgens stdm
if (dienst_key > -1)
  transitParam += "&dienst_key="+dienst_key;






var curPage = 0;
var rSql = "";
//
  if ((opdr_key == -1) && (opdr_type == -1) ) //&& (dienst_key == -1))
  { // OPDRACHTTYPE
    curPage = 1;
    var buttons = [];

    if (stdmld_vast == 1)
    {
      opdr_type = opdr_type_def;
      transitParam += "&opdr_type="+opdr_type;
      rSql = "";
    }
    else
    {
      myTOsql = "SELECT t.mld_typeopdr_key"   /* markeer de specifiekste met een * */
              + "     , CASE WHEN ins_discipline_key IS NULL THEN '' ELSE '*' END || " + lcl.xsqla("t.mld_typeopdr_omschrijving", "t.mld_typeopdr_key")
              + "     , t.mld_typeopdr_kosten kosten"
              + " FROM mld_typeopdr t"
              + "    , mld_typeopdr_srtdiscipline ts"
              + " WHERE t.mld_typeopdr_key = ts.mld_typeopdr_key"
              + "   AND t.mld_typeopdr_isofferte = 0" /* mobiel ondersteund vooralsnog geen offerte */
              + "   AND (ins_discipline_key = " + disckey /* en dan doet de srtdiscipline er echt niet meer toe */
              + "     OR (ts.ins_srtdiscipline_key = " + srtdisckey + " AND ins_discipline_key IS NULL))"
              + " ORDER BY 2";
      oRs = Oracle.Execute(myTOsql);
      while (!oRs.eof)
      {
        var btn =  { title: oRs("mld_typeopdr_omschrijving").Value
                   , action: oRs("mld_typeopdr_key").Value
                   };
        if (oRs("mld_typeopdr_key").Value == opdr_type_def)
        {
          buttons.unshift(btn); // vooraan
        }
        else
          buttons.push(btn);
        oRs.MoveNext();
      }
      oRs.Close();

      if (buttons.length == 0)
      {
        // Geef een waarschuwing op het scherm.
        rSql = "";
      }
      else
      {
        rSql = "SELECT " + buttons[0].action + " opdr_type"
             + ", " + safe.quoted_sql(buttons[0].title) + " typeopdr_omschr"
             + " FROM DUAL";
        if (buttons.length == 1)
        {
          var oRs = Oracle.Execute(rSql);
          var opdr_type = oRs("opdr_type").Value;
          oRs.Close();
          transitParam += "&opdr_type="+opdr_type;
        }
        else
        {
          var i = 1;
          while (i < buttons.length)
          {
            rSql = rSql + " UNION ALL "
                 + "SELECT " + buttons[i].action + ", " + safe.quoted_sql(buttons[i].title) + " FROM DUAL";
            i++;
          }
        }
      }
    }
  };

  if ((opdr_key == -1) && (opdr_type != -1) && (dienst_key == -1))
  { // DIENST
    curPage = 2;
    // Eerst kijken of dit scherm wel getoond moet worden: meer dan 1 dienst aanwezig.
    myTOsql = "SELECT count(*) aantal FROM prs_dienst"
    var oRs = Oracle.Execute(myTOsql);
    var aantalDienst = oRs("aantal").Value;
    oRs.Close();
    if (aantalDienst == 0)
    {
      // geen dienst
      dienst_key = 0;
      transitParam += "&dienst_key="+dienst_key;
      rSql = "";
    }
    else
    {
      // 1 of meer diensten aanwezig, dus gebruiker moet kiezen (of alle).
      rSql = "SELECT 0 prs_dienst_key"
           + " ," + safe.quoted_sql(L("lcl_all")) + " prs_dienst_omschrijving"
           + "  FROM DUAL"
           + " UNION ALL "
           + "SELECT *"
           + "  FROM (SELECT prs_dienst_key"
                   + "," +lcl.xsqla('prs_dienst_omschrijving', 'prs_dienst_key')
                   + "  FROM prs_dienst pd"
                   + " WHERE EXISTS (SELECT 1"
                   + "         FROM prs_bedrijfdienstlocatie bdl"
                   + "        WHERE bdl.prs_dienst_key = pd.prs_dienst_key)"
                   + " ORDER BY 2"
                   + ")";
    }
  };
__Log("curpage==>3:"+opdr_key+":"+opdr_type+":"+dienst_key);

  if ((opdr_key == -1) && (opdr_type != -1) && (dienst_key > -1))
  { // UITVOERENDE
    curPage = 3;
__Log("curpage="+curPage);
    var params = { chars: "*"
                 , autlevel: authparams.PRSwritelevel
                 , bAll: true
                 , dienst: (dienst_key > 0 ? dienst_key : -1)
                 , onrgoed_key: onrgoed_key
                 , loc_key: loc_key
                 , bld_key: -1
                 , disc_key: disckey
                 , lutype: "U"
                 , filtcode: ""
                 , externonly: false
                 , exhandler: false
                 };
    var filtClause = getFiltClauseUitvoerende(params.filtcode, params);
    var rSql = mld.suggest_uitvoerende_sql(filtClause, params);
    myTOsql = "SELECT count(*) aantal FROM ("+ rSql + ")";
    var oRs = Oracle.Execute(myTOsql);
    var aantalUitvoerenden = oRs("aantal").Value;
    oRs.Close();
__Log("#uitv="+aantalUitvoerenden);
    if (aantalUitvoerenden == 1)
    {
      var oRs = Oracle.Execute(rSql);
      uitv_key = oRs("uitv_key").Value;
      oRs.Close();
      transitParam = "?mld_key="+mld_key + "&x=1"
                   + "&opdr_type="+opdr_type
                   + "&dienst_key="+dienst_key
                   + "&uitv_key="+uitv_key;
      // Alles is nu bekend, dus terug naar order.asp
      Response.Redirect("order.asp"+transitParam);
    }
  };
%>
<html>
  <head>
<% FCLTMHeader.Generate({title:  L("lcl_mobile_newopdracht") });
%>
    <script type="text/javascript">
        function fn_opdr_type(opdr_type)
        {
          window.location.href = "order_new.asp?opdr_type=" + opdr_type   + "<%=safe.jsstring(transitParam)%>";
        }
        function fn_opdr_dienst(dienst_key)
        {
          window.location.href = "order_new.asp?dienst_key=" + dienst_key + "<%=safe.jsstring(transitParam)%>";
        }
        function fn_opdr_uitv(uitv_key)
        {
          window.location.href = "order.asp?uitv_key=" + uitv_key         + "<%=safe.jsstring(transitParam)%>";
        }
    </script>
  </head>
  <body>
<%
  if (curPage == 1)
  {
    if (rSql == "")
    {
       %><script>alert(L("lcl_mobile_no_stdmelding"));window.history.back(1)</script><%
    }
    else
    {
    PAGE_START({id: "page-1-"+opdr_key});
      HEADER({title: L("lcl_mld_choose_typeodr"), back:!qrc });
      CONTENT_START();
          function fncol1Link(oRs)
          {
            var nurl = "javascript:fn_opdr_type("+oRs("opdr_type").Value+")";
            return nurl;
          };

          var rst = new ResultsetTable({ sql: rSql
                                       , keyColumn: "opdr_type"
                                       , linkColumn: fncol1Link
                                       , headerColumn: "typeopdr_omschr"
                                       , ID: "ord_type_m"
                                       , showAll: true
                                       , noSearch: true
                                       , emptySetString: L("lcl_empty")
                                       });
          var cnt = rst.processResultset();
      CONTENT_END();
      FOOTER();
    PAGE_END();
    }
  };

  if (curPage == 2)
  {
    if (rSql != "")
    {
    PAGE_START({id: "page-2-"+opdr_key})
      HEADER({title: L("lcl_select_dienst_filter") , back:!qrc });
      CONTENT_START();
        function fncol2Link(oRs)
        {
          var nurl = "javascript:fn_opdr_dienst("+oRs("prs_dienst_key").Value+")";
          return nurl;
        };

        var rst = new ResultsetTable({ sql: rSql
                                     , keyColumn: "prs_dienst_key"
                                     , linkColumn: fncol2Link
                                     , headerColumn: "prs_dienst_omschrijving"
                                     , ID: "ord_dienst_m"
                                     , showAll: true
                                     , noSearch: true
                                     , emptySetString: L("lcl_empty")
                                     });
        var cnt = rst.processResultset();
      CONTENT_END();
    PAGE_END();
    }
  };

  if (curPage == 3)
  {
    if (rSql != "")
    {
    PAGE_START({id: "page-3-"+opdr_key})
      HEADER({title: L('lcl_select_uitvoerende') , back:!qrc });
      CONTENT_START();
        function fncol3Link(oRs)
        {
          var nurl = "javascript:fn_opdr_uitv("+oRs("uitv_key").Value+")";
          return nurl;
        };
        var rst = new ResultsetTable({ sql: rSql
                                     , keyColumn: "uitv_key"
                                     , linkColumn: fncol3Link
                                     , headerColumn: "naam"
                                     , ID: "ord_uitv_m"
                                     , showAll: true
                                     , noSearch: false
                                     , emptySetString: L("lcl_empty")
                                     });
        var cnt = rst.processResultset();
      CONTENT_END();
    PAGE_END();
    }
  }
PDA_PAGE_END(); %>
	</body>
</html>
<% ASPPAGE_END(); %>
