<% /*
    $Revision$
    $Id$

    File:           model_res_kenmerk.inc

    Description:    Vanuit CodeCharge gegenereerd model voor res_kenmerk

    Context:

    Notes:
*/
%>
<!-- #include file="model_res_srtkenmerk.inc"-->
<%
function model_res_kenmerk(niveau, params)
{
    params = params || {};
    this.records_name = "bookingproperties";
    this.record_name = "bookingproperty";
    this.table = "res_kenmerk";
    this.primary = "res_kenmerk_key";
    this.soft_delete = "res_kenmerk_verwijder";
    this.autfunction = params.internal? false : "WEB_RESMGT";
    this.record_title = L("res_kenmerk");
    this.records_title = L("res_kenmerk_m");
    this.module = "RES";
    this.defaultIcon = "fa-table-tree";

    this.fields = {
        "id": {
            "dbs": "res_kenmerk_key",
            "label": L("lcl_key"),
            "typ": "key",
            "required": true,
            "seq": "res_s_res_kenmerk_key"
        },
        "bookingpropertytype": {
            "dbs": "res_srtkenmerk_key",
            "label": L("res_srtkenmerk_key"),
            "typ": "key",
            "required": true,
            "insertonly": true,
            "foreign": {
                "tbl": "(SELECT res_srtkenmerk_key"
                     + "      , res_srtkenmerk_omschrijving"
                     + (mode != "list"
                       ? " || ' (' || res_srtkenmerk_kenmerktype || ')'"
                       : "")
                     + " oms"
                     + "      , res_srtkenmerk_kenmerktype"
                     + "   FROM res_srtkenmerk"
                     + "  WHERE res_srtkenmerk_verwijder is null"
                     + "  ORDER BY res_srtkenmerk_omschrijving ASC)",
                "key": "res_srtkenmerk_key",
                "desc": "oms",
                "extravalue": "res_srtkenmerk_kenmerktype"
            },
            "LOVinit": "",
            "friendlyname": true,
            "showtransit": true
        },
        "objectpropertydesc": {
            "dbs": "res_kenmerk_omschrijving",
            "label": L("mgt_kenmerk_omschrijving"),
            "typ": "varchar",
            "translate": true,
            "filter": "like"
        },
        "level": {
            "dbs": "res_kenmerk_niveau", // "A"=Activiteit of "D"=Discipline of "C"=Catering artikel.
            "label": L("lcl_niveau"),
            "typ": "varchar",
            "required": true,
            "hidden_fld": true,
            "LOV": buildLevelList(),
            "defaultvalue": "A"
        },
        "sequence": {
            "dbs": "res_kenmerk_volgnummer",
            "label": L("res_kenmerk_volgnummer"),
            "typ": "number",
            "len": 3,
            "required": true
        },
        "rolecode": { 
            "dbs": "res_kenmerk_rolcode",
            "label": L("mgt_kenmerk_rolcode"),
            "typ": "number",
            "LOV": buildKenmerkRoleCodeLOV({ module: "RES" }),
            "required": true,
            "multiedit": true
        },
        "bookingactivity": {
            "dbs": "res_activiteit_key",
            "label": L("res_activiteit_key"),
            "typ": "key",
            "required": true,
            "foreign": {
                "tbl": "(SELECT res_activiteit_key, res_activiteit_omschrijving"
                     + "   FROM res_activiteit"
                     + "  WHERE res_activiteit_verwijder IS NULL"
                     + "  ORDER BY res_activiteit_upper ASC)",
                "key": "res_activiteit_key",
                "desc": "res_activiteit_omschrijving"
            },
            "LOVinit": "",
            "multiedit": true,
            "showtransit": true
        },
        "article": {
            "dbs": "res_artikel_key",
            "label": L("res_artikel"),
            "typ": "key",
            "required": true,
            "foreign": {
                "tbl": "(SELECT res_artikel_key, res_artikel_omschrijving"
                     + "   FROM res_artikel"
                     + "  WHERE res_artikel_verwijder IS NULL"
                     + "    AND (res_artikel_vervaldatum IS NULL OR res_artikel_vervaldatum > SYSDATE)"
                     + "  ORDER BY UPPER(res_artikel_omschrijving) ASC)",
                "key": "res_artikel_key",
                "desc": "res_artikel_omschrijving"
            },
            "LOVinit": "",
            "multiedit": true
        },
        "bookingdiscipline": {
            "dbs": "res_discipline_key",
            "label": L("res_discipline_key"),
            "typ": "key",
            "insertonly": true,
            "foreign": {
                "tbl": "res_discipline",
                "key": "ins_discipline_key",
                "desc": "ins_discipline_omschrijving",
                "where": "ins_discipline_verwijder is null"
            }
        },
        "required": {
            "dbs": "res_kenmerk_verplicht",
            "label": L("res_kenmerk_verplicht"),
            "typ": "key",
            "LOV": buildVerplichtingList(),
            "emptyisrealvalue": true,
            "emptyoption": null,
            "multiedit": true
        },
        "group": {
            "dbs": "res_kenmerk_groep",
            "label": L("res_kenmerk_groep"),
            "typ": "key",
            "required": true,
            "LOV": buildGroepsverplichtingList(),
            "emptyoption": null,
            "multiedit": true
        },
        "readonly": {
            "dbs": "res_kenmerk_toonbaar",
            "label": L("res_kenmerk_toonbaar"),
            "typ": "check",
            "multiedit": true
        },
        "inlist": {
            "dbs": "res_kenmerk_inlijst",
            "label": L("mgt_kenmerk_systeem_overview"),
            "typ": "check0",
            "multiedit": true
        },
        "propertydefault": {
            "dbs": "res_kenmerk_default",
            "label": L("res_kenmerk_default"),
            "typ": "memo",
            "translate": true
        },
        "showexpression": {
            "dbs": "res_kenmerk_show_expr",
            "label": L("mgt_kenmerk_show_expr"),
            "typ": "memo"
        },
        "hint": {
            "dbs": "res_kenmerk_hint",
            "label": L("res_kenmerk_hint"),
            "typ": "memo",
            "translate": true
        },
        "regexp": {
            "dbs": "res_kenmerk_regexp",
            "label": L("res_kenmerk_regexp"),
            "typ": "varchar"
        },
        "code": {
            "dbs": "res_kenmerk_code",
            "label": L("mgt_kenmerk_code"),
            "typ": "varchar",
            "readonly": !user.has("WEB_PRSSYS")
        },
        "authorization": {
            "dbs": "fac_functie_key",
            "label": L("mgt_kenmerk_autorisatie"),
            "typ": "key",
            "foreign": "fac_functie"
        }
    };

    this.list = {
        "columns": ["id",
                    "bookingpropertytype",
                    "objectpropertydesc",
                    "sequence",
                    "level",
                    "required",
                    "group",
                    "authorization"
                   ]
    };

    this.search = {
        "autosearch": true,
        "filters": ["level",
                    "bookingpropertytype",
                    "bookingactivity",
                    "article",
                    "bookingdiscipline"
                   ]
    };

    var gparams = { GET: { wheres: [ "res_kenmerk.res_kenmerk_verwijder IS NULL"]} };

    if (niveau == "D")
    { // Kenmerk niveau: "D"=Discipline/Catalogus .
      // Toevoegen extra kolom voor discipline kenmerken.
      this.fields.level.defaultvalue = "D";
      delete this.fields.bookingactivity;
      delete this.fields.article;
      this.record_title = L("res_article_kenmerk");
      this.records_title = L("res_article_kenmerk_m");
      gparams.GET.wheres.push("res_kenmerk.res_kenmerk_niveau = 'D'");
    }
    else if (niveau == "C")
    { // Kenmerk niveau: "C"= Catering artikel.
      // Toevoegen extra kolom voor artikel kenmerken.
      // Deze kolom wordt trouwens niet getoond in het include frame onder in het artikel scherm, omdat het joinfield van de include de "article" is.
      // De details van het artikel staan dan in de gegevens erboven. De extra kolom met het artikel is dan overtollige informatie.
      this.fields.level.defaultvalue = "C";
      delete this.fields.bookingactivity;
      delete this.fields.bookingdiscipline;
      this.record_title = L("res_article_kenmerk");
      this.records_title = L("res_article_kenmerk_m");
      gparams.GET.wheres.push("res_kenmerk.res_kenmerk_niveau = 'C'");
    }
    else if (niveau == "A")
    { // Kenmerk_niveau: "A"=Activiteit.
      // Toevoegen extra kolom voor activiteit kenmerken.
      this.fields.level.defaultvalue = "A";
      delete this.fields.article;
      delete this.fields.bookingdiscipline;
      this.record_title = L("res_kenmerk");
      this.records_title = L("res_kenmerk_m");
      gparams.GET.wheres.push("res_kenmerk.res_kenmerk_niveau = 'A'");
    }
    else
    { // overzicht, alles in beeld
    }

    this.getPropertyType = function (kenmerkdata)
    {
        var typedata = api2.GET(new model_res_srtkenmerk({internal: params.internal}), kenmerkdata.bookingpropertytype.id);
        return typedata;
    }

    this.hook_pre_show = function (obj, fld)
    {
        var srttype = get_bookingpropertytype_srttype(obj);
        fld.propertydefault.label = srttype.lbl_default;
        fld.readonly.label = srttype.lbl_readonly;
    }
    this.hook_pre_edit = function (obj, fld)
    {
        var srttype = get_bookingpropertytype_srttype(obj);
        fld.propertydefault.label = srttype.lbl_default;
        fld.readonly.label = srttype.lbl_readonly;
    }

    get_bookingpropertytype_srttype = function (obj)
    {
        var result = { lbl_default: L("mgt_kenmerk_default")
                     , lbl_readonly: L("res_kenmerk_toonbaar")
                     };
        if (obj.bookingpropertytype && obj.bookingpropertytype.id)
        {
            var sql = "SELECT s.res_srtkenmerk_kenmerktype"
                    + "  FROM res_srtkenmerk s"
                    + " WHERE s.res_srtkenmerk_key = "+ obj.bookingpropertytype.id;
            var oRs = Oracle.Execute(sql);
            if (!oRs.eof)
                result.lbl_default  = (oRs("res_srtkenmerk_kenmerktype").Value == "l" ? L("mgt_kenmerk_icon"): L("mgt_kenmerk_default"));
                result.lbl_readonly = (oRs("res_srtkenmerk_kenmerktype").Value == "l" ? L("mgt_kenmerk_collapsed"): L("mgt_kenmerk_toonbaar"));
            oRs.Close();
        }
        return result;
    }

    function buildLevelList()
    {   // LOV waarden voor required.
        var s = "A;" + L("res_activiteit_key")
              + ";D;" + L("res_discipline_key")
              + ";C;" + L("res_artikel");
        return s;
    }


    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 = generic_REST_DELETE(this);
}
%>