<% /*
    $Revision$
    $Id$

    File:           model_prs_afdeling.inc

    Description:    Afdelingen model.
    Parameters:
    Context:

    Notes:
*/
%>
<!-- #include file="../prs/prs.inc" -->
<!-- #include file="./model_custom_fields.inc"-->
<!-- #include file="./model_prs_kenmerk.inc"-->
<%

function model_prs_afdeling()
{
    this.table = "prs_afdeling";
    this.primary = "prs_afdeling_key";
    this.records_name = "departments";
    this.record_name = "department";
    this.autfunction = "WEB_PRSUSE";
    this.soft_delete = "prs_afdeling.prs_afdeling_verwijder";

    this.fields = {
        "id": {
            "dbs": "prs_afdeling_key",
            "typ": "key",
            "seq": "prs_s_prs_afdeling_key"
        },
        "name": {
            "dbs": "prs_afdeling_naam",
            "typ": "varchar"
        },
        "description": {
            "dbs": "prs_afdeling_omschrijving",
            "typ": "varchar"
        },
        "externalid": {
            "dbs": "prs_afdeling_externid",
            "label": L("prs_afdeling_externid"),
            "typ": "varchar"
        },
        "externsyncdate": {
            "dbs": "prs_afdeling_externsyncdate",
            "label": L("extern_syncdate"),
            "typ": "datetime",
            "readonly": !(user.has("WEB_FACTAB") || user.has("WEB_FACXNR"))
        },
        "parent": {
            "dbs": "prs_afdeling_parentkey",
            "label": L("lcl_prs_organisatie"),
            "typ": "key",
            "foreign": "prs_afdeling"
        },
        "company": {
            "dbs": "prs_bedrijf_key",
            "label": L("lcl_prs_company"),
            "typ": "key",
            "foreign": {
                "tbl": "prs_bedrijf",
                "key": "prs_bedrijf_key",
                "desc": "prs_bedrijf_naam",
                "desc_is_unique": "prs_bedrijf_verwijder IS NULL"
            }
        },
        "xcompany": {
            "dbs": "top_prs_bedrijf_key",
            "sql": "pa.prs_bedrijf_key",
            "label": L("lcl_prs_company"),
            "typ": "key",
            "foreign": "prs_bedrijf"
        },
        "costcentre": {
            "dbs": "prs_kostenplaats_key",
            "label": L("prs_kostenplaats_key"),
            "typ": "key",
            "foreign": {
                "tbl": "prs_kostenplaats",
                "key": "prs_kostenplaats_key",
                "desc": "prs_kostenplaats_nr",
                "desc_is_unique": "prs_kostenplaats_verwijder IS NULL"
            }
        },
            "created": {
            "dbs": "prs_afdeling_aanmaak",
            "label": "Aanmaakdatum",
            "typ": "datetime",
            "readonly": true
        }
    };

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

    var gparams = { "GET": {
                         tables: [ "prs_v_afdeling  pa"],
                         wheres: [ "pa.prs_afdeling_key = prs_afdeling.prs_afdeling_key" ]
                    }
                  }

    var authparamsUSE = user.checkAutorisation("WEB_PRSUSE");
    if (authparamsUSE.PRSreadlevel == 0) // prs_bedrijf
    {
        gparams["GET"].wheres.push("pa.prs_bedrijf_key = " + user.afdeling().prs_bedrijf_key());
    }
    else if (authparamsUSE.PRSreadlevel > 0)
    {
        gparams["GET"].wheres.push(
               "prs_afdeling.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 + "))");
    }


    this.REST_GET    = generic_REST_GET(this, gparams);
    this.REST_POST   = generic_REST_POST(this);
    this.REST_PUT    = generic_REST_PUT(this);
    this.REST_DELETE = function (params, the_key)
                        {
                            var autparams = user.checkAutorisation(this.autfunction);
                            user.auth_required_or_abort(autparams.PRSwritelevel < 9 && autparams.ALGwritelevel < 9);
                            var sql = "BEGIN prs.set_afdeling_verwijder({0}); END;".format(the_key);
                            var err = Oracle.Execute(sql, true);
                            if (err.friendlyMsg)
                                api2.error(400, err.friendlyMsg); // Veronderstel Bad Request

                            return { key: the_key, warning: "" };
                        }
}
%>