<% /*
    $Revision$
    $Id$

    File:           model_buildings.inc

    Description:    Gebouwen model.
    Parameters:
    Context:

    Notes:
*/

%>
<!-- #include file="../alg/alg.inc"-->
<!-- #include file="../Shared/discxalg3d.inc"-->
<!-- #include file="./model_custom_fields.inc"-->
<!-- #include file="./model_alg_kenmerk.inc"-->
<%
function model_buildings()
{
    this.table        = "alg_gebouw";
    this.primary      = "alg_gebouw_key";
    this.records_name = "buildings";
    this.record_name  = "building";
    this.fields = {"id"          : { "dbs": "alg_gebouw_key",             "typ": "key" },
                   "code"        : { "dbs": "alg_gebouw_code",            "typ": "varchar",               "filter": "like", "label": L("lcl_estate_gebouw_man_code") },
                   "name"        : { "dbs": "alg_gebouw_naam",            "typ": "varchar", "track": true,  "filter": "like", "label": L("lcl_estate_gebouw_man_name") },
                   "description" : { "dbs": "alg_gebouw_omschrijving",    "typ": "varchar", "track": true, "label": L("lcl_estate_gebouw_man_descr") },
                   "visitable"   : { "dbs": "alg_gebouw_bez",             "typ": "check",                 "label": L("lcl_estate_gebouw_bez")},
                   "callregistration" : { "dbs": "alg_gebouw_mld",        "typ": "check",                 "label": L("lcl_estate_gebouw_mld")},
                   "location"    : { "dbs": "alg_locatie_key",            "typ": "key",                   "foreign": "alg_locatie", "label": L("lcl_location")},
                   "buildingfunction": { "dbs": "alg_srtgebouw_key",
                                         "typ": "key",
                                         "foreign":  { tbl: "alg_srtgebouw",
                                               key: "alg_srtgebouw_key",
                                               desc: "alg_srtgebouw_omschrijving"
                                             },
                                        "label" : L("lcl_estate_gebouw_srtgebouw")
                                       },
                   "remark"      : { "dbs": "alg_gebouw_opmerking",       "typ": "varchar" ,              "label": L("lcl_estate_gebouw_man_opmerk")},
                   "openfrom"    : { "dbs": "alg_gebouw_beginuur",        "typ": "float",    "track": true, "label": L("lcl_estate_gebouw_beginuur") },
                   "opento"      : { "dbs": "alg_gebouw_einduur",         "typ": "float",    "track": true, "label": L("lcl_estate_gebouw_einduur") },
                   "workdays"    : { "dbs": "alg_gebouw_werkdagen",       "typ": "number"  ,              "label": L("lcl_estate_gebouw_werkdagen") },
                   "email"       : { "dbs": "alg_gebouw_email",           "typ": "varchar" ,              "label": L("lcl_alg_gebouw_email")  },
                   "area1"       : { "dbs": "alg_gebouw_bruto_vloeropp",  "typ": "number"  ,              "label": L("lcl_estate_gebouw_man_bruto_vloeropp")},
                   "perimeter"   : { "dbs": "alg_gebouw_omtrek",          "typ": "number"  ,              "label": L("lcl_estate_gebouw_man_omtrek")},
                   "volume"      : { "dbs": "alg_gebouw_inhoud",          "typ": "number"  ,              "label": L("lcl_estate_gebouw_man_inhoud")},
                   "expires"     : { "dbs": "alg_gebouw_vervaldatum",     "typ": "date"  ,                "label": L("lcl_alg_vervaldatum")},
                   // TODO alleen if S("ins_score_enabled") == 1
                   "mjb_score"   : { "dbs": "alg_gebouw_mjb_score1",      "typ": "number"  ,              "label": L("lcl_alg_gebouw_mjb_score1")},
                   "coordinate_x": { "dbs": "alg_gebouw_x",               "typ": "number"  ,              "label": L("lcl_geoxcoord")},
                   "coordinate_y": { "dbs": "alg_gebouw_y",               "typ": "number"  ,              "label": L("lcl_geoycoord")},
                   "externnr"    : { "dbs": "alg_gebouw_externnr",        "typ": "varchar" ,              "label": L("extern_nr"),       readonly: !(user.has("WEB_FACTAB") || user.has("WEB_FACXNR"))},
                   "externsyncdate": { "dbs": "alg_gebouw_externsyncdate", "typ": "datetime",             "label": L("extern_syncdate"), readonly: !(user.has("WEB_FACTAB") || user.has("WEB_FACXNR"))},
                   "deleted"     : { "dbs": "alg_gebouw_verwijder",       "typ": "datetime",              "label": L("lcl_deleted")}
                  },
     this.includes = {
                        "custom_fields" : {
                            "model": new model_custom_fields(this, new model_alg_kenmerk("G", { 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 bld_key = params.filter.id;
                var this_alg = alg.func_enabled(bld_key, "G");
                user.auth_required_or_abort(this_alg.readman || this_alg.readuse);
            }

            // TODO: Add authorization
            var query = api2.sqlfields(params, this);
            query.wheres.push("alg_gebouw_verwijder IS NULL");

            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_gebouw_key"; // key voor eventuele includes

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

            return json;
        }
}
%>