<% /*
    $Revision$
    $Id$

    File:           model_districts.inc

    Description:    District model.
    Parameters:
    Context:

    Notes:
*/

%>
<!-- #include file="../Shared/discxalg3d.inc"-->
<!-- #include file="./model_custom_fields.inc"-->
<!-- #include file="./model_alg_kenmerk.inc"-->
<%
function model_districts()
{
    this.table = "alg_district";
    this.primary = "alg_district_key";
    this.records_name = "districts";
    this.record_name = "district";
    this.fields = {
        "id":             { dbs: "alg_district_key",             typ: "key",      label: "Key" },
        "name":           { dbs: "alg_district_omschrijving",    typ: "varchar",  label: L("lcl_district")},
        "region":         { dbs: "alg_regio_key",                typ: "key",      foreign: "alg_regio", comment: "reference to Region"},
        "externnr":       { dbs: "alg_district_externnr",        typ: "varchar",  label: L("extern_nr"),       readonly: !(user.has("WEB_FACTAB") || user.has("WEB_FACXNR"))},
        "externsyncdate": { dbs: "alg_district_externsyncdate",  typ: "datetime", label: L("extern_syncdate"), readonly: !(user.has("WEB_FACTAB") || user.has("WEB_FACXNR"))},
        "deleted":        { dbs: "alg_district_verwijder",       typ: "datetime", label: L("lcl_deleted")}
    };

    this.list = {
        columns: ["id", "name"]
    };

    this.includes = {
        "custom_fields" : {
            "model": new model_custom_fields(this, new model_alg_kenmerk("D", { internal: true }), { readman: true, readuse: true }),
            "joinfield": "flexparentkey",
            "enable_update": true
        }
    };

    this.REST_GET = function _GET(params)
        {
            var urole = "fe"; // TODO: Moet echt niet ter zake doen
            var autfunction = urole == "fe"? "WEB_ALGUSE" : "WEB_ALGMAN";
            params.authparams = user.checkAutorisation(autfunction, null, null, true); // pessimistisch

            // TODO: Add authorization
            var query = api2.sqlfields(params, this);
            query.wheres.push("alg_district_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_district_key, alg_district_omschrijving";

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

            return json;
        };

    this.REST_PUT = function _PUT(params) /* update district */
    {
    };
    this.REST_POST = function _POST(params) /* new district */
    {
    };
    this.REST_DELETE = function _DELETE(params) /* delete district */
    {
    };
}
%>