<% /*
    $Revision$
    $Id$

    File:               prs_afdeling_list.inc
    Description:        Show an overview of prs_afdeling items in list-form,
                        which meets with the given requirements
    Parameters:
    Context:            Include for afdeling-overzichten, like Search action from prs_afdeling_search_list.asp form
    Note:
*/ %>

<!-- #include file="../Shared/json2.js" -->
<!-- #include file="../Shared/resultset_table_v2.inc" -->
<!-- #include file="../Shared/getkenmerksql.inc" -->
<!-- #include file="prs.inc" -->

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

function fnrowData(oRs)
{
  var key = oRs("prs_afdeling_key").value;
  var oms = oRs("prs_afdeling_naam").value;
  var niveau = oRs("niveau").value;

  var data = {key: key, oms: oms, niveau: niveau};

  return JSON.stringify(data);
}

function afdeling_list(pautfunction, params)
{
  // De functie checkAutorisation(autfunction) geeft een Response.End indien er geen autorisatie voor autfunction wordt gevonden
  var addButton = [];

  // Voor resulttable de globalen zetten; zou ng anders moeten.
  var outputmode = params.outputmode; // print to 0 = screen, 1 = printer, 2 = excel, etc.
  var showall = params.showall;

  var deptName = params.deptName;
  var deptDesc = params.deptDesc;
  var kstpl_key = params.kstpl_key;
  var bdr_key = params.bdr_key;
  var dep_key = params.dep_key;
  var afd_niveau = params.afd_niveau;
  var room_key = params.room_key;
  var tiny = params.tiny;
  if (room_key > 0)
  {
    var autfunction = "WEB_PRSBOF";
} else {
    var autfunction = "WEB_PRSUSE";
  }
  var authparams = user.checkAutorisation(autfunction);

  if (room_key > 0)
  {
    var canPRSBOF = authparams.PRSwritelevel < 9;
  } else {
    var canAdd = (authparams.PRSwritelevel <= afd_niveau);
  }

  var parentlevel = 0
  if (afd_niveau > 1)
    parentlevel = afd_niveau - 1;
%>

  <html>
    <head>
      <% FCLTHeader.Generate({outputmode:outputmode}) %>
      <script type="text/javascript">
        var autosearch = 1; //automatisch laden van resultaat op search pagina

        function afdelingEdit(row)
        {
          var prsData = eval('(' + row.getAttribute("ROWDATA") + ')');
          var url = "appl/prs/prs_afdeling.asp?key=" + prsData.key + "&afd_niveau=" + prsData.niveau;
          FcltMgr.openDetail(url, "<%=L("lcl_department")%>" + ' ' + prsData.oms);
        }

        function doDelete(rowArray, isMulti)
        {
          FcltMgr.confirm(L("lcl_prs_del_txt_afdeling"), { autoconfirm: isMulti }, function() {
              var afdKeyString = getKeyString(rowArray);
              var data = {
                  key: afdKeyString,
                  level: "A"
              };
              <% protectRequest.dataToken("data"); %>
              $.post("prs_delete.asp",
                  data,
                  FcltCallbackRefresh,
                  "json");
          });
        }

        function alg_ruimteafdeling()
        {
          FcltMgr.openModalDetail("../alg/alg_ruimteafdeling.asp?ruimte_keys=<%=room_key%>",
                                  L("lcl_prs_ruimtebezetting"),
                                  { callback: FcltMgr.reload });
        }

      </script>
    </head>
    <body id="listbody">
<%
      var sql1 = "";
      var sql2 = "";
      if (!afd_niveau || afd_niveau == 1)
      {
        sql1 = "SELECT dd.prs_afdeling_key,"
             + "       dd.prs_afdeling_naam,"
             + "       UPPER(dd.prs_afdeling_naam) uppernaam,"
             + "       dd.prs_afdeling_omschrijving,"
             + "       k.prs_kostenplaats_nr,"
             + "       '' AS afdelingnaam2,"
             + "       b.prs_bedrijf_naam,"
             + "       1 niveau"
             + (tiny && room_key >= 0
               ? ",      (SELECT v.prs_ruimteafdeling_bezetting"
                 + "        FROM prs_ruimteafdeling v"
                 + "       WHERE v.alg_ruimte_key = " + room_key
                 + "        AND v.prs_afdeling_key = dd.prs_afdeling_key"
                 + "        AND prs_ruimteafdeling_verwijder IS NULL) bezetting"
               : "")
             + "  FROM prs_afdeling dd,"
             + "       prs_kostenplaats k,"
             + "       prs_bedrijf b"
             + " WHERE k.prs_kostenplaats_key(+) = dd.prs_kostenplaats_key"
             + "   AND dd.prs_afdeling_verwijder IS NULL"
             + "   AND b.prs_bedrijf_key = dd.prs_bedrijf_key"
             + "   AND dd.prs_afdeling_parentkey IS NULL"
      }

      if (!afd_niveau || afd_niveau > 1)
      {
        sql2 = "SELECT dd.prs_afdeling_key,"
             + "       dd.prs_afdeling_naam,"
             + "       UPPER(dd.prs_afdeling_naam) uppernaam,"
             + "       dd.prs_afdeling_omschrijving,"
             + "       k.prs_kostenplaats_nr,"
             + "       d.prs_afdeling_naam1 AS afdelingnaam2,"
             + "       b.prs_bedrijf_naam,"
             + "       (d.niveau + 1)niveau"
             + (tiny && room_key >= 0
               ? ",      (SELECT v.prs_ruimteafdeling_bezetting"
                 + "        FROM prs_ruimteafdeling v"
                 + "       WHERE v.alg_ruimte_key = " + room_key
                 + "        AND v.prs_afdeling_key = dd.prs_afdeling_key"
                 + "        AND prs_ruimteafdeling_verwijder IS NULL) bezetting"
               : "")
             + "  FROM prs_afdeling dd,"
             + "       prs_kostenplaats k,"
             + "       prs_v_aanwezigafdeling d,"
             + "       prs_bedrijf b"
             + " WHERE k.prs_kostenplaats_key(+) = dd.prs_kostenplaats_key"
             + "   AND d.prs_afdeling_key = dd.prs_afdeling_parentkey"
             + "   AND b.prs_bedrijf_key = d.prs_bedrijf_key"
             + (afd_niveau
               ? "   AND d.niveau = " + parentlevel
               : "")
             + "   AND dd.prs_afdeling_verwijder IS NULL"
      }

      var sqln = "";
      if (authparams.PRSreadlevel > 0)
      {  // Er is een scope-beperking van kracht
        sqln += " AND dd.prs_afdeling_key IN "
             +  "    (SELECT prs_afdeling_key FROM prs_v_afdeling_familie a"
             +  "      WHERE a.prs_afdeling_elder_key IN"
             +  "        (SELECT aa.prs_afdeling_elder_key"
             +  "           FROM prs_v_afdeling_familie aa"
             +  "          WHERE aa.prs_afdeling_key = " + user.prs_afdeling_key()
             +  "            AND aa.niveau = " + authparams.PRSreadlevel + "))";
      }
      if (authparams.PRSreadlevel == 0) // (PRSreadlevel == 0) ==> prs_bedrijf
      {
        sqln += " AND dd.prs_afdeling_key IN "
             +  " (SELECT a.prs_afdeling_key FROM prs_v_afdeling a "
  	         +  " WHERE a.prs_bedrijf_key = " + user.afdeling().prs_bedrijf_key() + ")";
      }

      sqln += getKenmerkSql("PRS", "dd.prs_afdeling_key");

      if (room_key >= 0)
      {
        sqln += " AND dd.prs_afdeling_key IN"
                + " (SELECT v.prs_afdeling_key"
                + "    FROM prs_ruimteafdeling v"
                + "   WHERE v.alg_ruimte_key = "  + room_key
                + "     AND prs_ruimteafdeling_verwijder IS NULL)"
      }

      if (deptName)
      {
        sqln += " AND UPPER(dd.prs_afdeling_naam) LIKE " + safe.quoted_sql_wild("%" + deptName + "%");
      }

      if (deptDesc)
      {
        sqln += " AND UPPER(dd.prs_afdeling_omschrijving) LIKE " + safe.quoted_sql_wild("%" + deptDesc + "%");
      }

      if (kstpl_key)
      {
        sqln += " AND dd.prs_kostenplaats_key = " + kstpl_key;
      }

      if (bdr_key)
      {
        sqln += " AND dd.prs_bedrijf_key = " + bdr_key;
      }
      if (dep_key)
      {
        sqln += " AND dd.prs_afdeling_parentkey = " + dep_key;
      }

      var addurl = "appl/prs/prs_afdeling.asp?afd_niveau=" + afd_niveau;

      if (room_key >=0)
      {
        if (canPRSBOF)
            addButton = [{ icon: "wijzigen.png",
                           title: L("lcl_change"),
                           action: "alg_ruimteafdeling()"
                          }
                        ];
      }
      else if (canAdd)
      {
        addButton = [{ icon: "plus.png", title: L("lcl_add"), action: "FcltMgr.openDetail('" + addurl + "', '" + L("lcl_add") + "')" }]
      }

      var sql = (sql1 != ""
                ? sql1 + sqln
                : "")
              + (sql1 != "" && sql2 != ""
                ? " UNION "
                : "")
              + (sql2 != ""
                ? sql2 + sqln
                : "")
              + " ORDER BY uppernaam";

      var rst = new ResultsetTable({ sql: sql,
                                     keyColumn: "prs_afdeling_key",
                                     ID: "prstable",
                                     showAll: showall,
                                     noPrint: tiny,
                                     rowData: fnrowData,
                                     outputmode: outputmode,
                                     title: afd_niveau > 0? L("lcl_dep_name_level"+ afd_niveau) : L("lcl_prs_afdelingen"),
                                     flexModule: "PRS",
                                     flexId: "prs_afdeling_key",
                                     filterParams: params,
                                     buttons: addButton
                                    });


      if (afd_niveau >1)
      {
        rst.addColumn(new Column({caption: L("lcl_prs_dept_company"), content: "PRS_BEDRIJF_NAAM"}));
        rst.addColumn(new Column({caption: L("lcl_dep_name_level" + parentlevel), content: "afdelingnaam2"}));
      }
      else
        rst.addColumn(new Column({caption: L("lcl_prs_dept_company"), content: "prs_bedrijf_naam"}));
      rst.addColumn(new Column({caption: L("lcl_prs_dept_name"), content: "prs_afdeling_naam", hasActions:true}));
      rst.addColumn(new Column({caption: L("lcl_prs_dept_desc"), content: "prs_afdeling_omschrijving"}));
      rst.addColumn(new Column({caption: L("lcl_prs_dept_kosten"), content: "prs_kostenplaats_nr"}));
      if (tiny && room_key >= 0)
      {
        rst.addColumn(new Column({caption: L("lcl_alg_bezetting") + " (" + L("lcl_procent") + ")", content: "bezetting"}));
      }

      rst.addAction({ action: "afdelingEdit", caption: L("lcl_edit"), isDefault:  true});
      rst.addAction({ action: "doDelete", caption: L("lcl_delete"), enabler: "eDelete", single: false, multi: !tiny, multiOnce: true});

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

