Juan Salcedo
Juan Salcedo

Reputation: 1668

How to hide the edit button form based on state field of invoice Odoo v8?

I want to hide the edit button when a invoice' state is paid, just like the image below.

enter image description here

And I was inheriting the invoice_form and add the corresponding attribute.

<record id="invoice_form_inherit" model="ir.ui.view">
    <field name="name">invoice.form.inherit</field>
    <field name="model">account.invoice</field>
    <field name="inherit_id" ref="account.invoice_form"/>
    <field name="arch" type="xml">
        <xpath expt='//form[@string="Invoice"]' possition='attributes'>

            <!-- Frist intent : nothing happened -->
            <attribute name="edit" attrs="{'invisible:[('state','=','paid')]'}"/>

            <!-- Second intent : edit, always hide -->
            <attribute name="edit" attrs="{'invisible:[('state','=','paid')]'}">false</field>

            <!-- Thirds intent : edit, never hide -->
            <attribute name="edit" attrs="{'invisible:[('state','=','paid')]'}">true</field>
    </field>

Please help me, what it's wrong? Thanks!!

EDIT

Following the recomendations of @Sathiyan, I created a /security/invoice_security.xml file and add in my __opnenerp__.py, inside I added this lines:

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data noupdate="1">
        <record id="rule_no_edit_invoice_paid" model="ir.rule">
            <field name="name">rule.no.edit.invoice.paid</field>
            <field name="model_id" ref="account.model_account_invoice"/>
            <field name="group" eval="[(4,ref('account.group_account_invoice'))]"/>
            <field name="domain_force">[('state','=','paid')]</field>
            <field eval="1" name="perm_read"/>
            <!--
            <field eval="0" name="perm_create"/>
            <field eval="0" name="perm_write"/>
            <field eval="0" name="perm_unlink"/>
            -->
        </record>
    </data>
</openerp>

As I put noupdate="1" I created a new database and installed it there, but nothing is happened! Can you tell me what I doing wrong? please.

Upvotes: 4

Views: 6768

Answers (3)

Charif DZ
Charif DZ

Reputation: 14721

You can do this by overriding load_record of FormView widget:

openerp.module_name = function(instance, local) {
    var instance = openerp;
    var FormView = instance.web.FormView;

    // override load_record
    FormView.include({
        load_record: function(record) {
        // disable only for purchase.order
        if (record){
            // allow this behavior only for purchase.order  
            if (this.model == 'purchase.order' & _.contains(['done', 'cancel'], record.state)){
                    $('button.oe_form_button_edit').hide()
                }else {
                    $('button.oe_form_button_edit').show()
                }
        }
        // call super
        return this._super(record);
        }
    });
}

Check this app if you looking for full code:

Disable edit button for paid invoice

Upvotes: 0

Soaad
Soaad

Reputation: 112

Try replacing fields through inheritance and adding attrs to it. Using attrs, you can set the field invisible when state is paid like this,

        <field name="edit" attrs="{'invisible':[('state', '=', 'paid')]}"/>

Upvotes: 0

Sathiyan S
Sathiyan S

Reputation: 49

Add a Record Rule for account.invoice object with Read permission alone. And a domain filter as [('state','=','paid')].

Upvotes: 0

Related Questions