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

    File:               opdr_delivery.asp
    Description:        Invoeren van de levering van een opdracht
    Parameters:         opdr_key
    Context:            Vanuit overzicht of self-submitting
    Note:               De code is ondersteunt nog enigszins dat één opdracht meerdere bestellingen
                        kan bedienen (bundelingen). Zulke opdrachten worden echter nooit aangemaakt.

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

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

<%
FCLTHeader.Requires({ plugins: ["jQuery"],
                      js: ["./opdr_delivery.js", "num2curr.js"] })

var opdr_key = getQParamInt("opdr_key");
var submitting = getQParamInt("submit", 0) == 1;

var this_bestelopdr = bes.func_enabled_bestelopdracht(opdr_key);
user.auth_required_or_abort(this_bestelopdr.canDeliver);
%>

<html>
  <head>
<%  FCLTHeader.Generate(); %>
    <script type="text/javascript" >
      jQuery(document).ready(function() {
            $('textarea').resize(function () { FcltMgr.resized(window) } );
            $('textarea').autogrow();
       });

       function opdr_submit()
       {
         document.forms.u2.submit();
         return true; // disable button
       }
    </script>
  </head>
<%

  if (submitting)
  {
    protectRequest.validateToken();
    max_o_i = getFParamInt( 'max_o_i');
    var trackarray = [];
    var besopdr_id = "";
    for (i = 0; i < max_o_i; i++)
    {
      ontv = getFParamInt('cnt' + i, 0);
      oldcnt = getFParamInt('amto' + i);
      bokey = getFParamInt('bokey' + i);
      bikey = getFParamInt('bi_key' + i);

      if ( !isNaN(ontv) && ontv != 0 && !isNaN(oldcnt))
      {
        sql = "UPDATE bes_bestelopdr_item B"
            + "   SET bes_bestelopdr_item_aantalontv = "
            + "           COALESCE(bes_bestelopdr_item_aantalontv, 0) + " + ontv
            + "     , bes_bestelopdr_item_ontvangen = SYSDATE"
            + " WHERE bes_bestelopdr_item_key = " + bokey;
        Oracle.Execute(sql);

        sql = "UPDATE bes_bestelling_item"
            + "   SET bes_bestelling_item_aantalontv = "
            + "           COALESCE(bes_bestelling_item_aantalontv, 0) + " + ontv
            + " WHERE bes_bestelling_item_key = " + bikey;
        Oracle.Execute(sql);

        // Tracking
        sql = "SELECT boi.bes_bestelopdr_item_omschrijv"
            + "     , bo.bes_bestelopdr_id"
            + "  FROM bes_bestelopdr_item boi"
            + "     , bes_bestelopdr bo"
            + " WHERE boi.bes_bestelopdr_key = bo.bes_bestelopdr_key"
            + "   AND bes_bestelopdr_item_key = " + bokey;
        oRs = Oracle.Execute(sql);
        var item_oms = oRs("bes_bestelopdr_item_omschrijv").Value;
        besopdr_id = oRs("bes_bestelopdr_id").Value;

        trackarray.push(L("lcl_bes_delivery") + ": " + ontv + " " + item_oms);
      }
    }
    // Laat de gevolgen bepalen..
    bes.updatebestelopdrstatus(opdr_key);
    // moet ik verdorie de bestelling_key nog speciaal ophalen..
    sql = "SELECT bes_bestelling_key"
        + "  FROM bes_bestelling_item"
        + " WHERE bes_bestelling_item_key = " + bikey;
    var oRs = Oracle.Execute(sql);
    bes.updatebestellingstatus(oRs("bes_bestelling_key").value);
    oRs.Close();

    var newremark = getFParam("notsat", "");

    if (newremark != "")
    {
      var fields = [{dbs: "bes_bestelopdr_delivery_opmerk", typ: "varchar", val: newremark, len: 320 }];
      sql = buildUpdate("bes_bestelopdr", fields) + " bes_bestelopdr_key = " + opdr_key;
      Oracle.Execute(sql);
    }

    // Update nog tracken
    shared.trackaction("BES2UP",
                       opdr_key,
                       L("lcl_bes_is_bes2uptrack").format(S("bes_bestelopdr_prefix") + besopdr_id) + "\n" + trackarray.join("\n"));

%>  <script>FcltMgr.closeDetail(window, { } );</script>
<%  Response.End;
  }
  // end submitting

  var sql = "SELECT bes_bestelopdr_id "
          + "  FROM bes_bestelopdr "
          + " WHERE bes_bestelopdr_key = " + opdr_key;
  oRs = Oracle.Execute(sql);
  ordernr_id = oRs("bes_bestelopdr_id").value;
  oRs.Close();

%>
  <body class="modal" id="mod_beslever">
    <form action="opdr_delivery.asp?opdr_key=<%=opdr_key%>&submit=1" method=post name=u2 onsubmit="return false;">
<%    sql = "SELECT DISTINCT b.bes_bestelopdr_key,"
          + "       bd.prs_bedrijf_naam,"
          +         lcl.xsqla('s.bes_srtdeel_omschrijving','s.bes_srtdeel_key') +", "
          +         lcl.xsqla('s.bes_srtdeel_eenheid','s.bes_srtdeel_key') +", "
          + "       boi.bes_bestelopdr_item_prijs,"
          + "       bi.bes_bestelling_item_aantal,"
          + "       s.bes_srtdeel_key,"
          + "       bi.bes_bestelling_item_key,"
          + "       boi.bes_bestelopdr_item_aantal,"
          + "       boi.bes_bestelopdr_item_aantalontv,"
          + "       m.mld_adres_bezoek_adres || ' ' ||"
          + "         m.mld_adres_bezoek_postcode || ' ' ||"
          + "         m.mld_adres_bezoek_plaats delivery_place, "
          + "       b.bes_bestelopdr_opmerking,"
          + "       bes.prs_perslid_key,"
          + "       boi.bes_bestelopdr_item_key,"
          + "       s.bes_srtdeel_nr,"
          + "       b.bes_bestelopdr_delivery_opmerk"
          + "  FROM ins_tab_discipline d, "
          + "       bes_srtgroep g,"
          + "       bes_srtdeel s,"
          + "       prs_bedrijf bd,"
          + "       bes_bestelopdr b,"
          + "       mld_adres m,"
          + "       bes_bestelling_item bi,"
          + "       bes_bestelopdr_item boi,"
          + "       bes_bestelling bes"
          + " WHERE d.ins_discipline_key = g.ins_discipline_key AND"
          + "       g.bes_srtgroep_key = s.bes_srtgroep_key AND"
          + "       s.bes_srtdeel_key = bi.bes_srtdeel_key AND"
          + "       bi.bes_bestelling_key = bes.bes_bestelling_key AND"
          + "       b.bes_bestelopdr_key = boi.bes_bestelopdr_key AND"
          + "       bi.bes_bestelopdr_item_key = boi.bes_bestelopdr_item_key AND"
          + "       b.prs_bedrijf_key = bd.prs_bedrijf_key AND"
          + "       m.mld_adres_key(+)= b.mld_adres_key_lev AND"
          + "       b.bes_bestelopdr_key = " + opdr_key
          + " ORDER BY bes_srtdeel_nr";

      oRs = Oracle.Execute(sql);
      count = 0;

      BLOCK_START("besdelivery", L("lcl_bes_delvery_h_pref") + S("bes_bestelopdr_prefix") + ordernr_id + L("lcl_bes_delvery_h_suf"));
      ROFIELDTR("fld", L("lcl_bes_Supplier"), oRs("prs_bedrijf_naam").value);
      RWTEXTAREATR("notsat",
                   "fldremark",
                   L("lcl_bes_not_satisfied") + " " + L("lcl_bes_your_comment"),
                   oRs("bes_bestelopdr_delivery_opmerk").value,
                   {html: "rows='3'"});
      ROFIELDTR("fld", L("lcl_bes_adres_lev"), oRs("delivery_place").value);
      BLOCK_END();

      BLOCK_START("besItems");
%>
      <tr>
          <th><%=L("lcl_bes_srtdeel_nr")%></th>
          <th><%=L("lcl_bes_srtdeel")%></th>
          <th><%=L("lcl_bes_total_order")%></th>
          <th style="text-align:right"><%=L("lcl_bes_delivered_so_far")%></th>
          <th style="text-align:right"><%=L("lcl_bes_delivered_now")%></th>
          <th style="text-align:right"><%=L("lcl_bes_remaining")%></th>
      </tr>
<%    while(!oRs.eof)
      {
        var fitemstr = oRs("bes_srtdeel_omschrijving").value + " (" + S("currency_pref") + safe.curr(oRs("bes_bestelopdr_item_prijs").value || 0)
                 + S("currency_suff") + "/" + (oRs("bes_srtdeel_eenheid").value || "") + ")";
        var fitemcnt   = oRs("bes_bestelopdr_item_aantal").value;
        var fitemcnttv = oRs("bes_bestelopdr_item_aantalontv").value || 0;
        var oikey      = oRs("bes_bestelopdr_item_key").value;
        var bi_key     = oRs("bes_bestelling_item_key").value;

        %>
        <tr>
            <td><%=safe.html(oRs("bes_srtdeel_nr").value)%></td>
            <td><%=safe.html(fitemstr)%></td>
            <td align='right' valign='top'><nobr>
<%              if (fitemcnttv != -1)
                { %>
                    <%=fitemcnt%>
                    <input type='hidden' id='amto<%=count%>' name='amto<%=count%>'
                    value='<%=fitemcnttv%>'>
<%              }%>
                </nobr>
                </td>
                <td align='right' valign='top'>
<%              if (fitemcnttv != -1){%>
                    <%=fitemcnttv%>
<%              }
                else
                { %>
                  0
<%              } %>
            </td>
            <td align='right' valign='top'><NOBR>
                <input type="text" maxlength=5 style="width:50px;text-align:right"
                       onBlur='checkInput(event);' id='cnt<%=count%>' name='cnt<%=count%>'
                       value=''>
                <input type='hidden' id='bokey<%=count%>' name='bokey<%=count%>' value='<%=oikey%>'>
                <input type='hidden' id='bi_key<%=count%>' name='bi_key<%=count%>' value='<%=bi_key%>'>
                </NOBR>
            </td>
            <td align='right' valign='top' id='remtd<%=count%>'>
                <%=fitemcnt-fitemcnttv%>&nbsp;
                <input type='hidden' id='rem<%=count%>' name='rem<%=count%>' value='<%=fitemcnt-fitemcnttv%>'>
            </td>
        </tr>
<%
        oRs.MoveNext();
        count++;
      }
      oRs.Close();

      BLOCK_END();
%>
      <input type='hidden' id='max_o_i' name='max_o_i' value='<%=count%>'>
<%    var buttons = [{title: L("lcl_bes_complete"), action: "complete();"},
                     {title: L("lcl_submit"), action: "opdr_submit();", singlepress: true, id: "btn_delivery_submit"},
                     {title: L("lcl_cancel"), action: "FcltMgr.closeDetail(window, {cancel: true});"}
                    ];
      CreateButtons(buttons);
IFACE.FORM_END();
%>
    </form>
  </body>
</html>
