<%@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"] })

var opdr_key = getQParamIntArray("opdr_key", 0);
var isMulti = getQParamInt("is_multi", 0);
var this_bestelopdr = bes.func_enabled_bestelopdracht(opdr_key);
var bes_bestelopdr = bes.bestelopdracht_info(opdr_key);
user.auth_required_or_abort(this_bestelopdr.canDeliver);

%>

<html>
  <head>
<%  FCLTHeader.Generate(); %>
    <script type="text/javascript">

      $(function()
      {
        $('textarea').resize(function () { FcltMgr.resized(window) } );
        $('textarea').autogrow();
      });

       function hasInStock()
       {
         var max_o_i = $("#max_o_i").val();
         var art_text = "";
         var count_insuf = 0;
         for (var i = 0; i < max_o_i; i++)
         {
           var instock = parseInt($("#instock" + i).val(), 10);
           var delivered = parseInt($("#cnt" + i).val(), 10) || 0;
           var retour = instock < 0 || delivered < 0;
           if ((retour && instock > delivered) || (!retour && instock < delivered))
             return false;
         }
         return true;
       }

       function opdr_submit()
       {
         var max_o_i = $("#max_o_i").val();
         var valid = true;
         for (var i = 0; i < max_o_i; i++)
         {
           $("#cnt" + i).blur();
           valid = valid && !$("#cnt" + i).hasClass("bad");
         }
         if (!valid) return false;

         if (hasBadFields() || !hasInStock())
         {
            FcltMgr.alert(L("lcl_shared_validator_invalid"));
            return false;
         }
         var data = $(document.forms.u2).serialize();
         $.post(document.forms.u2.getAttribute("action"), data, FcltCallbackRefresh, "json");
//         document.forms.u2.close();
         FcltMgr.closeDetail();
         return true; // disable button
       }

    </script>
  </head>
<%

  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="fclt-modal" id="mod_beslever">
  <% MODAL_START(); %>
    <form action="opdr_delivery_save.asp?opdr_key=<%=opdr_key%>&urole=bo&submit=1&is_multi=<%=isMulti%>" 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"
          + "     , b.bes_bestelopdr_opmerking"
          + "     , bes.prs_perslid_key"
          + "     , boi.bes_bestelopdr_item_key"
          + "     , s.bes_srtdeel_nr"
          + "     , b.bes_bestelopdr_delivery_opmerk"
          + "     , bdp.bes_disc_params_ins_mode"
          + "     , CASE"
          + "       WHEN bes.bes_bestelling_retourvan_key IS NOT NULL"
          + "       THEN 1"
          + "       ELSE 0"
          + "       END isRetour"
          + "     , s.ins_srtdeel_key"
          + "  FROM ins_tab_discipline d"
          + "     , bes_disc_params bdp"
          + "     , 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 d.ins_discipline_key = bdp.bes_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;

      var delivery_place = (oRs("mld_adres_bezoek_adres").Value
                  ? oRs("mld_adres_bezoek_adres").Value + ' '
                    + oRs("mld_adres_bezoek_postcode").Value + ' '
                    + oRs("mld_adres_bezoek_plaats").Value
                  : "");

      MODAL_BLOCK_START("besdelivery", L("lcl_bes_delvery_h_pref") + S("bes_bestelopdr_prefix") + ordernr_id + L("lcl_bes_delvery_h_suf"), { icon: "fa-shipping-fast" });
      ROFIELDTR("fld", L("lcl_bes_Supplier"), oRs("prs_bedrijf_naam").value);
      RWFIELDTR("pakbon", "fld", L("lcl_bes_bestellevr_pakbon"), "");
      RWTEXTAREATR("notsat",
                   "fldtxt",
                   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"), delivery_place, {suppressEmpty: true});
      MODAL_BLOCK_END();
      MODAL_BLOCK_START("besItems", L("lcl_bes_bestelling_items"), { icon: "fa-list", nopadding: true });
%>
      <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 fitemoms = oRs("bes_srtdeel_omschrijving").value;
        var fitemeenh = oRs("bes_srtdeel_eenheid").value || "";
        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;

        var ins_mode = oRs("bes_disc_params_ins_mode").Value; // Discipline is voor elke regel hetzelfde. Dus de mode ook.
        var instock;
        var isRetour = oRs("isRetour").Value == 1;
        var ins_srtdeel_key = oRs("ins_srtdeel_key").Value || -1;
        if (!isRetour && ins_mode > 0 && ins_srtdeel_key > 0)
        {
          var sql_instock = bes.getMatchObjectsSql(ins_srtdeel_key, bi_key, {getcount: true, retour: isRetour}); // isRetour is false.
          var oRsI = Oracle.Execute(sql_instock);
          instock = oRsI("aantal").Value;
          oRsI.Close();
        }

        // Zoek de gegevens van het artikel uit alle artikelgegevens van de bestelopdracht.
        var artikel_data = {};
        for (var i=0; i<bes_bestelopdr.artikel_arr.length; i++)
        {
            if (bes_bestelopdr.artikel_arr[i].bes_item_key == bi_key)
                artikel_data = bes_bestelopdr.artikel_arr[i];
        }
        %>
        <tr>
            <td><%=safe.html(oRs("bes_srtdeel_nr").value)%></td>
            <td>
                <%=safe.html(fitemstr)%>
<%              // En de flexkenmerken per artikel
                for (var j = 0; j < artikel_data.flex.length; j++)
                {%>
                    <div class="besitemflex">
                      <span><%=safe.html(artikel_data.flex[j].knaam)%>:&nbsp;<%=safe.html(artikel_data.flex[j].kwaarde)%></span>

                    </div>
<%              }%>
            </td>
            <td align="right" valign="top"><nobr>
<%              if (!isRetour && ins_mode > 0 && ins_srtdeel_key > 0)
                { %>
                  <input type="hidden" id="itemeenh<%=count%>" name="itemeenh<%=count%>" value="<%=safe.html(fitemeenh)%>">
<%              } %>
<%              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%>"
                       class="fld" 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%>">
<%              if (!isRetour && ins_mode > 0 && ins_srtdeel_key > 0)
                { %>
                  <input type="hidden" id="instock<%=count%>" name="instock<%=count%>" value="<%=instock%>">
<%              } %>
                </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();
%>
      <script type="text/javascript">
        var ins_mode = <%=ins_mode%>;
        var ins_srtdeel_key = <%=ins_srtdeel_key%>;
        var isRetour = <%=isRetour? 1 : 0%> == 1;
      </script>
      <input type='hidden' id='max_o_i' name='max_o_i' value='<%=count%>'>
<%    MODAL_BLOCK_END();
      SIMPLE_BLOCK_START();
        var buttons = [{title: L("lcl_bes_complete"), icon: "fa-check", action: "complete();", id:"btn_bes_del_complete", importance: 1}];
        CreateButtons(buttons, { showIcons: true });
      SIMPLE_BLOCK_END();
      SIMPLE_BLOCK_START();
      var buttons1 = [{title: L("lcl_submit"), action: "opdr_submit();", singlepress: true, id: "btn_delivery_submit", importance: 1, icon: "fa-fclt-save"},
                     {title: L("lcl_cancel"), action: "FcltMgr.closeDetail(window, {cancel: true});", importance: 3, icon: "fa-fclt-cancel"}];
      CreateButtons(buttons1);
      SIMPLE_BLOCK_END();
IFACE.FORM_END();
%>
    </form>
    <% MODAL_END(); %>
  </body>
</html>
<% ASPPAGE_END(); %>
