<% /*
    $Revision$
    $Id$

    File:           model_persons.inc

    Description:    Perslid model.
    Parameters:
    Context:

    - Notes:        - Standaard voegen we de gegevens van de hoofdwerkplek toe
                    - Ooit doen we er nog een include bij van alle werkplekken.
*/

%>
<!-- #include file="../Shared/discxalg3d.inc"-->
<!-- #include file="../api2/model_custom_fields.inc"-->
<!-- #include file="../api2/model_tracking.inc"-->
<!-- #include file="../api2/model_fac_gebruikersgroep.inc" -->
<%

function fnApiToken(oRs, field, model)
{
    var hmac = protectHMAC.create(oRs("prs_perslid_oslogin").Value, { sleutel: oRs("prs_perslid_apikey").Value });
    return hmac;
}

function genderLOV()
{
    var s = ";"   + L("lcl_prs_person_geslachtO")
          + ";0;" + L("lcl_prs_person_geslachtV")
          + ";1;" + L("lcl_prs_person_geslachtM");
    return s;
}

function model_persons()
{
    this.table = "prs_perslid";
    this.primary = "prs_perslid_key";
    this.records_name = "persons";
    this.record_name = "person";
    this.record_title = L("prs_perslid");
    this.records_title = L("prs_perslid_m");

    this.fields = {
             "id"        : { dbs: "prs_perslid_key",              "label": "Key",                             typ: "key",           filter: "exact"},
             "name"      : { dbs: "pf.prs_perslid_naam_friendly", "label": L("lcl_prs_person_name"),          typ: "varchar" },
             "lastname"  : {
                 dbs: "prs_perslid_naam",
                 label: L("lcl_prs_person_achternaam"),
                 typ: "varchar",
                 filter: "like"},
             "firstname" : { dbs: "prs_perslid_voornaam",         "label": L("lcl_prs_person_voornaam"),      typ: "varchar",       filter: "like"},
             "title"     : { dbs: "prs_perslid_titel",            "label": L("lcl_prs_person_title"),         typ: "varchar" },
             "initials"  : { dbs: "prs_perslid_voorletters",      "label": L("lcl_prs_person_initials"),      typ: "varchar" },
             "prefix"    : { dbs: "prs_perslid_tussenvoegsel",    "label": L("lcl_prs_person_tussen"),        typ: "varchar" },
             "gender"    : { dbs: "prs_perslid_geslacht",         "label": L("lcl_prs_person_geslacht"),      typ: "varchar",       LOV: genderLOV() },
             "language"  : { dbs: "prs_perslid_lang",             "label": L("lcl_lcl_taal"),                 typ: "varchar" },
             "phone"     : { dbs: "prs_perslid_telefoonnr",       "label": L("lcl_prs_person_phone"),         typ: "varchar" },
             "mobile"    : { dbs: "prs_perslid_mobiel",           "label": L("lcl_prs_person_mobile"),        typ: "varchar" },
             "email"     : { dbs: "prs_perslid_email",            "label": L("lcl_prs_person_email"),         typ: "varchar",       filter: "exact"},
             "login"     : { dbs: "prs_perslid_oslogin",          "label": L("lcl_prs_person_login"),         typ: "varchar",       filter: "like"},
             "employment": { dbs: "prs_perslid_dienstverband",    "label": L("lcl_prs_person_dienstverband"), typ: "float" },
             "startdate" : { dbs: "prs_perslid_ingangsdatum",     "label": L("lcl_prs_person_ingangsdatum"),  typ: "date"  },
             "enddate"   : { dbs: "prs_perslid_einddatum",        "label": L("lcl_prs_person_einddatum"),     typ: "date"  },
             "function"  : { dbs: "prs_srtperslid_key",           "label": L("lcl_prs_person_function"),      typ: "key", foreign: { tbl: "prs_srtperslid", key: "prs_srtperslid_key", desc: "prs_srtperslid_omschrijving"} },
             "department": { dbs: "prs_afdeling_key",             "label": L("lcl_prs_organisatie"),          typ: "key", foreign: "prs_afdeling"},
             "company"   : { dbs: "pa.prs_bedrijf_key",           "label": L("lcl_prs_company"),              typ: "key", foreign: "prs_bedrijf"},
             "location"  : { dbs: "wg.alg_locatie_key",           "label": L("lcl_location"),                 typ: "key", foreign: "alg_locatie", filter: "exact"},
             "building"  : { dbs: "wg.alg_gebouw_key",            "label": L("lcl_building"),                 typ: "key", foreign: "alg_gebouw" },
             "floor"     : { dbs: "wg.alg_verdieping_key",        "label": L("lcl_floor"),                    typ: "key", foreign: "alg_verdieping" },
             "room"      : { dbs: "wg.alg_ruimte_key",            "label": L("lcl_room"),                     typ: "key", foreign: "alg_ruimte" }
    };

    this.includes = {
        "authorization": {
            "model": new model_fac_gebruikersgroep(),
            "joinfield": "user"
        },
        "custom_fields" : {
            "model": new model_custom_fields(this, "PRS", { readman: true, readuse: true, pNiveau: "P" }),
            "joinfield": "flexparentkey"
        },
        "tracking": {
            "model": new model_tracking(["perslid"]),
            "joinfield": "fac_tracking_refkey"
        }
    };

    this.REST_GET = function _GET(params)
        {
            if (app_user_key > 0) // Alleen App's mogen dit opvragen. TODO: Ook echt authorisatie controleren?
            {
                if (user.prs_perslid_apikey() && user.oslogin())
                {
                    this.fields["authtoken"] = { dbs: "prs_perslid_apikey", typ: "varchar", val: fnApiToken };
                }
            }

            var query = api2.sqlfields(params, this);
            query.tables.push("prs_v_perslid_fullnames_all  pf");
            query.wheres.push("pf.prs_perslid_key(+) = prs_perslid.prs_perslid_key");
            query.tables.push("prs_v_werkplek_gegevens  wg");
            query.tables.push("prs_v_afdeling  pa");
            query.wheres.push("pa.prs_afdeling_key = prs_perslid.prs_afdeling_key");
            query.tables.push("prs_v_hoofdperslidwerkplek2 pw");
            query.wheres.push("pw.prs_perslid_key(+) = prs_perslid.prs_perslid_key");
            query.wheres.push("pw.prs_werkplek_key = wg.prs_werkplek_key(+)");

            var hasPRSSYS = user.has("WEB_PRSSYS");
            query.wheres.push("prs_perslid.prs_perslid_verwijder IS NULL");
            var authparamsUSE = user.checkAutorisation("WEB_PRSUSE", true);
            if (hasPRSSYS)
			{
                /* zijn we verder klaar */
			}
            else if (!authparamsUSE) // PAS OP: Dit verwacht AAFM-API niet!!!
                query.wheres.push("prs_perslid.prs_perslid_key = " + user_key);
            else
            {
                // TODO: Ook nog ALG autorisatie?
                if (authparamsUSE.PRSreadlevel == 0) // prs_bedrijf
                {
                    query.wheres.push("pa.prs_bedrijf_key = " + user.afdeling().prs_bedrijf_key());
                }
                else if (authparamsUSE.PRSreadlevel > 0)
                {
                    query.wheres.push(
                           "prs_perslid.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 = " + authparamsUSE.PRSreadlevel + "))");
                }
            }

            var wheres = api2.sqlfilter(params, this);
            query.wheres = query.wheres.concat(wheres);

            var sql = "SELECT " + query.selects.join(", ")
                    + "  FROM " + query.tables.join(", ")
                    + " WHERE " + query.wheres.join(" AND " )
                    + " ORDER BY prs_perslid.prs_perslid_naam, prs_perslid.prs_perslid_voornaam, prs_perslid_key";
            if (query.orderbys.length)
                sql += ", " + query.orderbys.join(", ");

            var json = api2.sql2json (params, sql, this);

            return json;
        };
    this.PUT = function (params) /* update perslid */
    {
            // Nog niet ondersteund
    };
    this.POST = function (params) /* new perslid */
    {
            // Nog niet ondersteund
    };
    this.DELETE = function (params) /* delete perslid */
    {
            // Nog niet ondersteund
    };
}
%>