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

    File:               mld_opdr_materiaal.asp
    Description:        (Modal)dialoog om materialen aan een opdracht te kunnen koppelen
    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", "num2curr.js"]
                    })

var opdr_key = getQParamInt("opdr_key");
var uitv_key = getQParamInt("uitv_key");
var readonly = getQParamInt("readonly", 0) == 1;
if (!readonly && opdr_key > 0)
{
  this_opdr = mld.func_enabled_opdracht(opdr_key); // wat mag ik zoal op deze opdracht
  readonly = !this_opdr.canChange;
}
%>

<html>
  <head>
<%  FCLTHeader.Generate() %>
    <script type="text/javascript">
      var matkeystr = 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>

  <body class="modal" id="mod_mldmat">
    <form name="u2" method="post">

<%    if (!readonly)
      { %>
        <div id="materialselect" style="display: <%=readonly? "none" : "block"%>">
<%        BLOCK_START("mld_selobj", L("lcl_mld_opdr_materials_select")) %>
          <tr>
            <td style="width:45%" align="middle">
              <img class='details' src='<%=rooturl%>/appl/Pictures/detail.png' onclick='mat_info()'><label 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">
                jQuery(document).ready(function()
                   {
                     $("#matPossible").load("loadMaterialOptions.asp",
                                            {
                                              matkeystr: matkeystr,
                                              filter: "P"
                                            });
                   });
              </script>
            </td>
            <td>
              <div class="mv r" onclick="moveRight()"><%=L("lcl_mld_opdr_add")%></div>
              <div class="mv l" onclick="moveLeft()"><%=L("lcl_mld_opdr_remove")%></div>
            </td>
            <td style="width:45%" align="middle">
              <img class='details' src='<%=rooturl%>/appl/Pictures/detail.png' onclick='mat_info()'><label><%=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">
                jQuery(document).ready(function()
                   {
                     $("#matExist").load("loadMaterialOptions.asp",
                                         {
                                           matkeystr: matkeystr,
                                           filter: "E"
                                         },
                                         function()
                                         {
                                           showRightButtons();
                                         });
                   });
              </script>
            </td>
          </tr>
<%        BLOCK_END() %>

          <div id="buttons1" style="display:none">
<%          var buttons = [{title: L("lcl_next"), icon: "opslaan.png", action: "nextInvoer()"},
                           {title: L("lcl_cancel"), icon: "undo.png", action: "FcltMgr.closeDetail(window, { cancel: true })"}];
            CreateButtons(buttons);
%>
          </div>
          <div id="buttons2" style="display:block">
<%          var buttons = [{title: L("lcl_submit"), icon: "opslaan.png", action: "submit()"},
                           {title: L("lcl_cancel"), icon: "undo.png", action: "FcltMgr.closeDetail(window, { cancel: true })"}];
            CreateButtons(buttons);
%>
          </div>
        </div>
<%    } %>

      <div id="aantalpriceselect" style="display: <%=readonly? "block" : "none"%>">
<%      if (readonly)
        { // Bestaande opdracht
          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;
          }

          var sql = "SELECT ud.mld_opdr_materiaal_key"
                  + "     , fu.fac_usrdata_key"
                  + "     , ud.mld_opdr_materiaal_aantal"
                  + "     , ud.mld_opdr_materiaal_eenheid"
                  + "     , ud.mld_opdr_materiaal_prijs"
                  + "     , (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
                  + " ORDER BY fu.fac_usrdata_volgnr"
                  + "        , UPPER(" + lcl.xsql('fu.fac_usrdata_omschr', 'fu.fac_usrdata_key' )+ ")";
          var rst = new ResultsetTable({ keyColumn: "mld_opdr_materiaal_key",
                                         sql: sql,
                                         ID: "materiaaltable",
                                         noPrint: true
                                        });
          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_eenheid"), content: "mld_opdr_materiaal_eenheid" }));
          rst.addColumn(new Column({caption: L("lcl_mld_opdr_prijs"), content: "mld_opdr_materiaal_prijs", 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"}));
          var mld = rst.processResultset();
        }
        else
        {
          BLOCK_START("mld_selobj", L("lcl_mld_opdr_materials_quantity")) %>
          <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>
<%        BLOCK_END() %>


<%      } %>

<%      var buttons = [];
        if (!readonly)
        {
          buttons.push({title: L("lcl_previous"), icon: "opslaan.png", action: "previousInvoer()"})
          buttons.push({title: L("lcl_submit"), icon: "opslaan.png", action: "submit()"})
        }
        buttons.push({title: readonly?L("lcl_close_window"):L("lcl_cancel"), icon: "undo.png", action: "FcltMgr.closeDetail(window, { cancel: true })"})
        CreateButtons(buttons);
%>
      </div>
<% IFACE.FORM_END(); %>
    </form>
  </body>
</html>
