<% /*
    $Revision$
    $Id$

    File:           model_fac_srtnotificatie.inc

    Description:    Vanuit CodeCharge gegenereerd model voor fac_srtnotificatie

    Context:

    Notes:
*/
%>
<%

function model_fac_srtnotificatie(cust)
{
    this.table = "fac_srtnotificatie";
    this.primary = "fac_srtnotificatie_key";
    this.records_name = "fac_srtnotificaties";
    this.record_name = "fac_srtnotificatie";
    this.autfunction = "WEB_PRSSYS";
    this.record_title = L("fac_srtnotificatie");
    this.records_title = L("fac_srtnotificatie_m");

    this.fields = {
        "id": {
            "dbs": "fac_srtnotificatie_key",
            "label": "Key",
            "typ": "key",
            "required": true,
            "seq": "fac_s_fac_srtnotificatie_key",
            "filter": "exact"
        },
        "name": {
            "dbs": "fac_srtnotificatie_code",
            "label": L("fac_srtnotificatie_code"),
            "typ": "varchar",
            "readonly": true
        },
        "description": {
            "dbs": "fac_srtnotificatie_oms",
            "label": L("fac_srtnotificatie_oms"),
            "typ": "memo",
            "translate": true,
            "islcl": true,
            "filter": "like"
        },
        "xmlnode": {
            "dbs": "fac_srtnotificatie_xmlnode",
            "label": L("fac_srtnotificatie_xmlnode"),
            "typ": "varchar",
            "readonly": true,
            "LOV": fill_srtnotificatie_LOV(),
            "emptyoption": null
        },
        "notification_url": {
            "dbs": "fac_srtnotificatie_url",
            "label": L("fac_srtnotificatie_url"),
            "typ": "varchar",
            "readonly": true
        },
        "group": {
            "dbs": "fac_srtnotificatie_groep",
            "label": L("fac_srtnotificatie_groep"),
            "typ": "varchar",
            "readonly": true
        },
        "notification_mode": {
            "dbs": "fac_srtnotificatie_mode",
            "label": "",
            "typ": "number",
            "bits": [
                {
                    "name": "",
                    "label": "",
                    "typ": "check",
                    "mask": 31,
                    "radios": [
                        {
                            "name": "portal_statusinfo",
                            "label": L("fac_srtnotificatie_portal_statusinfo"),
                            "mask": 1
                        },
                        {
                            "name": "send_email",
                            "label": L("fac_srtnotificatie_send_email"),
                            "mask": 2
                        },
                        {
                            "name": "send_sms",
                            "label": L("fac_srtnotificatie_send_sms"),
                            "mask": 4
                        },
                        {
                            "name": "portal_popup",
                            "label": L("fac_srtnotificatie_portal_popup"),
                            "mask": 8
                        },
                        {
                            "name": "stylesheet",
                            "label": L("fac_srtnotificatie_stylesheet"),
                            "mask": 16
                        }
                    ]
                }
            ]
        },
        "attachment": {
            "dbs": "fac_srtnotificatie_srtkm_key",
            "label": L("fac_srtnotificatie_srtkenmerk"),
            "typ": "key",
            "foreign": {
                "tbl": "(" + xxx_srtkenmerk_sql() + ")",
                "key": "srtkenmerk_key",
                "desc": "omschrijving"
            },
            "defaultvalue": null
        },
        "usermode": {
            "dbs": "fac_srtnotificatie_usermode",
            "label": L("fac_srtnotificatie_usermode"),
            "typ": "check0",
            "listfunction": fnshowusermode
        },
        "mail_building": {
            "dbs": "fac_srtnotificatie_gebouw",
            "label": L("fac_srtnotificatie_gebouw"),
            "typ": "check0",
            "listfunction": fnshowmailbuilding
        },
        "delay": {
            "dbs": "fac_srtnotificatie_delay",
            "label": L("fac_srtnotificatie_delay"),
            "typ": "number",
            "required": true,
            "defaultvalue": "0"
        },
        "mode_status": {
            "dbs": "notimode.status",
            "label": L("fac_srtnotificatie_status"),
            "typ": "varchar",
            "hidden_fld": true
        },
        "mode_email": {
            "dbs": "notimode.email",
            "label": L("fac_srtnotificatie_mail"),
            "typ": "varchar",
            "hidden_fld": true
        },
        "mode_sms": {
            "dbs": "notimode.sms",
            "label": L("fac_srtnotificatie_sms"),
            "typ": "varchar",
            "hidden_fld": true
        },
        "mode_alert": {
            "dbs": "notimode.portal",
            "label": L("fac_srtnotificatie_alert"),
            "typ": "varchar",
            "hidden_fld": true
        }
    };

    if (!S("puo_smsaccount"))
        this.fields.notification_mode.bits[0].radios.splice(2, 1);

    var ext_sql = "(SELECT fac_srtnotificatie_key"
                + "      , DECODE(BITAND(fac_srtnotificatie_mode,  1),  1,"+ safe.qL("lcl_on") +", '') status"
                + "      , DECODE(BITAND(fac_srtnotificatie_mode,  2),  2,"+ safe.qL("lcl_on") +", '') email"
                + "      , DECODE(BITAND(fac_srtnotificatie_mode,  4),  4,"+ safe.qL("lcl_on") +", '') sms"
                + "      , DECODE(BITAND(fac_srtnotificatie_mode,  8),  8,"+ safe.qL("lcl_on") +", '') portal"
                + "   FROM fac_srtnotificatie) notimode ";

    var get_param = {
            "GET": {
                "tables": [ext_sql],
                "wheres": ["fac_srtnotificatie.fac_srtnotificatie_key = notimode.fac_srtnotificatie_key"]
            }
        };

    function xxx_srtkenmerk_sql()
    {
        var sql = "(select sk.mld_srtkenmerk_key srtkenmerk_key"
                + "      , sk.mld_srtkenmerk_omschrijving || ' (' || sk.mld_srtkenmerk_key || ')' omschrijving"
                + "   from mld_srtkenmerk sk"
                + "  where sk.mld_srtkenmerk_verwijder is null"
                + "    and sk.mld_srtkenmerk_kenmerktype in ('M', 'F')" // -- 'E', 'X')
                + "  order by sk.mld_srtkenmerk_omschrijving)"
        return sql;
    }

    if (cust==1 && user.has("WEB_FACTAB"))
    {
        // Een gebruiker met WEB_FACTAB-rechten mag meer velden wijzigen...
        this.fields.name.label = "CUST";
        this.fields.name.readonly = false;
        this.fields.name.required = true;
        if (mode == "edit")
            this.fields.name.len = 2;  // Maximale lengte van de code = 6, maar er wordt CUST voor gezet (zie: hook_pre_post).
        this.fields.xmlnode.readonly = false;
        this.fields.xmlnode.required = true;
        this.fields.description.required = true;
        this.fields.notification_url.readonly = false;
        this.fields.group.readonly = false;

        get_param.GET.wheres.push("UPPER(SUBSTR(fac_srtnotificatie_code, 1, 4)) = 'CUST' ");
        this.REST_POST   = generic_REST_POST(this);    // ... ook toevoegen...
        this.REST_DELETE = generic_REST_DELETE(this);  // en verwijderen.
    }

    this.REST_GET    = generic_REST_GET(this, get_param);
    this.REST_PUT    = generic_REST_PUT(this);


    this.hook_pre_edit = function(obj, fld)
    {
        // Voorlopig mag alleen voor meldingen en opdrachten het vinkje 'E-mail sturen naar gebouw'
        // gezet worden
        if (iscust == 1 || !inArray(obj.name.substring(0,3), ["MLD", "ORD"]))
            fld.mail_building.readonly = true;

        if (inArray(obj.name, ["MLDAFM", "MLDAFR"]))
        {
            fld.attachment.foreign.tbl = xxx_srtkenmerk_sql();
        }
        else
        {
            delete fld.attachment;
        }

        if (iscust == 1 && obj.id != -1)
            obj.name = obj.name.substring(4,7); // De "CUST" ervoor weg halen.
    }

    this.hook_pre_put = function(params, obj, key)
    {
        if (iscust == 1)
            obj.name = "CUST" + obj.name.toUpperCase(); // De "CUST" er weer voor zetten.
    }

    this.hook_pre_post = function(params, obj)
    {
        // Wordt alleen gebruikt bij toevoegen, dus met url-param &cust=1.
        obj.name = "CUST" + obj.name.toUpperCase(); // Dus altijd hoofdletters, en beginnen met "CUST".
    }

    function fnshowusermode(oRs)
    {
        return (oRs.Fields("usermode").Value == 1 ? L("lcl_Yes") : "");
    }

    function fnshowmailbuilding(oRs)
    {
        return (oRs.Fields("mail_building").Value == 1 ? L("lcl_Yes") : "");
    }
}
%>