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

     File:               bes_voorraad_search.asp
     Description:        Filterscherm voor overzicht voorraad bestellingen
     Parameters:         urole                Rol van de gebruiker (frontend, frontoffice, backoffice, management info)
                         disc                 Inkoop catalogus
     Globals:
     Context:
     Note:
*/ %>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../Shared/selector.inc" -->
<!-- #include file="bes.inc" -->

<%
FCLTHeader.Requires({ plugins: ["jQuery"],
                      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 disctype = getQParamInt("disctype", -1);           // Catalogus type.
var disc = getQParamInt("disc", -1);                   // Inkoop catalogus.
var srtgroup = getQParamInt("srtgroup", -1);           // Groep.
var srtdeel = getQParamInt("srtdeel", -1);             // Item (key voor select box).
var tebestellen = getQParamInt("tebestellen", 0) == 1; // Te bestellen.
/***** 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": autfunction = "WEB_BESBAC";
             break;
}

// De functie checkAutorisation(autfunction) geeft een Response.End indien er geen autorisatie voor autfunction wordt gevonden
var authparams = user.checkAutorisation(autfunction);
%>

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

    <script type="text/javascript">
      async function doSubmit()
      {
        document.activeElement.blur(); // trigger laatste onChanges

        // Standaard checks op verplichte velden, datum formaat, numeriek en float formaat, currency formaat.
        if (!await validateForm("u2"))
          return false;

        document.forms.u2.submit();
      }
    </script>
  </head>

  <body id="searchbody">
    <form name="u2" target="workFrame" action="bes_voorraad_search_list.asp" method="get">
<%    SEARCH_PAGE_START({ header: L("lcl_filterblok") });
        SEARCH_BLOCK_START(); %>
          <input type="hidden" name="urole" value="<%=urole%>">
<%        // <!-- Catalogus type -->
          var sql = "SELECT SUM(CASE"
                  + "           WHEN dp.bes_disc_params_bestelmode = 0"
                  + "           THEN 1"
                  + "           ELSE 0"
                  + "           END) aantal_uitcat"
                  + "     , SUM(CASE"
                  + "           WHEN dp.bes_disc_params_bestelmode = 1"
                  + "           THEN 1"
                  + "           ELSE 0"
                  + "           END) aantal_vrij"
                  + "     , SUM(CASE"
                  + "           WHEN dp.bes_disc_params_bestelmode = 2"
                  + "           THEN 1"
                  + "           ELSE 0"
                  + "           END) aantal_bestel"
                  + "     , SUM(CASE"
                  + "           WHEN dp.bes_disc_params_bestelmode = 3"
                  + "           THEN 1"
                  + "           ELSE 0"
                  + "           END) aantal_actueel"
                  + "  FROM bes_discipline bd"
                  + "     , bes_disc_params dp"
                  + " WHERE bd.ins_discipline_key = dp.bes_ins_discipline_key"
                  + "   AND bd.ins_discipline_verwijder IS NULL"
                  + "   AND (bd.ins_discipline_key IN (SELECT dp.bes_ins_discipline_key_inkoop"
                    + "                                  FROM bes_disc_params dp"
                    + "                                 WHERE dp.bes_ins_discipline_key_inkoop IS NOT NULL)"
                    + "  OR dp.bes_disc_params_bestelmode = 3)"
                  + "   AND ins_discipline_key IN (SELECT ins_discipline_key"
                    + "                              FROM fac_v_webgebruiker g"
                    + "                                 , fac_functie f"
                    + "                             WHERE g.prs_perslid_key = " + user_key
                    + "                               AND f.fac_functie_key = g.fac_functie_key "
                    + "                               AND f.fac_functie_code = " + safe.quoted_sql(autfunction) + ")";
          var oRs = Oracle.Execute(sql);
          var aantal_uitcat = oRs("aantal_uitcat").Value;   // Uit catalogus(0).
          var aantal_vrij = oRs("aantal_vrij").Value;       // Vrij aanmaken(1).
          var aantal_bestel = oRs("aantal_bestel").Value;   // Bestellijst(2).
          var aantal_actueel = oRs("aantal_actueel").Value; // Actuele lijst(3).
          oRs.Close();
          var sql_arr = [];
          if (aantal_uitcat > 0)
            sql_arr.push("SELECT 0, " + safe.quoted_sql("Uit catalogus") + " FROM DUAL");
          if (aantal_vrij > 0)
            sql_arr.push("SELECT 1, " + safe.quoted_sql("Vrij aanmaken") + " FROM DUAL");
          if (aantal_bestel > 0)
            sql_arr.push("SELECT 2, " + safe.quoted_sql(L("lcl_bes_orderlist")) + " FROM DUAL");
          if (aantal_actueel > 0)
            sql_arr.push("SELECT 3, " + safe.quoted_sql(L("lcl_bes_currentlist")) + " FROM DUAL");

          FCLTselector("disctype",
                       sql_arr.join(" UNION "),
                       { initKey: disctype,
                         label: L("lcl_bes_catalogus_type"),
                         onChange: 'updateDisc("' + autfunction + '");', // discChanged
                         trclass: "primsearch",
                         emptyOption: "",
                         selectjustone: true, // required zorgt er eigenlijk ook al voor dat als er maar 1 waarde is, dat deze wordt geselecteerd.
                         required: true
                       });

          // <!-- Catalogus -->
          var sql = "SELECT bd.ins_discipline_key"
                  + "     , " + lcl.xsqla('bd.ins_discipline_omschrijving','bd.ins_discipline_key')
                  + "  FROM bes_discipline bd"
                  + "     , bes_disc_params dp"
                  + " WHERE bd.ins_discipline_key = dp.bes_ins_discipline_key"
                  + "   AND bd.ins_discipline_verwijder IS NULL"
                  + "   AND (bd.ins_discipline_key IN (SELECT dp.bes_ins_discipline_key_inkoop"
                    + "                                  FROM bes_disc_params dp"
                    + "                                 WHERE dp.bes_ins_discipline_key_inkoop IS NOT NULL)"
                    + "  OR dp.bes_disc_params_bestelmode = 3)"
                  + "   AND ins_discipline_key IN (SELECT ins_discipline_key"
                    + "                              FROM fac_v_webgebruiker g"
                    + "                                 , fac_functie f"
                    + "                             WHERE g.prs_perslid_key = " + user_key
                    + "                               AND f.fac_functie_key = g.fac_functie_key "
                    + "                               AND f.fac_functie_code = " + safe.quoted_sql(autfunction) + ")"
                  + " ORDER BY upper("+ lcl.xsql('bd.ins_discipline_omschrijving','bd.ins_discipline_key') +")";
          FCLTselector("disc",
                       sql,
                       { initKey: disc,
                         label: L("lcl_bes_Catalogus"),
                         onChange: 'updateSrtGroup("' + autfunction + '");', // discChanged
                         trclass:"primsearch",
                         emptyOption: "",
                         selectjustone: true
                       });
                       %>

          <!-- Groep -->
<%        FCLTselector("srtgroup",
                       "", // sql: lege selectbox die onload wordt geladen
                       { initKey: srtgroup,
                         label: L("lcl_bes_srtgroup"),
                         onChange: 'updateSrtDeel("' + autfunction + '");', // srtgroupChanged
                         trclass: "secsearch",
                         emptyOption: "",
                         selectjustone: true
                       }); %>

          <!-- Item select box -->
<%        FCLTselector("srtdeelmulti",
                       "", // sql: lege selectbox die onload wordt geladen
                       { initKey: srtdeel,
                         label: L("lcl_bes_srtdeel"),
                         trclass: "secsearch",
                         emptyOption: "",
                         selectjustone: true
                       }); %>
          <script type="text/javascript">
            // Laad de groupen en de items
            // Selecteer eventueel de initiële groepen en items die meegegeven kunnen worden
            $(function()
            {
             updateDisc("<%=autfunction%>", "<%=disc%>", "<%=srtgroup%>", "<%=srtdeel%>");
            });
          </script>
<%      SEARCH_BLOCK_END();
        SEARCH_BLOCK_START(); %>
          <!-- Zoektekst -->
          <tr class="primsearch">
            <td class="label"><label for="searchtekst"><%=L("lcl_search_diff_fields")%></label></td>
            <td>
              <input type="text" class="fld" id="searchtekst" name="searchtekst" value="">
            </td>
          </tr>
          <!-- Statuses -->
          <tr class="primsearch">
            <td class="label"><label for="tebestellen"><%=L("lcl_bes_Bes_status")%></label></td>
            <td>
              <nobr>
                <input type="checkbox" class="fldtebestellen" name="tebestellen" id="tebestellen" value="1" <%=tebestellen? "checked" : ""%>>
                <label for="rfoNew"><%=L("lcl_bes_te_bestellen")%></label>
              </nobr>
            </td>
          </tr>
<%
          // Actuele voorraad
          RWCHECKBOXTR("instock", "fldcheck", L("lcl_bes_artikel_voorraad"), 0, { trclass: "primsearch" });
        SEARCH_BLOCK_END();

        var buttons =  [{ title: L("lcl_overview_list"), action: "doSubmit();", icon: "fa-clipboard-list"}];
        if (!frontend)
          buttons.push ( {title: L("lcl_shared_more"),  action:"iface_toggleSecondarySearchblock()", id : "btnMoreLess", icon: "fa-chevron-right" } );
        SIMPLE_BLOCK_START();
          CreateButtons(buttons, { entersubmit: true, showIcons: true} );
        SIMPLE_BLOCK_END();
      SEARCH_PAGE_END(); %>
    </form>

    <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(); %>
