<%@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="../Shared/persoonselector.inc" -->
<!-- #include file="bes.inc" -->
<!-- #include file="bes_flexkenmerk.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()
      {
          FcltMgr.windowopen("bes_opdr_xml.asp?opdr_key=<%=opdr_key%>");
      }

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

      function bes_opdr_accept()
      {
        FcltMgr.confirm(L("lcl_bes_confirm_accept").format("<%=S("bes_bestelopdr_prefix") + bes_bestelopdr.bestelopdr_id%>"),
                        function()
                        {
                          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_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%>";
      }

      $(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 class="showmode">
<% SUBFRAME_START();
    var buttons =  [];
    if (this_bestelopdr.canChange)
      buttons.push({icon: "fa-fclt-edit", title: L("lcl_change"), action:"bes_opdr_change()" });

    if (this_bestelopdr.canSend)
      buttons.push({icon: "fa-badge-check", title: L("lcl_ord_is_ordsnt"), action:"bes_opdr_sent()" });

    if (this_bestelopdr.canAccept)
      buttons.push({icon: "fa-check", title: L("lcl_accept"), action:"bes_opdr_accept()" });
    if (this_bestelopdr.canReject)
      buttons.push({icon: "fa-times", title: L("lcl_reject"), action:"bes_opdr_reject()" });

    buttons.push({ icon: "fa-print", title: L("lcl_print"),          action: 'bes_opdr_print()' });
    buttons.push({ icon: "fa-envelope", title: L("lcl_noti_bes_opdr_email"), action: 'bes_opdr_mail()' });

    IFRAMER_HEADER(L("lcl_bes_opdrframe_algemeen"), buttons)
%>
    <form action="order_details.asp" method="post" name="u2" >
<%
    BLOCK_START("besOpdr", L("lcl_bes_opdrframe_algemeen"), {icon: "fa-shipping-fast"});

        ROFIELDTR("fld", L("lcl_bes_BesOrder"), S("bes_bestelopdr_prefix") + bes_bestelopdr.bestelopdr_id);
        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);
        if (bes_bestelling.perslid_key_voor > 0 && bes_bestelling.perslid_key_voor != bes_bestelopdr.perslid_key)
        {
            FCLTpersoonselector(
               "personFor",
               "sgPersonFor",
                 { perslidKey: bes_bestelling.perslid_key_voor,
                   label: L("lcl_bes_order_for"),
                   readonly: true,
                   moreinfo: true
                 });
        }


        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 : '?');
        if (S("bes_afleverruimte_verplicht") < 2) {
            ROFIELDTR ("fld", L("lcl_bes_del_room"), bes_bestelling.afleverruimte);
        }

        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});
        ROTEXTAREATR("fldtxt", L("lcl_bes_bestelling_lev_opm"), bes_bestelling.lev_opm, {suppressEmpty: true} );
    BLOCK_END();

    BLOCK_START("besOpdrAdm", L("lcl_bes_opdralgemeengegevens"), {icon: "fa-question-square"});
        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);
            }
        }

        // Zijn wij een incidentele bestelling via een melding?
        if (bes_bestelling.mld_key != null)
        {
            var params = { infoPointer: { Url: "appl/mld/mld_melding.asp?urole=" + urole + "&mld_key=" + bes_bestelling.mld_key,
                                          Title: L("lcl_complain") + " " + bes_bestelling.mld_key
                                        }
                         }
            ROFIELDTR("fld", L("lcl_bes_onbe_call"), bes_bestelling.mld_prefix+bes_bestelling.mld_key, params);
        }


        var statustxt = bes_bestelopdr.statustxt;
        var aantal = 0;
        var aantalontv = 0;
        for (var i = 0; i < bes_bestelopdr.artikel_arr.length; i++)
        {
          aantal = aantal + bes_bestelopdr.artikel_arr[i].aantal;
          aantalontv = aantalontv + bes_bestelopdr.artikel_arr[i].aantalontv;
        }
        if (bes_bestelopdr.status == 4 && aantalontv > 0 && aantalontv < aantal)
          statustxt += "\n" + L("lcl_bes_ord_deelsgeleverd");
        ROFIELDTR("fld", L("lcl_bes_Bes_status"), statustxt);
        ROFIELDTR("fld", L("lcl_bes_Bes_Owner"), bes_bestelopdr.prs_perslid_naam);

        if (this_bestelopdr.canWriteFlags && bes_bestelopdr.flag)
        {
            ROFIELDTR("fld", L("lcl_bestelopdr_flags"), L("lcl_bestelopdr_flag" + bes_bestelopdr.flag), { icon: "fa-fclt-flag", iconclass: "besopdrflag" + bes_bestelopdr.flag });
        }
    BLOCK_END();

    generateFlexKenmerkCode ({ urole: urole,
                               bes_key: bes_bestelling.bes_key, // Bestelling key
                               disc_key: bes_bestelling.dis_key,
                               kenmerktype: "B",
                               bestelopdracht: true,
                               reado: true
                             });

    BLOCK_START("besOpdrItems", L("lcl_bes_geselecteerd"), { wide: true, nopadding: true, icon: "fa-list" });

        /* is magazijnlocatie gevuld bij een artikel dan tonen we die ook */
        var aantmagloc = 0;
        for (var i = 0; i < bes_bestelopdr.artikel_arr.length; i++)
        {
             if (bes_bestelopdr.artikel_arr[i].bes_mag_loc)
               aantmagloc++;
        }
%>

        <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>
          <% if (aantmagloc > 0) { %>
            <th><%=L("lcl_bes_notitie")%></th>
          <%}%>
          <th><%=L("lcl_bes_item_comment")%></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)%>
<%          // En de flexkenmerken per artikel
            for (var j = 0; j < bes_bestelopdr.artikel_arr[i].flex.length; j++)
            {%>
                <div class="besitemflex">
                  <span><%=safe.html(bes_bestelopdr.artikel_arr[i].flex[j].knaam)%>:&nbsp;<%=safe.html(bes_bestelopdr.artikel_arr[i].flex[j].kwaarde)%></span>
                </div>
<%          }%>
            </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>
            <% if (aantmagloc > 0) { %>
            <td><%=safe.html(bes_bestelopdr.artikel_arr[i].bes_mag_loc)%></td>
            <%}%>
            <td><%=safe.html(bes_bestelopdr.artikel_arr[i].opmerk)%></td>
          </tr>
<%      }
        var total = bes_bestelopdr.totaal - bes_bestelopdr.korting + bes_bestelopdr.levkosten;

        if (bes_bestelopdr.korting > 0)
        { %>
          <tr>
              <td align="left" colspan="5"><label><%=L("lcl_bes_order_discount")%></label></td>
              <td align="right">
                  <span><%=safe.curr(bes_bestelopdr.korting * -1)%></span>
              </td>
          </tr>
<%      }

        if (bes_bestelopdr.levkosten > 0)
        { %>
          <tr>
              <td align="left" colspan="5"><label><%=L("lcl_bes_tpcosts")%></label></td>
              <td align="right">
                  <span><%=safe.curr(bes_bestelopdr.levkosten)%></span>
              </td>
          </tr>
<%      } %>
        <tr class="besitemstotal">
            <td align="left" colspan="5"><label><%=L("lcl_Total")%></label></td>
            <td align="right">
                <span><%=safe.curr(total)%></span>
            </td>
        </tr>
<%  BLOCK_END();
%>
      </form>
      <% SUBFRAME_END(); %>
  </body>
</html>
<% ASPPAGE_END(); %>
