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

<!--#include file="../../appl/Shared/common.inc"-->
<% FCLTHeader.Set({root: "../../appl/"}); %>
<!--#include file="../../appl/Shared/resultset_table_v2.inc" -->
<!--#include file="../../appl/CAD/FGII.inc" -->
<%
var sql="";
var highlight_arr = getQParamIntArray("highlight", []); // Comma-separated room_key_list (terrains). Optional

var floorKey = getQParamInt("vKey", -1);
var gebouwKey = getQParamInt("gKey", -1);

var outputmode = getQParamInt("outputmode", 0);
var showall    = getQParamInt("showall", 0) == 1;

var autfunction = "WEB_FGCNT";
var authparams = user.checkAutorisation(autfunction);
%>

<html>
  <head>
<%  FCLTHeader.Generate(); %>
    <script>
      function onClickContract(cid)
      {
        FcltMgr.openModalDetail('../../../' + '<%=custpath%>' + '/contract_info.asp?cid=' + cid, 'ContractInfo');
      }
    </script>
  </head>

  <body class="tabpage">
    <form name='u2'>
<%
      if (gebouwKey != -1)
      {
        sql = "SELECT r.ruimtenr"
            + "     , r.omschrijving"
            + "     , r.ruimteoppervlakte"
            + "     , r.gehuurdeoppervlakte"
            + "     , r.ruimtesoort"
            + "     , r.tarieftype"
            + "     , r.huurder"
            + "     , r.huurdernr"
            + "     , r.budgetcode"
            + "     , r.businessunit"
            + "     , r.cnt_contract_nummer"
            + "     , r.rechten"
            + "     , r.alg_ruimte_key x1"
            + "  FROM (SELECT rg.alg_ruimte_key alg_ruimte_key"
            + "             , rg.alg_ruimte_omschrijving omschrijving"
            + "             , rg.ruimteoppervlakte"
            + "             , rg.ruimtesoort ruimtesoort"
            + "             , rg.alg_srtruimte_code tarieftype"
            + "             , rg.alg_gebouw_upper || '-' || TO_CHAR(rg.alg_verdieping_code) || '-' || rg.alg_ruimte_nr ruimtenr"
            + "             , rg.gehuurdeoppervlakte"
            + "             , rg.huurder"
            + "             , rg.huurdernr"
            + "             , rg.budgetcode budgetcode"
            + "             , rg.businessunit businessunit"
            + "             , rg.cnt_contract_nummer"
            + "             , NVL((SELECT '1'"
            + "                    FROM prs_v_afdeling_boom prs_ab, prs_afdeling prs_a"
            + "                    WHERE"
            + "                       prs_a.prs_afdeling_key = prs_ab.prs_afdeling_key1"
            + "                       AND prs_ab.prs_afdeling_key = '" + user.prs_afdeling_key() + "'"
            +                         ((authparams.ALGreadlevel > -1)
                                        ? " AND rg.alg_locatie_key IN (SELECT alg_locatie_key FROM fac_v_my_locations"
                                          + " WHERE prs_perslid_key = " + user_key + " AND niveau = " + authparams.ALGreadlevel + ")"
                                        : " AND 1 = 1")
            +                         ((authparams.PRSreadlevel > -1)
                                        ? " AND rg.businessunit = prs_a.prs_afdeling_omschrijving"
                                        : " AND 1 = 1")
            + "                    ), '0') AS rechten"
            + "          FROM conn_v_ruimtegeg rg"
            + "             , alg_ruimte ar"
            + "          WHERE ar.alg_verdieping_key = " + floorKey
            + "           AND rg.alg_ruimte_key = ar.alg_ruimte_key"
            + "       ) R"
            + (highlight_arr.length > 0 && highlight_arr.join(",").indexOf("-1") == -1
              ? " WHERE r.alg_ruimte_key IN (" + highlight_arr.join(",") + ")"
                + " ORDER BY 1"
              : "");
      }
      else
      {
        sql = "SELECT * FROM DUAL WHERE 1 = 0";
      }

      function refCnt(oRs)
      {
          var txt = (oRs("cnt_contract_nummer").value != null)? oRs("cnt_contract_nummer").value : "";

          if (oRs("rechten").value != '1')
            return txt; // Alleen tonen, niet klikbaar
          else
            return "<a title='Klik voor volledig contract' " +
                   "   href='javascript:onClickContract(\"" + txt + "\")'>" + txt + "</a>";
      }

      var rst = new ResultsetTable({ sql: sql,
                                     ID: "cadcnttable",
                                     keyColumn: "cnt_contract_nummer",
                                     title: L("lcl_cnt_contracts"),
                                     outputmode: outputmode,
                                     showAll: showall
                                   });

      rst.addColumn(new Column({caption: L("lcl_obj_inf_room"), content: "ruimtenr"}));
      rst.addColumn(new Column({caption: L("lcl_descr"), content: "omschrijving"}));
      rst.addColumn(new Column({caption: L("lcl_opp"), content: "ruimteoppervlakte"}));
      rst.addColumn(new Column({caption: "CAD opp", content: "gehuurdeoppervlakte"}));

      rst.addColumn(new Column({caption: "Ruimtesoort", content: "ruimtesoort"}));
      rst.addColumn(new Column({caption: "Tarieftype", content: "tarieftype"}));
      rst.addColumn(new Column({caption: "Huurder", content: "huurder"}));
      rst.addColumn(new Column({caption: "Debiteurnr", content: "huurdernr"}));
      rst.addColumn(new Column({caption: "Budgetcode", content: "budgetcode"}));
      rst.addColumn(new Column({caption: "Businessunit", content: "businessunit"}));
      rst.addColumn(new Column({caption: "Contractnr", content: refCnt}));

      var cnt = rst.processResultset();
%>
    </form>
  </body>
</html>

