<% /*
    $Revision$
    $Id$

    File:           model_ins_kenmerk.inc

    Description:    Vanuit CodeCharge gegenereerd model voor ins_kenmerk

    Context:        paramater: niveau=O:    Object kenmerken (level: D=Discipline, G=Objectgroep, S=Objectsoort)
                               niveau=T:    Taak kenmerken (level:  T=Alle taken, C=Periodiek taken, E=Taakcategorie)

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

function model_ins_kenmerk(niveau, params)
{
    var isTaakKenmerk = (niveau == "T" || niveau == "E" || niveau == "C");

    params = params || {};
    this.records_name = "objectproperties";
    this.record_name = "objectproperty";
    this.table = "ins_kenmerk";
    this.primary = "ins_kenmerk_key";
    this.soft_delete = "ins_kenmerk_verwijder";
    this.autfunction = params.internal?false:"WEB_INSMGT";
    this.record_title = (isTaakKenmerk? L("ins_srtcontrole_kenmerk")    : L("ins_kenmerk"));
    this.records_title = (isTaakKenmerk? L("ins_srtcontrole_kenmerk_m") : L("ins_kenmerk_m"));
    this.module = "INS";
    this.defaultIcon = "fa-table-tree";

    this.fields = {
        "id": {
            "dbs": "ins_kenmerk_key",
            "label": L("lcl_key"),
            "typ": "key",
            "required": true,
            "seq": "ins_s_ins_kenmerk_key",
            "readonly": true
        },
        "objectpropertytype": {
            "dbs": "ins_srtkenmerk_key",
            "label": L("mgt_srtkenmerk_key"),
            "typ": "key",
            "required": true,
            "insertonly": true,
            "foreign": {
                "tbl": "(SELECT ins_srtkenmerk_key"
                     + "      , ins_srtkenmerk_omschrijving"
                     + (mode != "list"
                       ? " || ' (' || ins_srtkenmerk_kenmerktype || ')'"
                       : "")
                     + " oms"
                     + "      , ins_srtkenmerk_kenmerktype"
                     + "   FROM ins_srtkenmerk"
                     + "  WHERE ins_srtkenmerk_verwijder IS NULL"
                     + "  ORDER by ins_srtkenmerk_omschrijving ASC)",
                "key": "ins_srtkenmerk_key",
                "desc": "oms",
                "extravalue": "ins_srtkenmerk_kenmerktype"
            },
            "showtransit": true
        },
        "objectpropertydesc": {
            "dbs": "ins_kenmerk_omschrijving",
            "label": L("mgt_kenmerk_omschrijving"),
            "typ": "varchar",
            "translate": true,
            "filter": "like"
        },
        "objectdiscipline": {
            "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"
                       },
            "showtransit": true
        },
        "objectgroup": {
            "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"
                       },
            "showtransit": true
        },
        "objecttype": {
            "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"
                       },
            "showtransit": true,
            "infoPointer": { Url: "appl/ins/ins_srtdeel.asp?srtdeel_key=" }
        },
//        "attributetypehide": { //het datatype van het kenmerksoort, zal-wel gekopieerd van CNT
//            "dbs": "ins_srtkenmerk.ins_srtkenmerk_kenmerktype",
//            "label": L("mgt_srtkenmerk_kenmerktype"),
//            "typ": "varchar",
//            "hidden_fld": true,
//            "LOV": buildKenmerktypeLOV(),
//            "showtransit": true
//        },
        "level": {
            "dbs": "ins_kenmerk_niveau",
            "label": L("mgt_kenmerk_niveau"),
            "typ": "varchar",
            "LOV": fill_ins_niveau_LOV(),
            "defaultvalue": "T",
            "showtransit": true,
            "insertonly": true
        },
        "taskdiscipline": {
            "dbs": "ctr_v_allsrtinstallatie.ins_discipline_key",
            "label":  L("ctr_discipline"),
            "typ": "key",
            "foreign": { "tbl": "ctr_v_aanwezigdiscipline",
                         "key": "ins_discipline_key",
                         "desc": "ins_discipline_omschrijving",
                         "where": "ins_discipline_verwijder IS NULL"
                       },
            "showtransit": true,
            "insertonly": true,
            "emptyoption": L("mgt_kenmerk_controle_all")
        },
        "task": {
            "dbs": "ctr_v_allsrtinstallatie.ins_srtcontrole_key",
            "label": L("ins_srtcontrole"),
            "typ": "key",
            "foreign": "ins_srtcontrole",
            "foreignfiltercode": "PI",
            "urlAdd": [{urlParam: "ctrdisc_key_arr", field: "taskdiscipline"}],
            "showtransit": true,
            "insertonly": true,
            "emptyoption": " "
        },
        "objectkey": {
            "dbs": "ins_srtinstallatie_key",
            "label": L("lcl_ins_object"),
            "typ": "key",
            "hidden_fld": true,
            "required": true
        },
        "editlevel": {
            "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",
            "len": 3,
            "required": true
        },
        "required": {
            "dbs": "ins_kenmerk_verplicht",
            "label": L("ins_kenmerk_verplicht"),
            "typ": "key",
            "LOV": buildVerplichtingList(),
            "emptyisrealvalue": true,
            "emptyoption": null,
            "listfunction": function(oRs) {
                var val = oRs.Fields("required.name").Value;
                if (val === null) {
                    return L("lcl_mld_niet_verplicht");
                }
                return val;
            },
            "multiedit": true
        },
        "requiredstatus": {
            "dbs": "ins_kenmerk_verplicht_status",
            "label": L("ins_kenmerk_verplicht_status"),
            "typ": "key",
            "LOV": L("ins_kenmerk_requiredstatusLOV"),
            "emptyoption": null
        },
        "rolecode": {
            "dbs": "ins_kenmerk_rolcode",
            "label": L("mgt_kenmerk_rolcode"),
            "typ": "number",
            "LOV": buildKenmerkRoleCodeLOV({ module: (isTaakKenmerk ? "CTR" : "INS") }),
            "required": true
        },
        "group": {
            "dbs": "ins_kenmerk_groep",
            "label": L("mgt_kenmerk_groep"),
            "typ": "key",
            "required": true,
            "LOV": buildGroepsverplichtingList(),
            "defaultvalue": "0",
            "multiedit": true
        },
        "readonly": {
            "dbs": "ins_kenmerk_toonbaar",
            "label": L("mgt_kenmerk_toonbaar"),
            "typ": "check",
            "multiedit": true
        },
        "inlist": {
            "dbs": "ins_kenmerk_inlijst",
            "label": L("mgt_kenmerk_systeem_overview"),
            "typ": "number",
            "bits": [
                {
                    "name": "",
                    "label": "",
                    "showasgroup": false,
                    "typ": "check",
                    "mask": 3,
                    "radios": [
                        {
                            "name": "listobj",
                            //"label": L("mgt_kenmerk_systeem_overview_obj") + " " + L("mgt_kenmerk_systeem_overview"),
                            "label": L("mgt_kenmerk_systeem_overview"),
                            "mask": 1,
                            "defaultvalue": 0
                        },
                        {
                            "name": "listmjob",
                            //"label": L("mgt_kenmerk_systeem_overview_mjob") + " " + L("mgt_kenmerk_systeem_overview"),
                            "label": L("mgt_kenmerk_systeem_overview_mjob"),
                            "mask": 2,
                            "defaultvalue": 0
                        }
                    ]
                }
            ],
            "multiedit": true
        },
        "measurement": {
            "dbs": "ins_kenmerk_meetwaarde",
            "label": L("ins_kenmerk_meetwaarde"),
            "typ": "check",
            "multiedit": true
        },
        "clear": {
            "dbs": "ins_kenmerk_wissen",
            "label": L("ins_kenmerk_wissen"),
            "typ": "key",
            "LOV": L("ins_kenmerk_wissenLOV"),
            "defaultvalue": "0",
            "emptyoption": null,
            "multiedit": true
        },
        "propertydefault": {
            "dbs": "ins_kenmerk_default",
            "label": L("mgt_kenmerk_default"),
            "typ": "memo",
            "translate": true
        },
        "showexpression": {
            "dbs": "ins_kenmerk_show_expr",
            "label": L("mgt_kenmerk_show_expr"),
            "typ": "memo"
        },
        "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"
        },
        "code": {
            "dbs": "ins_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_ins_srtkenmerk({internal: params.internal}), kenmerkdata.objectpropertytype.id);
        return typedata;
    }

    this.get_objectpropertytype_srttype = function (obj)
    {
        var srttype_label = L("mgt_kenmerk_default");
        var result = { lbl_default:  L("mgt_kenmerk_default")
                     , lbl_readonly: L("mgt_kenmerk_toonbaar")
                     };
        if (obj.objectpropertytype && obj.objectpropertytype.id)
        {
            var sql = "SELECT ins_srtkenmerk_kenmerktype"
                    + "  FROM ins_srtkenmerk"
                    + " WHERE ins_srtkenmerk_key = " + obj.objectpropertytype.id;
            var oRs = Oracle.Execute(sql);
            if (!oRs.eof)
                result.lbl_default  = (oRs("ins_srtkenmerk_kenmerktype").Value == "l" ? L("mgt_kenmerk_icon"): L("mgt_kenmerk_default"));
                result.lbl_readonly = (oRs("ins_srtkenmerk_kenmerktype").Value == "l" ? L("mgt_kenmerk_collapsed"): L("mgt_kenmerk_toonbaar"));

            oRs.Close();
        }
        return result;
    }

    // Server side code!
    //
    // Afhankelijk van niveau:

    var gparams = { GET: { } };
    if (isTaakKenmerk)
    {
      delete this.fields.objectdiscipline;
      delete this.fields.objectgroup;
      delete this.fields.objecttype;
      delete this.fields.editlevel;
      delete this.fields.measurement;

      gparams.GET = {tables: ["ctr_v_allsrtinstallatie"],
                     wheres: [ "ins_kenmerk.ins_kenmerk_niveau IN ('T', 'E', 'C')" // E=Taakcategorie (discipline), C=Periodiek taak.
                             , "(ins_kenmerk.ins_srtinstallatie_key = ctr_v_allsrtinstallatie.ctr_srtinstallatie_key OR ins_kenmerk.ins_srtinstallatie_key IS NULL)"
                             , "ins_kenmerk.ins_kenmerk_niveau = ctr_v_allsrtinstallatie.niveau"
                             , "ins_kenmerk.ins_kenmerk_verwijder IS NULL"
                             ]
                    };
      this.fields.inlist.bits = [
                                  {
                                    "name": "",
                                    "label": "",
                                    "typ": "check",
                                    "mask": 1,
                                    "radios": [
                                                {
                                                  "name": "listctr",
                                                  "label": L("mgt_kenmerk_systeem_overview_ctr"),
                                                  "mask": 1,
                                                  "defaultvalue": 0
                                                }
                                              ]
                                  }
                                ]
    }
    else
    {   //kenmerk_niveau: D | G | S
      delete this.fields.requiredstatus;
      delete this.fields.taskdiscipline;
      delete this.fields.task;

      gparams.GET = {tables: ["ins_v_allsrtinstallatie"],
                     wheres: [ "ins_kenmerk.ins_kenmerk_niveau IN ('D', 'G', 'S')",
                               "ins_kenmerk.ins_srtinstallatie_key = ins_v_allsrtinstallatie.ins_srtinstallatie_key",
                               "ins_kenmerk.ins_kenmerk_niveau = ins_v_allsrtinstallatie.niveau"
                             ]
                    };
    }
  
    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  "T;" + L("mgt_kenmerk_controle_all")
           + ";E;" + L("mgt_kenmerk_ctrdiscipline")
           + ";C;" + L("mgt_kenmerk_srtcontrole")
           + ";D;" + L("mgt_srtkenmerk_discipline")
           + ";G;" + L("mgt_srtkenmerk_srtgroep")
           + ";S;" + L("mgt_srtkenmerk_srtdeel")
           + ";O;" + "Objecten";
    }

    this.hook_pre_edit = function (obj, fld)
    {
      fld.propertydefault.label = this_model.get_objectpropertytype_srttype(obj).lbl_default;
      fld.readonly.label = this_model.get_objectpropertytype_srttype(obj).lbl_readonly;
      if (!obj.level.name)
      {
        if (obj.level.id == "O")
            obj.level.name = L("lcl_ins_objects");
        else if (obj.level.id == "T")
            obj.level.name = L("ins_srtcontrole_m");
        else
            obj.level.name = obj.level.id;
      }
%>
      <script>
        var srtinstallatieselector_available = 1; // toon alleen actuele disciplines/groepen en objecten.
      </script>
<%
    }

    this.hook_pre_search = function(fld)
    {
%>
      <script>
        var srtinstallatieselector_available = 1; // toon alleen actuele disciplines/groepen en objecten.
      </script>
<%
    }
}
%>