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

    File:         cnt_change_cnttype.asp
    Description:  Verander het contract type
                  Impliciet: cnt_info.ins_discipline_key wordt aangepast
                  Expliciet: flexkenmerken moeten worden overgezet naar het nieuwe contracttype met zijn eigen kenmerk keys (cnt_kenmerkcontract.cnt_kenmerk_key)
    Parameters:
      cnt_key        Contract key
      cnt_srt_new_key    Nieuwe contract soort
      submitting     Mode {0=invoer / 1=opslaan}

    Context:      cnt_show_contract.asp wijzigen contractsoort
    Note:

*/ %>

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

<%
FCLTHeader.Requires({ plugins:["jQuery"] })

var cnt_key = getQParamInt("cnt_key");
var this_cnt = cnt.func_enabled_contract(cnt_key);
user.anything_todo_or_abort(this_cnt.canChangeSrt);
%>

<html>
  <head>
<%  FCLTHeader.Generate();
    var submitting = getQParamInt("submit", 0) == 1;
    if (submitting)
    {
      protectRequest.validateToken();
      var cnt_srt_new_key = getFParamInt("cnt_srt_new_key"); // Nieuwe contractsoort

      var cnt_info = cnt.cnt_contract_info(cnt_key);
      var cnt_srt_current_key = cnt_info.discipline_key;

      // Documentfolders hernoemen.
      if (cnt_srt_current_key != cnt_srt_new_key)
      { // Match de kenmerk keys van de oude en nieuwe type
        var flexsql = "SELECT k_old.cnt_kenmerk_key k_old"
                  + ", k_new.cnt_kenmerk_key k_new"
                  + "  FROM cnt_srtkenmerk t"
                  + "     , cnt_kenmerk k_old"
                  + "     , cnt_kenmerk k_new"
                  + " WHERE t.cnt_srtkenmerk_kenmerktype IN ('F', 'E', 'M')"
                  + "   AND ((k_old.cnt_srtcontract_key = " + cnt_srt_current_key + " AND k_new.cnt_srtcontract_key = " + cnt_srt_new_key + ")"
                  + "     OR (k_old.cnt_srtcontract_key IS NULL AND k_new.cnt_srtcontract_key IS NULL))"
                  + "   AND k_old.cnt_kenmerk_niveau = 'S'"
                  + "   AND k_old.cnt_kenmerk_verwijder IS NULL"
                  + "   AND k_old.cnt_srtkenmerk_key = t.cnt_srtkenmerk_key"
                  + "   AND k_new.cnt_kenmerk_niveau = 'S'"
                  + "   AND k_new.cnt_kenmerk_verwijder IS NULL"
                  + "   AND k_new.cnt_srtkenmerk_key = t.cnt_srtkenmerk_key"
                  + "   AND t.cnt_srtkenmerk_verwijder IS NULL"
                  + "   AND k_old.cnt_kenmerk_groep = k_new.cnt_kenmerk_groep"
        RenameFlexFolders(flexsql, "CNT", "C", cnt_key);
      }
      sql_waarde = " (SELECT vkc.cnt_kenmerkcontract_waarde"
                 + "   FROM cnt_kenmerkcontract vkc"
                 + "      , cnt_kenmerk vk"
                 + "      , cnt_srtkenmerk vsk"
                 + "  WHERE vkc.cnt_kenmerk_key = vk.cnt_kenmerk_key"
                 + "    AND vk.cnt_srtkenmerk_key = vsk.cnt_srtkenmerk_key"
                 + "    AND vkc.cnt_contract_key = " + cnt_key
                 + "    AND (vk.cnt_srtcontract_key = " + cnt_srt_current_key + " OR vk.cnt_srtcontract_key IS NULL)"
                 + "    AND COALESCE (vk.cnt_kenmerk_omschrijving, vsk.cnt_srtkenmerk_omschrijving)"
                 + "      = COALESCE (k.cnt_kenmerk_omschrijving, sk.cnt_srtkenmerk_omschrijving)"
                 + "    AND vk.cnt_srtkenmerk_key = k.cnt_srtkenmerk_key"
                 + "    AND vk.cnt_kenmerk_groep = k.cnt_kenmerk_groep)";

      var sql = "SELECT k.cnt_kenmerk_key new_kenmerk_key"
              + "     , sk.cnt_srtkenmerk_kenmerktype kenmerk_kenmerktype"
              + "     , " + sql_waarde + " kenmerk_waarde"
              + "  FROM cnt_srtkenmerk sk"
              + "     , cnt_kenmerk k"
              + " WHERE (k.cnt_srtcontract_key = " + cnt_srt_new_key + "OR k.cnt_srtcontract_key IS NULL)"
              + "   AND k.cnt_kenmerk_niveau = 'S'"
              + "   AND k.cnt_kenmerk_verwijder IS NULL"
              + "   AND sk.cnt_srtkenmerk_verwijder IS NULL"
              + "   AND k.cnt_srtkenmerk_key = sk.cnt_srtkenmerk_key";
      var oRs = Oracle.Execute(sql);

      // Verwijder eerst alle kenmerkwaarden van de oude contractsoort
      sql = "DELETE FROM cnt_kenmerkcontract"
          + " WHERE cnt_contract_key = " + cnt_key;
      Oracle.Execute(sql);

      while (!oRs.eof)
      {
        var new_kkey = oRs("new_kenmerk_key").value;
        var kwaarde = oRs("kenmerk_waarde").value;

        // Heeft het kenmerk een waarde.
        if (kwaarde)
        { // Kenmerkwaarde van het overeenkomende kenmerk opslaan voor de nieuwe contractsoort
          sql = "INSERT INTO cnt_kenmerkcontract"
              + "(cnt_contract_key"
              + ", cnt_kenmerk_key"
              + ", cnt_kenmerkcontract_waarde)"
              + " VALUES "
              + "(" + cnt_key
              + ", " + new_kkey
              + ", " + safe.quoted_sql(kwaarde) + ")";
          Oracle.Execute(sql);
        }
        oRs.MoveNext();
      }
      oRs.Close();

      var old_cntsrt = cnt_info.discipline_omschrijving;

      // Nu nog de contractsoort aanpassen.
      sql = "UPDATE cnt_contract"
          + "   SET ins_discipline_key = " + cnt_srt_new_key
          + " WHERE cnt_contract_key = " + cnt_key;
      Oracle.Execute(sql);

      // Tracking
      cnt_info = cnt.cnt_contract_info(cnt_key);
      var new_cntsrt = cnt_info.discipline_omschrijving;

      var formattedID = safe.jsstring(cnt_info.nummer_intern);
      cnt.trackcontractupdate(cnt_key, L("lcl_cnt_is_cntupdtrack").format(formattedID) + "\n"
                                        + L("lcl_cnt_typecnt") + ": " + old_cntsrt + L("lcl_trackto") + new_cntsrt);


%>    <script>
        FcltMgr.closeDetail(window, { refresh: true } );
      </script>
<%    Response.End;
    }
%>
    <script type="text/javascript">
      async function cnt_submit()
      {
          document.activeElement.blur(); // trigger laatste onChanges
          if (!await validateForm("u2"))
              return false;
          document.forms.u2.submit();
      }
      function cnt_cancel()
      {
          FcltMgr.closeDetail(window);
      }
    </script>
  </head>

  <body class="fclt-modal" id="mod_changecnttype">
<% MODAL_START(); %>
    <form name="u2"
          action="cnt_change_cnttype.asp?submit=1<%=buildTransitParam(["cnt_key"])%>"
          method="post"
          onSubmit="cnt_submit();">
<%
      MODAL_BLOCK_START("changecntsrt", L("lcl_select_cntsrt"), { icon: "fa-file-signature" });
      // Contractsoort
      // Geef een lijst en laat een contractsoort selecteren
      // Bepaal eerst cnt_srt_key en cnt_type_key
      sql = "SELECT cc.ins_discipline_key"
          + ", dp.cnt_disc_params_key"
          + ", dp.cnt_srtcontract_type"
          + ", dp.cnt_disc_params_binding"
          + ", cc.cnt_prs_perslid_key"
          + ", cc.cnt_prs_bedrijf_key"
          + " FROM cnt_contract cc"
          + ", cnt_disc_params dp"
          + " WHERE cc.ins_discipline_key = dp.cnt_ins_discipline_key"
          + " AND cc.cnt_contract_key = " + cnt_key;
      var oRs = Oracle.Execute(sql);
      var cnt_disc_params_key = oRs("cnt_disc_params_key").value;
      var cnt_srt_key = oRs("ins_discipline_key").value;
      var cnt_type_key = oRs("cnt_srtcontract_type").value;
      var discipline_binding = oRs("cnt_disc_params_binding").Value;

      sql_typecnt = "SELECT  td.ins_discipline_key, "
                   + lcl.xsqla('td.ins_discipline_omschrijving','td.ins_discipline_key')
                   + " FROM  ins_tab_discipline td"
                   + ",      cnt_disc_params dp"
                   + " WHERE td.ins_discipline_module = 'CNT'"
                   + "   AND td.ins_discipline_verwijder IS NULL"
                   + "   AND td.ins_discipline_key = dp.cnt_ins_discipline_key"
                   + (oRs("cnt_prs_bedrijf_key").Value != null ? " AND BITAND (dp.cnt_disc_params_binding, 1) = 1" : "")
                   + (oRs("cnt_prs_perslid_key").Value != null ? " AND BITAND (dp.cnt_disc_params_binding, 2) = 2" : "")
                   + "   AND dp.cnt_srtcontract_type = " + cnt_type_key
                   + "   ORDER BY 2";
      oRs.Close();
      FCLTselector("cnt_srt_new_key",
                   sql_typecnt,
                   { initKey: cnt_srt_key,
                     label: L("lcl_cnt_srttype")
                   });
      MODAL_BLOCK_END();
SIMPLE_BLOCK_START();
      CreateButtons([{ title: L("lcl_submit"), action: "cnt_submit()", icon: "fa-fclt-save", importance: 1},
                     { title: L("lcl_cancel"), action: "cnt_cancel()", icon: "fa-fclt-cancel", importance: 3}]);
SIMPLE_BLOCK_END();
IFACE.FORM_END();
%>
    </form>
    <% MODAL_END(); %>
  </body>
</html>
<% ASPPAGE_END(); %>
