<% /*
    $Revision$
    $Id$

    File:           model_bes_bestelling_item.inc

    Description:    model voor bes_bestelling_item

    Context:

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

function model_bes_bestelling_item()
{
    this.records_name = "purchaseorderitems";
    this.record_name = "purchaseorderitem";
    this.table = "bes_bestelling_item";
    this.primary = "bes_bestelling_item_key";
    this.autfunction = null;
    this.record_title = "Bestelitem";   //L("lcl_bes_bestelling_item");
    this.records_title = "Bestellijst"; //L("lcl_bes_bestelling_items");

    this.fields = {
        "id": {
            "dbs": "bes_bestelling_item_key",
            "seq": "bes_s_bes_bestelling_item_key",
            "typ": "key",
            "label": L("lcl_key"),
            "required": true
        },
        "purchase": {
            "dbs": "bes_bestelling_key",
            "typ": "key",
            "foreign": {
                "tbl": "bes_bestelling",
                "key": "bes_bestelling_key",
                "desc": "bes_bestelling_ordernr"
            },
            "label": L("lcl_mld_inf_Ordernr"),
            "required": true
        },
        "article": {
            "dbs": "bes_srtdeel_key",
            "typ": "key",
            "foreign": "ins_srtdeel",
            "label": L("lcl_bes_srtdeel"),
            "required": true
        },
        "amount": {
            "dbs": "bes_bestelling_item_aantal",
            "typ": "float",
            "label": L("lcl_bes_amount"),
            "required": true
        },
        "itemgrossprice": {
            "dbs": "bes_bestelling_item_brutoprijs",
            "typ": "currency",
            "label": L("lcl_bes_brutoprijs")
        },
        "itemprice": {
            "dbs": "bes_bestelling_item_prijs" ,
            "typ": "currency",
            "label": L("lcl_bes_price_per_unity")
        },
        "itempurchaseprice": {
            "dbs": "bes_bestelling_item_inkprijs",
            "typ": "currency",
            "label": L("lcl_bes_srtdeel_prijs_inkprijs")
        },
        "purchaseorderitem": {
            "dbs": "bes_bestelopdr_item_key",
            "typ": "key",
            "foreign": {
                "tbl": "bes_bestelopdr_item",
                "key": "bes_bestelopdr_item_key",
                "desc": "bes_bestelopdr_item_omschrijv"
            },
            "label": L("lcl_bes_opdracht_item")
        },
        "received": {
            "dbs": "bes_bestelling_item_aantalontv",
            "typ": "number",
            "label": L("lcl_bes_ontvangen")
        },
        "status": {
            "dbs": "bes_bestelling_item_status",
            "typ": "number",
            "label": L("lcl_bes_RFOs_status")
        }
    };


    function _check_authorization(params, method)
    {
        var sql = "SELECT bes_bestelling_key"
                + "     , bes_srtdeel_key"
                + "  FROM bes_bestelling_item"
                + " WHERE bes_bestelling_item_key = " + params.filter.id;
        var oRs = Oracle.Execute(sql);
        var bes_key = oRs("bes_bestelling_key").Value;
        var item_key = oRs("bes_srtdeel_key").Value;

        switch (method)
        {
            case "GET": // Als de bestelling mag worden ingezien, dan vast ook wel de bestelregels.
                        var bes_bestelling = bes.bestelling_info(bes_key);
                        var this_bes = bes.func_enabled_bestelling(bes_bestelling);
                        user.auth_required_or_abort(this_bes.canReadAny);
                        break;
            //case "PUT": var this_bes = bes.func_enabled_bestelitem(bes_key, item_key);
            //            user.auth_required_or_abort(this_bes.canChangeLine);
            //            break;
        }
        params.func_enabled = this_bes || {};
    };

    this.REST_GET    = function _GET(params, jsondata)
        {
            _check_authorization(params, "GET");

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

            var sql = "SELECT " + query.selects.join(", ")
                    + "  FROM " + query.tables.join(", ");
            if (query.wheres.length)
                sql += " WHERE " + query.wheres.join(" AND " );

            var json = api2.sql2json (params, sql, this );

            return json;
        };
    //this.REST_POST   = generic_REST_POST(this);
    //this.REST_PUT    = generic_REST_PUT(this);
    //this.REST_DELETE = generic_REST_DELETE(this);
}
%>