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

    File:           fac_scan_users.asp
    Description:    API om active users op te vragen van één klant
    Context:        FACILITOR remote support

*/
var JSON_Result = true;
%>
<!-- #include file="../Shared/common.inc"-->
<%
    user.checkAutorisation("WEB_FACFAC");

    var result = { cust: customerId.toUpperCase(),
                   facname: L("lcl_facilitor_ie_title"),
                   power : [],
                   recent: [] };
    if (FCLToffline)
        result.FCLToffline = FCLToffline;

    var tmStart2 = new Date();
    result.datasource = Oracle.RealConnection.Properties("Data source").Value;
    result.uname = Oracle.RealConnection.Properties("User Name").Value;

    var sql = "SELECT SYS_CONTEXT('USERENV','DB_NAME') dbname,"
            + "       SYS_CONTEXT('USERENV','SERVER_HOST') server FROM DUAL";
    var oRs = Oracle.Execute(sql);
    result.dbdomain = oRs('dbname').Value + '@' + oRs('server').Value;
    oRs.Close();

    var sql = "SELECT user, created, expiry_date FROM user_users";
    var oRs = Oracle.Execute(sql);
    result.created = new Date(oRs("created").Value);
    if (oRs("expiry_date").Value != null)
        result.expiry_date = new Date(oRs("created").Value);
    var txt = "Database user {0} was created on {1}".format(oRs("user").Value, toDateTimeString(new Date(oRs("created").Value)));
    oRs.Close();

    var sql = "select fac.getdbversion() fac_version,"
            + " 'DB'||fac_version_schema fac_version_schema,"
            + "       fac_version_putorders_date,"
            + "       fac_version_notify_date,"
            + "       fac_version_scheduler_date"
            + " from fac_version";
    var oRs = Oracle.Execute(sql);
    result.facversion = oRs("fac_version").Value;
    result.schemaversion = oRs("fac_version_schema").Value;
    result.putorders_date = oRs("fac_version_putorders_date").Value!=null?new Date(oRs("fac_version_putorders_date").Value):null;
    result.notify_date = oRs("fac_version_notify_date").Value!=null?new Date(oRs("fac_version_notify_date").Value):null;
    result.scheduler_date = oRs("fac_version_scheduler_date").Value!=null?new Date(oRs("fac_version_scheduler_date").Value):null;
    oRs.Close();

    var sql = "SELECT MAX(adm_tracking_date) datum"
            + "  FROM adm_tracking"
            + " WHERE adm_tracking_name LIKE '%recreate.%'";
    var oRs = Oracle.Execute(sql);
    if (!oRs.Eof && oRs("datum").Value != null)
    {
        result.recreate_date = new Date(oRs("datum").Value);
    }
    oRs.Close();

    var sql = "SELECT adm_tracking_date, adm_tracking_name, adm_tracking_revision, adm_tracking_osuser"
            + "  FROM adm_tracking"
            + " WHERE UPPER(adm_tracking_name) LIKE '{0}%.SQL%'".format(result.cust)
            + " ORDER BY adm_tracking_key DESC";
    var oRs = Oracle.Execute(sql);
    if (!oRs.Eof)
    {
        result.custsql =
            { name : oRs("adm_tracking_name").Value,
              date : new Date(oRs("adm_tracking_date").Value),
              osuser : oRs("adm_tracking_osuser").Value,
              revision : oRs("adm_tracking_revision").Value
            }
    }
    oRs.Close();

    var puo_const = { STATUS_PORTAL: 1 // Niet via Putorders.exe
                    , STATUS_EMAIL: 2
                    , STATUS_SMS:  4
                    , STATUS_POPUP: 8 // Niet via Putorders.exe
                    , STATUS_SUMMARY_XSL: 16 // SMS/Email subject via XSL?
                    , STATUS_APPPUSH: 32 // Naar Azure app
                    , STATUS_CUST: 64
                    };

    var sql = "SELECT count(*) cnt,"
                + "       MIN(fac_notificatie_datum) fac_notificatie_datum,"
                + "       MAX(fac_notificatie_datum) max_notificatie_datum"
            + "  FROM fac_notificatie"
            + " WHERE (   BITAND (fac_notificatie_status, " + (puo_const.STATUS_EMAIL | puo_const.STATUS_SMS| puo_const.STATUS_APPPUSH) + ") > 0"
            + "        OR fac_notificatie_systeemadres IS NOT NULL"
            + "        OR prs_bedrijfadres_key IS NOT NULL)";
    var oRs = Oracle.Execute(sql);
    result.notiqueue = oRs("cnt").Value;
    if (result.notiqueue)
    {
        result.notidate = new Date(oRs("fac_notificatie_datum").Value);
        result.lastnotidate = new Date(oRs("max_notificatie_datum").Value);
    }
    oRs.Close();

    // Custom queue old
    var sql = "SELECT count(*) cnt,"
                + "       MIN(fac_notificatie_datum) fac_notificatie_datum,"
                + "       MAX(fac_notificatie_datum) max_notificatie_datum"
            + "  FROM fac_notificatie"
            + " WHERE BITAND (fac_notificatie_status, " + (puo_const.STATUS_CUST) + ") > 0"
            + "   AND fac_notificatie_datum < TRUNC(SYSDATE)";
    var oRs = Oracle.Execute(sql);
    result.custqueue = oRs("cnt").Value;
    if (result.custqueue)
    {
        result.custnotidate = new Date(oRs("fac_notificatie_datum").Value);
        result.lastcustnotidate = new Date(oRs("max_notificatie_datum").Value);
    }
    oRs.Close();

    // Custom queue today
    var sql = "SELECT count(*) cnt,"
                + "       MIN(fac_notificatie_datum) fac_notificatie_datum,"
                + "       MAX(fac_notificatie_datum) max_notificatie_datum"
            + "  FROM fac_v_notifyqueue"
            + " WHERE BITAND (fac_notificatie_status, " + (puo_const.STATUS_CUST) + ") > 0"
            + "   AND fac_notificatie_datum > TRUNC(SYSDATE)"; // minstens half uur oud
    var oRs = Oracle.Execute(sql);
    result.custqueuenew = oRs("cnt").Value;
    if (result.custqueuenew)
    {
        result.custnotidatenew = new Date(oRs("fac_notificatie_datum").Value);
        result.lastcustnotidatenew = new Date(oRs("max_notificatie_datum").Value);
    }
    oRs.Close();

    // Krachtige users. Pas op: fac_v_webgebruiker is hier uitgewerkt omdat die bij UWV te traag was
    var sql = "SELECT p.prs_perslid_key, "
            + "       prs_perslid_oslogin, "
            +         S("prs_pers_string") + " naam,"
            +         "prs_afdeling_naam || ' ' || prs_afdeling_omschrijving afdeling,"
            + "       p.prs_perslid_login, "
            + "       p.prs_perslid_email, "
            + "       p.prs_perslid_loglevel, "
            + "       aantal "
            + "  FROM (SELECT gg.prs_perslid_key, "
            + "               COUNT(DISTINCT g.fac_functie_key) aantal"
            + "        FROM   fac_groeprechten g, "
            + "               fac_gebruikersgroep gg "
            + "        WHERE  g.fac_groep_key = gg.fac_groep_key "
            + "           AND fac_gebruiker_prs_level_write < 9 "
            + "           AND fac_gebruiker_alg_level_write < 9 "
            + "        GROUP  BY gg.prs_perslid_key) fun, "
            + "       prs_v_aanwezigperslid p "
            + "     , prs_afdeling d "
            + " WHERE p.prs_perslid_key = fun.prs_perslid_key "
            + "   AND p.prs_afdeling_key = d.prs_afdeling_key"
            + "   AND prs_perslid_login IS NOT NULL "
            + " ORDER BY aantal DESC, "
            + "          prs_perslid_login DESC ";
    //var sql = "SELECT * FROM ({0}) WHERE ROWNUM < 10".format(sql);
    var oRs = Oracle.Execute(sql);
    var i = 0;
    while (!oRs.eof && i < 10)
    {
        i++;
        result.power.push({ key: oRs("prs_perslid_key").Value,
                            oslogin: oRs("prs_perslid_oslogin").Value,
                            email: oRs("prs_perslid_email").Value,
                            naam: oRs("naam").Value,
                            afdeling: oRs("afdeling").Value,
                            logon: new Date(oRs("prs_perslid_login").Value),
                            loglevel: oRs("prs_perslid_loglevel").Value,
                            fnaantal: oRs("aantal").Value
                          });
        oRs.MoveNext();
    }
    oRs.Close();

    // Recente users
    var sql = "SELECT prs_perslid_key, "
            + "       prs_perslid_oslogin, "
            +         S("prs_pers_string") + " naam,"
            +         "prs_afdeling_naam || ' ' || prs_afdeling_omschrijving afdeling,"
            + "       p.prs_perslid_login,"
            + "       p.prs_perslid_email, "
            + "       p.prs_perslid_loglevel "
            + "  FROM prs_perslid p"
            + "     , prs_afdeling d "
            + " WHERE prs_perslid_login IS NOT NULL"
            + "   AND p.prs_afdeling_key = d.prs_afdeling_key"
            + " ORDER BY prs_perslid_login DESC ";
    var oRs = Oracle.Execute(sql);
    var i = 0;
    while (!oRs.eof && i < 10)
    {
        i++;
        result.recent.push({ key: oRs("prs_perslid_key").Value,
                             oslogin: oRs("prs_perslid_oslogin").Value,
                             email: oRs("prs_perslid_email").Value,
                             naam: oRs("naam").Value,
                             afdeling: oRs("afdeling").Value,
                             logon: new Date(oRs("prs_perslid_login").Value),
                             loglevel: oRs("prs_perslid_loglevel").Value
                          });
        oRs.MoveNext();
    }
    oRs.Close();

    try
    {
        var WshShell = new ActiveXObject("WScript.Shell");
        result.computername = WshShell.ExpandEnvironmentStrings("%COMPUTERNAME%");
    }
    catch (e)
    {
        result.computername = "???";
    }

    var fso = Server.CreateObject("Scripting.FileSystemObject")
    var logfilename = "log_" + customerId + ".html";
    var filePath = shared.tempFolder();
    if (fso.FileExists(filePath + "/" + logfilename))
    {
        var f = fso.getFile(filePath + "/" + logfilename);
        result.logdate = new Date(f.DateLastModified);
        result.logsize = f.Size;
    }

    var diff = new Date() - tmStart2;
    result.duration = diff;

    result.retrieved = new Date();
    result.svnrevision = parseInt("$Revision$".replace(/\D/g,''));

    Response.Write(JSON.stringify(result));
%>
<% ASPPAGE_END(); %>
