<% /*
    $Revision$
    $Id$

    File:           model_mld_kenmerk.inc

    Description:    Model voor mld_kenmerk

    Context:

    Notes:          Melding- en opdracht kenmerk definities worden aangemaakt via dit model.
*/
%>
<!-- #include file="model_mld_srtkenmerk.inc"-->
<%

function model_mld_kenmerk(niveau, params)
{
    var isOpdrachtKenmerk = (niveau == "P" || niveau == "O");
    var is_clone = getQParamInt("scf_clone", 0) == 1;

    params = params || {};
    this.records_name = "issueproperties";
    this.record_name = "issueproperty";
    this.table = "mld_kenmerk";
    this.primary = "mld_kenmerk_key";
    this.soft_delete = "mld_kenmerk_verwijder";
    this.autfunction = params.internal?false:"WEB_MLDMGT";
    this.record_title  = (isOpdrachtKenmerk? L("mld_kenmerk")           : L("lcl_mld_kenmerk"));
    this.records_title = (isOpdrachtKenmerk? L("lcl_mld_ord_kenmerken") : L("lcl_mld_kenmerken"));
    this.module = "MLD";

    var fields_main = {
        "id": {
            "dbs": "mld_kenmerk_key",
            "label": L("lcl_key"),
            "typ": "key",
            "required": true,
            "seq": "mld_s_mld_kenmerk_key",
            "readonly": true
        },
        "issuepropertytype": {
            "dbs": "mld_srtkenmerk_key",
            "label": L("mgt_srtkenmerk_key"),
            "typ": "key",
            "required": true,
            "insertonly": !is_clone,
            "foreign": {
                "tbl": "(SELECT mld_srtkenmerk_key"
                     + "      , mld_srtkenmerk_omschrijving"
                     + (mode != "list"
                       ? " || ' (' || mld_srtkenmerk_kenmerktype || ')'"
                       : "")
                     + " oms"
                     + "      , mld_srtkenmerk_kenmerktype"
                     + "   FROM mld_srtkenmerk"
                     + "  WHERE mld_srtkenmerk_verwijder IS NULL"
                     + "  ORDER BY mld_srtkenmerk_upper ASC)",
                "key": "mld_srtkenmerk_key",
                "desc": "oms",
                "extravalue": "mld_srtkenmerk_kenmerktype"
            },
            "defaultvalue": null,
            "LOVinit": "",
            "friendlyname": true,
            "showtransit": true
        },
        "objectpropertydesc": {
            "dbs": "mld_kenmerk_omschrijving",
            "label": L("mgt_kenmerk_omschrijving"),
            "typ": "varchar",
            "translate": true,
            "filter": "like"
        },
        "level": {
            "dbs": "mld_kenmerk_niveau",
            "label": L("mgt_kenmerk_niveau"),
            "typ": "varchar",
            "LOV": buildNiveauList(),
            "defaultvalue": (isOpdrachtKenmerk? "O" : ""),
            "showtransit": true,
            "insertonly": !is_clone
        },
        "issuetype": {
            "dbs": "mld_stdmelding_key",
            "label": L("mld_stdmelding_key"),
            "typ": "key",
            "hidden_fld": true
        },
        "sequence": {
            "dbs": "mld_kenmerk_volgnummer",
            "label": L("mgt_kenmerk_volgnummer"),
            "typ": "number",
            "len": 3,
            "required": true
        },
        "rolecode": {
            "dbs": "mld_kenmerk_rolcode",
            "label": L("mgt_kenmerk_rolcode"),
            "typ": "number",
            "LOV": buildKenmerkRoleCodeLOV({ module: (isOpdrachtKenmerk ? "OPD" : "MLD") }),
            "required": true
        },
        "group": {
            "dbs": "mld_kenmerk_groep",
            "label": L("mgt_kenmerk_groep"),
            "typ": "key",
            "required": true,
            "LOV": buildGroepsverplichtingList(),
            "defaultvalue": "0"
        },
        "required": {
            "dbs": "mld_kenmerk_verplicht",
            "label": L("mgt_kenmerk_verplicht"),
            "typ": "key",
            "LOV": buildVerplichtingList(),
            "emptyisrealvalue": true,
            "emptyoption": null
        },
        "requiredstatus": {
            "dbs": "mld_kenmerk_verplicht_status",
            "label": L("mld_kenmerk_verplicht_status"),
            "typ": "key",
            "LOV": buildStatusList(niveau),
            "emptyoption": null
        },
        "readonly": {
            "dbs": "mld_kenmerk_toonbaar",
            "label": (isOpdrachtKenmerk ? L("mgt_kenmerk_toonbaar") : L("lcl_mld_kenmerk_readonly") ),
            "typ": "check"
        },
        "code": {
            "dbs": "mld_kenmerk_code",
            "label": L("mgt_kenmerk_code"),
            "typ": "varchar",
            "readonly": !user.has("WEB_PRSSYS")
        },
        "clear": {
            "dbs": "mld_kenmerk_wissen",
            "label": L("mld_kenmerk_wissen_label"),
            "typ": "key",
            "bits": [
                {
                    "name": "",
                    "label": "",
                    "showasgroup": false,
                    "typ": "check",
                    "mask": 0,      // Afhankelijk van mld/opdr en setting: wordt straks gezet.
                    "radios": []    // Afhankelijk van mld/opdr en setting: wordt straks toegevoegd.
                }
            ],
            "defaultvalue": "0",
            "emptyoption": null,
            "multiedit": true
        },
        "default": {
            "dbs": "mld_kenmerk_default",
            "label": L("mgt_kenmerk_default"),
            "typ": "memo",
            "translate": true
        },
        "showexpression": {
            "dbs": "mld_kenmerk_show_expr",
            "label": L("mgt_kenmerk_show_expr"),
            "typ": "memo"
        },
        "hint": {
            "dbs": "mld_kenmerk_hint",
            "label": L("mgt_kenmerk_hint"),
            "typ": "memo",
            "translate": true
        },
        "regexp": {
            "dbs": "mld_kenmerk_regexp",
            "label": L("mgt_kenmerk_regexp"),
            "typ": "varchar"
        },
        "authorization": {
            "dbs": "fac_functie_key",
            "label": L("mgt_kenmerk_autorisatie"),
            "typ": "key",
            "foreign": "fac_functie"
        },
        "treatmentteam": {
            "dbs": "ins_discipline_key",
            "label": L("mld_processing_group_kenm"),
            "typ": "key",
            //"foreign": "mld_discipline"
            "foreign": {
                "tbl": "(SELECT d.ins_discipline_key"
                     + "      , d.ins_discipline_omschrijving"
                     + "   FROM mld_v_aanwezigdiscipline d"
                     + "      , ins_srtdiscipline sd"
                     + "      , mld_disc_params dp"
                     + "  WHERE d.ins_srtdiscipline_key = sd.ins_srtdiscipline_key"
                     + "    AND dp.mld_ins_discipline_key = d.ins_discipline_key"
                     + "    AND BITAND(dp.mld_disc_params_srtgroep, 2) = 2)",
                "key": "ins_discipline_key",
                "desc": "ins_discipline_omschrijving"
            }
        }
    };

    if (isOpdrachtKenmerk)
    {
        fields_main.clear.bits[0].mask = 1;
        fields_main.clear.bits[0].radios.push(
                {"name": "copy_on",
                 "label": L("mld_kenmerk_wissen_label") + " " + L("opd_kenmerk_wissen_copy_on"),
                 "mask": 1,
                 "defaultvalue": 1
                });
    }
    else
    {
        fields_main.clear.bits[0].mask = 3;
        fields_main.clear.bits[0].radios.push(
                {"name": "copy_on",
                 "label": L("mld_kenmerk_wissen_label") + " " + L("mld_kenmerk_wissen_copy_on"),
                 "mask": 1,
                 "defaultvalue": 1
                });
        fields_main.clear.bits[0].radios.push(
                {"name": "fwd_on",
                 "label": L("mld_kenmerk_wissen_label") + " " + L("mld_kenmerk_wissen_fwd_on"),
                 "mask": 2,
                 "defaultvalue": 1
                });
        if (S("mld_ord_flexsummary") == 2)
        {
            fields_main.clear.bits[0].mask = 7;  // meenemen kenmerk naar opdracht
            fields_main.clear.bits[0].radios.push(
                {"name": "opdr_on",
                 "label": L("mld_kenmerk_wissen_opdr_on"),
                 "mask": 4,
                 "defaultvalue": 0
                });
        }
    }

    var fields_mld = {
        "issuepropertytypename": {
            "dbs": "view_mld_kenmerk.mld_srtkenmerk_omschrijving",
            "label": L("mgt_srtkenmerk_key"),
            "typ": "varchar",
            "filter": "like"
        },
        "filled": {
            "dbs": "view_mld_kenmerk.aantal",
            "label": L("lcl_mld_kenmerk_count"),
            "typ": "number"
        },
        "attributetype": {
            "dbs": "view_mld_kenmerk.mld_srtkenmerk_kenmerktype", //"mld_srtkenmerk.mld_srtkenmerk_kenmerktype",
            "label": L("mgt_srtkenmerk_kenmerktype"),
            "typ": "varchar",
            "required": true,
            "insertonly": !is_clone,
            "LOV": buildKenmerktypeLOV(null, { module: "MLD" })
        },
        "disciplinetype": {
            "dbs": "view_mld_kenmerk.ins_srtdiscipline_key", //"mld_discipline.ins_srtdiscipline_key",
            "label": L("lcl_mld_vakgroeptype"),
            "typ": "key",
            "foreign": {
                "tbl": "ins_srtdiscipline",
                "key": "ins_srtdiscipline_key",
                "desc": "ins_srtdiscipline_omschrijving",
                "where": "ins_srtdiscipline_verwijder IS NULL"
            },
            "showtransit": true,
            "insertonly": !is_clone,
            "emptyoption": L("mld_srtdiscipline_m_all")
        },
        "discipline": {
            "dbs": "view_mld_kenmerk.discipline", //"mld_discipline.ins_discipline_key",
            "label": L("lcl_mld_vakgroep"),
            "typ": "key",
            "foreign": "mld_discipline",
            "module": "MLDP",
            "urlAdd": [{urlParam: "srtdisc_key", field: "disciplinetype"}],
            "showtransit": true,
            "insertonly": !is_clone,
            "emptyoption": " "
        },
        "mld_issuetype": {
            "dbs": "view_mld_kenmerk.mld_mld_stdmelding_key",
            "label": L("mld_stdmelding_key"),
            "typ": "key",
            "foreign": "mld_stdmelding",
            "urlAdd": [{urlParam: "srtdisc_key", field: "disciplinetype"},
                       {urlParam: "disc_key", field: "discipline"}],
            "showtransit": true,
            "insertonly": !is_clone,
            "search": false
        },
        "inlist": {
            "dbs": "mld_kenmerk_inlijst",
            "label": L("lcl_mld_kenmerk_visibility"),
            "typ": "key",
            "bits": [
                {
                    "name": "",
                    "label": "",
                    "typ": "check",
                    "mask": 3,
                    "radios": [
                        {
                            "name": "in_list",
                            "label": L("mgt_kenmerk_systeem_overview"),
                            "mask": 1,
                            "defaultvalue": 0
                        },
                        {
                            "name": "in_bord",
                            "label": L("lcl_mld_kenmerk_in_bord"),
                            "mask": 2,
                            "defaultvalue": 1
                        }
                    ]
                }
            ],
            "multiedit": true
        }
    };

    var fields_opd = {
        "ordertype": {
            "dbs": "mld_typeopdr_key",
            "label": L("mld_typeopdr"),
            "typ": "key",
            "foreign": {
                "tbl": "(select mld_typeopdr_key"
                     + "      , mld_typeopdr_omschrijving"
                     + "   from mld_typeopdr"
                     + "  order by mld_typeopdr_upper asc)",
                "key": "mld_typeopdr_key",
                "desc": "mld_typeopdr_omschrijving"
            },
            "defaultvalue": null,
            "showtransit": true,
            "insertonly": !is_clone,
            "emptyoption": L("mld_typeopdr_all")
        },
        "obligationfill": {
            "dbs": "mld_kenmerk_obligation_fill",
            "label": L("mgt_kenmerk_obligation_fill"),
            "typ": "check0"
        },
        "hltproperty": {
            "dbs": "mld_kenmerk_onderbreken",
            "label": L("mld_kenmerk_onderbreken"),
            "typ": "key",
            "LOV": buildHltPropList(),
            "emptyoption": null
        },
        "inlist": {
            "dbs": "mld_kenmerk_inlijst",
            "label": L("mgt_kenmerk_systeem_overview"),
            "typ": "check0",
            "multiedit": true
        }
    };

    this.fields = object_merge({}, (isOpdrachtKenmerk ? fields_opd : fields_mld), fields_main);

    function object_merge()
    {
        for (var i = 1; i < arguments.length; i++)
            for (var a in arguments[i])
                arguments[0][a] = arguments[i][a];
        return arguments[0];
    }

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

    function buildNiveauList()
    {
        return "P;" + L("lcl_mld_opdr_all")
            + ";O;" + L("lcl_mld_typeopdr")
            + ";A;" + L("mld_srtdiscipline_m_all")
            + ";S;" + L("lcl_complain")
            + ";D;" + L("lcl_mld_vakgroep")
            + ";T;" + L("lcl_mld_vakgroeptype")
            + ";M;" + "meldingen";
    }


    this.hook_pre_search = function(fld)
    {
    %>
        <script>
            var module = "MLD";
            var mode = "<%=mode%>";
            var niveau = "<%=niveau%>";
        </script>
    <%
    }

    function get_issuepropertytype_srttype(obj)
    {
        var issuepropertytype = getQParamInt("issuepropertytype", ( (obj.issuepropertytype && obj.issuepropertytype.id) ? obj.issuepropertytype.id : -1));
        var mld_opdr_readonly = (isOpdrachtKenmerk ? L("mgt_kenmerk_toonbaar") : L("lcl_mld_kenmerk_readonly"));
        var result = { lbl_default:  L("mgt_kenmerk_default")
                     , lbl_readonly: mld_opdr_readonly
                     };
        if (issuepropertytype > 0)
        {
            var sql = "SELECT mld_srtkenmerk_kenmerktype"
                    + "  FROM mld_srtkenmerk"
                    + " WHERE mld_srtkenmerk_key = " + issuepropertytype;
            var oRs = Oracle.Execute(sql);
            if (!oRs.eof) {
                result.ktype = oRs("mld_srtkenmerk_kenmerktype").Value;
                result.lbl_default  = (result.ktype == "l" || result.ktype == "b" ? L("mgt_kenmerk_icon") : L("mgt_kenmerk_default"));
                result.lbl_readonly = (result.ktype == "l" || result.ktype == "b" ? L("mgt_kenmerk_collapsed") : mld_opdr_readonly);
            }
            oRs.Close();
        }
        return  result;
    }


    this.hook_pre_edit = function (obj, fld)
    {
        delete this.fields.filled;
        delete this.fields.issuepropertytypename;

       if (obj.id != -1 && obj.level.id == "D")
            delete this.fields.mld_issuetype;

        // Het behandelteam alleen tonen bij behandelbare bloktitels in de edit-mode.
        if (obj.id != -1 && obj.attributetype && obj.attributetype.id != "b")
          delete this.fields.treatmentteam;

       delete this.fields.attributetype;

        if (!obj.level.name)
        {
            if (obj.level.id == "M")
                obj.level.name = L("lcl_complains");
            else if (obj.level.id == "P")
                obj.level.name = L("lcl_orders");
            else
                obj.level.name = obj.level.id;
        }

        var v_ordertype_key = ((niveau == "O" && obj.ordertype) ? obj.ordertype.id : -1);
        var v_isclone = getQParamInt("scf_clone", 0);
        var issuepropertytype = getQParamInt("issuepropertytype", ( (obj.issuepropertytype && obj.issuepropertytype.id) ? obj.issuepropertytype.id : -1));
        var srttype = get_issuepropertytype_srttype(obj);
        fld["default"].label = srttype.lbl_default;
        fld["readonly"].label = srttype.lbl_readonly;
        if (obj.id > 0 && !isOpdrachtKenmerk) { // Nieuwe kenmerken togglen zelf via clientside code
            if (srttype.ktype && inArray(srttype.ktype, ['R', 'r', 'S'])) {
                // Alles staat al goed
            } else {
                fld.inlist.bits[0].radios.splice(1, 1);
            }
        }

        if (obj.attributetype && (obj.attributetype.id == "b" || obj.attributetype.id == "l"))
            fld["default"].typ = "icon";
    %>
        <script>
            var module = "MLD";
            var mode = "<%=mode%>";
            var niveau = "<%=niveau%>";
            var ordertype_key = "<%=v_ordertype_key%>";
            var kenmerk_key = <%=kenmerk_key%>;
            var is_clone = <%=v_isclone%>;
            var attributetype = "<%=( (obj.attributetype && obj.attributetype.id) ? obj.attributetype.id : "")%>";
            var issuepropertytype = "<%=( (obj.issuepropertytype && obj.issuepropertytype.id) ? obj.issuepropertytype.id : -1)%>";
        </script>
    <%
    }

    this.hook_pre_multi_edit = function (obj, fld)
    {
        var v_ordertype_key = ((niveau == "O" && obj.ordertype) ? obj.ordertype.id : -1);
        var v_isclone = getQParamInt("scf_clone", 0);
    %>
        <script>
            var mode = "<%=mode%>";
            var niveau = "<%=niveau%>";
            var ordertype_key = "<%=v_ordertype_key%>";
            var kenmerk_key = <%=kenmerk_key%>;
            var is_clone = <%=v_isclone%>;
        </script>
    <%
    }

    this.hook_pre_show = function (obj, fld)
    {
        delete this.fields.filled;
        delete this.fields.issuepropertytypename;
        delete this.fields.attributetype;
        var srttype = get_issuepropertytype_srttype(obj);
        fld["default"].label = srttype.lbl_default;
        fld["readonly"].label = srttype.lbl_readonly;
        if (!isOpdrachtKenmerk) {
            if (srttype.ktype && inArray(srttype.ktype, ['R', 'r', 'S'])) {
                // Alles staat al goed
            } else {
                fld.inlist.bits[0].radios.splice(1, 1);
            }
        }
    }

    this.hook_pre_post = function (params, obj)
    {   // Verwijder eventuele waarde voor treatmentteam (ins_discipline_key) als het geen bloktitel met behandelaar is.
        var sql = "SELECT mld_srtkenmerk_kenmerktype"
                + "  FROM mld_srtkenmerk"
                + " WHERE mld_srtkenmerk_key = " + obj.issuepropertytype;
        var oRs = Oracle.Execute(sql);
        var kenmerktype = oRs("mld_srtkenmerk_kenmerktype").Value;
        oRs.Close();
        if (kenmerktype != "b") {
            obj.treatmentteam = null;
        }
    }

    var gparams = {"GET": {}};
    if (isOpdrachtKenmerk)
    {
        gparams.GET = { wheres: [ "mld_kenmerk.mld_kenmerk_niveau IN ('P', 'O')" ] };
    }
    else
    {
        var sql_kenmerk = "SELECT mkm.mld_kenmerk_key"
                        + "     , mkm.mld_kenmerk_omschrijving"
                        + "     , msk.mld_srtkenmerk_key"
                        + "     , msk.mld_srtkenmerk_kenmerktype"
                        + "     , msk.mld_srtkenmerk_omschrijving"
                        + "     , s.mld_kenmerk_niveau"
                        + "     , s.ref_mld_stdmelding_key"
                        + "     , s.mld_stdmelding_key  mld_mld_stdmelding_key"
                        + "     , s.mld_stdmelding_omschrijving"
                        + "     , s.ins_discipline_key discipline"
                        + "     , s.ins_srtdiscipline_key"
                        + "     , aantal"
                        + "     , aantal365"
                        + "  FROM mld_kenmerk mkm"
                        + "     , mld_srtkenmerk msk"
                        + "     , (SELECT 'S' mld_kenmerk_niveau"
                        + "             , mld_stdmelding_key ref_mld_stdmelding_key"
                        + "             , mld_stdmelding_key"
                        + "             , mld_stdmelding_omschrijving"
                        + "             , ins_discipline_key"
                        + "             , m2.ins_srtdiscipline_key"
                        + "          FROM mld_stdmelding md"
                        + "             , mld_discipline m2"
                        + "         WHERE md.mld_ins_discipline_key = m2.ins_discipline_key"
                        + "           AND mld_stdmelding_verwijder IS NULL"
                        + "           AND ins_discipline_verwijder IS NULL"
                        + "        UNION ALL"
                        + "        SELECT 'D' mld_kenmerk_niveau"
                        + "             , ins_discipline_key ref_mld_stdmelding_key"
                        + "             , NULL mld_stdmelding_key"
                        + "             , NULL mld_stdmelding_omschrijving"
                        + "             , ins_discipline_key ins_discipline_key"
                        + "             , ins_srtdiscipline_key"
                        + "          FROM mld_discipline m2"
                        + "         WHERE ins_discipline_verwijder IS NULL"
                        + "        UNION ALL"
                        + "        SELECT 'T' mld_kenmerk_niveau"
                        + "             , ins_srtdiscipline_key ref_mld_stdmelding_key"
                        + "             , NULL mld_stdmelding_key"
                        + "             , NULL mld_stdmelding_omschrijving"
                        + "             , NULL ins_discipline_key"
                        + "             , ins_srtdiscipline_key"
                        + "          FROM ins_srtdiscipline m2"
                        + "        UNION"
                        + "        SELECT 'A' mld_kenmerk_niveau"
                        + "             , NULL ref_mld_stdmelding_key"
                        + "             , NULL mld_stdmelding_key"
                        + "             , NULL mld_stdmelding_omschrijving"
                        + "             , NULL ins_discipline_key"
                        + "             , NULL ins_srtdiscipline_key"
                        + "          FROM DUAL"
                        + "       ) s"
                        + "     , (SELECT mld_kenmerk_key"
                        + "             , count(*) aantal"
                        + "          FROM mld_kenmerkmelding"
                        + "         GROUP BY mld_kenmerk_key"
                        + "       ) nn"
                        + "     , (SELECT mld_kenmerk_key"
                        + "             , count(*) aantal365"
                        + "          FROM mld_kenmerkmelding"
                        + "         WHERE mld_kenmerkmelding_aanmaak > SYSDATE - 365"
                        + "         GROUP BY mld_kenmerk_key"
                        + "       ) nn365"
                        + " WHERE (mkm.mld_stdmelding_key = ref_mld_stdmelding_key OR mkm.mld_stdmelding_key IS NULL)"
                        + "   AND mkm.mld_kenmerk_niveau = s.mld_kenmerk_niveau"
                        + "   AND mkm.mld_srtkenmerk_key = msk.mld_srtkenmerk_key"
                        + "   AND mkm.mld_kenmerk_key = nn.mld_kenmerk_key(+)"
                        + "   AND mkm.mld_kenmerk_key = nn365.mld_kenmerk_key(+)";

        gparams.GET = {
            tables: [ "(" + sql_kenmerk + ") view_mld_kenmerk" ],
            wheres: [ "mld_kenmerk.mld_kenmerk_key = view_mld_kenmerk.mld_kenmerk_key" ]
        };
    }
    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);
}
%>