<% /*
    $Revision$
    $Id$

    File:           model_bes_kenmerk.inc

    Description:    Model voor bes_kenmerk = Kenmerken van bestelregels

    Context:

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

function model_bes_kenmerk(params)
{
    params = params || {};
    this.records_name = "orderlineproperties";
    this.record_name = "orderlineproperty";
    this.table = "bes_kenmerk";
    this.primary = "bes_kenmerk_key";
    this.soft_delete = "bes_kenmerk_verwijder";
    this.autfunction = params.internal?false:"WEB_BESMGT";
    this.record_title = L("bes_kenmerk");
    this.records_title = L("bes_kenmerk_m");
    this.module = "BES";

    this.fields = {
        "id": {
            "dbs": "bes_kenmerk_key",
            "label": L("lcl_key"),
            "typ": "key",
            "required": true,
            "seq": "bes_s_bes_kenmerk_key",
            "defaultvalue": null
        },
        "orderdisciplinehide": {
            "dbs": "bes_srtinstallatie_key",
            "label": L("bes_ins_discipline_key"),
            "typ": "key",
            "hidden_fld": true,
            "foreign": {
                "tbl": "bes_v_aanwezigdiscipline",
                "key": "ins_discipline_key",
                "desc": "ins_discipline_omschrijving"
            }
        },
        "orderpropertytype": {
            "dbs": "bes_srtkenmerk_key",
            "label": L("mgt_srtkenmerk_key"),
            "typ": "key",
            "required": true,
            "insertonly": true,
            "foreign": {
                "tbl": "(SELECT bes_srtkenmerk_key"
                     + "      , bes_srtkenmerk_omschrijving"
                     + (mode != "list"
                       ? " || ' (' || bes_srtkenmerk_kenmerktype || ')'"
                       : "")
                     + " oms"
                     + "      , bes_srtkenmerk_kenmerktype"
                     + "   FROM bes_srtkenmerk"
                     + "  WHERE bes_srtkenmerk_verwijder IS NULL"
                     + "  ORDER by bes_srtkenmerk_omschrijving ASC)",
                "key": "bes_srtkenmerk_key",
                "desc": "oms",
                "extravalue": "bes_srtkenmerk_kenmerktype"
            },
            "defaultvalue": null,
            "LOVinit": "",
            "friendlyname": true,
            "showtransit": true
        },
        "orderpropertydesc": {
            "dbs": "bes_kenmerk_omschrijving",
            "label": L("mgt_kenmerk_omschrijving"),
            "typ": "varchar",
            "translate": true,
            "filter": "like"
        },
        "orderdiscipline": {
            "dbs": "bes_kenmerkbestelregel.ins_discipline_key",
            "label": L("bes_ins_discipline_key"),
            "typ": "key",
            "required": true,
            "insertonly": true,
            "foreign": {
                "tbl": "bes_v_aanwezigdiscipline",
                "key": "ins_discipline_key",
                "desc": "ins_discipline_omschrijving"
            },
            "showtransit": true
        },
        "orderarticlegroup": {
            "dbs": "bes_kenmerkbestelregel.bes_srtgroep_key",
            "label": L("bes_srtgroep"),
            "typ": "key",
            "insertonly": true,
            "foreign": {
                "tbl": "bes_srtgroep",
                "key": "bes_srtgroep_key",
                "desc": "bes_srtgroep_omschrijving"
            }
        },
        "orderarticle": {
            "dbs": "bes_kenmerkbestelregel.bes_srtdeel_key",
            "label": L("bes_srtdeel"),
            "typ": "key",
            "insertonly": true,
            "foreign": {
                "tbl": "bes_srtdeel",
                "key": "bes_srtdeel_key",
                "desc": "bes_srtdeel_omschrijving"
            }
        },
        "level": {
            "dbs": "bes_kenmerk_niveau",
            "label": L("mgt_kenmerk_niveau"),
            "typ": "varchar",
            "hidden_fld": true,
            "LOV": buildBESKenmerkNiveauList(),
            "emptyoption": null,
            "defaultvalue": "D"
        },
        "attributetype": {
            "dbs": "bes_kenmerk_type",
            "label": L("bes_kenmerk_type"),
            "typ": "varchar",
            "hidden_fld": true,
            "defaultvalue": "I"
        },
        "sequence": {
            "dbs": "bes_kenmerk_volgnummer",
            "label": L("mgt_kenmerk_volgnummer"),
            "typ": "number",
            "len": 3,
            "required": true
        },
        "rolecode": {
            "dbs": "bes_kenmerk_rolcode",
            "label": L("mgt_kenmerk_rolcode"),
            "typ": "number",
            "LOV": buildKenmerkRoleCodeLOV({ module: "BESI" }),
            "required": true
        },
        "required": {
            "dbs": "bes_kenmerk_verplicht",
            "label": L("mgt_kenmerk_verplicht"),
            "typ": "key",
            "LOV": buildVerplichtingList(),
            "emptyisrealvalue": true,
            "emptyoption": null
        },
        "group": {
            "dbs": "bes_kenmerk_groep",
            "label": L("mgt_kenmerk_groep"),
            "typ": "key",
            "required": true,
            "LOV": buildGroepsverplichtingList(),
            "emptyoption": null
        },
        "readonly": {
            "dbs": "bes_kenmerk_toonbaar",
            "label": L("mgt_kenmerk_toonbaar"),
            "typ": "check"
        },
        "system": {
            "dbs": "bes_kenmerk_inlijst",
            "label": L("mgt_srtkenmerk_systeem_adv"),
            "typ": "number",
            "bits": [
                {
                    "name": "",
                    "label": "",
                    "showasgroup": false,
                    "typ": "check",
                    "mask": 3,
                    "radios": [
                        {
                            "name": "overview_req",
                            "label": L("mgt_kenmerk_systeem_overview_req"),
                            "mask": 1,
                            "defaultvalue": 0
                        },
                        {
                            "name": "overview_pur",
                            "label": L("mgt_kenmerk_systeem_overview_pur"),
                            "mask": 2,
                            "defaultvalue": 0
                        }
                    ]
                }
            ],
            "defaultvalue": 0,
            "required": true
        },
        "propertydefault": {
            "dbs": "bes_kenmerk_default",
            "label": L("mgt_kenmerk_default"),
            "typ": "memo",
            "translate": true
        },
        "showexpression": {
            "dbs": "bes_kenmerk_show_expr",
            "label": L("mgt_kenmerk_show_expr"),
            "typ": "memo"
        },
        "hint": {
            "dbs": "bes_kenmerk_hint",
            "label": L("mgt_kenmerk_hint"),
            "typ": "varchar",
            "translate": true,
            "filter": "like",
            "defaultvalue": null
        },
        "regexp": {
            "dbs": "bes_kenmerk_regexp",
            "label": L("mgt_kenmerk_regexp"),
            "typ": "varchar"
        },
        "code": {
            "dbs": "bes_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.getPropertyType = function (kenmerkdata)
    {
        var typedata = api2.GET(new model_bes_srtkenmerk({internal: params.internal}), kenmerkdata.orderpropertytype.id);
        return typedata;
    }

    function buildBESKenmerkNiveauList()
    {
        return  "D;"+L("bes_ins_discipline_key")
             + ";G;"+L("bes_srtgroep")
             + ";S;"+L("bes_srtdeel");
    }

    function get_orderpropertytype_srttype(obj)
    {
        var result = { lbl_default:  L("mgt_kenmerk_default")
                     , lbl_readonly: L("mgt_kenmerk_toonbaar")
                     };
        if (obj.orderpropertytype && obj.orderpropertytype.id)
        {
            var sql = "SELECT bes_srtkenmerk_kenmerktype"
                    + "  FROM bes_srtkenmerk"
                    + " WHERE bes_srtkenmerk_key = " + obj.orderpropertytype.id;
            var oRs = Oracle.Execute(sql);
            if (!oRs.eof)
                result.lbl_default  = (oRs("bes_srtkenmerk_kenmerktype").Value == "l" ? L("mgt_kenmerk_icon"): L("mgt_kenmerk_default"));
                result.lbl_readonly = (oRs("bes_srtkenmerk_kenmerktype").Value == "l" ? L("mgt_kenmerk_collapsed"): L("mgt_kenmerk_toonbaar"));
            oRs.Close();
        }
        return result;
    }


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

    this.hook_pre_edit = function (obj, fld)
    {
        if (obj.id == -1)
        {
            fld.orderarticlegroup.foreign.where = "bes_srtgroep_key IS NULL";
            fld.orderarticle.foreign.where = "bes_srtdeel_key IS NULL"; // Geen artikelen ophalen: zijn er veel te veel.
        }
        var srttype = get_orderpropertytype_srttype(obj);
        fld.propertydefault.label = srttype.lbl_default;
        fld.readonly.label = srttype.lbl_readonly;
    %>
    <script type="text/javascript">
      var module = "BES";
    </script>
    <%
    }

    this.hook_pre_post = function (params, jsondata)
    {
        var checkdata = { module: "bes",
                          srtkenmerk: true,
                          key: (jsondata.orderpropertytype.id ? jsondata.orderpropertytype.id : jsondata.orderpropertytype),
                          value: jsondata.propertydefault
                        };
        checkSrtkenmerkValue(checkdata);
        if (checkdata.err != "")
            abort_with_warning(checkdata.err);

        jsondata.propertydefault = checkdata.value;
    }


    var ext_sql = "(SELECT DISTINCT"
                + "        B.bes_kenmerk_key"
                + "      , S.bes_srtkenmerk_omschrijving"
                + "      , B.bes_kenmerk_volgnummer"
                + "      , B.bes_kenmerk_niveau"
                + "      , B.bes_kenmerk_type"
                + "      , B.bes_kenmerk_inlijst"
                + "      , B.bes_srtinstallatie_key"
                + "      , B.bes_srtkenmerk_key"
                + "      , B.bes_kenmerk_hint"
                + "      , S.bes_srtkenmerk_key"
                + "      , I.bes_discipline_key ins_discipline_key" //I.ins_discipline_key
                + "      , CASE WHEN b.bes_kenmerk_niveau <> 'D' THEN I.bes_srtgroep_key END bes_srtgroep_key"
                + "      , CASE WHEN b.bes_kenmerk_niveau = 'S'  THEN I.bes_srtdeel_key  END bes_srtdeel_key"
                + "   FROM bes_kenmerk B"
                + "      , bes_srtkenmerk S"
                + "      , BES_V_ALLSRTINSTALLATIE I"
                + "  WHERE B.bes_srtkenmerk_key = S.bes_srtkenmerk_key"
                + "    AND B.bes_kenmerk_type = 'I'"
                + "    AND S.bes_srtkenmerk_verwijder IS NULL"
                + "    AND B.bes_srtinstallatie_key = I.bes_srtinstallatie_key"
                + "    AND B.bes_kenmerk_niveau = I.niveau"
                + ") bes_kenmerkbestelregel";


    this.REST_GET    = generic_REST_GET(this, {
        "GET": {
            "tables": [ ext_sql],
            "wheres": [ "bes_kenmerk.bes_kenmerk_key = bes_kenmerkbestelregel.bes_kenmerk_key" ]
        }
    });

    this.REST_POST   = generic_REST_POST(this);
    this.REST_PUT    = generic_REST_PUT(this);
    this.REST_DELETE = generic_REST_DELETE(this);
}
%>