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

    File:               pda/factuur.asp
    Description:        Invoices data (Mobile version)
    Parameters:

    Status:             opzetje

    Note:               Since Mobile is limited to actual actions, only invoices that need approval by me are shown

*/ %>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../fin/fin.inc" -->
<!-- #include file="../fin/fin_flexkenmerk.inc" -->
<!-- #include file="./resultset_table.inc" -->
<!-- #include file="./mobile.inc" -->
<!-- #include file="./iface.inc" -->
<%
var fin_key = getQParamInt("fin_key");
var qrc = getQParamInt("qrc", 0) != 0;
var mode = "F"; // Mobile alleen Fiatteren(F). Nog geen goedkeuren(G).

var this_fin = fin.func_enabled_factuur(fin_key); // Wat heb ik zoal aan rechten
var fin_factuur = this_fin.factuur_info;
var ref_info = this_fin.referentieinfo;

var subject = L("lcl_fin_invoice") + " " + fin_key;
%>
<html>
<head>
<% FCLTMHeader.Generate({title: subject}); %>
    <script type="text/javascript">
        // draft, kopie van bes
      function fin_approve()
      {
        if (confirm('<%=L("lcl_fin_approve_factuur").format(fin_key)%>'))
        {
          var data = { fin_key: <%=fin_key%>
                     };
          <% protectRequest.dataToken("data"); %>
          $.post("<%=rooturl%>/appl/fin/fin_approve.asp?a=1"
                , data
                , McltCallbackAndThenAlways(fin_approve_callback)
                , "json");
        }
      }
<%    if (fin_key > -1 && this_fin.canAccept) { %>
        function fin_reject_confirm() {
          mobile.changePage("frej-1-<%=fin_key%>");
          return false;
        };
<%    } %>
     function fin_approve_callback()
      {
        //window.location.href = "fiat_list.asp"; // Als je terug bent in fiat_list en doet dan "terug" kom je in de zojuist gefiatteerde bestelling.
        // Het fiatteren van een bestelling is aangeroepen vanuit fiat_list.
        // Dus 1 stap terug in history om daar weer te komen.
        window.history.go(-1);
      }
    </script>
</head>
<body>
<%
  PAGE_START();
  HEADER ({title: subject, back:!qrc, home:!qrc});
  CONTENT_START();

    var nurl="";
    switch (ref_info.ref_type)
    {
      case "B":
      {
        nurl = "../pda/bestelling.asp?bes_key=" + ref_info.bestelling_key;
        break;
      }
      case "C":
      {
        nurl = "../pda/contract.asp?cnt_key=" + ref_info.ref_key;
        break;
      }
      case "O":
      {
        nurl = "../pda/order.asp?opdr_key=" + ref_info.ref_key;
        break;
      }
    }
    ROFIELD("fld", L("lcl_fin_referentie"), ref_info.referentie, {moreinfo : nurl}); // Referentie.
    ROFIELD("flddate", L("lcl_fin_findate"), toDateString(fin_factuur.fin_date)); // Factuur datum
    ROFIELD("fld", L("lcl_fin_invoice_nr_extern"), fin_factuur.fin_nr); // Factuurnummer (extern).
    ROFIELD("fldshort", L("lcl_fin_divide_period"), fin_factuur.fin_boekm != null? fin_factuur.fin_boekm : ""); // Verdeelperiode
    ROFIELD("fld", L("lcl_fin_debtor_nr"), fin_factuur.fin_debiteur, {suppressEmpty: true}); // Eigen debiteurnummer
    ROFIELD("fld", L("lcl_shared_charge_type"), fin_factuur.fin_costsrt); // Kostensoort
    ROFIELD("fld", L("lcl_fin_goedkeurder"), fin_factuur.fin_perslid_goedkeur, {suppressEmpty: true}); // Goedkeurder.
    ROFIELD("fld", L("lcl_shared_created_by"), fin_factuur.fin_perslid_user, {suppressEmpty: true}); // Aangemaakt door
    ROFIELD("fld", L("lcl_fin_fin_status"), fin_factuur.fin_status_oms); // Factuur status
    ROFIELD("fldtxt", L("lcl_fin_remark"), (fin_key != -1? fin_factuur.fin_opm : ""), {suppressEmpty: true, multi: true});
    if (fin_factuur.fin_status_key == 1)
      ROFIELD("fldtxt", L("lcl_fin_remark_reject"), fin_factuur.fin_opm_afw, {multi: true});

    //BLOCK_START({collapsed: true, title: L("lcl_fin_invoicerows")});
      sql = "SELECT fr.fin_factuurregel_key"
                 + "     , fr.fin_factuurregel_totaal"
                 + "     , fr.fin_factuurregel_omschrijving"
                 + "  FROM fin_factuurregel fr"
                 + " WHERE fr.fin_factuur_key = " + fin_key
                 + " ORDER BY fr.fin_factuurregel_nr, fr.fin_factuurregel_key ";
      oRs = Oracle.Execute(sql);
      GRID_START(2);
      while (!oRs.eof) {
         COL_START(1);
              ROFIELD("fldfinreg1", null, oRs.fields("fin_factuurregel_omschrijving").value );
         COL_END();
         COL_START(2);
              ROFIELD("fldfinreg2", null, S("currency_pref")+safe.curr(oRs.fields("fin_factuurregel_totaal").value));
         COL_END();
          oRs.movenext();
       }
       GRID_END();

  //BLOCK_END();

  // Plus flexkenmerken
  generateFlexKenmerkCode ({
     fin_key: fin_key,
     kenmerk_niveau: "F",
     reado          : true,
     mobile: true
  });

  CONTROLGROUP_START();
        if (this_fin.canAccept)
        {
          BUTTON(L("lcl_approve"), {click: "fin_approve()", icon: "fa-check"});
          BUTTON(L("lcl_reject"),  {click: "fin_reject_confirm()",  icon: "fa-times"});
        }
        trackinglines = mobile.hastrackingpage('factuur', fin_key);
        if (trackinglines > 0) {
          BUTTON(L("lcl_history"), {linkid: "#page-2-"+fin_key, icon: "fa-list"});
        }

  CONTROLGROUP_END();
  CONTENT_END();
  FOOTER();
  PAGE_END();

  if (trackinglines > 0) {
      PAGE_START({id: "page-2-"+fin_key, dialog: true})
      mobile.trackingpage('factuur', fin_key, subject);
      PAGE_END();
  }

  if (fin_key > -1 && this_fin.canAccept)
  { // scherm: Afwijzen melding
    PAGE_START({id: "frej-1-" + fin_key, dialog: true})
      HEADER({title: L("lcl_fin_invoice") + " " + fin_key, back: false});
      CONTENT_START();
%>
      <script>
        $(function()
        {
          $("#opmerk").focus();
        });

        async function fin_reject()
        { // Standaard checks op verplichte velden, datum formaat, numeriek en float formaat, currency formaat.
          if (!await validateForm("rejectform"))
            return false;

          $.post( $("form[name=rejectform]")[0].action,
                  $("[name=rejectform]").serialize(),
                  McltCallbackAndThenAlways(fin_reject_close),
                  "json"
                );

          return true; // disable button
        }

        function fin_reject_close(data)
        {
          window.location.href = "<%=rooturl%>/appl/pda/factuur.asp?fin_key=" + "<%=fin_key%>";
        }

        function fin_cancel()
        {
          window.history.back();
        }
      </script>

      <form id="rejectform" name="rejectform" action="<%=rooturl%>/appl/fin/fin_reject_save.asp" method="post">
        <input type="hidden" id="fin_key" name="fin_key" value="<%=fin_key%>">
        <input type="hidden" id="mode" name="mode" value="<%=mode%>">
<%      // Reden afwijzing.
        RWFIELD("opmerk", L("lcl_fin_remark_reject"), "", {name: "opmerk", multi: true, disabled: false, pclass: "fldtxt" + " required"});

        CONTROLGROUP_START();
          BUTTON(L("lcl_reject"), {click: "fin_reject();", icon: "fa-times"});
          BUTTON(L("lcl_cancel"), {datarel: "back", icon: "fa-fclt-cancel"} );
        CONTROLGROUP_END();

        IFACE.FORM_END();
%>
      </form>
<%
      CONTENT_END();
    PAGE_END();
  }



PDA_PAGE_END(); %>
	</body>
</html>
<% ASPPAGE_END(); %>
