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

    File:               bes_show_bestelling.asp
    Description:        Toon bestelling
    Parameters:         urole (required)
                        bes_key (required)
                        -
    Context:
    Note:

*/ %>
<!--#include file="../Shared/common.inc" -->
<!--#include file="../Shared/iface.inc" -->
<!--#include file="../Shared/persoonselector.inc" -->
<!-- #include file="../Shared/bedrijfselector.inc" -->
<!--#include file="../Shared/selector.inc" -->

<!--#include file="../Shared/plaatsselector.inc" -->
<!--#include file="../Shared/kostenplaatsselector.inc" -->
<!-- #include file="../Shared/json2.js" -->
<!-- #include file="../Shared/rater.inc" -->

<!-- #include file="bes_flexkenmerk.inc" -->
<!-- #include file="bes.inc" -->
<!-- #include file="Details.inc" -->
<%
FCLTHeader.Requires({plugins:["suggest","jQuery"],
                          js: ["date.js", "./bes_bestelling.js",
                               "./sel_items_tab.js", "num2curr.js"] })

// AUTORISATIEPARAMETERS
   var urole = getQParamSafe("urole");
   var bes_key = getQParamInt("bes_key");

   fronto=true;
   backo=false;
   frontend=false;

  var bes_bestelling = bes.bestelling_info(bes_key); // Globale variabele met alle bes_bestelling informatie
  var bes_disc_info = bes.disc_info(bes_bestelling.dis_key); // alle discipline informatie
  var this_bes = bes.func_enabled_bestelling(bes_bestelling);

  user.auth_required_or_abort(this_bes.canReadAny);
  var subjectMail = L("lcl_bes_RFO") + S("bes_bestelling_prefix") + bes_key;
  var hasFOBOread = this_bes.canReadFOF || this_bes.canReadBOF;
  var urlMail = "../shared/queuemail.asp?pcode=BESMAI&defemail_key=" + bes_bestelling.perslid_key
              + "&key=" + bes_key
              + "&subject=" + Server.URLencode(subjectMail)
              + "&mailfrom=" + (hasFOBOread? 1 : 0);
%>
<html>
<head>
<%
   FCLTHeader.Generate();

%>
<script  type="text/javascript" >
  var details_Items = new Array(); // voor details.inc
</script>
  <% generateDetails(bes_key, false); %>
<script  type="text/javascript" >
  jQuery(document).ready(function() {
        $("#besFlex").toggle($("#k_all").val()>0); // verbergen als leeg
        $("#besFlex2").toggle($("#k_all").val()>0); // verbergen als leeg
        $('textarea').resize(function () { FcltMgr.resized(window) } );
        $('textarea').autogrow();
   });

    function bes_change()
    {

<% if (bes_bestelling.rfoStatus == 5) { %> // extra waarschuwing
        if (confirm(L("lcl_bes_confirm_embargo")))
<% } %>
        if (FcltMgr.startEdit(window))
            window.location.href = "bes_edit_bestelling.asp?urole=<%=urole%>&bes_key=<%=bes_key%>&embedded=1"
    }
    function bes_delete()
    {
        var txt = "";
<% if (bes_bestelling.rfoStatus == 5) { %> // extra waarschuwing
        txt = L("lcl_bes_confirm_embargo") + "\n";
<% } %>
        if (confirm(txt + "<%=L("lcl_bes_delete_1") + S("bes_bestelling_prefix") + bes_key + L("lcl_bes_delete_2")%>") )
        {
            var data = { urole: "<%=urole%>",
                         bes_key: <%=bes_key%>
                       };
            <% protectRequest.dataToken("data"); %>
            $.post("bes_delete.asp",
                   data,
                   function(json, textStatus)
                   {
                     if (json.change)
                       FcltCallbackRefresh(json, textStatus); // Er is een change bestelling aangemaakt
                     else
                       FcltCallbackClose(json, textStatus); // Bestelling is verwijderd
                   },
                   "json");
        }
    }
    function bes_mail()
    {
      var subject = "<%=safe.jsstring(subjectMail)%>";
      var url = "<%=safe.jsstring(protectQS.create(urlMail))%>";
      FcltMgr.openModalDetail(url, subject);
    }

    function bes_print() {
        window.open('./bes_opdr_xml.asp?bes_key='+'<% = bes_key %>').focus();
    }

</script>
</head>
<%
    var cls = "";
    if (bes_bestelling.parent_key > 0)
        cls = "besischange";
    else
        if (bes_bestelling.change_key)
        cls = "beshaschange";
%>
<body id="showbody" class="<%=cls%>" onload="AddAllItems(false, true)"> <% /* Not editable, show received amount */ %>
<%
// TODO: Accepteren door BO erbij
var buttons =  [];

  if (this_bes.canChange)
    buttons.push({title: L("lcl_change"), icon: "wijzigen.png", action: "bes_change()"});

  if (this_bes.canAccept ||
      this_bes.canAccept2 ||
      this_bes.canAccept3 ||
      this_bes.canAccept4 ||
      this_bes.canAccept5)
  {
    buttons.push({ icon: "accept.png", title: L("lcl_approve"), action: "bes_accept()" });
  }

  if (this_bes.canReject)
    buttons.push({ icon: "reject.png", title: L("lcl_reject"), action: "bes_reject()" });

  if (bes_bestelling.fac_activiteit_key == -1)
  {
    if (this_bes.canDelete)
      buttons.push({ icon: "delete.png", title: L("lcl_delete"), action: "bes_delete()" });

    if (this_bes.canApprove)
    {
      buttons.push({ icon: "accept.png", title: L("lcl_approve"), action: "bes_approve()" });
      buttons.push({ icon: "reject.png", title: L("lcl_reject"), action: "bes_reject()" });
    }

    if (this_bes.canRetourURL)
      buttons.push({ icon: "terugzet.png", title: L("lcl_bes_retour"), action: "window.location.href='" + bes_bestelling.rfoRetourURL + "';"});

    if (this_bes.canRetourBES) // Retouren via negatief bestellen
      buttons.push({  icon: "terugzet.png", title: L("lcl_bes_retour"), action: "RetourItems("+bes_key+",'"+urole+"');"});
  }

buttons.push({ icon: "print.png",    title: L("lcl_print"),               action: "bes_print()"});
buttons.push({ icon: "email.png",    title: L("lcl_noti_bes_aanv_email"), action: "bes_mail()" });

IFRAMER_HEADER((bes_bestelling.fac_activiteit_key > 0? L("lcl_bes_frame_algemeen_actie") : L("lcl_bes_frame_algemeen")), buttons);
%>
<div id="show">
<form name=u2 onSubmit="return false;">

<% BLOCK_START("besHeader", L("lcl_bes_aanvragergegevens"));

   FCLTpersoonselector(
         "person",
         "sgPerson",
       { perslidKey: bes_bestelling.perslid_key,
         label: L("lcl_name"),
         readonly: true,
         moreinfo: !frontend
       });

   if (bes_disc_info.disc_params_for_others)
   {
     FCLTpersoonselector(
           "personFor",
           "sgPersonFor",
         { perslidKey: bes_bestelling.perslid_key_voor,
           label: L("lcl_bes_order_for"),
           readonly: true,
           moreinfo: !frontend
         });
   }

   FCLTkostenplaatsselector("account",
                            "sgAccount",
                            bes_bestelling.perslid_key,
                           { kostenplaatsKey: bes_bestelling.kostenplaats_key,
                             label: L("lcl_account"),
                             readonly: true,
                             moreinfo: !frontend
                          });

    ROFIELDTR ("fld", L("lcl_bes_deli_date"), toDateString(bes_bestelling.rfoDeliverydate));
    ROFIELDTR ("deliveryAddr", L("lcl_delivery_address"), bes_bestelling.mld_adres_naam);
    ROFIELDTR ("fld", L("lcl_bes_del_room"), bes_bestelling.afleverruimte);
    if (S("show_ordernr")!=0) {
        ROFIELDTR ("fld", L("lcl_mld_inf_Ordernr"), bes_bestelling.orderNr);
    }
BLOCK_END();

bes.generateBesInfoBlock(bes_bestelling, true);

BLOCK_START("besFlex"+(S("bes_flexcolumns")!=1?"2":"") , L("lcl_bes_flexblok"));
        %><tr><td class="besflex"><%
    generateFlexKenmerkCode ({ urole: urole,
                               bes_key: bes_bestelling.bes_key, // Bestelling key
                               disc_key: bes_bestelling.dis_key,
                               kenmerktype: "B",
                               reado: true,
                               flexcolumns: S("bes_flexcolumns")
                             });
        %></td></tr><%
BLOCK_END();

// enkele vars voor AddAllItems
  show_price = (backo ? 1 : (fronto ? (S("bes_show_RFO_price") > 0 ? 1 : 0) : (S("bes_show_RFO_price") == 2 ? 1 : 0)));
%>
<script type="text/javascript">
var currency_pref = '<%=S("currency_pref")%>';
var currency_suff = '<%=S("currency_suff")%>';
var show_price = '<%=show_price%>';
var lower = false;
var inclBTW = <%=bes_bestelling.inclBTW%> == 1;

function bes_accept()
{
  if (confirm(L("lcl_bes_accepteren1").format("<%=bes_key%>")))
  {
    $.post("bes_accept.asp",
           { urole: "<%=urole%>",
             bes_key: <%=bes_key%> },
           FcltCallbackRefresh,
           "json");
  }
}

function bes_approve()
{
  if (confirm(L("lcl_bes_approve").format("<%=bes_key%>")))
  {
    var data = { bes_key: <%=bes_key%>
               };
    <% protectRequest.dataToken("data"); %>
    $.post("bes_approve.asp",
           data,
           FcltCallbackRefresh,
           "json");
  }
}

function bes_reject()
{
  var url = "../BES/bes_reject.asp?bes_key=<%=bes_key%>";
  FcltMgr.openModalDetail(url, L("lcl_bes_reject_order_req"), {callback: FcltMgr.reload});
}
</script>

<% BLOCK_START("besItems" , L("lcl_bes_geselecteerd") + " " + (bes_bestelling.inclBTW? L("lcl_shared_inclBTW") : L("lcl_shared_exclBTW"))); %>
<tr><td>
<!-- SELECTED ITEMS -->
<table width="100%" id="sel_items" class="rstable">
<thead>
  <tr>
    <th></th>
    <th><%=L("lcl_bes_srtdeel_nr")%></th>
    <th><%=L("lcl_bes_srtdeel")%></th>
    <th><%=L("lcl_bes_unity")%></th>
    <th style="text-align:right"><%=L("lcl_bes_price_per_unity")%></th>
    <th style="text-align:right"><%=L("lcl_btw_percentage")%></th>
    <th style="text-align:right"><%=L("lcl_bes_amount")%></th>
    <th style="text-align:right"><%=L("lcl_bes_geleverd")%></th>
    <th style="text-align:right" <% = show_price ? "" : " style='display:none;'" %> ><%=L("lcl_bes_subtotal")%></th>
  </tr>
</thead>
<tbody>
<!--hier komen de geselecteerde artikelen-->
</tbody>
</table>
<br>
<%
 if (show_price)
 {
    %>
    <table align="right"><%
      if (bes_bestelling.rfoBesKosten||0 != 0)
        ROFIELDTR("fldtotkosten", L("lcl_bes_order_discount"), S("currency_pref") + " " + safe.curr(bes_bestelling.rfoBesKosten * -1), {html: "style='text-align:right'"} );

      if (bes_bestelling.rfoLevKosten||0 != 0)
        ROFIELDTR("fldtotkosten", L("lcl_bes_tpcosts"), S("currency_pref") + " " + safe.curr(bes_bestelling.rfoLevKosten), {html: "style='text-align:right'"} );

      var totaal = bes_bestelling.totaal - bes_bestelling.rfoBesKosten + bes_bestelling.rfoLevKosten;
      ROFIELDTR("fldtotkosten", L("lcl_total"), S("currency_pref") + " " + safe.curr(totaal), {html: "style='text-align:right'"} );
    %></table><%
}
%></td></tr><%
   BLOCK_END();
%>
</form>
</div> <!-- edit -->
</body>
</html>
