<% /*
    $Revision$
    $Id$

    File:           model_fac_usrdata.inc

    Description:    Vanuit CodeCharge gegenereerd model voor fac_usrdata

    Context:

    Notes:
*/
%>
<%
function model_fac_usrdata(tab_key, params)
{
    params = params || {};

    this.records_name = "customtablevalues";
    this.record_name = "customtablevalue";
    this.table = "fac_usrdata"
    this.primary = "fac_usrdata_key";
    this.soft_delete = "fac_usrdata_verwijder";
    this.record_title = L("fac_usrdata");
    this.records_title = L("fac_usrdata_m");
    this.defaultIcon = "fa-pen-line";

    this.autfunction = false; // we controleren het zelf

    this.fields = {
        "id": {
            "dbs": "fac_usrdata_key",
            "label": L("lcl_key"),
            "typ": "key",
            "required": true,
            "seq": "fac_s_fac_usrdata_key"
        },
        "customtable": {
            "dbs": "fac_usrtab_key",
            "label": L("fac_usrtab_naam"),
            "typ": "key",
            "hidden_fld": true,
            "required": true
        },
        "name": {
            "dbs": "fac_usrdata_code",
            "sql": "fac_usrdata_code || CASE WHEN fac_usrdata_verwijder IS NOT NULL THEN ' (x)' ELSE '' END",
            "sqlshow": true,
            "sqlsave": true,
            "label": L("fac_usrdata_code"),
            "typ": "varchar",
            "required": true
        },
        "description": {
            "dbs": "fac_usrdata_omschr",
            "label": L("fac_usrdata_omschr"),
            "required": true,
            "typ": "varchar",
            "translate": true
        },
        "description2": {
            "dbs": "fac_usrdata_omschr2",
            "label": L("fac_usrdata_omschr2"),
            "required": false,
            "typ": "varchar",
            "translate": true
        },
        "sequence": {
            "dbs": "fac_usrdata_volgnr",
            "label": L("fac_usrdata_volgnr"),
            "typ": "number"
        },
        "price": {
            "dbs": "fac_usrdata_prijs",
            "label": L("fac_usrdata_prijs"),
            "typ": "float",
            "multiedit": true
        },
        "expirationdate": {
            "dbs": "fac_usrdata_vervaldatum",
            "label": L("fac_usrdata_vervaldatum"),
            "typ": "date",
            "autosize": true,
            "multiedit": true
        },
        "refdata": {
            "dbs": "fac_usrdata_parentkey",
            "label": L("fac_usrdata_parentkey"),
            "typ": "key",
            "foreign": {
                "tbl": "fac_usrdata",
                "key": "fac_usrdata_key",
                "desc": "fac_usrdata_omschr || CASE WHEN fac_usrdata_verwijder IS NOT NULL THEN ' ' || " + safe.quoted_sql(L("lcl_inactive_data_suffix")) + " ELSE '' END"
            }
        }
    },

    this.list = {
        "columns": [
            "id",
            "description",
            "description2",
            "name",
            "sequence",
            "price",
            "expirationdate"
        ]
    };
    this.search = {
        "autosearch": true
    };
    this.edit = {
        "modal": true
    };

    this.hook_pre_edit = function (obj, fld)
    { // Is de waarde een verwijzing naar een andere waarde in een eigen tabel?
      if (fld.refdata)
        fld.refdata.foreign.tbl = "(    SELECT fac_usrdata_key"
                                + "          , " + lcl.xsqla("fac_usrdata_omschr", "fac_usrdata_key")
                                + "          , fac_usrtab_key"
                                + "          , fac_usrdata_verwijder"
                                + "       FROM fac_usrdata"
                                + "      WHERE (fac_usrdata_vervaldatum IS NULL OR fac_usrdata_vervaldatum > SYSDATE)"
                                + "        AND fac_usrdata_verwijder IS NULL"
                                + (obj.refdata && obj.refdata.id
                                  ? "   UNION"
                                    + " SELECT fac_usrdata_key"
                                    + "      , " + lcl.xsqla("fac_usrdata_omschr", "fac_usrdata_key") //+ " ||"
                                    + "      , fac_usrtab_key"
                                    + "      , fac_usrdata_verwijder"
                                    + "   FROM fac_usrdata"
                                    + "  WHERE fac_usrtab_key = (SELECT ut.fac_usrtab_parentkey FROM fac_usrtab ut WHERE ut.fac_usrtab_key = " + tab_key + ")"
                                    + "    AND fac_usrdata_key = " + obj.refdata.id
                                  : "")
                                + ")";
    }

    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, {});

    // Server side code!
    //
    if (tab_key > 0)
    {
        // Autorisatie
        var authparams = user.checkAutorisation("WEB_PRSSYS", true);
        if (!authparams)
        {
            var sql = "SELECT fac_functie_key"
                    + "  FROM fac_usrtab"
                    + " WHERE fac_usrtab_key = " + tab_key;
            var oRs = Oracle.Execute(sql);
            this.autfunction = oRs("fac_functie_key").Value || "WEB_PRSSYS";
            oRs.Close();
        }
        else
            this.autfunction = authparams.autfunction;

        // Referentie tabel
        var sql = "SELECT C.fac_usrtab_parentkey"
                + "     , P.fac_usrtab_omschrijving"
                + "     , P.fac_usrtab_naam"
                + "  FROM fac_usrtab P"
                + "     , fac_usrtab C"
                + " WHERE C.fac_usrtab_key = " + tab_key
                + "   AND P.fac_usrtab_key = C.fac_usrtab_parentkey";
        var oRs = Oracle.Execute(sql);
        if (!oRs.Eof)
        {
            this.fields.refdata.foreign.where = " fac_usrtab_key = " + oRs("fac_usrtab_parentkey").Value;
            this.fields.refdata.label = oRs("fac_usrtab_omschrijving").Value? oRs("fac_usrtab_omschrijving").Value : oRs("fac_usrtab_naam").Value; // Als omschrijving niet is ingevuld dan (verplichte) tabelnaam nemen.
            this.list.columns.push("refdata"); // dan ook tonen in overzicht
        }
        else
            delete this.fields.refdata;
        oRs.Close();
    }
    else
        delete this.fields.refdata;
}
%>