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

    File:               bes_opdr.asp
    Description:        Wrapper voor bestellingen-opdrachten
    Parameters:         urole
                        ordernr
    Context:
    Note:
*/ %>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../fin/fin_verkoopfactuur.inc" -->
<!-- #include file="bes.inc" -->
<%
FCLTHeader.Requires({ plugins: ["suggest","jQuery"],
                      js: ["jquery-ui.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 opdr_key = getQParamInt('ordernr');
var autoopdr = getQParam('autoopdr', '');

var sql = "SELECT bo.bes_bestelopdr_id"
        + "     , b.bes_bestelling_parentkey"
        + "  FROM bes_bestelopdr bo"
        + "     , bes_bestelling_item bi"
        + "     , bes_bestelopdr_item boi"
        + "     , bes_bestelling b"
        + " WHERE bo.bes_bestelopdr_key = boi.bes_bestelopdr_key"
        + "   AND boi.bes_bestelopdr_item_key = bi.bes_bestelopdr_item_key"
        + "   AND bi.bes_bestelling_key = b.bes_bestelling_key"
        + "   AND bo.bes_bestelopdr_key = " + opdr_key;
oRs = Oracle.Execute(sql);
var ordernr_id = oRs("bes_bestelopdr_id").value;
var bes_parent_key = oRs("bes_bestelling_parentkey").value;
var opdr_parent_key;
var sql = "SELECT boi.bes_bestelopdr_key"
        + "  FROM bes_bestelling_item bi"
        + "     , bes_bestelopdr_item boi"
        + " WHERE boi.bes_bestelopdr_item_key = bi.bes_bestelopdr_item_key"
        + "   AND bi.bes_bestelling_key = " + bes_parent_key;
oRs = Oracle.Execute(sql);
if (!oRs.eof)
  opdr_parent_key = oRs("bes_bestelopdr_key").value;
oRs.Close();

var this_bestelopdr = bes.func_enabled_bestelopdracht(opdr_key);

%>
<html>
  <head>
<%  FCLTHeader.Generate(); %>
    <script>
      $(function ()
      {
        FcltMgr.setTitle(L("lcl_bes_BesOrder") + " " + "<%=safe.jsstring(ordernr_id)%>", {hot: false});
      });
    </script>
  </head>
<%
// Autorisatie wordt geregeld in iframe bestanden (bes_show_opdr.asp en fin_list.asp)
%>
  <body>
  <% PAGE_START(); %>
<%  page = "./bes_show_opdr.asp?urole=" + urole + "&opdr_key=" + opdr_key + "&autoopdr=" + autoopdr;

    IFRAMER("opdrFrame", page, { title: L("lcl_opdr_frame_algemeen"),
                                refreshOnClose: true,
                                srcparent: (opdr_parent_key? "./bes_show_opdr.asp?urole=" + urole + "&opdr_key=" + opdr_parent_key : null) } );

    if (opdr_key > -1)
    { // 2. Het optionele facturenframe
      var authparamsFINBOF = user.checkAutorisation("WEB_FINBOF", true);
      var authparamsFINFOF = user.checkAutorisation("WEB_FINFOF", true);
      var finbof = (authparamsFINBOF || authparamsFINFOF);
      // Als FINBOF rechten en er is een factuur bij de opdracht, dan een facturen frame tonen
      var sql = "SELECT f.bes_bestelopdr_key"
              + "     , fin_factuur_key"
              + "  FROM fin_factuur f"
              + " WHERE f.bes_bestelopdr_key = " + opdr_key;
      oRs = Oracle.Execute(sql);
      if (finbof && !oRs.eof)
      {
        page="../fin/fin_list.asp?embedded=1&bes_key=" + opdr_key;
        IFRAMER("finfactuur", page, { refreshOnClose: true, title: L("lcl_fin_invoices_frame")} );
      }

      // Zijn er delivery records?
      var sql = "SELECT bbl.bes_bestellevr_key"
              + "  FROM bes_bestellevr_item  bbli, "
              + "       bes_bestellevr       bbl, "
              + "       bes_bestelopdr       bbo, "
              + "       bes_bestelopdr_item  bbi"
              + " WHERE     bbo.bes_bestelopdr_key = " + opdr_key
              + "       AND bbo.bes_bestelopdr_key = bbi.bes_bestelopdr_key "
              + "       AND bbli.bes_bestelopdr_item_key = bbi.bes_bestelopdr_item_key "
              + "       AND bbl.bes_bestellevr_key = bbli.bes_bestellevr_key ";
      var oRs = Oracle.Execute(sql);
      var hasDelivery = !oRs.EOF;
      oRs.Close();
      if (hasDelivery || this_bestelopdr.canDeliver)
      {
        page="../bes/bes_bestellevr_list.asp?embedded=1&opdr_key=" + opdr_key;
        IFRAMER("besdelivery", page, { refreshOnClose: true, title: L("lcl_bes_delivery")} );
      }

      verkoopfactuur_IFRAMER('bestelopdr', opdr_key);

      var autfunction = {fo: 'WEB_BESFOF', bo: ['WEB_BESBOR', 'WEB_BESBOF'], mi: 'WEB_BESBAC'}[urole] || 'WEB_BESUSE';
      var authparams = user.checkAutorisation(autfunction, true);
      if (authparams && authparams.ALGreadlevel < 9 && authparams.PRSreadlevel < 9)
      {
        page = protectQS.create("../shared/status_info.asp?ord_key=" + opdr_key + "&urole=" + urole);
        IFRAMER("alghistoryframe", page, { icon: "fa-list", title: L("lcl_tracking")} );
      }
    }

%>
<% PAGE_END(); %>
  </body>
</html>
<% ASPPAGE_END(); %>
