<% /*
    $Revision$
    $Id$

    File:           model_locations.inc

    Description:    Locatie model.
    Parameters:
    Context:

    Notes:
*/

%>
<!-- #include file="../alg/alg.inc"-->
<!-- #include file="../Shared/discxalg3d.inc"-->
<!-- #include file="./model_custom_fields.inc"-->
<!-- #include file="./model_alg_kenmerk.inc"-->
<%
function model_alg_locatie()
{
    this.table = "alg_locatie";
    this.primary = "alg_locatie_key";
    this.records_name = "locations";
    this.record_name = "location";
    this.fields = {"id"            : { dbs: "alg_locatie_key"            , typ: "key"                     },
                   "code"          : { dbs: "alg_locatie_code"           , typ: "varchar", filter: "like"  , "label": L("lcl_estate_locatie_man_code")},
                   "name"          : { dbs: "alg_locatie_omschrijving"   , typ: "varchar"                  , "label": L("lcl_location")},
                   "district"      : { dbs: "alg_district_key"           , typ: "key",     foreign: "alg_district"  , "label": L("lcl_district")},
                   "postaddress"   : { dbs: "alg_locatie_post_adres"     , typ: "varchar", filter: "like" , "label": L("lcl_prs_address_post_adres")},
                   "postzipcode"   : { dbs: "alg_locatie_post_postcode"  , typ: "varchar", filter: "like" , "label": L("lcl_prs_address_post_postcode") },
                   "postcity"      : { dbs: "alg_locatie_post_plaats"    , typ: "varchar", filter: "like" , "label": L("lcl_prs_address_post_plaats") },
                   "postcountry"   : { dbs: "alg_locatie_post_land"      , typ: "varchar", filter: "like" , "label": L("lcl_prs_address_post_land") },
                   "visitaddress"  : { dbs: "alg_locatie_adres"          , typ: "varchar", filter: "like" , "label": L("lcl_prs_address_bezoek_adres") },
                   "visitzipcode"  : { dbs: "alg_locatie_postcode"       , typ: "varchar", filter: "like" , "label": L("lcl_prs_address_bezoek_postcode") },
                   "visitcity"     : { dbs: "alg_locatie_plaats"         , typ: "varchar", filter: "like" , "label": L("lcl_prs_address_bezoek_plaats") },
                   "visitcountry"  : { dbs: "alg_locatie_land"           , typ: "varchar", filter: "like" , "label": L("lcl_prs_address_bezoek_land") },
                   "phone"         : { dbs: "alg_locatie_verantw_tel"    , typ: "varchar", filter: "like" , "label": L("lcl_prs_address_contact_telefoon") },
                   "email"         : { dbs: "alg_locatie_email"          , typ: "varchar", filter: "like" , "label": L("lcl_prs_address_contact_email") },
                   // TODO alleen if S("ins_score_enabled") == 1
                   "mjb_score"     : { dbs: "alg_locatie_mjb_score1"     , typ: "number"                  , "label": L("lcl_alg_locatie_mjb_score1")},
                   "coordinate_x"  : { dbs: "alg_locatie_x"              , typ: "number"                  , "label": L("lcl_geoxcoord")},
                   "coordinate_y"  : { dbs: "alg_locatie_y"              , typ: "number"                  , "label": L("lcl_geoycoord")},
                   "externnr"      : { dbs: "alg_locatie_externnr"       , typ: "varchar"                 , "label": L("extern_nr"),       "readonly": !(user.has("WEB_FACTAB") || user.has("WEB_FACXNR")) },
                   "externsyncdate": { dbs: "alg_locatie_externsyncdate" , typ: "datetime"                , "label": L("extern_syncdate"), "readonly": !(user.has("WEB_FACTAB") || user.has("WEB_FACXNR")) },
                   "seqnr"         : { dbs: "alg_locatie_volgnr"         , typ: "number"                  , "label": L("lcl_alg_locatie_volgnr") },
                   "deleted"       : { dbs: "alg_locatie_verwijder"      , typ: "datetime"                , "label": L("lcl_deleted")}
                  };

    this.includes =
            {
                "custom_fields" : {
                    "model": new model_custom_fields(this, new model_alg_kenmerk("L", { internal: true }), { pNiveau: "L", readman: true, readuse: true }),
                    "joinfield": "flexparentkey",
                    "enable_update": true
                }
            };

    function _check_authorization (params, method)
        {
            switch (method)
            {
                case "GET":
                    var autfunction = ["WEB_ALGUSE","WEB_ALGMAN"];
                    params.authparams = user.checkAutorisation(autfunction, null, null, true); // pessimistisch
                    if (params.filter.id)
                    {
                        var loc_key = params.filter.id;
                        var this_alg = alg.func_enabled(loc_key, "L");
                        user.auth_required_or_abort(this_alg.readman || this_alg.readuse);
                    }
                    break;
                //case "DELETE":
                //    var autfunction = "WEB_ALGMAN";
                //    var authParams = user.checkAutorisation(autfunction);
                //    // en verder met de autorisatie van PUT...
                case "PUT":
                    var loc_key = params.filter.id;
                    var this_alg = alg.func_enabled(loc_key, "L");
                    user.auth_required_or_abort(this_alg.writeman || this_alg.writeuse);
                    params.isNew = false;
                    break;
                case "POST":
                    var loc_key = -1;
                    var this_alg = alg.func_enabled(loc_key, "L");
                    user.auth_required_or_abort(this_alg.writeman);
                    params.isNew = true;
                    break;
            }

            params.func_enabled = this_alg || {};
        }

    function _analyze_fields (dbfields, params, jsondata) /* analyseer inkomende data, common voor PUT en POST */
        {
        }

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

            var query = api2.sqlfields(params, this);
            query.wheres.push("alg_locatie_verwijder IS NULL");

            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 alg_locatie_code";

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

            return json;
        };

    this.REST_PUT = function (params, jsondata, the_key) /* update location */
        {
            _check_authorization (params, "PUT");

            if (!jsondata.id) jsondata.id = the_key;
            var loc_key = the_key;

            var dbfields = api2.update_fields(params, this, jsondata); // Build updater
            _analyze_fields(dbfields, params, jsondata);

            var wheres = [" alg_locatie_key = " + loc_key];
            var locUpd = buildTrackingUpdate("alg_locatie", wheres.join(" AND " ), dbfields, { noValidateToken: true });

            var err = Oracle.Execute(locUpd.sql, true);
            if (err.friendlyMsg)
                abort_with_warning(err.friendlyMsg);

            var algtrack = api2.process_includes(params, this, jsondata, the_key);

            // update nog tracken
            var all_track = (locUpd && locUpd.trackarray ? locUpd.trackarray : []);
            all_track = all_track.concat(algtrack);
            if (all_track.length)
                shared.trackaction("ALGLUP", loc_key, all_track.join("\n") );

            return { key: loc_key, warning: "" };
        }

    this.REST_POST = function (params, jsondata) /* new location */
        {
            _check_authorization(params, "POST");

            var dbfields = api2.update_fields(params, this, jsondata); // Build updater
            _analyze_fields(dbfields, params, jsondata);
            dbfields["id"] = {dbs: "alg_locatie_key", typ: "key", seq: "alg_s_alg_locatie_key" };
            var locIns = buildInsert("alg_locatie", dbfields, { noValidateToken: true });
            var loc_key = locIns.sequences["alg_locatie_key"];

            var err = Oracle.Execute(locIns.sql, true);
            if (err.friendlyMsg)
                abort_with_warning(err.friendlyMsg);

            var algtrack = api2.process_includes(params, this, jsondata, loc_key);

            // update nog tracken
            var all_track = (locIns && locIns.trackarray ? locIns.trackarray : []);
            all_track = all_track.concat(algtrack);
            if (all_track.length)
                shared.trackaction("ALGNEW", loc_key, all_track.join("\n") );

            return { key: loc_key, warning: "" };
        }

}
%>