<% /*
    $Revision$
    $Id$

    File:           model_prs_ruimteafdeling.inc

    Description:    Toont de afdelingen bij een ruimte en zorgt voor het wijzigen
                    van de gegevens in de tabel prs_ruimteafdeling.

    Context:        Vanuit ../mgt/prs_ruimteafdeling of als include bij model_rooms.

    Notes:
*/
%>
<%

function model_prs_ruimteafdeling()
{
    this.table = "prs_ruimteafdeling";
    this.primary = "prs_ruimteafdeling_key";
    this.records_name = "prs_ruimteafdelings";
    this.record_name = "prs_ruimteafdeling";
    this.autfunction = "WEB_ALGUSE";
    this.record_title = L("lcl_prs_ruimteafdeling");
    this.records_title = L("lcl_prs_ruimteafdeling");

    this.fields = {
        "id": {
            "dbs": "prs_ruimteafdeling_key",
            "label": "Key",
            "typ": "key",
            "required": true,
            "seq": "prs_s_prs_ruimteafdeling_key",
            "hidden_fld": true
        },
        "company": {
            "dbs": "nn_bedrijf_naam",
            "sql": "(SELECT b.prs_bedrijf_naam"
                 + "   FROM prs_bedrijf b"
                 + "      , prs_afdeling a"
                 + "  WHERE a.prs_bedrijf_key = b.prs_bedrijf_key(+)"
                 + "    AND a.prs_afdeling_key = prs_ruimteafdeling.prs_afdeling_key"
                 + ")",
            "label": L("lcl_prs_dept_company"),
            "typ": "varchar",
            "readonly": true
        },
        "departmentcode": {
            "dbs": "nn_afdeling_naam",
            "sql": "(SELECT a.prs_afdeling_naam"
                 + "   FROM prs_afdeling a"
                 + "  WHERE a.prs_afdeling_key = prs_ruimteafdeling.prs_afdeling_key"
                 + ")",
            "label": L("lcl_prs_dept_name"),
            "typ": "varchar",
            "readonly": true
        },
        "department": {
            "dbs": "prs_afdeling_key",
            "label": L("lcl_prs_organisatie"),
            "typ": "key",
            "required": true,
            "foreign": "PRS_AFDELING",
            "track": true
        },
        "room": {
            "dbs": "alg_ruimte_key",
            "label": L("lcl_room"),
            "typ": "key",
            "foreign": "ALG_RUIMTE",
            "required": true
        },
        "costcentre": {
            "dbs": "nn_kostenplaats",
            "sql": "(SELECT k.prs_kostenplaats_nr"
                 + "   FROM prs_kostenplaats k"
                 + "      , prs_afdeling a"
                 + "  WHERE k.prs_kostenplaats_key(+) = a.prs_kostenplaats_key"
                 + "    AND a.prs_afdeling_key = prs_ruimteafdeling.prs_afdeling_key"
                 + ")",
            "label": L("lcl_prs_dept_kosten"),
            "typ": "varchar",
            "readonly": true
        },
        "responsible": {
            "dbs": "nn_naam_friendly",
            "sql": "(SELECT p.prs_perslid_naam_friendly"
                 + "   FROM prs_v_perslid_fullnames_all p"
                 + "      , prs_afdeling a"
                 + "  WHERE p.prs_perslid_key(+) = a.prs_perslid_key_verantw"
                 + "    AND a.prs_afdeling_key = prs_ruimteafdeling.prs_afdeling_key"
                 + ")",
            "label": L("lcl_prs_dept_verantw"),
            "typ": "varchar",
            "readonly": true
        },
        "occupation": {
            "dbs": "prs_ruimteafdeling_bezetting",
            "label": L("lcl_prs_ruimtebezetting"),
            "typ": "float",
            "iscurrency": true,  // bezettingspercentage in 2 cijfers achter de komma.
            "required": true,
            "multiedit": true,
            "track": true
        },
        "startdate": {
            "dbs": "prs_ruimteafdeling_ingangsdatum",
            "label": L("lcl_start_date"),
            "typ": "date",
            "defaultvalue": new Date(),
            "required": true,
            "multiedit": true,
            "track": true
        },
        "enddate": {
            "dbs": "prs_ruimteafdeling_einddatum",
            "label": L("lcl_end_date"),
            "typ": "date",
            "multiedit": true,
            "track": true
        }
    };

    this.hook_pre_edit = function(obj, fld)
    {
        fld.departmentcode.hidden_fld = true;
        fld.room.hidden_fld = true;
        fld.costcentre.hidden_fld = true;
    }

    function huidig_ruimteafdeling(the_key)
    {
        var sql = "SELECT ra.alg_ruimte_key"
                + "     , ra.prs_afdeling_key"
                + "  FROM prs_ruimteafdeling ra"
                + " WHERE ra.prs_ruimteafdeling_key = " + the_key;
        var oRs = Oracle.Execute(sql);
        var prs = { ruimte_key: oRs("alg_ruimte_key").Value
                  , afdeling_key: oRs("prs_afdeling_key").Value
                  };
        oRs.Close();
        return prs;
    }

    this.REST_GET  = generic_REST_GET(this);
    this.REST_POST = function (params, obj)
        {
            var fkTable = foreignKeyTable(this.fields.department.foreign);
            var track_arr = [ (L("lcl_prs_ruimteafdeling_new").format(getForeignText(obj.department, fkTable))) ];
            var post_result = generic_REST_POST(this)(params, obj);
            shared.trackaction("ALGRUP", obj.room, track_arr.join("\n"));
            return post_result;
        }

    this.REST_PUT = function (params, obj, the_key)
        {
            var cur_ra = huidig_ruimteafdeling(the_key);
            var dbfields = api2.update_fields(params, this, obj); // Build updater
            var wheres = [" prs_ruimteafdeling_key = " + the_key];
            var prsUpd = buildTrackingUpdate("prs_ruimteafdeling", wheres, dbfields, { noValidateToken: true });
            var put_result = { key: the_key, warning: "" };
            var track_arr = [];
            if (prsUpd.trackarray.length > 0)
            {
                var fkTable = foreignKeyTable(this.fields.department.foreign);
                var track_arr = [L("lcl_prs_ruimteafdeling_upd").format(getForeignText(cur_ra.afdeling_key, fkTable))];
                track_arr = track_arr.concat(prsUpd.trackarray);

                put_result = generic_REST_PUT(this)(params, obj, the_key);
                shared.trackaction("ALGRUP", cur_ra.ruimte_key, track_arr.join("\n"));
            }
            return put_result;
        };
    this.REST_DELETE = function (params, the_key)
        {
            var cur_ra = huidig_ruimteafdeling(the_key);
            var fkTable = foreignKeyTable(this.fields.department.foreign);
            var track_arr = [L("lcl_prs_ruimteafdeling_del").format(getForeignText(cur_ra.afdeling_key, fkTable))];

            var delete_result = generic_REST_DELETE(this)(params, the_key);
            shared.trackaction("ALGRUP", cur_ra.ruimte_key, track_arr.join("\n"));
            return delete_result;
        }
}
%>