<% /*
    $Revision$
    $Id$

    File:           model_ins_kenmerk.inc

    Description:    Vanuit CodeCharge gegenereerd model voor ins_kenmerk

    Context:        Met parameter 'niveau' worden alleen de inspectiekenmerken getoond.
                    Zonder, alle niet-inspectie kenmerken.

    Notes:
*/
%>
<%

function model_ins_kenmerk(fnparams)
{
    this.table = "ins_kenmerk";
    this.primary = "ins_kenmerk_key";
    this.records_name = "ins_kenmerks";
    this.record_name = "ins_kenmerk";
    this.soft_delete = "ins_kenmerk_verwijder";
    this.autfunction = "WEB_INSMGT";
    this.record_title = L("ins_kenmerk");
    this.records_title = L("ins_kenmerk_m");


    this.fields = {
        "id": {
            "dbs": "ins_kenmerk_key",
            "label": "Key",
            "typ": "key",
            "required": true,
            "filter": "exact",
            "seq": "ins_s_ins_kenmerk_key",
            "readonly": true
        },
        "property_key": {
            "dbs": "ins_srtkenmerk_key",
            "label": L("mgt_srtkenmerk_key"),
            "typ": "key",
            "required": true,
            "foreign": {
                "tbl": "(select ins_srtkenmerk_key"
                     + "      , ins_srtkenmerk_omschrijving"
                     + "   from ins_srtkenmerk"
                     + "  where ins_srtkenmerk_verwijder is null"
                     + "  order by ins_srtkenmerk_omschrijving asc)",
                "key": "ins_srtkenmerk_key",
                "desc": "ins_srtkenmerk_omschrijving"
            },
            "filter": "exact"
        },
        "object_discipline": {
            "dbs": "ins_v_allsrtinstallatie.ins_discipline_key",
            "label": L("ins_srtinstallatie_discipline"),
            "typ": "key",
            "required": true,
            "insertonly": true,
            "foreign": { "tbl": "ins_v_aanwezigdiscipline",
                         "key": "ins_discipline_key",
                         "desc": "ins_discipline_omschrijving"
                       }
        },
        "object_group": {
            "dbs": "ins_v_allsrtinstallatie.ins_srtgroep_key",
            "label": L("ins_srtinstallatie_srtgroep"),
            "typ": "key",
            "insertonly": true,
            "foreign": { "tbl": "ins_srtgroep",
                         "key": "ins_srtgroep_key",
                         "desc": "ins_srtgroep_omschrijving"
                       }
        },
        "object_type": {
            "dbs": "ins_v_allsrtinstallatie.ins_srtdeel_key",
            "label": L("ins_srtinstallatie_srtdeel"),
            "typ": "key",
            "insertonly": true,
            "foreign": { "tbl": "ins_srtdeel",
                         "key": "ins_srtdeel_key",
                         "desc": "ins_srtdeel_omschrijving"
                       }
        },
        "object_key": {
            "dbs": "ins_srtinstallatie_key",
            "label": "ins_srtinstallatie_key",
            "typ": "key",
            "hidden_fld": true,
            "required": true,
            "foreign": {"tbl": "ins_srtcontrole",
                        "key": "ins_srtcontrole_key",
                        "desc": "ins_srtcontrole_omschrijving"
                       },
            "filter": "exact"
        },
        "level": {
            "dbs": "ins_kenmerk_niveau",
            "label": L("mgt_kenmerk_niveau"),
            "typ": "varchar",
            "hidden_fld": true,
            "LOV": fill_ins_niveau_LOV(),
            "defaultvalue": "C"
        },
        "edit_level": {
            "dbs": "ins_kenmerk_bewerkniveau",
            "label": L("ins_kenmerk_bewerkniveau"),
            "typ": "varchar",
            "required": true,
            "insertonly": true,
            "defaultvalue": "D",
            "LOV": L("ins_kenmerk_bewerkniveauLOV")
        },
        "sequence": {
            "dbs": "ins_kenmerk_volgnummer",
            "label": L("ins_kenmerk_volgnummer"),
            "typ": "number",
            "required": true
        },
        "required": {
            "dbs": "ins_kenmerk_verplicht",
            "label": L("ins_kenmerk_verplicht"),
            "typ": "key",
            "LOV": fill_verplicht_LOV(),
            "emptyoption": L("lcl_mld_niet_verplicht")
        },
        "property_group": {
            "dbs": "ins_kenmerk_groep",
            "label": L("ins_kenmerk_groep"),
            "typ": "key",
            "required": true,
            "LOV": fill_groepsverplichting_LOV(),
            "defaultvalue": "0"
        },
        "readonly": {
            "dbs": "ins_kenmerk_toonbaar",
            "label": L("mgt_kenmerk_toonbaar"),
            "typ": "check"
        },
        "measurement": {
            "dbs": "ins_kenmerk_meetwaarde",
            "label": L("ins_kenmerk_meetwaarde"),
            "typ": "check"
        },
        "clear": {
            "dbs": "ins_kenmerk_wissen",
            "label": L("ins_kenmerk_wissen"),
            "typ": "key",
            "LOV": L("ins_kenmerk_wissenLOV"),
            "defaultvalue": "0",
            "emptyoption": null
        },
        "location": {
            "dbs": "ins_kenmerk_locatiekolom",
            "label": L("ins_kenmerk_locatiekolom"),
            "typ": "varchar"
        },
        "default": {
            "dbs": "ins_kenmerk_default",
            "label": L("mgt_kenmerk_default"),
            "typ": "memo",
            "translate": true
        },
        "hint": {
            "dbs": "ins_kenmerk_hint",
            "label": L("mgt_kenmerk_hint"),
            "typ": "memo",
            "translate": true
        },
        "regexp": {
            "dbs": "ins_kenmerk_regexp",
            "label": L("mgt_kenmerk_regexp"),
            "typ": "varchar"
        }
    };


    this.list = {
        "columns": ["id",
                    "property_key",
                    "sequence",
                    "required",
                    "property_group"
                   ]
    };
    this.search = {
        "autosearch": true,
        "filters": ["level",
                    "object_discipline",
                    "object_group",
                    "object_type",
                    "property_key"
                   ]
    };
    this.edit = { modal: true};



    // Server side code!
    //
    // Afhankelijk van niveau:
    var niveau = fnparams.niveau;

    var gparams = { GET: { } };
    if (niveau == "C")
    {
        this.list.columns.splice(2, 0, "object_key");
        delete this.fields.object_discipline;
        delete this.fields.object_group;
        delete this.fields.object_type;
        delete this.fields.edit_level;
        delete this.fields.measurement;
        this.fields.object_key.label = L("ins_srtcontrole");
        this.record_title = L("ins_srtcontrole_kenmerk");
        this.records_title = L("ins_srtcontrole_kenmerk_m");
        gparams.GET = {wheres: [ "ins_kenmerk.ins_kenmerk_niveau = " + safe.quoted_sql(niveau)
                               , "ins_kenmerk.ins_kenmerk_verwijder IS NULL"
                               ]
                      };
    }
    else
    {   //kenmerk_niveau: D | G | S
        // Toevoegen extra kolommen voor niet-inspectie kenmerken.
        // NB: Standaard in this.list opnemen en bij niveau=C verwijderen geeft fout op niet bestaande ins_discipline_key etc.
        this.list.columns.splice(2, 0, "object_type");
        this.list.columns.splice(2, 0, "object_group");
        this.list.columns.splice(2, 0, "object_discipline");
        //
        this.record_title = L("ins_kenmerk");
        this.records_title = L("ins_kenmerk_m");
        gparams.GET = {tables: ["ins_v_allsrtinstallatie"],
                       wheres: [ "ins_kenmerk.ins_kenmerk_niveau <> 'C'",
                                 "ins_kenmerk.ins_srtinstallatie_key = ins_v_allsrtinstallatie.ins_srtinstallatie_key",
                                 "ins_kenmerk.ins_kenmerk_niveau = ins_v_allsrtinstallatie.niveau"
                               ]
                         };
    }

    function fill_verplicht_LOV()
    {   // Listbox verplicht vullen. Bij het veld is de emptyoption gezet.
        // Hierdoor is er altijd een waarde geselecteerd.
        return  "1;" + L("lcl_mld_altijd_verplicht")
             + ";2;" + L("lcl_mld_groepsverplichting");
    }

    function fill_groepsverplichting_LOV()
    {   // Listbox groepsverplichting vullen.
        var groep_array = ["0", "<"+L("lcl_none")+">"];
        for (i=1; i<=9; i++)
        {
            groep_array.push(i)
            groep_array.push(L("ins_kenmerk_groep")+" "+i);
        }
        return groep_array.join(";");
    }

    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, {});


    function fill_ins_niveau_LOV()
    {
        return  "D;"+L("mgt_srtkenmerk_discipline")
             + ";G;"+L("mgt_srtkenmerk_srtgroep")
             + ";S;"+L("mgt_srtkenmerk_srtdeel");
    }

}
%>