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

     File:               bes_search.asp
     Description:        Filterscherm voor overzicht bestellingen
     Parameters:         urole                Rol van de gebruiker (frontend, frontoffice, backoffice, management info)
                         datefrom             Periode van
                         dateto               Periode tot
                         ord                  Via ORD binnengekomen zodat ook ORD rechten gepakt kan worden
                         disc                 Catalogus
                         srtgroup             Groep
                         srtdeel              Item (key)
                         supplier             Leverancier
                         groupby              Groepering 1
                         groupby2             Groepering 2
                         rfo_key              Aanvraag nummer (rfo key, request for order)
                         rfostatus_key_str    Aanvraag status string
                         order_key            Opdracht nummer
                         orderstatus_key_str  Opdracht status string
                         prs_key              Aanvrager
                         comp_key             Bedrijf
                         dep_key              Afdeling
                         kp                   Kostenplaats
                         item                 Item (string)
                         rfoNew               Status nieuw
                         rfoFiat              Status gefiatteerd
                         rfoOpen              Status open
                         rfoDelivered         Status geleverd
                         rfoClosed            Status archief
     Globals:
     Context:
     Note:               Merk op dat de kostenplaats er ook altijd bij wordt gezocht, handig met withExtraParamField
*/ %>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../Shared/selector.inc" -->
<!-- #include file="../Shared/calendar.inc" -->
<!-- #include file="../Shared/afdelingselector.inc" -->
<!-- #include file="../Shared/bedrijfselector.inc" -->
<!-- #include file="../Shared/kostenplaatsselector.inc" -->
<!-- #include file="../Shared/persoonselector.inc" -->
<!-- #include file="../Shared/disciplineselector.inc" -->
<!-- #include file="../Shared/srtgroepselector.inc" -->
<!-- #include file="../Shared/srtdeelselector.inc" -->
<!-- #include file="bes.inc" -->
<%
FCLTHeader.Requires({ plugins:["jQuery", "kenmerk"],
                      js: ["../bes/bes_search.js"]
                    });

/***** Get webform parameters *****/
var urole = getQParamSafe("urole");
var fronto = (urole == "fo");
var backo = (urole == "bo");
var minfo = (urole == "mi");
var frontend = (urole == "fe" || (!fronto && !backo && !minfo));

var datefrom = getQParamDate("date_from", null);
var dateto = getQParamDate("date_to", null);

if (minfo)
{ // Datums zijn alleen zichtbaar bij management info
  if (!datefrom && dateto)
    datefrom = dateto;
  else if (datefrom && !dateto)
    dateto = datefrom;
  else if (!datefrom && !dateto)
  {
    var dt = new Date(); // Vandaag
    dt = new Date(dt.getFullYear(), dt.getMonth(), 1); // Eerste dag van de huidige maand
    dateto = dt.setDate(0); // Laatste dag van de vorige maand
    datefrom = dt.setDate(1); // Eerste dag van de vorige maand

    var datefrom = new Date(parseInt(datefrom, 10));
    var dateto = new Date(parseInt(dateto, 10));
  }

  var date_from = datefrom;
  var date_to = dateto;
}

var ord = getQParamInt("ord", 0) == 1; // Via ORD binnengekomen
var disc = getQParamInt("disc", -1); // Catalogus
var srtgroup = getQParamInt("srtgroup", -1); // Groep
var srtdeel = getQParamInt("srtdeel", -1); // Item (key voor select box)
var supplier = getQParamInt("supplier", -1); // Leverancier
var groupby = getQParamInt("groupby", 0); // Groepering 1 (Aanvraag (key = 0) is default)
var groupby2 = getQParamInt("groupby2", -1); // Groepering 2
var rfo_key = getQParamInt("rfo_key", ""); // Aanvraag nummer (rfo key, request for order)
var rfostatus_key_arr = getQParamIntArray("rfostatus", [2,3,4,5,6,7]); // Aanvraag status string
var order_key = getQParamInt("order_key", ""); // Opdracht nummer
var orderstatus_key_arr = getQParamIntArray("orderstatus", []); // Opdracht status string
var prs_key = getQParamInt("prs_key", -1); // Aanvrager (Requestor_key)
var comp_key = getQParamInt("comp_key", -1); // Bedrijf
var dep_key = getQParamInt("dep_key", -1); // Afdeling
var kp = getQParam("kp", ""); // Kostenplaats (string)
var item = getQParam("item", ""); // item (string voor input box)

var rfoNew = getQParamInt("rfoNew", frontend? 1 : fronto? 1 : 0) == 1; // Status nieuw
var rfoFiat = getQParamInt("rfoFiat", frontend? 1 : fronto || backo? 1 : 0) == 1; // Status gefiatteerd
var rfoOpen = getQParamInt("rfoOpen", frontend? 1 : 0) == 1; // Status open
var rfoDelivered = getQParamInt("rfoDelivered", 0) == 1; // Status geleverd
var rfoClosed = getQParamInt("rfoClosed", 0) == 1; // Status archief
if (!rfoNew && !rfoFiat && !rfoOpen && !rfoDelivered && !rfoClosed)
  rfoNew = true;
/***** End get webform parameters *****/

var autfunction = "";
switch(urole)
{
  case "fe": autfunction = "WEB_BESUSE";
             break;
  case "fo": autfunction = "WEB_BESFOF";
             break;
  case "bo": autfunction = "WEB_BESBOF";
             break;
  case "mi": if (ord)
               autfunction = "WEB_BESBOR"; // Via ORD binnengekomen dan ook ORD rechten pakken
             else
               autfunction = "WEB_BESBAC"; // Via BES binnengekomen dan ook BES rechten pakken
             break;
}

var authparams = user.checkAutorisation(autfunction);

sql = "select count(*) aantal"
    + " from bes_v_aanwezigdiscipline bd, bes_disc_params bdp"
    + " where bdp.bes_ins_discipline_key = bd.ins_discipline_key"
    + " and bes_disc_params_for_others = 1"
oRs = Oracle.Execute(sql);
var has_bes_disc_params_for_others = (oRs("aantal").value != 0); // minimaal 1 discipline waarvoor "Bestelling voor" kan worden gebruikt.
oRs.Close();
%>

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

    <script type="text/javascript">

      function doSubmit()
      {
<%      if (backo)
        { %>
          // Submitten met kenmerken
          doSubmitWithKenmerken();
<%      }
        else
        { %>
          document.forms.u2.submit();
<%      } %>
      }

<%    if (backo)
      { %>
        async function myModal(init)
        {
// #65836 11.3 geavanceerd kunnen zoeken zonder eerst een catalogus te hebben gekozen.
// Dit kan dus nu weg.
//          if ($('#disc').val() == -1)
//          {
//            FcltMgr.alert(L("lcl_bes_nocat_selected"));
//            return;
//          }

          var key = $('#disc').val()
          var url = "load_kenmerk.asp?disc_key=" + $('#disc').val()
                  + "&urole=<%=urole%>"
                  + "<%=ord? '&ord=1' : ''%>"
                  + "&kenmerktype=B"
                  + "&advanced=1"
                  + "&hasFilter=1";
          var titel = L("lcl_bes_kenmerken");
          await showKenmerkModal(url, titel, init)
        }
<%    }
      if (frontend)
      { %>
        $(doSubmit);
<%    } %>

    </script>
  </head>

  <body id="searchbody">
      <form name="u2" target="workFrame" action="bes_search_list.asp" method="get">
        <% SEARCH_PAGE_START({ header: L("lcl_filterblok") });
        SEARCH_BLOCK_START();%>
        <input type="hidden" name="urole" value="<%=urole%>">
        <input type="hidden" name="user_key" value="<%=user_key%>">
<%
     if (ord) // via ORD binnengekomen dan ook ORD rechten pakken
        { %>
          <input type=hidden name=ord value="1">
<%      }
          if (!frontend)
            { // (groupby == 1 || groupby == 6): Opdracht groepering is geselecteerd
              // (groupby != 1 && groupby != 6): Aanvraag groepering is geselecteerd
%>            <!-- Aanvraag number / Opdrachtnummer -->
              <tr class="primsearch" id="rfoOrOrder">
                <td class="label"><label for="rfonum"><%=groupby == 1 || groupby == 6? L("lcl_ord_order_num") : L("lcl_bes_key")%></label></td>
                <td><input type="text" class="fldshort" name="rfonum" id="rfonum" value="<%=groupby == 1 || groupby == 6? order_key : rfo_key%>"></td>
              </tr>

<%
              if (minfo)
              { %>
                </tr>
                <script type="text/javascript">
                  function showRfoOrOrder()
                  {
                    if (($("#groupby").val() == 1 || $("#groupby").val() == 6)) // Opdracht groepering is geselecteerd en aanvraagnummer is weergegeven
                    { // Geef opdrachtnummer weer
                      $("label[for=rfonum]").html(L("lcl_ord_order_num"));
                    }
                    else if (($("#groupby").val() != 1 && $("#groupby").val() != 6)) // Aanvraag groepering is geselecteerd en opdrachtnummer is weergegeven
                    { // Geef aanvraagnummer weer
                      $("label[for=rfonum]").html(L("lcl_bes_key"));
                    }
                  }
                </script>
<%            } %>
<%          } %>

<%          if (minfo || backo)
            {
              // <!-- Datum van -->
              FCLTcalendar( "date_from",
                              { label : L("lcl_period_from"),
                                datum: date_from,
                                initEmpty: backo,
                                trclass: "primsearch",
                                volgnr: 1
                              });
              // <!-- Datum tot -->
              FCLTcalendar( "date_to",
                              { label: L("lcl_period_to"),
                                datum: date_to,
                                initEmpty: backo,
                                trclass: "primsearch",
                                volgnr: 2
                              });
            }

            if (!frontend)
            {
              // Catalogus
              FCLTdisciplineselector("disc",
                                     "sgDisc",
                                     { label: L("lcl_bes_Catalogus"),
                                       module: "BES",
                                       disc_key: disc,
                                       srtdisc_key: null,
                                       perslidKey: user_key,
                                       autfunctionKey: authparams.autfunctionkey,
                                       autRW: "R",
                                       trclass: "primsearch",
                                       onChange: "onChangeDiscipline",
                                       whenEmpty: L("lcl_search_generic")  // want filter
                                     });

              if (!minfo)
              {
                // <!-- Leverancier -->
                FCLTbedrijfselector("sSuppl_key", // supplier
                                    "sgSuppl",
                                    { companyKey: supplier,
                                      label: L("lcl_bes_Supplier"),
                                      trclass: "primsearch",
                                      filtercode: "BL",
                                      whenEmpty: L("lcl_search_generic")
                                    });
              }
              //Groep
              FCLTsrtgroepselector("srtgroep",
                                   "sgSrtgroep",
                                   { label: L("lcl_obj_group"),
                                     module: "BES",
                                     srtgroep_key: srtgroup,
                                     autfunction: autfunction,
                                     trclass: "secsearch",
                                     onChange: "onChangeSrtgroep",
                                     urlAdd: [{urlParam: "disc_key", field: "disc"}],
                                     whenEmpty: L("lcl_search_generic")  // want filter
                                   });
              // Item select box
              FCLTsrtdeelselector("srtdeel",
                                  "sgSrtdeel",
                                  { label: L("lcl_bes_srtdeel"),
                                    module: "BES",
                                    srtdeel_key: srtdeel,
                                    autfunction: autfunction,
                                    filtercode: "VERW",
                                    trclass: "secsearch",
                                    onChange: "onChangeSrtdeel",
                                    urlAdd: [{urlParam: "srtgroep_key", field: "srtgroep"},
                                             {urlParam: "disc_key", field: "disc"}],
                                    whenEmpty: L("lcl_search_generic")  // want dit is een filter
                                  });
            }

            if (!frontend)
            {
              if (fronto || backo)
              {
%>
                <!-- Item input box -->
                <tr class="secsearch">
                  <td class="label"><label for="item"><%=L("lcl_bes_srtdeel_zoek")%></label></td>
                  <td><input type="text" class="fld wildcard" name="item" value="<%=safe.htmlattr(item)%>">
                </tr>
<%
              }
            }

            if (minfo)
            { // <!-- Groepering1 -->
                sql = "SELECT 12, '', 0 FROM DUAL"
                    + " UNION SELECT  0, " + safe.qL("lcl_bes_RFOs")                                                    + ",  1 FROM DUAL"
                    + " UNION SELECT  3, " + safe.qL("lcl_bes_RFOs")     + "||' - '||" + safe.qL("lcl_bes_Catalogus")   + ",  2 FROM DUAL"
                    + " UNION SELECT  4, " + safe.qL("lcl_bes_RFOs")     + "||' - '||" + safe.qL("lcl_bes_srtgroup")    + ",  3 FROM DUAL"
                    + " UNION SELECT  5, " + safe.qL("lcl_bes_RFOs")     + "||' - '||" + safe.qL("lcl_bes_srtdeel")     + ",  4 FROM DUAL"
                    + " UNION SELECT  2, " + safe.qL("lcl_bes_RFOs")     + "||' - '||" + safe.qL("lcl_dep_name_level1") + ",  5 FROM DUAL"
                    + " UNION SELECT  7, " + safe.qL("lcl_bes_RFOs")     + "||' - '||" + safe.qL("lcl_dep_name_level2") + ",  6 FROM DUAL"
                    + " UNION SELECT  8, " + safe.qL("lcl_bes_RFOs")     + "||' - '||" + safe.qL("lcl_bes_ordernr")     + ",  7 FROM DUAL"
                    + " UNION SELECT  1, " + safe.qL("lcl_shared_order")                                                + ",  8 FROM DUAL"
                    + " UNION SELECT  6, " + safe.qL("lcl_shared_order") + "||' - '||" + safe.qL("lcl_bes_Supplier")    + ",  9 FROM DUAL"
                    + " UNION SELECT 10, " + safe.qL("lcl_district")                                                    + ", 10 FROM DUAL"
                    + " UNION SELECT 11, " + safe.qL("lcl_mi_location")                                                 + ", 11 FROM DUAL"
                    + " ORDER BY 3";
                FCLTselector("groupby",
                             sql,
                             { initKey: groupby,
                               label: L("lcl_bes_group"),
                               trclass:"primsearch",
                               onChange: 'showRfoOrOrder();'
                             });

              // <!-- Groepering2 -->
                sql = "SELECT 8, " + safe.qL("lcl_bes_RFOs") + "||' - '||" + safe.qL("lcl_bes_ordernr") + ", 2 FROM DUAL"
                    + " UNION SELECT 10, " + safe.qL("lcl_district") + ", 5 FROM DUAL"
                    + " UNION SELECT 11, " + safe.qL("lcl_mi_location") + ", 5 FROM DUAL"
                    + " ORDER BY 3";
                FCLTselector("groupby2",
                             sql,
                             { initKey: groupby2,
                               label: L("lcl_bes_group2"),
                               trclass:"secsearch",
                               emptyOption: ""
                             });
            }
 SEARCH_BLOCK_END();
 SEARCH_BLOCK_START();
            if (!frontend)
            {
                //  <!-- Aanvrager -->
                FCLTpersoonselector("sName_key", // requestor
                                    "sgPerson",
                                    { perslidKey: prs_key,
                                      label: L("lcl_rfo_requestor"),
                                      filtercode: "BES",
                                      trclass: "primsearch",
                                      autlevel: authparams.PRSreadlevel,
                                      whenEmpty: L("lcl_search_generic")  // want filter
                                    });
                // <!-- Bestelling voor -->
                if (has_bes_disc_params_for_others)
                  FCLTpersoonselector("personFor",
                                      "sgPersonFor",
                                      { label: L("lcl_bes_order_for"),
                                        filtercode: 'BESV',
                                        trclass: "primsearch",
                                        whenEmpty: L("lcl_search_generic")  // want filter
                                      });
            }
            if (minfo)
            {
                //<!-- Bedrijf -->
                FCLTbedrijfselector("sComp_key", // company
                                    "sgComp",
                                    { companyKey: comp_key,
                                      label: L("lcl_bes_company"),
                                      autlevel: authparams.PRSreadlevel, // Was voorheen niet aanwezig
                                      trclass: "secsearch",
                                      filtercode: "I",
                                      whenEmpty: L("lcl_search_generic")  // want filter
                                    });
            }
            if (!frontend)
            {
                // <!-- Organisatie -->
                FCLTafdelingselector("sDept_key", // organisation
                                     "sgDept",
                                     { departmentKey: dep_key,
                                       label: L("lcl_prs_organisatie"), // Nu Afdeling, was L("lcl_bes_organisation") => Organisatie
                                       autlevel: authparams.PRSreadlevel, // Was voorheen niet aanwezig
                                       trclass: "secsearch",
                                       whenEmpty: L("lcl_search_generic")  // want filter
                                     });

            }
            if (minfo)
            {
                // <!-- Leverancier -->
                FCLTbedrijfselector("sSuppl_key", // supplier
                                    "sgSuppl",
                                    { companyKey: supplier,
                                      label: L("lcl_bes_Supplier"),
                                      trclass: "secsearch",
                                      filtercode: "B",
                                      whenEmpty: L("lcl_search_generic")  // want filter
                                    });
            } %>

<%          if (!frontend)
            { %>
              <!-- Kostenplaats -->
              <tr class="secsearch">
                <td class="label"><label for="kp"><%=L("lcl_bes_ordernr")%></label></td>
                <td><input type="text" class="fldshort" name="kp" value="<%=safe.htmlattr(kp)%>">
              </tr>
<%          } %>

<%          if (minfo)
            {
              // <!-- Aanvraagstatus -->
              sql = "SELECT        1, " + safe.quoted_sql(bes.getbesbestellingstatustext(1)) + ",8 FROM DUAL"
                  + " UNION SELECT 2, " + safe.quoted_sql(bes.getbesbestellingstatustext(2)) + ",1 FROM DUAL"
                  + " UNION SELECT 3, " + safe.quoted_sql(bes.getbesbestellingstatustext(3)) + ",2 FROM DUAL"
                  + " UNION SELECT 4, " + safe.quoted_sql(bes.getbesbestellingstatustext(4)) + ",3 FROM DUAL"
                  + " UNION SELECT 5, " + safe.quoted_sql(bes.getbesbestellingstatustext(5)) + ",4 FROM DUAL"
                  + " UNION SELECT 6, " + safe.quoted_sql(bes.getbesbestellingstatustext(6)) + ",5 FROM DUAL"
                  + " UNION SELECT 7, " + safe.quoted_sql(bes.getbesbestellingstatustext(7)) + ",6 FROM DUAL"
                  + " UNION SELECT 8, " + safe.quoted_sql(bes.getbesbestellingstatustext(8)) + ",7 FROM DUAL"
                  + " ORDER BY 3";
              FCLTselector("rfostatus",
                           sql,
                           { initKey: rfostatus_key_arr.join(","),
                             label: L("lcl_bes_RFOs_status"),
                             multi: true,
                             trclass: "primsearch",
                             emptyOption: L("lcl_search_generic")
                           });

              // <!-- Opdrachtstatus -->
              sql = "SELECT        1, " + safe.quoted_sql(bes.getbesopdrstatustext(1)) + ",7 FROM DUAL"
                  + " UNION SELECT 2, " + safe.quoted_sql(bes.getbesopdrstatustext(2)) + ",1 FROM DUAL"
                  + " UNION SELECT 3, " + safe.quoted_sql(bes.getbesopdrstatustext(3)) + ",2 FROM DUAL"
                  + " UNION SELECT 4, " + safe.quoted_sql(bes.getbesopdrstatustext(4)) + ",3 FROM DUAL"
                  + " UNION SELECT 5, " + safe.quoted_sql(bes.getbesopdrstatustext(5)) + ",4 FROM DUAL"
                  + " UNION SELECT 6, " + safe.quoted_sql(bes.getbesopdrstatustext(6)) + ",5 FROM DUAL"
                  + " UNION SELECT 7, " + safe.quoted_sql(bes.getbesopdrstatustext(7)) + ",6 FROM DUAL"
                  + " ORDER BY 3";
              FCLTselector("orderstatus",
                           sql,
                           { initKey: orderstatus_key_arr.join(","),
                             label: L("lcl_bes_orders_status"),
                             multi: true,
                             trclass: "primsearch",
                             emptyOption: L("lcl_search_generic")
                           });

            }

            if (!minfo)
            { %>
              <!-- Statuses -->
              <tr class="primsearch">
                <td class="label"><label><%=L("lcl_bes_Bes_status")%></label></td>
                <td>
                  <nobr>
                    <input type="checkbox" class="fldrfonew" name="rfoNew" id="rfoNew" onClick="checkCheckboxesRFO();" value="1" <%=rfoNew? "checked" : ""%>>
                    <label for="rfoNew"><%=L("lcl_bes_chek3")%></label>
                    <input type="checkbox" class="fldrfofiat" name="rfoFiat" id="rfoFiat" onClick="checkCheckboxesRFO();" value="1" <%=rfoFiat? "checked" : ""%>>
                    <label for="rfoFiat"><%=L("lcl_bes_gefiatteerd")%></label>
                    <input type="checkbox" class="fldrfoopen" name="rfoOpen" id="rfoOpen" onClick="checkCheckboxesRFO();" value="1" <%=rfoOpen? "checked" : ""%>>
                    <label for="rfoOpen"><%=L("lcl_bes_chek1")%></label>
                    <input type="checkbox" class="fldrfodelivered" name="rfoDelivered" id="rfoDelivered" onClick="checkCheckboxesRFO();" value="1" <%=rfoDelivered? "checked" : ""%>>
                    <label for="rfoDelivered"><%=L("lcl_bes_chek2")%></label>
<%                  if (!frontend)
                    { %>
                      <input type="checkbox" class="fldrfoclosed" name="rfoClosed" id="rfoClosed" onClick="checkCheckboxesRFO();" value="1" <%=rfoClosed? "checked" : ""%>>
                      <label for="rfoClosed"><%=L("lcl_bes_chek4")%></label>
<%                  } %>
                  </nobr>
                </td>
              </tr>
<%          } %>
<%    SEARCH_BLOCK_END();


      SIMPLE_BLOCK_START();
        var buttons =  [{ title: L("lcl_overview_list"), action: "doSubmit();", importance: 1, icon: "fa-clipboard-list"}];
        if (!frontend)
          buttons.push ({title: L("lcl_shared_more"),  action:"iface_toggleSecondarySearchblock()", id : "btnMoreLess", importance: 3, icon: "fa-chevron-right"});
        if (backo)
          buttons.push ({title: L("lcl_shared_advanced"),  action:"myModal()", id : "bAdvanced", importance: 3, icon: "fa-filter"}); // met actuele waarden van catalogus en vorig filter
        CreateButtons(buttons, { entersubmit: true, showIcons: true} );
      SIMPLE_BLOCK_END();
      SEARCH_PAGE_END(); %>
      </form>
    <!-- search -->
    <div id="result">
      <iframe width="100%" height="100%"
              src="../Shared/empty.asp"
              name="workFrame" id="workFrame"
              onload='FcltMgr.iframeLoaded(this)'
              frameborder="0" scrolling="no">
      </iframe>
    </div>
  </body>
</html>
<% ASPPAGE_END(); %>
