<% /*
    $Revision$
    $Id$

    File:           model_contracts.inc

    Description:    contract model.
    Parameters:
    Context:

    Notes:          Zonder filters worden de lopende actieve contracten opgeleverd (binnen scope)
                    TODO: 3D autorisatie, ook fe/eigen conracten vs CNTUSE/CNTMAN
*/
%>
<!-- #include file="../Shared/discx3d.inc" -->
<!-- #include file="../cnt/cnt.inc" -->
<!-- #include file="./model_cnt_contract_plaats.inc"-->
<!-- #include file="./model_custom_fields.inc"-->
<!-- #include file="./model_cnt_kenmerk.inc"-->
<!-- #include file="./model_fac_tracking.inc"-->
<!-- #include file="./model_cnt_factuurschema.inc"-->
<%
//    uglyincludefix: true, // voor CUST#54504

function model_contracts()
{
    this.module =       "CNT";
    this.table =        "cnt_contract";
    this.primary =      "cnt_contract_key";
    this.records_name = "contracts";
    this.record_name =  "contract";
    this.fields = {
        "id": {
            dbs: "cnt_contract_key",
            typ: "key"
        },
        "name": {
            dbs: "cnt_contract_nummer_intern",
            typ: "varchar",
            label: L("lcl_cnt_intern_nr")
        },
        "description": {
            dbs: "cnt_contract_omschrijving",
            typ: "varchar",
            label: L("lcl_cnt_descr")
        },
        "startdate": {
            dbs: "cnt_contract_looptijd_van",
            typ: "date",
            label: L("lcl_cnt_date_start"),
            filter: "range",
            track: true
        },
        "reminderdate": {
            dbs: "cnt_contract_rappeldatum",
            sql: "cnt.cnt_getRappeldatum(cnt_contract.cnt_contract_key)",
            typ: "date",
            label: L("lcl_cnt_rappel_date"),
            track: true
        },
        "canceldate": {
            dbs: "cnt_contract_opzegdatum",
            sql: "cnt.cnt_getOpzegdatum(cnt_contract.cnt_contract_key)",
            typ: "date",
            label: L("lcl_cnt_cancel_date"),
            track: true
        },
        "enddate": {
            dbs: "cnt_contract_looptijd_tot",
            typ: "date",
            label: L("lcl_cnt_enddate"),
            filter: "range",
            track: true
        },
        "contractor": {
            dbs: "cnt_prs_bedrijf_key",
            typ: "key",
            label: L("lcl_cnt_contractant"),
            foreign: "prs_bedrijf",
            track: true
        },
        "person": {
            dbs: "cnt_prs_perslid_key",
            typ: "key",
            label: L("lcl_cnt_name"),
            foreign: "prs_perslid",
            track: true
        },
        "status": {
            dbs: "cnt_contract_status",
            typ: "key",
            label: L("lcl_status"),
            foreign: cnt.getcntstatustext,
            track: true
        },
        "department": {
            dbs: "prs_afdeling_key_eig",
            typ: "key",
            label: L("lcl_cnt_owner"),
            foreign: "prs_afdeling",
            track: true
        },
        "service": {
            dbs: "prs_dienst_key",
            typ: "key",
            label: L("lcl_prs_companies_dienst"),
            foreign: "prs_dienst",
            track: true
        },
        "contracttype": {
            dbs: "ins_discipline_key",
            typ: "key",
            label: L("lcl_cnt_srttype"),
            foreign: "cnt_discipline",
            track: true
        }
    };

    this.includes = {
        "concerns": {
            "model": new model_cnt_contract_plaats({ internal: true }),
            "joinfield": "contract"
        },
        "custom_fields" : {
            "model": new model_custom_fields(this, new model_cnt_kenmerk({ internal: true }), { readman: true, readuse: true }),
            "joinfield": "flexparentkey",
            "enable_update": true
        },
        "tracking": {
            "model": new model_tracking(['contract']),
            "joinfield": "trackingrefkey"
        },
        "schedule": {
            "model": new model_cnt_factuurschema(),
            "joinfield": "cnt_contract_key"
        }
    };

    this.REST_GET = function _GET(params)
        {
            var scope = params.filter.scope || "fe";
            var autfunction = { fe : "WEB_CNTUSE", fo : "WEB_CNTUSE", bo : "WEB_CNTMAN"} [scope];  /* gokje; fe hoefde vroeger geen rechten voor eigen contracten...*/
            params.authparams = user.checkAutorisation(autfunction, null, null, true); /* pessimistic */

            // Hier kom je niet meer terecht als bovenstaande autorisatiecheck niet tot succes leidt.

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

            if (scope == "fe")
            {
                query.wheres.push("(prs_perslid_key_eig=" + user_key + " OR prs_perslid_key_beh=" + user_key + ")");
            }

            if (!params.filter.id)
            {
                if (!params.filter.status)
                {
                    /* Limitation: if no status filter, then default to the active statusses (0) */
                    query.wheres.push("cnt_contract_status = 0");
                }
                if (!(params.filter.startdate || params.filter.reminderdate || params.filter.canceldate || params.filter.enddate))
                {
                    /* Limitation: if no date filters, then default to the active contracts */
                    query.wheres.push("TRUNC(SYSDATE) BETWEEN cnt_contract_looptijd_van AND cnt_contract_looptijd_tot");
                }
            }

            if (params.filter.location)
            {
              query.wheres.push("cnt_contract_plaats.cnt_alg_plaats_code = 'L'");
              query.wheres.push("cnt_contract_plaats.cnt_alg_plaats_key = " + parseInt(params.filter.location));
            }
            if (params.filter.building)
            {
              query.wheres.push("cnt_contract_plaats.cnt_alg_plaats_code = 'G'");
              query.wheres.push("cnt_contract_plaats.cnt_alg_plaats_key = " + parseInt(params.filter.building));
            }
            if (params.filter.terrain)
            {
              query.wheres.push("cnt_contract_plaats.cnt_alg_plaats_code = 'T'");
              query.wheres.push("cnt_contract_plaats.cnt_alg_plaats_key = " + parseInt(params.filter.terrain));
            }
            if (params.filter.floor)
            {
              query.wheres.push("cnt_contract_plaats.cnt_alg_plaats_code = 'V'");
              query.wheres.push("cnt_contract_plaats.cnt_alg_plaats_key = " + parseInt(params.filter.floor));
            }
            if (params.filter.room)
            {
              query.wheres.push("cnt_contract_plaats.cnt_alg_plaats_code = 'R'");
              query.wheres.push("cnt_contract_plaats.cnt_alg_plaats_key IN (" + params.filter.room + ")");
            }

            // Ik zeg in deze versie: autorisatiescope op contractsoort (discipline) en nog maar ns niet op ALG of PRS. Akkoord?
            query.wheres.push("ins_discipline_key IN"
                            + " (SELECT ins_discipline_key FROM fac_v_webgebruiker"
                            + "   WHERE fac_functie_key = (SELECT fac_functie_key FROM fac_functie WHERE fac_functie_code=" + safe.quoted_sql(autfunction) + ")"
                            + "     AND prs_perslid_key=" + user_key + " AND (fac_gebruiker_prs_level_read<9 OR fac_gebruiker_alg_level_read<9))");

            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 cnt_contract_key";
            if (query.orderbys.length)
                sql += ", " + query.orderbys.join(", ");

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

            return json;
        };


    //this.REST_POST   = generic_REST_POST(this);       /* new contract */
    //this.REST_PUT    = generic_REST_PUT(this);        /* update contract */
    //this.REST_DELETE = generic_REST_DELETE(this, {}); /* delete contract */
}
%>