<%@ language="javascript"%>
<% /*
     $Revision$
     $Id$
*/%>
<!--#include file="../../appl/Shared/common.inc"-->
<% FCLTHeader.Set({root: "../../appl/"}); %>
<!--#include file="../../appl/Shared/resultset_table_v2.inc" -->

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

<%
var sql = "";
var highlight_arr = getQParamIntArray("highlight", []); // Comma-separated room_key_list (terrains). Optional
var locKey = getQParamInt("vKey", -1);

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

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

  <body class="tabpage">
    <form name='u2'>
<%
      if (locKey != -1)
      {
        sql = " SELECT r.ruimtenr"
            + "      , r.omschrijving"
            + "      , r.ruimteoppervlakte opp"
            + "      , r.gehuurdeoppervlakte opp2"
            + "      , r.ruimtesoort"
            + "      , r.tarieftype"
            + "      , r.huurder huurder"
            + "      , r.huurdernr huurdernr"
            + "      , r.budgetcode"
            + "      , r.businessunit"
            + "      , r.cnt_contract_nummer contractnr"
            + "      , r.rechten rechten"
            + "      , r.alg_ruimte_key x1"
            + "    FROM (SELECT rg.alg_ruimte_key alg_ruimte_key"
            + "               , rg.alg_ruimte_omschrijving omschrijving"
            + "               , rg.ruimteoppervlakte"
            + "               , 'T' 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() + "'"
            + "                         AND  " + ((ALGreadlevel > -1)? " rg.alg_locatie_key IN (SELECT alg_locatie_key FROM fac_v_my_locations "
                                                                       + " WHERE prs_perslid_key = " + user_key + " AND niveau = " + ALGreadlevel + " )"
                                                                     : "1 = 1")
            + "                         AND  " + ((PRSreadlevel > -1)? " rg.BusinessUnit = prs_a.prs_afdeling_omschrijving "
                                                                     : "1 = 1")
            + "                      ),'0') AS rechten"
            + "            FROM conn_v_terreingeg rg"
            + "            WHERE alg_locatie_key = " + locKey
            + "      ) 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("contractnr").value != null)? safe.htmlattr(oRs("contractnr").value) : "";

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

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

      rst.addColumn(new Column({caption: L("lcl_terra"), content: "ruimtenr"}));
      rst.addColumn(new Column({caption: L("lcl_descr"), content: "omschrijving"}));
      rst.addColumn(new Column({caption: L("lcl_opp"), content: "opp"}));
      rst.addColumn(new Column({caption: "opp", content: "opp2"}));

      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>

