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

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

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

<%
FCLTHeader.Requires({ plugins: ["jQuery"] })

/***** Get webform parameters *****/
var urole = getQParamSafe("urole");
var opdr_key = getQParamInt('opdr_key');
var autoopdr = getQParam('autoopdr', '');

var bes_bestelopdr = bes.bestelopdracht_info(opdr_key);
var bes_bestelling = bes.bestelling_info(bes_bestelopdr.bes_key_str[0]);

var this_bestelopdr = bes.func_enabled_bestelopdracht(opdr_key);
var this_bestelling = bes.func_enabled_bestelling(bes_bestelling)
var hasBOread = this_bestelopdr.canReadBOF || this_bestelopdr.canReadBOR;

// Ik moet leesrechten (BESBOF, BESBOR of BESBAC) hebben of ik moet kostenplaatsverantwoordelijke zijn om de opdracht in te mogen zien.
user.auth_required_or_abort(this_bestelopdr.canReadAny || this_bestelling.isVerantwoordelijke);
%>
<html>
  <head>
<%  FCLTHeader.Generate(); %>
    <script>
      function bes_opdr_print()
      {
          window.open("bes_opdr_xml.asp?opdr_key=<%=opdr_key%>").focus();
      }

      function bes_opdr_sent()
      {
        if (confirm(L("lcl_bes_confirm_sent").format("<%=S("bes_bestelopdr_prefix") + bes_bestelopdr.bestelopdr_id%>")))
        {
          var data = { urole: "<%=urole%>",
                       opdr_key: <%=opdr_key%>
                     };
          <% protectRequest.dataToken("data"); %>
          $.post("opdr_sent.asp",
                 data,
                 FcltCallbackRefresh,
                 "json");
        }
      }

      function bes_opdr_accept()
      {
        if (confirm(L("lcl_bes_confirm_accept").format("<%=S("bes_bestelopdr_prefix") + bes_bestelopdr.bestelopdr_id%>")))
        {
          var data = { urole: "<%=urole%>",
                       opdr_key: <%=opdr_key%>
                     };
          <% protectRequest.dataToken("data"); %>
          $.post("opdr_accept.asp",
                 data,
                 <%=bes_bestelling.parent_key? "FcltCallbackClose" : "FcltCallbackRefresh"%>,
                 "json");
        }
      }

      function bes_opdr_reject()
      {
        var url = "../BES/opdr_reject.asp?opdr_key=<%=opdr_key%>&urole=<%=urole%>";
        FcltMgr.openModalDetail(url, L("lcl_bes_order_reject").format(<%=opdr_key%>),
            {callback: <%=bes_bestelling.parent_key?"FcltMgr.closeDetail":"FcltMgr.reload"%>});
      }

      function bes_opdr_deliver()
      {
        url = "../BES/opdr_delivery.asp?opdr_key=<%=opdr_key%>&urole=<%=urole%>";
        FcltMgr.openModalDetail(url, L("lcl_bes_delivery"), {xwidth: 800, callback: FcltMgr.reload});
      }

      function bes_opdr_mail()
      {
<%
        var defemail = "";
        sql = " SELECT prs_bedrijf_email email"
            + " FROM prs_bedrijf b, bes_bestelopdr bo"
            + " WHERE b.prs_bedrijf_key = bo.prs_bedrijf_key"
            + " AND bo.bes_bestelopdr_key = " + opdr_key;
        oRs = Oracle.Execute(sql);
        if (!oRs.eof)
        {
            defemail = oRs("email").value
        }
        var subjectMail = L("lcl_bes_BesOrder") + ' ' + S("bes_bestelopdr_prefix") + bes_bestelopdr.bestelopdr_id;
        var urlMail = "../shared/queuemail.asp?pcode=BES2MA&defemail=" + Server.URLencode(defemail || "")
                    + "&key=" + opdr_key
                    + "&subject=" + Server.URLencode(subjectMail)
                    + "&mailfrom=" + (hasBOread? 1 : 0);
%>
        var subject = "<%=safe.jsstring(subjectMail)%>";
        var url = "<%=safe.jsstring(protectQS.create(urlMail))%>";
        FcltMgr.openModalDetail(url, subject);
      }

      function bes_opdr_change()
      {
          if (FcltMgr.startEdit(window))
              window.location.href = "bes_edit_opdr.asp?urole=<%=urole%>&opdr_key=<%=opdr_key%>";
      }
     jQuery(document).ready(function()
                            {
                              $('textarea').resize(function() { FcltMgr.resized(window) });
                              $('textarea').autogrow();
<%                            if (autoopdr == 'BES2CO')
                              { %>
                                bes_opdr_accept();
<%                            }
                              else if (autoopdr == 'BES2RE')
                              { %>
                                bes_opdr_reject();
<%                            } %>
                            });
    </script>
  </head>

  <body id="showbody">
<%
    var buttons =  [];
    if (this_bestelopdr.canChange)
      buttons.push({icon: "wijzigen.png", title: L("lcl_change"), action:"bes_opdr_change()" });

    if (this_bestelopdr.canSend)
      buttons.push({icon: "forward.png", title: L("lcl_ord_is_ordsnt"), action:"bes_opdr_sent()" });

    if (this_bestelopdr.canDeliver)
      buttons.push({icon: "levering.png", title: L("lcl_bes_delivery"), action:"bes_opdr_deliver()" });

    if (this_bestelopdr.canAccept)
      buttons.push({icon: "accept.png", title: L("lcl_accept"), action:"bes_opdr_accept()" });
    if (this_bestelopdr.canReject)
      buttons.push({icon: "reject.png", title: L("lcl_reject"), action:"bes_opdr_reject()" });

    buttons.push({ icon: "print.png", title: L("lcl_print"),          action: 'bes_opdr_print()' });
    buttons.push({ icon: "email.png", title: L("lcl_noti_bes_opdr_email"), action: 'bes_opdr_mail()' });
    IFRAMER_HEADER(L("lcl_bes_opdrframe_algemeen"), buttons)
%>
    <div id="show">
      <form action=order_details.asp method="post" name="u2" >
<%      BLOCK_START("besOpdr", L("lcl_bes_opdralgemeengegevens"));

        var params = {};
        if (bes_bestelopdr.bes_key_str.length > 1) // in de praktijk (5.1.2) nog nooit
            ROFIELDTR("fld", L("lcl_bes_RFOs"), S("bes_bestelling_prefix") + bes_bestelopdr.bes_key_str.join(", "));
        else
        {
            params = { infoPointer: { Url: "appl/bes/bes_bestelling.asp?urole=" + urole + "&bes_key=" + bes_bestelopdr.bes_key_str[0]}};
            ROFIELDTR("fld", L("lcl_bes_RFOs"), S("bes_bestelling_prefix") + bes_bestelopdr.bes_key_str[0], params);

            // Originele aanvraag weergeven indien aanwezig
            if (bes_bestelling.parent_key)
            {
              params = { infoPointer: { Url: "appl/bes/bes_bestelling.asp?urole=" + urole + "&bes_key=" + bes_bestelling.parent_key,
                         Title: L("lcl_bes_bestelling") + " " + S("bes_bestelling_prefix") + bes_bestelling.parent_key,
                         suppressEmpty: true }};
              ROFIELDTR ("fld", L("lcl_bes_origineel"), S("bes_bestelling_prefix") + bes_bestelling.parent_key,  params);
            }
        }

        params = { infoPointer: { Url:"appl/prs/prs_bedrijf.asp?bedrijf_key=" + bes_bestelopdr.prs_bedrijf_key,
                            Title: L("lcl_details")}};
        ROFIELDTR("fld", L("lcl_bes_Supplier"), bes_bestelopdr.prs_bedrijf_naam, params);

        params = { infoPointer: { Url: "appl/shared/status_info.asp?ord_key=" + opdr_key + "&urole=" + urole,
                                  Title: L("lcl_status_details") + " " + S("bes_bestelopdr_prefix") + bes_bestelopdr.bestelopdr_id
                                    }
                     }
        ROFIELDTR("fld", L("lcl_bes_Bes_status"), bes_bestelopdr.statustxt, params);
        ROFIELDTR("fld", L("lcl_bes_Bes_Owner"), bes_bestelopdr.prs_perslid_naam);

        sql = "SELECT mld_adres_naam "
            + " FROM mld_adres"
            + " WHERE mld_adres_key = " + bes_bestelopdr.mld_adres_key_lev;
        oRs1 = Oracle.Execute( sql );

        ROFIELDTR("fld", L("lcl_bes_adres_lev"),  !oRs1.eof ? oRs1("mld_adres_naam").value : '?');

        sql = "SELECT mld_adres_naam "
            + " FROM mld_adres"
            + " WHERE mld_adres_key = " + bes_bestelopdr.mld_adres_key_fac;
        oRs1 = Oracle.Execute(sql);
        ROFIELDTR("fld", L("lcl_bes_adres_fac"),  !oRs1.eof ? oRs1("mld_adres_naam").value : '?');
        ROTEXTAREATR("fldtxt", L("lcl_remark"), bes_bestelopdr.opmerking, {id: "txt", suppressEmpty: true}); // Afhandeling, reden niet akkoord
        ROTEXTAREATR("fldtxt", L("lcl_bes_comment"), bes_bestelopdr.delivery_opmerk, {id: "odtxt", suppressEmpty: true});
        BLOCK_END();

        BLOCK_START("besOpdrItems", L("lcl_bes_geselecteerd"));
%>
        <tr>
          <th><%=L("lcl_bes_srtdeel_nr")%></th>
          <th><%=L("lcl_bes_discipline")%></th>
          <th><%=L("lcl_bes_srtdeel")%></th>
          <th style="text-align:right"><%=L("lcl_bes_amount")%></th>
          <th style="text-align:right"><%=L("lcl_bes_delivered_so_far")%></th>
          <th style="text-align:right"><%=L("lcl_bes_Price") + " " + (bes_bestelopdr.inclBTW? L("lcl_shared_inclBTW") : L("lcl_shared_exclBTW"))%></th>
        </tr>

<%      for (var i = 0; i < bes_bestelopdr.artikel_arr.length; i++)
        { %>
          <tr>
            <td><%=safe.html(bes_bestelopdr.artikel_arr[i].artikelnr)%></td>
            <td><%=safe.html(bes_bestelopdr.artikel_arr[i].catalogus_omschr)%></td>
            <td><%=safe.html(bes_bestelopdr.artikel_arr[i].omschr)%></td>
            <td align=right><%=bes_bestelopdr.artikel_arr[i].aantal%></td>
            <td align=right><%=bes_bestelopdr.artikel_arr[i].aantalontv%></td>
            <td align=right><%=safe.curr(bes_bestelopdr.artikel_arr[i].prijs)%></td>
          </tr>
<%      }
        var total = bes_bestelopdr.totaal - bes_bestelopdr.korting + bes_bestelopdr.levkosten;

        if (bes_bestelopdr.korting > 0)
        { %>
          <tr>
            <td align=left colspan=5><b><%=L("lcl_bes_order_discount")%></b></td>
            <td align=right>
            <input type="text" style="text-align:right" readonly class='disabled' value='<%=safe.curr(bes_bestelopdr.korting * -1)%>'>
            </td>
          </tr>
<%      }

        if (bes_bestelopdr.levkosten > 0)
        { %>
          <tr>
            <td align=left colspan=4><b><%=L("lcl_bes_tpcosts")%></b></td>
            <td align=right>
            <input type="text" style="text-align:right" readonly class='disabled'
            value='<%=safe.curr(bes_bestelopdr.levkosten)%>'>
            </td>
          </tr>
<%      } %>
        <tr>
          <td align=left colspan=5><b><%=L("lcl_total")%></b></td>
          <td align=right>
              <input type="text" style="text-align:right" readonly class='disabled' value='<%=safe.curr(total)%>'>
          </td>
        </tr>
<%      BLOCK_END();
%>
      </form>
    </div>
  </body>
</html>
