<% /*
    $Revision$
    $Id$

    File:           model_floors.inc

    Description:    floor model.
    Parameters:
    Context:

    Notes:          Eerste opzet, (geaggregeerde) data als oppervlakte e.d. is voorstelbaar
*/

%>
<!-- #include file="../alg/alg.inc"-->
<!-- #include file="../Shared/discxalg3d.inc"-->
<!-- #include file="./model_cad_tekening.inc"-->
<!-- #include file="./model_alg_kenmerk.inc"-->
<!-- #include file="./model_custom_fields.inc"-->
<%
function model_floors() {
    this.table = "alg_verdieping";
    this.primary = "alg_verdieping_key";
    this.records_name = "floors";
    this.record_name = "floor";
    this.autfunction = "WEB_ALGMAN";
    this.fields = {
        "id"      : { dbs: "alg_verdieping_key",          typ: "key" },
        "name"    : { dbs: "alg_verdieping_omschrijving", typ: "varchar"},
        "code"    : { dbs: "alg_verdieping_code",         typ: "varchar"},
        "sequence": { dbs: "alg_verdieping_volgnr",       typ: "number"}, /* range zou ook kunnen */
        "building": { dbs: "alg_gebouw_key",              typ: "key",     foreign: "alg_gebouw"},
        "dwgz"    : { dbs: "alg_verdieping_dwgz",         typ: "float"},
        "externnr"      : { "dbs": "alg_verdieping_externnr",       "typ": "varchar" , "label": L("extern_nr"),       "readonly": !(user.has("WEB_FACTAB") || user.has("WEB_FACXNR"))},
        "externsyncdate": { "dbs": "alg_verdieping_externsyncdate", "typ": "datetime", "label": L("extern_syncdate"), "readonly": !(user.has("WEB_FACTAB") || user.has("WEB_FACXNR"))},
        "deleted"       : { "dbs": "alg_verdieping_verwijder",      "typ": "datetime", "label": L("lcl_deleted")}
    };
    this.includes = {
        "drawings" : {
            "model": new model_cad_tekening("V", { internal: true }),
            "joinfield": "floor",
            "enable_update": true
        },
        "custom_fields" : {
            "model": new model_custom_fields(this, new model_alg_kenmerk("V", { internal: true }), { readman: true, readuse: true }),
            "joinfield": "flexparentkey",
            "enable_update": true
        }
    };
    this.REST_GET = function _GET(params) {
            var autfunction = ["WEB_ALGUSE", "WEB_ALGMAN"];
            params.authparams = user.checkAutorisation(autfunction, null, null, true); // pessimistisch
            if (params.filter.id)
            {
                var flr_key = params.filter.id;
                var this_alg = alg.func_enabled(flr_key, "V");
                user.auth_required_or_abort(this_alg.readman || this_alg.readuse);
            }

            var query = api2.sqlfields(params, this);
            query.wheres.push("alg_verdieping_verwijder IS NULL");

            if ( params.authparams.ALGreadlevel > -1 )
            {  // Er is een scope-beperking van kracht
                query.wheres.push("alg_verdieping_key IN "
                                   + "(SELECT alg_verdieping_key FROM fac_v_my_floors "
                                   +   "WHERE prs_perslid_key="+user_key
                                   +    " AND niveau=" + params.authparams.ALGreadlevel + ")");
            }

            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 alg_verdieping_omschrijving";

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

            return json;
        };
    this.REST_POST   = generic_REST_POST(this);
    this.REST_PUT    = generic_REST_PUT(this);

    /* DELETE not implemented (yet) */
    this.DELETE = function (params, jsondata, the_key) /* delete floor */
    {
        var autfunction = "WEB_ALGMAN";
        // TODO: echte autorisatie
        params.authparams = user.checkAutorisation(autfunction, null, null, true); // pessimistisch
        if (params.authparams.ALGwritelevel)
        {
            var wheres = [" alg_verdieping_key = " + the_key];
            var sql = "UPDATE alg_verdieping SET alg_verdieping_verwijder = SYSDATE "
                    + " WHERE " + wheres.join(" AND " )
                    + " AND alg_verdieping_verwijder IS NULL";

            // Pas als het safe is: Oracle.Execute(sql);
        }
    }
}
%>