<% /*
    $Revision$
    $Id$

    File:           model_res_deel.inc

    Description:    model voor res_deel

    Context:        Om reserveerbare objecten te *beheren*
                    Het zoeken naar een vrij reserveerbaar object
                    doe je via model_reservableequipment

    Notes:
*/
%>
<%

function model_res_deel(ins_min_level_arr, insMultiReserve)
{
    this.ins_min_level_arr = ins_min_level_arr || [1, 5];
    var lcl_header = { s: L("res_deel"), m: L("res_deel_m") };
    if (this.ins_min_level_arr.length == 1)
    {
        switch (this.ins_min_level_arr[0])
        {
            case 1: lcl_header = { s: L("res_deel"), m: L("res_deel_m") }; break;
            case 2: break; // Reserveerbaar artikel: wordt gedaan in model_res_artikel.
            case 3: break; // Reserverbare ruimte (vergaderzaal): wordt gedaan in model_res_ruimte.
            case 4: lcl_header = { s: L("res_deel_prs"), m: L("res_deel_prs_m") }; break;
            case 5: lcl_header = { s: L("res_deel_wpl"), m: L("res_deel_wpl_m") }; break;
        }
    }
    var insres = insMultiReserve || 0;

    this.records_name = "bookingitems";
    this.record_name = "bookingitem";
    this.table = "res_deel";
    this.primary = "res_deel_key";
    this.soft_delete = "res_deel_verwijder";
    this.soft_expire = "res_deel_vervaldatum";
    this.autfunction = "WEB_RESMSU";
    this.record_title = lcl_header.s;
    this.records_title = lcl_header.m;


    this.fields = {
        "id": {
            "dbs": "res_deel_key",
            "label": L("lcl_key"),
            "typ": "key",
            "required": true,
            "seq": "res_s_res_deel_key"
        },
        "res_ins_deel_key": {
            "dbs": "res_ins_deel_key",
            "label": L("res_ins_deel_key"),
            "typ": "key",
            "nosearch": true, // kan een extreem grote listbox geven anders
            "foreign": "INS_DEEL",
            infoPointer: { Url: "appl/ins/ins_deel.asp?urole=bo&ins_key=" }
        },
        "res_prs_perslid_key": {
            "dbs": "res_prs_perslid_key",
            "label": L("res_prs_perslid_key"),
            "typ": "key",
            "insertonly": true,
            "nosearch": true, // kan een extreem grote listbox geven anders
            "foreign": "prs_perslid",
            infoPointer: { Url: "appl/prs/prs_perslid.asp?prs_key=" }
        },
        "name": {
            "dbs": "res_deel_omschrijving",
            "label": L("res_deel_omschrijving"),
            "typ": "varchar",
            "translate": true,
            "required": true,
            "filter": "like",
            "multiedit": true
        },
        "ruimtekey": {
            "dbs": "res_v_ins_deelgegevens.alg_ruimte_key",
            "label": L("lcl_location"),
            "typ": "key",
            "hidden_fld": true,
            "foreign": {
                "tbl": get_tbl(),
                "key": "alg_ruimte_key",
                "desc": "res_v_ins_deelgegevens2.alg_plaatsaanduiding",
                "alias": "res_v_ins_deelgegevens2",
                "where": "res_deel.res_deel_key = res_v_ins_deelgegevens2.tbl_res_deel_key(+)",
                "selectorforeign": "ALG_RUIMTE",
                "selectorparams": {"startlevel": 2, "eindlevel": 5, "readonlylevel": 1}
            },
            "foreignfiltercode": null,  // Ook facilitor irrelevant
            "showtransit": true
        },
        "bookingdiscipline": {
            "dbs": "res_discipline_key",
            "label": L("res_discipline_key"),
            "typ": "key",
            "required": true,
            "foreign": {
                "tbl": "(SELECT ins_discipline_key"
                     + "      , ins_discipline_omschrijving"
                     + "   FROM ins_tab_discipline"
                     + "  WHERE ins_discipline_module = 'RES'"
                     + "    AND ins_discipline_min_level IN (" + this.ins_min_level_arr.join(",") + ")"
                     + "    AND ins_discipline_verwijder IS NULL"
                     + "  ORDER BY ins_discipline_omschrijving ASC)",
                "key": "ins_discipline_key",
                "desc": "ins_discipline_omschrijving"
            },
            "LOVinit": "",
            "multiedit": true
        },
        "unit": {
            "dbs": "res_deel_eenheid",
            "label": L("res_deel_eenheid"),
            "typ": "varchar",
            "translate": true,
            "multiedit": true
        },
        "price": {
            "dbs": "res_deel_prijs",
            "label": L("res_deel_prijs"),
            "iscurrency": true,
            "typ": "float",
            "multiedit": true
        },
        "fixedprice": {
            "dbs": "res_deel_prijs_vast",
            "label": L("res_deel_prijs_vast"),
            "typ": "check",
            "multiedit": true
        },
        "bulkitem": {
            "dbs": "res_deel_bulkreserveerbaar",
            "label": L("res_deel_bulkreserveerbaar"),
            "typ": "check0",
            "multiedit": true
        },
        "scope": {
            "dbs": "res_deel_alg_level",
            "label": L("res_deel_alg_level"),
            "typ": "number",
            "LOV": L("res_deel_alg_levelLOV"),
            "emptyoption": null,
            "multiedit": true
        },
        "availabilitybits": {
            "dbs": "res_deel_beschikbaar_bits",
            "label": L("res_deel_beschikbaar_bits"),
            "typ": "number",
            "hidden_fld": true,
            "required": true
        },
        "_available": {
            "dbs": "",
            "label": L("res_deel_beschikbaarheid"),
            "typ": "button",
            "defaultvalue": L("lcl_select")
        },
        "remark": {
            "dbs": "res_deel_opmerking",
            "label": L("res_deel_opmerking"),
            "typ": "memo",
            "translate": true
        },
        "image": {
            "dbs": "res_deel_image",
            "label": L("res_deel_image"),
            "typ": "file",
            "module": "RESPHD"
        },
        "expirationdate": {
            "dbs": "res_deel_vervaldatum",
            "label": L("res_deel_vervaldatum"),
            "typ": "date",
            "multiedit": true
        },
        "externnr": {
            "dbs": "res_deel_externnr",
            "typ": "varchar",
            "label": L("extern_nr")
        },
        "externsyncdate": {
            "dbs": "res_deel_externsyncdate",
            "typ": "datetime",
            "label": L("extern_syncdate"),
            "readonly": !(user.has("WEB_FACTAB") || user.has("WEB_FACXNR"))
        }
    };


    this.hook_pre_multi_edit = function(xxx_data, fld, scf_params)
    {
        if (scf_params.insMultiReserve)
        {   // Multi-edit vanuit ins_list willen we alleen discipline en scope van de res_delen aanpassen
            // die bij het ins_deel hoort.
            this.fields.unit.multiedit = false;
            this.fields.price.multiedit = false;
            this.fields.fixedprice.multiedit = false;
            this.fields.bulkitem.multiedit = false;
            this.fields.expirationdate.multiedit = false;
        }
%>
        <script>
        var s_vis_parking_key = <%=S("vis_parking_key")%>;
        var insMultiReserve = <%=scf_params.insMultiReserve ? 1 : 0%>;
        </script>
<%
    };

    function get_tbl(params)
    {
        var params = params || {};
        var sql = "(SELECT DISTINCT"
                + "        a.alg_ruimte_key"
                + "      , a.alg_onroerendgoed_keys"
                + "      , a.alg_onroerendgoed_type"
                + "      , a.alg_plaatsaanduiding"
                + "      , a.alg_locatie_key"
                + "      , a.alg_terreinsector_key"
                + "      , a.alg_gebouw_key"
                + "      , a.alg_verdieping_key"
                + "      , d.ins_deel_key"
                + "      , r.res_deel_key tbl_res_deel_key"
                + "   FROM res_deel r"
                + "      , ins_deel d"
                + "      , ins_v_alg_overzicht a"
                + "  WHERE d.ins_deel_key = r.res_ins_deel_key"
                + "    AND d.ins_alg_ruimte_key  = a.alg_onroerendgoed_keys"
                + "    AND d.ins_alg_ruimte_type = a.alg_onroerendgoed_type"
                + ")";
        if (params.alias)
            sql += " " + params.alias;
        return sql;
    }

    if (inArray(1, this.ins_min_level_arr) || inArray(5, this.ins_min_level_arr)) // gekoppeld aan ins_deel
    {
        var gparams = { GET: { tables: ["res_discipline"
                                       ,get_tbl({alias: "res_v_ins_deelgegevens"})
                                       ]
                             , wheres: ["res_deel.res_discipline_key = res_discipline.ins_discipline_key"
                                       ,"res_deel.res_deel_key = res_v_ins_deelgegevens.tbl_res_deel_key(+)"
                                       ,"res_discipline.ins_discipline_min_level in ("+this.ins_min_level_arr.join(",")+")"
                                       ]
                             }
                      };
        delete this.fields.res_prs_perslid_key;
    }
    else //gekoppeld aan prs_perslid: ins_discipline_min_level=4
    {
        var gparams = { GET: { tables: ["res_discipline"]
                             , wheres: ["res_deel.res_discipline_key = res_discipline.ins_discipline_key"
                                       ,"res_deel.res_prs_perslid_key IS NOT NULL"
                                       ,"res_discipline.ins_discipline_min_level = 4"
                                       ]
                             }
                      };
        delete this.fields.res_ins_deel_key;
        delete this.fields.ruimtekey;
    }

    this.REST_GET    = generic_REST_GET(this, gparams);
    this.REST_POST   = generic_REST_POST(this);
    this.REST_PUT    = function (params, jsondata, the_key)
        {
            if (insres)
            {
                // Voor elke multiselect gekozen ins_deel wordt deze REST_PUT aangeroepen.
                // Ins_deel kan op meerdere res_delen van toepassing zijn ivm vervaldatum.
                var sql = "SELECT res_deel_key"
                        + "  FROM res_deel"
                        + " WHERE res_deel_verwijder IS NULL"
                        + "   AND res_ins_deel_key = " + the_key;
                var oRs = Oracle.Execute(sql);
                if (!oRs.eof)
                {   // Dit ins_deel heeft al een res_deel, dus update.
                    while (!oRs.eof)
                    {
                        var res_key = oRs("res_deel_key").Value;
                        generic_REST_PUT(this)(params, jsondata, res_key);
                        oRs.MoveNext();
                    }
                }
                else
                {   // Toevoegen van dit ins_deel als res_deel.
                    var sql_i = "INSERT INTO res_deel"
                              + " (res_deel_omschrijving, res_ins_deel_key, res_discipline_key, res_deel_alg_level)"
                              + " SELECT ins_deel_omschrijving"
                              + "      , " + the_key
                              + "      , " + jsondata.bookingdiscipline
                              + "      , " + jsondata.scope
                              + "  FROM ins_deel WHERE ins_deel_key = " + the_key;
                    Oracle.Execute(sql_i);
                }
                oRs.Close();
            }
            else
            {
                generic_REST_PUT(this)(params, jsondata, the_key);
            }
            return { key: the_key };
        }
    this.REST_DELETE = generic_REST_DELETE(this);
}
%>