简体   繁体   中英

How hide button “Create invoice” but based in condition ? (Odoo 11)

there really I am blocked, I want to hide the button "Create invoice" but based on a condition, my condition is if order line has services, the button is hidden. I created a field and a function but in the end there's always an error that the field doesn't exist in the model, here's my code :

Error :

Field 'hide_invoice' used in attributes must be present in view but is missing

My field and function (Python) :

from odoo import api, fields, models,_


class SaleOrder(models.Model):
    _inherit = 'sale.order'

    hide_invoice = fields.Boolean(compute="_hide_button_invoice", string="",)


    @api.multi
    @api.depends('tasks_count')
    def _hide_button_invoice(self):
        for order in self:
            if order.tasks_count > 0:
                order.hide_invoice = True
            elif order.tasks_count == 0:
                order.hide_invoice = False 

My XML (I see on the form that it works) :

<odoo>
<record id="button_invoice_view_form" model="ir.ui.view">
        <field name="name">sale.order.button.create.form</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='partner_id']" position="before">
                <field name ="hide_invoice"/>
            </xpath>
        </field>
</record>
</odoo>

The button then i want to hide it :

<record id="sale_order_view_form" model="ir.ui.view">
    <field name="name">sale.order.form</field>
    <field name="model">sale.order</field>
    <field name="inherit_id" ref="sale.view_order_form"/>
    <field name="arch" type="xml">
        <xpath expr="//group[@name='sale_pay']/field[@name='invoice_status']" position="attributes">
            <attribute name="invisible" eval="False"/>
        </xpath>
        <xpath expr="//button[@name='action_quotation_send']" position="before">
            <button name="%(sale.action_view_sale_advance_payment_inv)d" string="Create Invoice"
                type="action" class="btn-primary"
                attrs="{'invisible': [('invoice_status', '!=', 'to invoice')]}"/>
            <button name="%(sale.action_view_sale_advance_payment_inv)d" string="Create Invoice"
                type="action" context="{'default_advance_payment_method': 'percentage'}"
                attrs="{'invisible': ['|','|',('hide_invoice', '=', True),('invoice_status', '!=', 'no'), ('state', '!=', 'sale')]}"/>
        </xpath>
    </field>
</record>

You have to display field on header level to handle these.

Like do following code before your button declared.

<field name='hide_invoice' invisible='1'/>

And remove it before partner_id field.

EDIT

Can you try with following xml code:

<record id="sale_order_view_form" model="ir.ui.view">
    <field name="name">sale.order.form</field>
    <field name="model">sale.order</field>
    <field name="inherit_id" ref="sale.view_order_form"/>
    <field name="arch" type="xml">
        <xpath expr="//group[@name='sale_pay']/field[@name='invoice_status']" position="attributes">
            <attribute name="invisible" eval="False"/>
        </xpath>
        <xpath expr="//button[@name='action_quotation_send']" position="before">
            <button name="%(sale.action_view_sale_advance_payment_inv)d" string="Create Invoice"
                type="action" class="btn-primary"
                attrs="{'invisible': [('invoice_status', '!=', 'to invoice')]}"/>
            <field name="hide_invoice" invisible="1"/>
            <button name="%(sale.action_view_sale_advance_payment_inv)d" string="Create Invoice"
                type="action" context="{'default_advance_payment_method': 'percentage'}"
                attrs="{'invisible': ['|','|',('hide_invoice', '=', True),('invoice_status', '!=', 'no'), ('state', '!=', 'sale')]}"/>
        </xpath>
    </field>
</record>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM