<% /*
    $Revision$
    $Id$

    File:           model_prs_kostenplaats.inc

    Description:    Vanuit CodeCharge gegenereerd model voor prs_kostenplaats

    Context:

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

function model_prs_kostenplaats()
{
    this.records_name = "costcentres";
    this.record_name = "costcentre";
    this.table = "prs_kostenplaats";
    this.primary = "prs_kostenplaats_key";
    this.autfunction = (S("bgt_enabled")==1 ? ["WEB_FINMSU", "WEB_BGTMGT"] : ["WEB_FINMSU"]);
    this.record_title = (S("bgt_enabled")==1 ? L("bgt_kostenplaats") : L("prs_kostenplaats"));
    this.records_title = (S("bgt_enabled")==1 ? L("bgt_kostenplaats_m") : L("prs_kostenplaats_m"));
    this.soft_delete = "prs_kostenplaats_verwijder";
    this.soft_expire = "prs_kostenplaats_eind";

    this.fields = {
        "id": {
            "dbs": "prs_kostenplaats_key",
            "label": L("lcl_key"),
            "typ": "key",
            "hidden_fld": true,
            "required": true,
            "seq": "prs_s_prs_kostenplaats_key"
        },
        "budgetdiscipline": {
            "dbs": "ins_discipline_key",
            "label": L("bgt_discipline_omschrijving"),
            "typ": "key",
            "required": true,
            "foreign": bgt_budgetdiscipline_foreign(),
            "showtransit": true
        },
        "costgroup": {
            "dbs": "prs_kostenplaatsgrp_key",
            "label": L("lcl_prs_kostenpl_group"),
            "typ": "key",
            "foreign": bgt_accountgroup_foreign(),
            "showtransit": true,
            "multiedit": true
        },
        "name": {
            "dbs": "prs_kostenplaats_nr",
            "label": L("lcl_prs_dept_name"),
            "typ": "varchar",
            "translate": true,
            "required": true,
            "filter": "like"
        },
        "module": {
            "dbs": "prs_kostenplaats_module",
            "label": L("lcl_prs_type"),
            "typ": "varchar",
            "LOV": "ALG;" + L("lcl_prs_kosttype_alg")
                + ";PRJ;" + L("lcl_prs_kosttype_prj")
                + ";PRS;" + L("lcl_prs_kosttype_prs"),
            "required": true,
            "defaultvalue": (S("bgt_enabled") ? "PRJ" : "PRS"),
            "hidden_fld": (S("bgt_enabled") ? true : false),
            "multiedit": true
        },
        "description": {
            "dbs": "prs_kostenplaats_omschrijving",
            "label": L("lcl_prs_descr"),
            "typ": "varchar",
            "translate": true,
            "required": true,
            "filter": "like"
        },
        "person": {
            "dbs": "prs_perslid_key",
            "label": L("lcl_prs_budgethouder"),
            "typ": "key",
            "foreign" : "PRS_PERSLID",
            "showtransit": true,
            "multiedit": true
        },
        "external": {
            "dbs": "prs_kostenplaats_extern",
            "label": L("lcl_prs_kpnextern"),
            "typ": "check0",
            "multiedit": true
        },
        "approval": {
            "dbs": "prs_kostenplaats_fiat",
            "label": L("lcl_prs_kpnfiat"),
            "typ": "check0",
            "multiedit": true
        },
        "begin": {
            "dbs": "prs_kostenplaats_begin",
            "label": L("lcl_start_date"),
            "typ": "date",
            "multiedit": true
        },
        "end": {
            "dbs": "prs_kostenplaats_eind",
            "label": L("lcl_end_date"),
            "typ": "date",
            "multiedit": true
        },
        "costlimit": {
            "dbs": "prs_kostenplaats_limiet",
            "label": L("lcl_prs_budget") + "&nbsp;(" + L("lcl_valutasign")+")",
            "iscurrency": true,
            "typ": "number"
        },
        "limitperiod": {
            "dbs": "prs_kostenplaats_limietperiode",
            "label": L("lcl_prs_budgetperiode"),
            "typ": "number",
            "LOV":  "0;" + L("lcl_prs_none") +
                   ";1;" + L("lcl_prs_month") +
                   ";2;" + L("lcl_prs_year"),
            "multiedit": true
        },
        "externalid": {
            "dbs": "prs_kostenplaats_externnr",
            "label": L("lcl_prs_kpn_externid"),
            "typ": "varchar"
        },
        "externsyncdate": {
            "dbs": "prs_kostenplaats_extsyncdate",
            "label": L("lcl_prs_kpn_externsyncdate"),
            "typ": "datetime",
            "readonly": !(user.has("WEB_FACTAB") || user.has("WEB_FACXNR"))
        }
    };

    if (S("bgt_enabled")) // alleen voor budgetcontrole, de rest nog even readonly
    {
        // Deze hoeven ze niet voor BGT te zien:
        delete this.fields.begin;
        delete this.fields.end;
        delete this.fields.person;
        delete this.fields.costlimit;
        delete this.fields.limitperiod;
        delete this.fields.external;
        delete this.fields.approval;
        delete this.fields.costgroup;
        delete this.fields.externalid;
        delete this.fields.externsyncdate;

        this.hook_pre_edit = function (obj, fld)
        {
        %>
        <script>
            var budgetdiscipline_key = <%=(obj.budgetdiscipline ? obj.budgetdiscipline.id : -1)%>;
        </script>
        <%
        }

        this.hook_pre_post = function (params, jsondata)
        {
        }

        this.REST_GET    = generic_REST_GET(this, {
            "GET": {
                "tables": [ "prs_kostenplaatsgrp"
                          ],
                "wheres": [ "prs_kostenplaatsgrp.prs_kostenplaatsgrp_key(+) = prs_kostenplaats.prs_kostenplaatsgrp_key"
                          ]
            }
        });
        this.REST_POST   = generic_REST_POST(this);
        this.REST_PUT    = generic_REST_PUT(this);
        this.REST_DELETE = generic_REST_DELETE(this);
    }
    else
    {
        delete this.fields.budgetdiscipline;
        delete this.fields.begin;
        delete this.fields.module.hidden_fld;
        delete this.fields.module.defaultvalue;

        this.hook_pre_edit = function (obj, fld, params)
        {
            var kpngrp_prs_key = -1;
            var kpg_limperiode = 0;
            var kpn_limietperiode = 0;
            if (obj.id > -1)
            {
                var sql = "SELECT kp.prs_kostenplaats_limietperiode"
                        + "     , kpg.prs_kostenplaatsgrp_limperiode"
                        + "     , kpg.prs_perslid_key"
                        + "  FROM prs_kostenplaats kp"
                        + "     , prs_kostenplaatsgrp kpg"
                        + " WHERE kp.prs_kostenplaatsgrp_key = kpg.prs_kostenplaatsgrp_key(+)"
                        + "   AND kp.prs_kostenplaats_key = " + obj.id;
                var oRs = Oracle.Execute(sql);
                kpngrp_prs_key    = oRs("prs_perslid_key").Value? oRs("prs_perslid_key").Value : -1;
                kpg_limperiode    = oRs("prs_kostenplaatsgrp_limperiode").Value ? oRs("prs_kostenplaatsgrp_limperiode").Value : 0;
                kpn_limietperiode = kpg_limperiode ? kpg_limperiode : (oRs("prs_kostenplaats_limietperiode").Value ? oRs("prs_kostenplaats_limietperiode").Value : 0);
                oRs.Close();
            }
            // Wijzigen van de kostenplaats(groep)? Setting prs_restrict_kpedit {0=volgens autorisatie (default) | 1=alleen eigen kp/kpg wijzigen}.
            // WEB_FINMSU rechten of budgethouder.
            var authparams = user.checkAutorisation("WEB_FINMSU", true);
            var canChangeAlg = (authparams && authparams.ALGwritelevel < 9);
            var canChange = (canChangeAlg ||
                             (S("prs_restrict_kpedit") == 1 && (obj.person == user_key || kpngrp_prs_key == user_key))) // Alleen eigen kp/kpg wijzigen
            user.auth_required_or_abort(canChange);

            fld.name.readonly = !canChangeAlg;
            fld.module.readonly = !canChangeAlg;
            fld.description.readonly = !canChangeAlg;
            fld.person.readonly = ( (obj.id == -1 && obj.person && obj.person.id > 0) ? true : !canChangeAlg);
            fld.costgroup.readonly = ( (obj.id == -1 && obj.costgroup && obj.costgroup.id > 0) ? true : !canChangeAlg);
            fld.external.readonly = !canChangeAlg;
            fld.approval.readonly = !canChangeAlg;
            fld.end.readonly = !canChangeAlg;
            fld.limitperiod.readonly = kpg_limperiode;

            %>
                <script type="text/javascript">
                    // Deze variabelen zijn nodig in prs_kostenplaats.js
                    var p_kpn_limietperiode = <%=kpn_limietperiode%>;
                </script>
            <%
        }

        this.REST_GET    = generic_REST_GET(this);
        this.REST_POST   = generic_REST_POST(this);
        this.REST_PUT    = generic_REST_PUT(this);
        this.REST_DELETE = generic_REST_DELETE(this);
    }
}
%>