<% /*
    $Revision$
    $Id$

    File:           model_companies.inc

    Description:    Bedrijven model.
    Parameters:
    Context:

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

function model_companies()
{
    this.table = "prs_bedrijf";
    this.primary = "prs_bedrijf_key";
    this.records_name = "companies";
    this.record_name = "company";


    this.fields = {
        "id"           : { dbs: "prs_bedrijf_key"             , typ: "key",     filter: "exact" },
        "name"         : { dbs: "prs_bedrijf_naam"            , typ: "varchar", filter: "like" },
        "postaddress"  : { dbs: "prs_bedrijf_post_adres"      , typ: "varchar", filter: "like" },
        "postzipcode"  : { dbs: "prs_bedrijf_post_postcode"   , typ: "varchar", filter: "like" },
        "postcity"     : { dbs: "prs_bedrijf_post_plaats"     , typ: "varchar", filter: "like" },
        "postcountry"  : { dbs: "prs_bedrijf_post_land"       , typ: "varchar", filter: "like" },
        "visitaddress" : { dbs: "prs_bedrijf_bezoek_adres"    , typ: "varchar", filter: "like" },
        "visitzipcode" : { dbs: "prs_bedrijf_bezoek_postcode" , typ: "varchar", filter: "like" },
        "visitcity"    : { dbs: "prs_bedrijf_bezoek_plaats"   , typ: "varchar", filter: "like" },
        "visitcountry" : { dbs: "prs_bedrijf_bezoek_land"     , typ: "varchar", filter: "like" },
        "phone"        : { dbs: "prs_bedrijf_telefoon"        , typ: "varchar", filter: "like" },
        "phone2"       : { dbs: "prs_bedrijf_telefoon2"       , typ: "varchar", filter: "like" }, /*?*/
        "contactname"  : { dbs: "prs_bedrijf_contact_persoon" , typ: "varchar", filter: "like" },
        "contactphone" : { dbs: "prs_bedrijf_contact_telefoon", typ: "varchar", filter: "like" },
        "remark"       : { dbs: "prs_bedrijf_opmerking"       , typ: "varchar", filter: "like" },
        "remark2"      : { dbs: "prs_bedrijf_opmerking2"      , typ: "varchar", filter: "like" }, /*?*/
        "hourrate"     : { dbs: "prs_bedrijf_uurloon"         , typ: "float",   "iscurrency": true, filter: "like" },
        "code"         : { dbs: "prs_leverancier_nr"          , typ: "varchar", filter: "like" },
        "agreement"    : { dbs: "prs_overeenkomst_nr"         , typ: "varchar", filter: "like" },
        "agreementdate": { dbs: "prs_overeenkomst_datum"      , typ: "date",    filter: "range" },
        "isinternal"   : { dbs: "prs_bedrijf_intern"          , typ: "varchar", filter: "exact" },
        "issupplier"   : { dbs: "prs_bedrijf_leverancier"     , typ: "varchar", filter: "exact" },
        "isexecutor"   : { dbs: "prs_bedrijf_uitvoerende"     , typ: "varchar", filter: "exact" }, /*naam?*/
        "iscontractor" : { dbs: "prs_bedrijf_contract"        , typ: "varchar", filter: "exact" },
        "isrenter"     : { dbs: "prs_bedrijf_huurder"         , typ: "varchar", filter: "exact" },
        "email"        : { dbs: "prs_bedrijf_email"           , typ: "varchar", filter: "like" },
        "location"     : { dbs: "prs_bedrijfdienstlocatie.alg_locatie_key", typ: "key", foreign: "alg_locatie", filter: prs.getLocationSql },
        "service"      : { dbs: "prs_bedrijfdienstlocatie.prs_dienst_key", typ: "key", foreign: "prs_dienst", filter: "like" }
    };


    this.includes = {
        "custom_fields" : {
            "model": new model_custom_fields(this, "PRS", { readman: true, readuse: true, pNiveau: "B" }),
            "joinfield": "flexparentkey"
        }
    };

    this.REST_GET = function _GET(params)
        {
            var autfunction = "WEB_RELMAN";
            params.authparams = user.checkAutorisation(autfunction, null, null, true); // pessimistisch

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

            /* we need the dienstlocatie for the service and location */
            query.tables.push("prs_bedrijfdienstlocatie");
            query.wheres.push("prs_bedrijfdienstlocatie.prs_bedrijf_key(+) = prs_bedrijf.prs_bedrijf_key")

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

            var sql = "SELECT " + query.selects.join(", ")
                    + "  FROM " + query.tables.join(", ")
                    + " WHERE " + query.wheres.join(" AND " )
                    + " ORDER BY prs_bedrijf_naam";

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

            return json;
        };
    this.PUT = function (params) /* update company */
    {
    };
    this.POST = function (params) /* new company */
    {
    };
    this.DELETE = function (params) /* delete company */
    {
    };
}
%>