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

    File:        bes_srtprod_search_list.asp
    Description: Vangt de parameters van bes_srtprod_search op en verwerkt die tot een lijst
    Parameters:
    Context:     Vanuit bes_srtprod_search.asp
    Note:
*/ %>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/resultset_table_v2.inc" -->

<%

var searchtext = getQParam("searchtext", null);
var disc_key = getQParamInt("disc", -1);
var lev_key = getQParamInt("lev_key", -1);
var bes_enddat = getQParamDate("bes_enddat", null);

var outputmode = getQParamInt("outputmode", 0); //  0 = screen, 1 = print, 2 = excel etc
var showall = getQParamInt("showall", 0) == 1;

var authparams = user.checkAutorisation("WEB_PRDMSU");

%>
<html>
  <head>
<%  FCLTHeader.Generate({outputmode: outputmode}); %>
    <script>
        function prd_edit(row)
        {
          var srtprod_key = row.getAttribute("ROWKEY");
          var url = "appl/bes/bes_srtprod.asp?disc_key=<%=disc_key%>&srtprod_key="+srtprod_key;
          FcltMgr.openDetail(url, "<%=L("lcl_bes_srtprod")%>", {callback: FcltMgr.reload } );
        }
    </script>
  </head>

  <body id="listbody">
<%
    var sql = "SELECT d.ins_discipline_omschrijving,"
            + "       g.bes_srtgroep_omschrijving,"
            + "       b.bes_srtprod_eenheid,"
            + "       b.bes_srtprod_key,"
            + "       b.bes_srtprod_nr,"
            + "       b.bes_srtprod_omschrijving,"
            + "       b.bes_srtprod_vervaldatum,"
            + "       bes.getsrtprodprijs(b.bes_srtprod_key, NULL) actualprijs,"
            + "       bedr.prs_bedrijf_naam"
            + "  FROM bes_srtprod B,"
            + "       bes_srtgroep g,"
            + "       ins_tab_discipline d,"
            + "       prs_bedrijf bedr"
            + "  WHERE b.bes_srtgroep_key = g.bes_srtgroep_key(+)"          // TODO: nog onduidelijk of deze verplicht is of niet
            + "    AND g.ins_discipline_key = d.ins_discipline_key(+)"
            + "    AND bes_srtprod_verwijder IS NULL"
            + "    AND bedr.prs_bedrijf_key(+) = b.prs_bedrijf_key"
            + (disc_key>0?" AND g.ins_discipline_key = " + disc_key:"")
            + (lev_key>0?" AND b.prs_bedrijf_key = " + lev_key:"")
            + (bes_enddat?" AND (bes_srtprod_vervaldatum IS NULL OR "
            + "                  bes_srtprod_vervaldatum >= " + bes_enddat.beginToSQL() + ")":"")
            + (searchtext?" AND (UPPER(b.bes_srtprod_nr) LIKE " + safe.quoted_sql_wild(searchtext + "%") + " OR "
            + "                  UPPER(b.bes_srtprod_omschrijving) LIKE " + safe.quoted_sql_wild("%"+searchtext + "%") + ")" :"")
            + "  ORDER BY b.bes_srtprod_nr";

    // TODO: gebruiken en actuele prijs highlighten?
    function fnrowClass(oRs)
    {
        // if laatste?
        return "expired2"; // Te laat
    }

    buttons = {};
    //if (canAdd)
    {
      var addurl = "appl/bes/bes_srtprod.asp?disc_key=" + disc_key;
      buttons = [{ icon: "plus.png", title: L("lcl_add"), action: "FcltMgr.openDetail('" + addurl + "', '" + L("lcl_add") + "', {callback: FcltMgr.reload })" }]
    }

    var rst = new ResultsetTable({ sql: sql,
                                   keyColumn: "bes_srtprod_key",
                                   ID: "srtprodtable",
                                   showAll: showall,
                                   outputmode: outputmode,
                                   title: L("lcl_bes_srtprod_list"),
                                   buttons: buttons
                                 });

    if (disc_key<0)
        rst.addColumn(new Column({caption: L("lcl_bes_discipline"),       content: "ins_discipline_omschrijving"}));
    rst.addColumn(new Column({caption: L("lcl_bes_srtgroep_oms"),       content: "bes_srtgroep_omschrijving"}));
    rst.addColumn(new Column({caption: L("lcl_bes_srtdeel_nr"),         content: "bes_srtprod_nr"}));
    if (lev_key<0)
        rst.addColumn(new Column({caption: L("lcl_bes_producer"),              content: "prs_bedrijf_naam"}));
    rst.addColumn(new Column({caption: L("lcl_bes_srtdeel_oms"),    content: "bes_srtprod_omschrijving"}));
    rst.addColumn(new Column({caption: L("lcl_bes_unity"),            content: "bes_srtprod_eenheid"}));
    rst.addColumn(new Column({caption: L("lcl_bes_srtprod_prijs_prijs"),    content: "actualprijs", datatype: "currency"}));
    rst.addColumn(new Column({caption: L("lcl_bes_vervaldatum"), content: "bes_srtprod_vervaldatum", datatype: "date"}));

    rst.addAction({ action: "prd_edit", caption: L("lcl_edit"), isDefault: true});

    var cnt = rst.processResultset();
%>
  </body>
</html>
