<% /*
    $Revision$
    $Id$

    File:           model_reservationconsumables.inc

    Description:    reserveren delen model.
    Parameters:
    Context:

    Notes:
*/

%>
<!-- #include file="../Shared/discxalg3d.inc"-->
<%
function status_bo(json)
{
    var status_bo_key = json;
    var status_bo = res.getbostatustext(status_bo_key) || "";

    return status_bo;
}

model_reservationconsumables =
{
    table: "res_rsv_artikel",
    primary: "res_rsv_artikel_key",
    records_name: "reservationconsumables",
    record_name: "reservationconsumable",

    fields: { "id"         : { dbs: "res_rsv_artikel_key",      typ: "key",     filter: "exact" },
              "reservation": { dbs: "res_rsv_ruimte_key",       typ: "key",     filter: "exact", foreign: "res_rsv_ruimte" },
              "consumable" : { dbs: "res_artikel_key",          typ: "key",     filter: "exact", foreign: "res_artikel" },
              "amount"     : { dbs: "res_rsv_artikel_aantal",      typ: "number"},
              "status"     : { dbs: "res_status_bo_key",        typ: "number",  filter: "exact", foreign: status_bo },
              "from"       : { dbs: "res_rsv_artikel_levering", typ: "time",    filter: "exact" },
              //: "to" {  dbs: "res_rsv_artikel_tot",         typ: "date"     },
              "price"      : { dbs: "res_rsv_artikel_prijs",       typ: "float",   "iscurrency": true },
              "processed"  : { dbs: "res_rsv_artikel_verwerkt",    typ: "datetime" },
              "closed"     : { dbs: "res_rsv_artikel_afgemeld",    typ: "datetime" },
              "changed"    : { dbs: "res_rsv_artikel_mutatie",     typ: "datetime" },
              "dirtlevel"  : { dbs: "res_rsv_artikel_dirtlevel",   typ: "number",  filter: "exact" }
             },
    list: { columns: [ "id", "from", "amount", "consumable", "price" ] },

    REST_GET: function _GET(params)
        {
            var urole = "fe"; // TODO: Moet echt niet ter zake doen
            var autfunction = urole == "fe"? "WEB_RESUSE" : "WEB_RESMAN";
            params.authparams = user.checkAutorisation(autfunction, null, null, true); // pessimistisch

            // TODO: Add authorization
            var query = api2.sqlfields(params, model_reservationconsumables);
            query.wheres.push("res_rsv_artikel_verwijder IS NULL");

            query.tables.push("res_rsv_ruimte");
            query.wheres.push("res_rsv_ruimte.res_rsv_ruimte_key = res_rsv_artikel.res_rsv_ruimte_key");
            query.wheres.push(user_key + " IN (res_rsv_ruimte_host_key, res_rsv_ruimte_contact_key)"); // Altijd fe vooralnog
            query.wheres.push("res_rsv_ruimte_van BETWEEN SYSDATE - " + S("facilitiespast_res") + " AND SYSDATE + " + S("facilitiesfuture_res"));

            var wheres = api2.sqlfilter(params, model_reservationconsumables);
            query.wheres = query.wheres.concat(wheres);

            var sql = "SELECT " + query.selects.join(", ")
                    + "  FROM " + query.tables.join(", ")
                    + " WHERE " + query.wheres.join(" AND " )
                    + " ORDER BY res_rsv_artikel_key";
            var json = api2.sql2json (params, sql, model_reservationconsumables);
            return json;
        },
    PUT: function (params) /* update reservable consumables */
    {
    },
    POST: function (params) /* new reservable object */
    {
    },
    DELETE: function (params) /* delete reservable object */
    {
    }
}
%>
