<% /*
    $Revision$
    $Id$

    File:           model_prs_dienst.inc

    Description:    Model voor prs_dienst. Toont diensten bij bedrijf of diensten bij persoon.

    Context:

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

function model_prs_dienst()
{
    var company_key = getQParamInt("company", -1);
    var prs_key = getQParamInt("person", -1);
    var gparams = {};
    var prsauthparams = prs.checkAutorisation(prs_key);

    this.records_name = "services";
    this.record_name = "service";
    this.table = "prs_dienst";
    this.primary = "prs_dienst_key";
    this.autfunction = ["WEB_PRSMSU"]
    if (prsauthparams.readcontact)
        this.autfunction.push("WEB_EXTREL");
    this.record_title = L("prs_dienst");
    this.records_title = L("prs_dienst_m");

    this.fields = {
        "id": {
            "dbs": "prs_dienst_key",
            "label": "Key",
            "typ": "key",
            "hidden_fld": prs_key > -1 || company_key > -1,
            "required": true,
            "seq": "prs_s_prs_dienst_key"
        },
        "name": {
            "dbs": "prs_dienst_omschrijving",
            "label": L("prs_dienst_omschrijving"),
            "typ": "varchar",
            "translate": true,
            "required": true,
            "filter": "like"
        },
        "hint": {
            "dbs": "prs_dienst_hint",
            "label": L("prs_dienst_hint"),
            "typ": "memo",
            "translate": true,
            "required": false,
            "filter": "like"
        }
    };

    if (mode == "list" || mode == "search")
    {
        if (prs_key > -1 || company_key > -1)
            this.fields.hasScope = {
                "dbs": "hasScope",
                "sql": "(SELECT COUNT(1)"
                     + "   FROM prs_bedrijfdienstlocatie bdl"
                     + "  WHERE prs_dienst.prs_dienst_key = bdl.prs_dienst_key"
                     + (company_key > -1
                        ? " AND bdl.prs_bedrijf_key = " + company_key
                        : " AND bdl.prs_perslid_key = " + prs_key)
                     + "    AND (bdl.alg_locatie_key IS NOT NULL"
                     + "      OR bdl.alg_gebouw_key IS NOT NULL)"
                     + ")",
                "listfunction": function(oRs)
                {
                    var result = "";
                    if (oRs.Fields("hasScope").Value > 0)
                        result = I("fa-location");
                    return result;
                },
                "label": L("lcl_prs_dienst_scope_loc"),
                "title": L("lcl_prs_dienst_scope_loc_hint"),
                "typ": "number",
                "tdClass": "fa-fw",
                "nosearch": true
            }
    }

    if (company_key > -1)
    {
        // Toch ook het bedrijf tonen in de show-mode.
        var fields_1 = this.fields;
        var fields_2 = {
            "company": {
                "dbs": "prsd.prs_bedrijf_key",
                "label": L("prs_bedrijf_key"),
                "typ": "key",
                "foreign": {
                    "tbl": "prs_bedrijf",
                    "key": "prs_bedrijf_key",
                    "desc": "prs_bedrijf_naam"
                }
            }
        };

        this.fields = object_merge({}, fields_1, fields_2);

        var tbl_prsd = "(SELECT DISTINCT prs_dienst_key"
                     + "      , prs_bedrijf_key"
                     + "   FROM prs_bedrijfdienstlocatie"
                     + ") prsd";
        gparams = {
            GET: {
                tables: [tbl_prsd],
                wheres: ["prs_dienst.prs_dienst_key = prsd.prs_dienst_key",
                         "prsd.prs_bedrijf_key = " + company_key
                        ]
            }
        };
    }

    if (prs_key > -1)
    {
        // Toch ook de persoon tonen in de show-mode.
        var fields_1 = this.fields;
        var fields_2 = {
            "person": {
                "dbs": "prsd.prs_perslid_key",
                "label": L("res_prs_perslid_key"),
                "typ": "key",
                "foreign": {
                    "tbl": "prs_v_perslid_fullnames",
                    "key": "prs_perslid_key",
                    "desc": "prs_perslid_naam_friendly"
                }
            }
        };

        this.fields = object_merge({}, fields_1, fields_2);

        var tbl_prsd = "(SELECT DISTINCT prs_dienst_key"
                     + "      , prs_perslid_key"
                     + "   FROM prs_bedrijfdienstlocatie"
                     + ") prsd";
        gparams = {
            GET: {
                tables: [tbl_prsd],
                wheres: ["prs_dienst.prs_dienst_key = prsd.prs_dienst_key",
                         "prsd.prs_perslid_key = " + prs_key
                        ]
            }
        };
    }

    this.REST_GET    = generic_REST_GET(this, gparams);
    this.REST_POST   = generic_REST_POST(this);
    if (user.has("WEB_PRSMSU") && // Nvt met alleen EXTREL
        (hasQParam("company") && !(company_key > -1)) ||
        (hasQParam("person") && !(prs_key > -1))) {
            // Vanuit het tabblad Service mogen de knoppen Wijzigen en Verwijderen niet aanwezig zijn.
            // Als dit scherm wordt aangeroepen vanuit het tabblad Admin moeten ze er natuurlijk wel zijn.
            this.REST_PUT    = generic_REST_PUT(this);
            this.REST_DELETE = generic_REST_DELETE(this, {});
    }

    else if (user.has("WEB_EXTREL") && hasQParam("person") && prs_key > -1) {
        // Niet de dienst zelf, maar de dienst bij relatie verwijderen.
        this.REST_DELETE = function (params, the_key)
            {
                var sql = "DELETE FROM prs_bedrijfdienstlocatie"
                        + " WHERE prs_dienst_key = " + the_key
                        + "   AND prs_perslid_key = " + prs_key;
                var err = Oracle.Execute(sql, true);
                if (err.friendlyMsg)
                    api2.error(400, err.friendlyMsg);

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

}
%>