如何在odoo中无法编辑树状视图和表单视图中的特定记录? [英] How to make specific records in tree view and form view not editable in odoo?

查看:618
本文介绍了如何在odoo中无法编辑树状视图和表单视图中的特定记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有状态"(选择)属性的实体帐单(西班牙事实).是否可以将(state ='pendiente'或state ='pagad')中的账单"中的所有记录设为只读?这意味着,当用户单击树视图上的特定账单时,他无法编辑任何账单"字段.这是我的代码

I have an entity bill (spanish factura) with an "state" (selection) attribute. Is it possible to make all records in "Bill" with (state='pendiente' or state='pagad') readonly ? That means that when the user clicks on an specific bill on the tree view, he cannot edit any of the "bill" fields. This is my code

class PlanificacionFactura(models.Model):
    _name = 'utepda_planificacion.factura'
    _rec_name = 'numero'
    _description = 'Factura'
    _inherit = ['mail.thread', 'mail.activity.mixin']

    fecha = fields.Date(string='Fecha')
    monto_total = fields.Monetary(string='Monto a pagar', currency_field='currency_id')
    pago_acumulado = fields.Monetary(compute='_compute_pago_acumulado', string='Pago Acumulado' ,currency_field='currency_id')
    currency_id = fields.Many2one('res.currency', string='Moneda', required=True, domain=[('name', 'in', ('USD', 'DOP'))] , default=lambda self: self.env.ref("base.DOP"))
    pago_pendiente = fields.Monetary(compute='_compute_pago_pendiente', string='Pendiente de pago', currency_field='currency_id')

    state = fields.Selection([
        ('creado', 'Creada'),
        ('pendiente','Pagada parcialmente'),
        ('pagado','Pagada')
    ], string='Estado', default='pagado', compute='_compute_state' )

    @api.depends('pago_acumulado','monto_total')
    def _compute_state(self):
        for record in self:
            if record.pago_acumulado > 0 and record.pago_acumulado < record.monto_total:
                record.state='pendiente'
            elif record.pago_acumulado == record.monto_total:
                record.state = 'pagado'
            else:
                record.state='creado'
    @api.model
    def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
        res = super(PlanificacionFactura, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu)
        if view_type == 'form':
            doc = etree.XML(res['arch'])
            for node in doc.xpath("//field[@name='fecha']"):
                node.set('options', "{'datepicker': {'maxDate': '%s'}}" % fields.Date.today().strftime(DEFAULT_SERVER_DATE_FORMAT))

            if 'params' in self.env.context and 'id' in self.env.context['params']:
                values = self.search_read([('id', '=', self.env.context['params']['id'])], fields=['state'])
                if values[0]['state'] == 'pagado':
                    # Disable edit mode on form view based on `state` field
                    for node in doc.xpath("//form"):
                        node.set('edit', '0')

            res['arch'] = etree.tostring(doc)
        return res

我在状态栏中显示了状态

I made the state visible i the statusbar

    <header>
        <field name="state" widget="statusbar"/>
    </header>

推荐答案

在表单加载后,您可以覆盖FormRenderer以检查state字段.

You can override FormRenderer to check for the state field just after the form has loaded.

您可以将条件指定为域,并使用 web.Domain 类,以使用当前记录字段值进行计算.

You can specify the condition as a domain and use web.Domain class to compute it using current record field values.

var FormRenderer = require('web.FormRenderer');
var Domain = require("web.Domain");
FormRenderer.include({

    autofocus: function () {
        this.show_hide_edit_button();
        return this._super();
    },

    show_hide_edit_button : function () {
        if( 'disable_edit_mode' in this.arch.attrs && this.arch.attrs.disable_edit_mode) {
            var domain = Domain.prototype.stringToArray(this.arch.attrs.disable_edit_mode)
            var button = $(".o_form_button_edit");
            if (button) {
                var hide_edit = new Domain(domain).compute(this.state.data);
                // If you just need to disable edit button
                // button.prop('disabled', hide_edit);

                // button is shown if the parameter is true and hidden if it is false
                button.toggle(!hide_edit);
            }
        }
    },

});

使用disable_edit_mode属性设置您的域条件:

Use disable_edit_mode attribute to set you domain conditions:

<form disable_edit_mode="[('state', '=', 'pagado')]">  

请参阅添加资产捆绑包中的文件以将上述代码添加到您的模块中.

Refer to Adding files in an asset bundle to add the above code to your module.

这篇关于如何在odoo中无法编辑树状视图和表单视图中的特定记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆