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

    File:               mld_opdr_materiaal.asp
    Description:        (Modal)dialoog om materialen aan een opdracht te kunnen koppelen
                        of (readonly) in een lijst te tonen
    Globalen:
        opdr_key        Opdracht key
    Context:
    Note:
*/
%>

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

<%
FCLTHeader.Requires({ plugins: ["jQuery"],
                      js: ["../mld/mld_opdr_materiaal.js"]
                    })

var mld_ordermaterial_frame = S("mld_ordermaterial_frame") == 1;
var opdr_key = getQParamInt("opdr_key");
var uitv_key = getQParamInt("uitv_key");
var readonly = getQParamInt("readonly", 0) == 1;
var outputmode = getQParamInt("outputmode", 0); //  0 = screen, 1 = print, 2 = excel etc
var showall = getQParamInt("showall", 0) == 1;
if (opdr_key > 0)
{
  this_opdr = mld.func_enabled_opdracht(opdr_key);
  user.auth_required_or_abort(this_opdr.canReadAny); // is toch wel het minste
  if (!readonly)
  {
      this_opdr = mld.func_enabled_opdracht(opdr_key);
      readonly = !this_opdr.canChange;
  }
}
%>

<html>
  <head>
<%  FCLTHeader.Generate({ outputmode: outputmode }); %>
    <script type="text/javascript">
      var matkeystr = <%=S("mld_ordermaterial_frame") == 1 ? 1 : 0%> ? "" : FcltMgr.dialogArguments().matkeystr;
      var mld_ordermaterial_fac_usrtab_key = <%=S("mld_ordermaterial_fac_usrtab_key")%>;
      var currency_pref = '<%=S("currency_pref")%>';
      var currency_suff = '<%=S("currency_suff")%>';
      var uitv_key = <%=uitv_key%>;
    </script>
  </head>

<%
    if (mld_ordermaterial_frame)
        Response.Write('<body>');
    else
        Response.Write('<body class="fclt-modal" id="mod_mldmat">');

    if (!mld_ordermaterial_frame)
        MODAL_START(); %>
    <form name="u2" method="post">

<%    if (!readonly)
      {
        if (!mld_ordermaterial_frame)
            MODAL_BLOCK_START("materialselect", L("lcl_mld_opdr_materials_select"), { hidden: readonly, extraclass: "materialselect", icon: "fa-tools" }); %>
          <tr>
            <td style="width:45%" align="middle">
              <span class='details' onclick='mat_info()'><%=I("fa-info-circle")%></span>&nbsp;<label for="matPossible" class="details"><%=L("lcl_mld_opdr_materials_available")%></label><br>
<%            sql = "";
              // Wordt via load methode vervangen omdat matkeystr clientside beschikbaar is.
              // Matkeystr is niet via url meegezonden. Maximum lengte van url is 2083 karakters (maximum path lengte is 2048 karakters). Dit kan anders problemen/foutmelding geven
              FCLTselector("matPossible",
                           sql,
                           { startmulti: true,
                             size: 20,
                             extraParamValue: "prijs"
                           });
%>
              <script type="text/javascript">
                $(function()
                {
                  $("#matPossible").load("loadMaterialOptions.asp",
                                         {
                                           matkeystr: matkeystr,
                                           filter: "P"
                                         });
                });
              </script>
            </td>
            <td class="nowrap">
              <div class="mv r" onclick="moveRight()"><%=I("fa-chevron-right")%><%=L("lcl_mld_opdr_add")%></div>
              <div class="mv l" onclick="moveLeft()"><%=L("lcl_mld_opdr_remove")%><%=I("fa-chevron-left")%></div>
            </td>
            <td style="width:45%" align="middle">
              <span class='details' onclick='mat_info()'><%=I("fa-info-circle")%></span>&nbsp;<label for="matExist"><%=L("lcl_mld_opdr_materials_selected")%></label><br>
<%            sql = "";
              // Wordt via load methode vervangen omdat matkeystr clientside beschikbaar is.
              // Matkeystr is niet via url meegezonden. Maximum lengte van url is 2083 karakters (maximum path lengte is 2048 karakters). Dit kan anders problemen/foutmelding geven
              FCLTselector("matExist",
                           sql,
                           { startmulti: true,
                             size: 20,
                             extraParamValue: "prijs"
                           });
%>
              <script type="text/javascript">
                $(function()
                {
                  $("#matExist").load("loadMaterialOptions.asp",
                                      {
                                        matkeystr: matkeystr,
                                        filter: "E"
                                      },
                                      function()
                                      {
                                        showRightButtons();
                                      });
                });
              </script>
            </td>
          </tr>
<%
        if (!mld_ordermaterial_frame)
        {
          MODAL_BLOCK_END();
          SIMPLE_BLOCK_START({ hidden: readonly, extraclass: "materialselect" });
        } %>
          <div id="buttons1" style="display:none">
<%          var buttons = [{title: L("lcl_next"), icon: "fa-arrow-right", action: "nextInvoer()", importance: 1},
                           {title: L("lcl_cancel"), icon: "fa-fclt-cancel", action: "FcltMgr.closeDetail(window, { cancel: true })", importance: 3}];
            CreateButtons(buttons);
%>
          </div>
          <div id="buttons2" style="display:block">
<%          var buttons = [{title: L("lcl_submit"), icon: "fa-fclt-save", action: "submit()", importance: 1},
                           {title: L("lcl_cancel"), icon: "fa-fclt-cancel", action: "FcltMgr.closeDetail(window, { cancel: true })", importance: 3}];
            CreateButtons(buttons);
%>
          </div>
<%
        if (!mld_ordermaterial_frame)
          SIMPLE_BLOCK_END();
      }

      if (readonly)
      { // Bestaande opdracht

        if (!mld_ordermaterial_frame)
          MODAL_BLOCK_START("aantalpriceselect", L("lcl_mld_material"), { hidden: !readonly, extraclass: "aantalpriceselect", nopadding: true, icon: "fa-tools" });

          var sql = "SELECT ud.mld_opdr_materiaal_key"
                  + "     , ud.mld_opdr_materiaal_id"
                  + "     , fu.fac_usrdata_key"
                  + "     , ud.mld_opdr_materiaal_aantal"
                  + "     , ud.mld_opdr_materiaal_eenheid"
                  + "     , ud.mld_opdr_materiaal_prijs"
                  + "     , ud.mld_opdr_materiaal_code"
                  + "     , ud.mld_opdr_materiaal_groep"
                  + "     , ud.mld_opdr_materiaal_extra2"
                  + "     , ud.mld_opdr_materiaal_extra3"
                  + "     , ud.mld_opdr_materiaal_extra4"
                  + "     , (ud.mld_opdr_materiaal_aantal * ud.mld_opdr_materiaal_prijs) totaal"
                  + "     , " + lcl.xsqla('fu.fac_usrdata_omschr', 'fu.fac_usrdata_key')
                  + "     , ud.mld_opdr_materiaal_omschr"
                  + "     , ud.mld_opdr_materiaal_info"
                  + "     , b.fin_btwtabelwaarde_oms"
                  + "     , (ud.mld_opdr_materiaal_aantal * ud.mld_opdr_materiaal_prijs * (100+fin_btwtabelwaarde_perc)/100) totaalincbtw"
                  + " FROM mld_opdr_materiaal ud"
                  + "    , fac_usrdata fu"
                  + "    , fin_btwtabelwaarde b"
                  + " WHERE ud.fac_usrdata_key = fu.fac_usrdata_key (+)"
                  + "   AND ud.fin_btwtabelwaarde_key = b.fin_btwtabelwaarde_key (+)"
                  + "   AND ud.mld_opdr_key = " + opdr_key
                  + (mld_ordermaterial_frame
                    ? " ORDER BY ud.mld_opdr_materiaal_id"
                      + "      , ud.mld_opdr_materiaal_groep"
                      + "      , ud.mld_opdr_materiaal_extra2"
                      + "      , ud.mld_opdr_materiaal_code"
                    : " ORDER BY fu.fac_usrdata_volgnr"
                      + "      , UPPER(" + lcl.xsql("fu.fac_usrdata_omschr", "fu.fac_usrdata_key") + ")");
          /**********************************
            * Callback functies ResultsetTable
            *********************************/
          function fncolOmschr(oRs)
          {
            var omschr = oRs("fac_usrdata_omschr").value || oRs("mld_opdr_materiaal_omschr").value;
            omschr = '<span title="'+safe.htmlattr(oRs("mld_opdr_materiaal_info").value)+'">'+safe.html(omschr)+'</span>';
            return omschr;
          }

          function fnMateriaalPrijs(oRs)  // determine float or currency representation
          {
            var price = oRs("mld_opdr_materiaal_prijs").Value;
            if (parseInt(String(price).split(".")[1], 10) > 99)       // price has more then 2 digits after de decimal point
            {
              return safe.displayfloat(price, 7, true); // show all digits after decimal point without trailing zeroes
            }
            else
            {
              return safe.curr(price);                  // always show 2 digits after deimal point
            }
          }

          var rst = new ResultsetTable({ keyColumn: "mld_opdr_materiaal_key",
                                          sql: sql,
                                          ID: "materiaaltable",
                                          title: (mld_ordermaterial_frame? L("lcl_mld_material") : null),
                                          outputmode: outputmode,
                                          showAll: showall,
                                          noPrint: (mld_ordermaterial_frame? null : true)
                                        });
          if (mld_ordermaterial_frame)
          {
            rst.addColumn(new Column({caption: L("lcl_mld_opdr_id"), content: "mld_opdr_materiaal_id", datatype: "number"}));
            rst.addColumn(new Column({caption: L("lcl_mld_opdr_groep"), content: "mld_opdr_materiaal_groep"}));
            rst.addColumn(new Column({caption: L("lcl_mld_opdr_extra2"), content: "mld_opdr_materiaal_extra2"}));
            rst.addColumn(new Column({caption: L("lcl_mld_opdr_extra3"), content: "mld_opdr_materiaal_extra3"}));
            rst.addColumn(new Column({caption: L("lcl_mld_opdr_extra4"), content: "mld_opdr_materiaal_extra4"}));
            rst.addColumn(new Column({caption: L("lcl_mld_opdr_code"), content: "mld_opdr_materiaal_code"}));
            rst.addColumn(new Column({caption: L("lcl_mld_opdr_omschr"), content: "mld_opdr_materiaal_omschr"}));
            rst.addColumn(new Column({caption: L("lcl_mld_opdr_aantal"), content: "mld_opdr_materiaal_aantal", datatype: "number"}));
            rst.addColumn(new Column({caption: L("lcl_mld_opdr_eenheid"), content: "mld_opdr_materiaal_eenheid" }));
            rst.addColumn(new Column({caption: L("lcl_mld_opdr_prijs"), content: fnMateriaalPrijs, datatype: "currency"}));
            rst.addColumn(new Column({caption: L("lcl_mld_opdr_btw"), content: "fin_btwtabelwaarde_oms"}));
            rst.addColumn(new Column({caption: L("lcl_mld_opdr_totaal"), content: "totaal", datatype: "currency"}));
          }
          else
          {
            rst.addColumn(new Column({caption: L("lcl_mld_opdr_aantal"), content: "mld_opdr_materiaal_aantal", datatype: "number"}));
            rst.addColumn(new Column({caption: L("lcl_descr"), content: fncolOmschr}));
            rst.addColumn(new Column({caption: L("lcl_mld_opdr_code"), content: "mld_opdr_materiaal_code"}));
            rst.addColumn(new Column({caption: L("lcl_mld_opdr_eenheid"), content: "mld_opdr_materiaal_eenheid" }));
            rst.addColumn(new Column({caption: L("lcl_mld_opdr_prijs"), content: fnMateriaalPrijs, datatype: "currency"}));
            rst.addColumn(new Column({caption: L("lcl_mld_opdr_btw"), content: "fin_btwtabelwaarde_oms"}));
            rst.addColumn(new Column({caption: L("lcl_mld_opdr_totaal"), content: "totaal", datatype: "currency"}));
          }

        if (!mld_ordermaterial_frame)
          Response.Write("</table>");

          var mld = rst.processResultset();

        if (!mld_ordermaterial_frame)
          MODAL_BLOCK_END();
      }

      if (!readonly)
      {
        if (!mld_ordermaterial_frame)
          MODAL_BLOCK_START("mld_selobj", L("lcl_mld_opdr_materials_quantity"), { hidden: !readonly, extraclass: "aantalpriceselect", nopadding: true, icon: "fa-balance-scale-left" }); %>
        <tr>
          <td>
            <div id="aantalpricetable"></div>
            <div id="total_list" align="right">
              <table>
                <tr>
                  <td class="label"><label><%=L("lcl_mld_opdr_totaal")%></label></td>
                  <td id="total_txt"><%=S("currency_pref") + " " + safe.curr(0) + " " + S("currency_suff")%></td>
                  <input type="hidden" id="total" name="total" value="0">
                </tr>
              </table>
            </div>
          </td>
        </tr>
<%      if (!mld_ordermaterial_frame)
          MODAL_BLOCK_END();
      }

      if (!mld_ordermaterial_frame)
      {
        SIMPLE_BLOCK_START({ hidden: !readonly, extraclass: "aantalpriceselect" });
          var buttons = [];
          if (!readonly)
          {
            buttons.push({title: L("lcl_previous"), icon: "fa-arrow-left", action: "previousInvoer()", importance: 2})
            buttons.push({title: L("lcl_submit"), icon: "fa-fclt-save", action: "submit()", importance: 1})
          }
          buttons.push({title: readonly?L("lcl_close_window"):L("lcl_cancel"), icon: "fa-fclt-cancel", action: "FcltMgr.closeDetail(window, { cancel: true })", importance: 3})
          CreateButtons(buttons);
        SIMPLE_BLOCK_END();
      }

    IFACE.FORM_END(); %>
    </form>
<%if (!mld_ordermaterial_frame)
    MODAL_END(); %>
  </body>
</html>
<% ASPPAGE_END(); %>
