<%@ LANGUAGE = JavaScript %>
<% /*
     $Revision$
     $Id$

     File:               cnt_split.asp
     Description:        Splitten van een contract door aanpassen huidige contract en het aanmaken van een nieuw (gesplit) contract.
     Parameters:         cnt_key              Contract key
     Context:
     Note:
*/ %>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../Shared/kenmerk_common.inc" -->
<!-- #include file="../Shared/calendar.inc" -->
<!-- #include file="cnt.inc" -->

<%
var cnt_key = getQParamInt("cnt_key");

var cnt_info = cnt.cnt_contract_info(cnt_key);
var this_cnt = cnt.func_enabled_contract(cnt_key, cnt_info); // Wat heb ik zoal aan rechten op dit contract?
user.auth_required_or_abort(this_cnt.canSplit); // Kan ik dit contract splitten

var sql = "SELECT cnt_contract_looptijd_van mindate"
        + "     , cnt_contract_looptijd_tot maxdate"
        + "     , cnt_contract_nummer_intern"
        + "     , COALESCE(cnt_contract_kosten, 0) kosten"
        + "  FROM cnt_contract"
        + " WHERE cnt_contract_key = " + cnt_key;
var oRs = Oracle.Execute(sql);
// bestaat zeker
var mindate = new Date(oRs("mindate").value);
var maxdate = new Date(oRs("maxdate").value);
var internr = oRs("cnt_contract_nummer_intern").value;
var kosten = oRs("kosten").value;

%>
<html>
  <head>
<%  FCLTHeader.Generate()
    var submitting = getQParamInt("submit", 0) == 1;
    if (submitting)
    {
      protectRequest.validateToken();
      splitdate = getFParamDate("splitdate");
      splitdate.setDate(splitdate.getDate() + 1); // Startdatum nieuwe contract is 1 dag later dan einddatum oude contract
      cnt_amount = getFParam("cnt_amount", "LEEG"); // Hier nog geen replace(...) en parseFloat(...) doen omdat parameter leeg kan zijn.
      if (cnt_amount == "" || cnt_amount == "LEEG") // 0 mag wel!
        cnt_amount = "null";
      else
      {
        cnt_amount = parseFloat(cnt_amount.replace(/,/g,"."));
      }
      sql = "BEGIN cnt.splitscontract (" + cnt_key + ", " + splitdate.toSQL() + "," + user_key + "," + cnt_amount + "); END;";
      oRs = Oracle.Execute(sql);

      sql = "SELECT cnt.getnieuwsteversiekey(" + cnt_key + ") newkey FROM DUAL";
      oRs = Oracle.Execute(sql);
      var newkey = oRs("newkey").value;

      var flexsql = "SELECT k_old.cnt_kenmerk_key k_old"
                  + ", k_old.cnt_kenmerk_key k_new"
                  + " FROM cnt_srtkenmerk t"
                  + ", cnt_discipline s"
                  + ", cnt_kenmerk k_old"
                  + ", cnt_contract c_old"
                  + " WHERE c_old.cnt_contract_key = " + cnt_key
                  + " AND s.ins_discipline_key = c_old.ins_discipline_key"
                  + " AND k_old.cnt_srtcontract_key = s.ins_discipline_key"
                  + " AND t.cnt_srtkenmerk_kenmerktype IN ('F', 'E', 'M') "
                  + " AND k_old.cnt_kenmerk_verwijder IS NULL "
                  + " AND k_old.cnt_srtkenmerk_key = t.cnt_srtkenmerk_key "
                  + " AND t.cnt_srtkenmerk_verwijder IS NULL";
      CopyFlexFolders("CNT/" + subfolderKey("C", cnt_key), "CNT/" + subfolderKey("C", newkey), flexsql);

      %><script>FcltMgr.closeDetail(window, { refresh: true } );</script><%
      Response.End;
    }
    else
    { %>
      <script type="text/javascript">
        function checkInput()
        {
          if ($("#cnt_amount") && $("#cnt_amount")[0] && $("#cnt_amount").val() != "")
          {
            var amount = $("#cnt_amount").val();
            if (amount < 0 || amount > <%=safe.jsfloat(kosten)%>)
            {
              alert (L("lcl_cnt_exceed_cnt_kosten"));
              $("#cnt_amount")[0].select();
              $("#cnt_amount")[0].focus();
              return false;
            }
          }
          return true;
        }

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

          if (!checkInput())
            return false;

          document.forms.u2.submit();
        }
      </script>
<%  } %>
  </head>

  <body class="modal" id="mod_cntsplit">
    <form name=u2 action=cnt_split.asp?submit=1&cnt_key=<%=cnt_key%> method="post">
<%
      BLOCK_START("cntSplit", L("lcl_cnt_newversion_make") + internr);

      var defaultdatum = new Date;  // vandaag
      FCLTcalendar( "splitdate",
          { label : L("lcl_cnt_einddatum_old_cnt"),
            datum: defaultdatum,
            initEmpty: (defaultdatum > maxdate || defaultdatum < mindate),
            minDate : mindate,
            maxDate: maxdate,
            timeField: false,
            autoopen: true,
            required: true
          });

      if (kosten > 0)
        RWFIELDTR("cnt_amount", "fldcntamount", L("lcl_cnt_eindbedrag_old_cnt") + " " + L("lcl_valutasign"), "", {datatype: "currency"});

      BLOCK_END();
      IFACE.FORM_END();
%>
    </form>
<%
    CreateButtons([ { title: L("lcl_submit"), action: "cnt_split_submit()" },
                    { title: L("lcl_cancel"), action: "FcltMgr.closeDetail(window, { cancel: true })"} ]);
%>
  </body>
</html>