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

    File:           fac_audit.asp

    Description:

    Context:

    Notes:
*/
%>
<!-- #include file="../scf/scaffolding.inc" -->
<!-- #include file="../api2/model_fac_audit.inc" -->
<%
function gettablesql(ptable, pchildtable, pchildsql, precord, pmtable)
{ // Parentkey's van de childwaarden.
  var pwhere_veldnaam = "";
  if (ptable == "prs_perslid")
  {
    user.checkAutorisation(["WEB_FACMSU", "WEB_PRSMSU", "WEB_PRSSYS"]);
    this_model.autfunction = false;
    var pfac_audit_tabelkey = " (SELECT fac.safe_to_number (x.fac_audit_waarde_oud)"
                            + "    FROM fac_audit x"
                            + "   WHERE x.fac_audit_veldnaam = 'prs_perslid_key'"
                            + "     AND fac_audit_tabelnaam = 'fac_gebruikersgroep'"
                            + "     AND x.fac_audit_waarde_oud IS NOT NULL"
                            + "     AND x.fac_audit_actie = 'D'"
                            + "     AND x.fac_audit_tabelkey = a.fac_audit_tabelkey) fac_audit_tabelkey";
    pwhere_veldnaam = " AND (a.fac_audit_veldnaam = 'prs_perslid_key' OR a.fac_audit_veldnaam = 'fac_groep_key')";
  }
  else
  {
    var pfac_audit_tabelkey = " COALESCE((SELECT x.{0}_key".format(pmtable? pmtable : ptable)
                            + "             FROM " + pchildtable + " x"
                            + "            WHERE x.{0}_key = a.fac_audit_tabelkey)".format(pchildtable) + ", " + precord[0] + ") fac_audit_tabelkey"; // Als waarde null is dan is het record verwijderd. Dan gewoon key (eerste) invullen (o.a. voor prs_kostensoort).
  }

  return "(SELECT fac_audit_key"
       + "      , fac_audit_tabelnaam"
       + "      , fac_audit_tabelkey"
       + "      , fac_audit_veldnaam"
       + "      , fac_audit_actie"
       + "      , fac_audit_waarde_oud"
       + "      , " + oldfriendlysql + " fac_audit_waarde_oud_friendly"
       + "      , fac_audit_waarde_nieuw"
       + "      , " + newfriendlysql + " fac_audit_waarde_nieuw_friendly"
       + "      , fac_audit_datum"
       + "      , prs_perslid_key"
       + "      , fac_audit_opmerking"
       + "   FROM fac_audit"
       + "  WHERE fac_audit_tabelnaam = " + safe.quoted_sql(ptable)
       + "  UNION "
       + " SELECT a.fac_audit_key"
       + "      , '{0}' fac_audit_tabelnaam".format(ptable)
       + "      , " + pfac_audit_tabelkey
       + "      , a.fac_audit_veldnaam"
       + "      , a.fac_audit_actie"
       + "      , fac_audit_waarde_oud"
       + "      , " + oldfriendlysql + " fac_audit_waarde_oud_friendly"
       + "      , fac_audit_waarde_nieuw"
       + "      , " + newfriendlysql + " fac_audit_waarde_nieuw_friendly"
       + "      , a.fac_audit_datum"
       + "      , a.prs_perslid_key"
       + "      , a.fac_audit_opmerking"
       + "   FROM fac_audit a"
       + "  WHERE a.fac_audit_tabelnaam = " + safe.quoted_sql(pchildtable)
       + pwhere_veldnaam
       + "    AND a.fac_audit_tabelkey" + " IN (" + pchildsql + ")"
       + ") fac_audit";
}

var this_model = new model_fac_audit();
// Hmac controle en anders FACTAB eisen?
if (getQParam("hmac", ""))
{
  protectQS.verify({ allowparams: ["outputmode", "showall", "autosortby", "no_autoscroll", "scf_highlight"]}); // tamper check
  this_model.autfunction = false; // model hoeft niet meer op PRSSYS te controleren
}

// Vertaal enkele bekende key's naar leesbare omschrijvingen. Analyseert veel gemakkelijker
var oldfriendlysql = "CASE"
                   + " WHEN fac_audit_veldnaam = 'prs_perslid_key'"
                   + " THEN (SELECT prs_perslid_naam_friendly"
                   + "         FROM prs_v_perslid_fullnames_all"
                   + "        WHERE prs_perslid_key = fac_audit_waarde_oud)"
                   + " WHEN fac_audit_veldnaam = 'fac_groep_key'"
                   + " THEN (SELECT fac_groep_omschrijving"
                   + "         FROM fac_groep"
                   + "        WHERE fac_groep_key = fac.safe_to_number (fac_audit_waarde_oud))"
                   + " WHEN fac_audit_veldnaam = 'fac_functie_key'"
                   + " THEN (SELECT fac_functie_code"
                   + "         FROM fac_functie"
                   + "        WHERE fac_functie_key = fac.safe_to_number (fac_audit_waarde_oud))"
                   + " WHEN fac_audit_veldnaam = 'ins_discipline_key'"
                   + " THEN (SELECT ins_discipline_omschrijving"
                   + "         FROM ins_tab_discipline"
                   + "        WHERE ins_discipline_key = fac.safe_to_number (fac_audit_waarde_oud))"
                   + " ELSE"
                   + "     NULL"
                   + " END";
var newfriendlysql = "CASE"
                   + " WHEN fac_audit_veldnaam = 'prs_perslid_key'"
                   + " THEN (SELECT prs_perslid_naam_friendly"
                   + "         FROM prs_v_perslid_fullnames_all"
                   + "        WHERE prs_perslid_key = fac_audit_waarde_nieuw)"
                   + " WHEN fac_audit_veldnaam = 'fac_groep_key'"
                   + " THEN (SELECT fac_groep_omschrijving"
                   + "         FROM fac_groep"
                   + "        WHERE fac_groep_key = fac.safe_to_number (fac_audit_waarde_nieuw))"
                   + " WHEN fac_audit_veldnaam = 'fac_functie_key'"
                   + " THEN (SELECT fac_functie_code"
                   + "         FROM fac_functie"
                   + "        WHERE fac_functie_key = fac.safe_to_number (fac_audit_waarde_nieuw))"
                   + " WHEN fac_audit_veldnaam = 'ins_discipline_key'"
                   + " THEN (SELECT ins_discipline_omschrijving"
                   + "         FROM ins_tab_discipline"
                   + "        WHERE ins_discipline_key = fac.safe_to_number (fac_audit_waarde_nieuw))"
                   + " ELSE"
                   + "     NULL"
                   + " END";

this_model.fields["oldfriendly"] = {
                            "dbs": "fac_audit_waarde_old_friendly",
                            "label": "--",
                            "sql": oldfriendlysql,
                            "typ": "varchar",
                            "readonly": true
                           };
this_model.fields["newfriendly"] = {
                            "dbs": "fac_audit_waarde_nieuw_friendly",
                            "label": "--",
                            "sql": newfriendlysql,
                            "typ": "varchar",
                            "readonly": true
                           };

scf_params =
    {
        "search": {
            "autosearch": true,
            "filters": [
                "table",
                "person"
            ]
        },
        "list": {
            "columns": [
                "id",
                "date",
                "type",
                "column",
                "old",
                "oldfriendly",
                "new",
                "newfriendly",
                "person",
                "description"
            ]
        },
        "edit": {
            "modal": false
        },
        "layout": {
            "miscellaneous": {
                "icon": "fa-magnifying-glass"
            }
        }
    }

var table = getQParam("table", "");
var childtable = getQParam("childtable", "");

if (!table) // gebeurt alleen bij intern gebruik/any audit.
  scf_params.list.columns.splice(3, 0, "table");
else
{
  this_model.tablesql = "(SELECT *"
                      + "   FROM fac_audit "
                      + "  WHERE fac_audit_tabelnaam = " + safe.quoted_sql(table) + ") fac_audit";
  var record = getQParamIntArray("record");
  var childrecord = getQParamIntArray("childrecord", []);

  // Bij discipline de disc_params tabel er bij halen.
  // Als er geen childtable (disc_params) is zoals bij kennisbank (fac_faq) dan kan er ook geen childsql bepaald worden.
  if (table == "ins_tab_discipline" && childtable != "")
  {
    var sql = "SELECT ins_discipline_module"
            + "  FROM ins_tab_discipline"
            + " WHERE ins_discipline_key = " + record[0]; // Eerste keys zijn de ins_tab_discipline keys, daarna de disc_params keys. Eerste key is dan voldoende.
    var oRs = Oracle.Execute(sql);
    var module = oRs("ins_discipline_module").Value.toLowerCase();
    if (module == "cht")
      module = "ins";
    oRs.Close();

    var key_field = "ins_discipline_key";
    if (module != "ins")
        key_field = "{0}_ins_discipline_key".format(module);
    var childsql = "SELECT {0}_disc_params_key disc_params_key".format(module)
                 + "  FROM {0}_disc_params".format(module)
                 + " WHERE {0} IN ({1})".format(key_field, record.join(","));

    if (module != "mrk")
    {
      this_model.tablesql = gettablesql(table, childtable, childsql, record, (module != "ins"? module + "_" : "") + "ins_discipline");
    }
  }
  else if (childtable != "") // dan fac_profielwaarde er bij halen.
  {
    //var modelfunc = getQParam("modelfunc", "");

    var childsql; // Moet hetzelfde zijn als model.audit.childaudit.sql.format(record);
    switch (table)
    {
      case "fac_groep":       childsql = "SELECT fac_groeprechten_key"
                                       + "  FROM fac_groeprechten"
                                       + " WHERE fac_groep_key IN ({0})".format(record.join(","))
                                       + " UNION ALL"
                                       + " SELECT fac_audit_tabelkey"
                                       + "   FROM fac_audit"
                                       + "  WHERE fac_audit_veldnaam = 'fac_groep_key'"
                                       + "    AND fac_audit_tabelnaam = 'fac_groeprechten'"
                                       + "    AND fac_audit_actie = 'D'"
                                       + "    AND fac_audit_waarde_oud IN ({0})".format(record.join(","));
                              break;
      case "fac_profiel":     childsql = "SELECT fac_profielwaarde_key"
                                       + "  FROM fac_profielwaarde"
                                       + " WHERE fac_profiel_key IN ({0})".format(record.join(","));
                              break;
      case "prs_kostensoort": childsql = "SELECT prs_kostencombinatie_key"  // Huidige kosten combinaties.
                                       + "  FROM prs_kostencombinatie"
                                       + " WHERE prs_kostensoort_key IN ({0})".format(record.join(","))
                                       + " UNION "
                                       + "SELECT fac_audit_tabelkey"  // Verwijderde kosten combinaties.
                                       + "  FROM fac_audit"
                                       + " WHERE fac_audit_tabelnaam = 'prs_kostencombinatie'"
                                       + "   AND fac_audit_veldnaam = 'prs_kostensoort_key'"
                                       + "   AND fac_audit_waarde_oud IN ('{1}')".format(record.join(","), record.join("','"));
                              break;
      case "prs_perslid":     childsql = "SELECT fac_audit_tabelkey"
                                       + "  FROM fac_audit"
                                       + " WHERE fac_audit_tabelnaam = 'fac_gebruikersgroep'"
                                       + "   AND fac_audit_veldnaam = 'prs_perslid_key'"
                                       + "   AND fac_audit_waarde_oud IN ('{0}')".format(record.join("','"));
                              break;
       default :              shared.simpel_page(L("lcl_internal_error")); // TODO: Abort en error geven.
    }
    this_model.tablesql = gettablesql(table, childtable, childsql, record);
  }
}

scaffolding(this_model, scf_params);
%>
<% ASPPAGE_END(); %>
