<% /*
    $Revision$
    $Id$

    File:           model_bes_bestelling.inc

    Description:    model voor bes_bestelling

    Context:

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

function model_bes_bestelling()
{
    this.records_name = "purchases";
    this.record_name = "purchase";
    this.table = "bes_bestelling";
    this.primary = "bes_bestelling_key";
    this.autfunction = null;
    this.record_title = L("lcl_bes_bestelling");
    this.records_title = L("lcl_bes_bestellingen");

    this.fields = {
        "id": {
            "dbs": "bes_bestelling_key",
            "seq": "bes_s_bes_bestelling_key",
            "typ": "key",
            "label": L("lcl_key"),
            "required": true
        },
        "module": {
            "dbs": "bes_bestelling_module",
            "typ": "varchar",
            "label": L("lcl_fac_module")
        },
        "status": {
            "dbs": "bes_bestelling_status",
            "typ": "number",
            "label": L("lcl_bes_Bes_status")
        },
        "ordernr": {
            "dbs": "bes_bestelling_ordernr",
            "typ": "varchar",
            "label": L("lcl_mld_inf_Ordernr")
        },
        "requestor": {
            "dbs": "prs_perslid_key",
            "typ": "key",
            "foreign": "prs_perslid",
            "label": L("lcl_bes_aanvragergegevens"),
            "required": true
        },
        "orderfor": {
            "dbs": "prs_perslid_key_voor",
            "typ": "key",
            "foreign": "prs_perslid",
            "label": L("lcl_bes_order_for")
        },
        "parent": {
            "dbs": "bes_bestelling_parentkey",
            "typ": "key",
            "foreign": {
                "tbl": "bes_bestelling",
                "key": "bes_bestelling_key",
                "desc": "bes_bestelling_ordernr"
            },
            "label": L("lcl_bes_parent")
        },
        "return": {
            "dbs": "bes_bestelling_retourvan_key",
            "typ": "key",
            "foreign": {
                "tbl": "bes_bestelling",
                "key": "bes_bestelling_key",
                "desc": "bes_bestelling_ordernr"
            },
            "label": L("lcl_bes_retour_of")
        },
        "orderdate": {
            "dbs": "bes_bestelling_datum",
            "typ": "datetime",
            "label": L("lcl_date_time"),
            "defaultvalue": new Date()
        },
        "deliverydate": {
            "dbs": "bes_bestelling_leverdatum",
            "typ": "date",
            "label": L("lcl_bes_deli_date")
        },
        "remark": {
            "dbs": "bes_bestelling_opmerking",
            "typ": "memo",
            "label": L("lcl_bes_remark")
        },
        "remarkrequest": {
            "dbs": "bes_bestelling_lev_opm",
            "typ": "memo",
            "label": L("lcl_bes_bestelling_lev_opm")
        },
        "account": {
            "dbs": "prs_kostenplaats_key",
            "typ": "key",
            "foreign": "prs_kostenplaats",
            "label": L("lcl_account")
        },
        "costcustomer": {
            "dbs": "bes_bestelling_kosten_klant",
            "typ": "check",
            "label": L("lcl_bes_kostenklant")
        },
        "deliveryaddress": {
            "dbs": "mld_adres_key_lev",
            "typ": "key",
            "foreign": "mld_v_afleveradres",
            "label": L("lcl_delivery_address")
        },
        "deliveryroom": {
            "dbs": "bes_bestelling_plaats",
            "typ": "varchar",
            "label": L("lcl_bes_del_room")
        },
        "approver": {
            "dbs": "bes_bestelling_fiat_user",
            "typ": "key",
            "foreign": "prs_perslid",
            "label": L("lcl_bes_approver")
        },
        "approved": {
            "dbs": "bes_bestelling_approved",
            "typ": "currency",
            "label": L("lcl_bes_gefiatteerd")
        },
        "approveamount": {
            "dbs": "bes_bestelling_fiatbedrag",
            "typ": "currency",
            "label": L("lcl_bes_gefiatteerd_bedrag")
        },
        "total": {
            "dbs": "bes_bestelling_levkosten",
            "typ": "currency",
            "label": L("lcl_bes_price_total")
        },
        "discount": {
            "dbs": "bes_bestelling_korting",
            "typ": "currency",
            "label": L("lcl_bes_discount")
        },
        "activity": {
            "dbs": "fac_activiteit_key",
            "typ": "key",
            "foreign": {
                "tbl": "fac_activiteit",
                "key": "fac_activiteit_key",
                "desc": "fac_activiteit_omschrijving"
            },
            "label": L("lcl_activity")
        },
        "complain": {
            "dbs": "mld_melding_key",
            "typ": "key",
            "foreign": {
                "tbl": "mld_melding",
                "key": "mld_melding_key",
                "desc": "mld_melding_omschrijving"
            },
            "label": L("lcl_complain")
        },
        "surveysend": {
            "dbs": "bes_bestelling_kto_verstuurd",
            "typ": "datetime",
            "label": L("lcl_bes_kto_verstuurd")
        },
        "surveynr": {
            "dbs": "bes_bestelling_satisfaction",
            "typ": "number",
            "label": L("lcl_bes_kto_nr")
        },
        "surveyremark": {
            "dbs": "bes_bestelling_satisfaction_op",
            "typ": "memo",
            "label": L("lcl_bes_kto_opmerking")
        },
        "externnr": {
            "dbs": "bes_bestelling_externnr",
            "typ": "varchar",
            "label": L("extern_nr"),
            "readonly": !(user.has("WEB_FACTAB") || user.has("WEB_FACXNR"))
        },
        "externsyncdate": {
            "dbs": "bes_bestelling_externsyncdate",
            "typ": "datetime",
            "label": L("extern_syncdate"),
            "readonly": !(user.has("WEB_FACTAB") || user.has("WEB_FACXNR"))
        }
    };


    function _check_authorization(params, method)
    {
        switch (method)
        {
            case "GET": var bes_bestelling = bes.bestelling_info(params.filter.id);
                        var this_bes = bes.func_enabled_bestelling(bes_bestelling);
                        user.auth_required_or_abort(this_bes.canReadAny);
                        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);
}
%>