Skip to Content
Menu
This question has been flagged
3 Replies
3760 Views

I want to hide the two validate buttons of the stock picking based on the following:
- I created the fields check_docs which is a boolean field that is enabled when check_docs_id (invisible) is true when finding 3 types of operations that I previously defined. check_docs is a reminder not to forget certain documentation. This works fine.
What does not work is that when one of these 3 types of operations is chosen, if check_docs is false, no "Validate" button for picking should be enabled. When check_docs is true, it is only possible to enable the "validate" buttons. But that this rule does not interfere with the validation of the picking of other types of operations other than these 3.

Python----

class CustomStockPicking(models.Model):
    _inherit = 'stock.picking'

    check_docs_id = fields.Boolean(default=False, compute='_check_docs')
    check_docs = fields.Boolean('Documentación', default=False)
    check_validate = fields.Boolean(compute='_check_validate', default=False)

    @api.depends('picking_type_id')
    def _check_docs(self):
        for picking in self:
            if picking.picking_type_id.id == 25 or picking.picking_type_id.id == 26 or picking.picking_type_id.id == 27:
                picking.check_docs_id = True
            else:
                picking.check_docs_id = False

    @api.depends('show_validate')
    def _check_validate(self):
        for picking in self:
            if picking.check_docs_id:
                if picking.check_docs and picking.show_validate:
                    picking.check_validate = True
                elif picking.check_docs and not picking.show_validate:
                    picking.check_validate = False
                elif not picking.check_docs and picking.show_validate:
                    picking.check_validate = True
                else:
                    picking.check_validate = False
            else:
                picking.check_validate = False
               
           
xml----

<?xml version="1.0" encoding="UTF-8"?>
<odoo>
    <data>
        <record id="custom_stock_picking_form" model="ir.ui.view">
            <field name="name">custom.stock.picking.form</field>
            <field name="model">stock.picking</field>
            <field name="inherit_id" ref="stock.view_picking_form"/>
            <field name="arch" type="xml">
                <xpath expr="//field[@name='origin']" position="after">
                    <field name="check_docs_id" invisible="1"/>
                    <field name="check_docs" attrs="{'invisible':[('check_docs_id', '!=', True)]}"/>
                    <field name="check_validate" invisible="1"/>
                </xpath>
                <xpath expr="//button[@name='button_validate']" position="attributes">
                    <attribute name="attrs">
                        {'invisible': ['|','|', ('state', 'in', ('waiting','confirmed')),
                        ('show_validate', '=', False),('check_validate', '=', False)]}
                    </attribute>
                    <attribute name="attrs">
                        {'invisible': ['|','|', ('state', 'not in', ('waiting', 'confirmed')),
                        ('show_validate', '=', False),('check_validate', '=', False)]}
                    </attribute>
                </xpath>
            </field>
        </record>
    </data>
</odoo>

Avatar
Discard
Best Answer

Hello Frank. Regarding with your problem, I use '&amp' (AND) operator for this condition: if state or show_validate (any of the 2 is true return True) and check_validate = false then return True(invisible) else return False(visible).

To select the buttons, you need to use indexing.

[ 1 ] for the first button

<xpath expr="//button[@name='button_validate'][1]" position="replace">

[ 2 ] for the second button 

<xpath expr="//button[@name='button_validate'][2]" position="replace">

Code:

<?xml version="1.0" encoding="UTF-8"?>
<odoo>
    <data>
        <record id="custom_stock_picking_form" model="ir.ui.view">
            <field name="name">custom.stock.picking.form</field>
            <field name="model">stock.picking</field>
            <field name="inherit_id" ref="stock.view_picking_form"/>
            <field name="arch" type="xml">
                <xpath expr="//field[@name='origin']" position="after">
                    <field name="check_docs_id" invisible="1"/>
                    <field name="check_docs" attrs="{'invisible':[('check_docs_id', '!=', True)]}"/>
                    <field name="check_validate" invisible="1"/>
                </xpath>
                <xpath expr="//button[@name='button_validate'][1]" position="replace">
                    <button name="button_validate" 
                        attrs="{'invisible': ['|', '&amp',('state', 'in', ('waiting','confirmed')), ('show_validate', '=', False),('check_validate', '=', False)]}" 
                        string="Validate" 
                        type="object" 
                        class="oe_highlight" 
                        groups="stock.group_stock_user"/>
                </xpath>
        
                <xpath expr="//button[@name='button_validate'][2]" position="replace">
                    <button name="button_validate"
                        attrs="{'invisible': ['|','&amp',('state', 'not in', ('waiting', 'confirmed')), ('show_validate', '=', False),('check_validate', '=', False)]}"
                        string="Validate"
                        type="object"
                        groups="stock.group_stock_user"
                        class="o_btn_validate"/>
                 </xpath>
            </field>
        </record>
    </data>
</odoo>


Please vote if it helps.

Avatar
Discard
Author

Hello Kev

I had the xpaths like this because in odoo's default picking 2 "Validate" buttons appear, so the visibility control has to affect those two buttons.

<button name="button_validate" attrs="{'invisible': ['|', ('state', 'in', ('waiting','confirmed')), ('show_validate', '=', False)]}" string="Validate" type="object" class="oe_highlight" groups="stock.group_stock_user"/>

<button name="button_validate" attrs="{'invisible': ['|', ('state', 'not in', ('waiting', 'confirmed')), ('show_validate', '=', False)]}" string="Validate" type="object" groups="stock.group_stock_user" class="o_btn_validate"/>

Thank you very much for the code sent, I applied it in my addon, but it is still not controlling the visibility of those buttons well. Where I have more problems is in the python code for that double check to the validate buttons. If you could help me with that that would be great.

Note: The syntax of the AND operator in attrs of the xml is no longer '&' but '&amp;'

Hello Frank, please check my updated answer.

Related Posts Replies Views Activity
0
May 23
1473
1
Nov 24
16357
0
Aug 22
10074
2
Jun 21
1828
1
Aug 20
3734