Setting a button active/readonly on inventory moves for a specific state

Hi,

We allow users to do a “pick” on inventory moves on customer shipment (custom code). So, we add a button ‘pick’ on the model ‘stock.move’. Those button should be only active/visible when move is in state ‘assigned’ and move is an inventory move.

So i define a function field to test if move is an inventory move out:

class Move(metaclass=PoolMeta):
    __name__ = 'stock.move'
...
    is_inventory_move_out = fields.Function(
        fields.Boolean("Is Inventory Move Out"),
        'on_change_with_is_inventory_move_out')

    @classmethod
    def __setup__(cls):
        super().__setup__()
        cls._buttons.update({
                'pick': {
                    'readonly': (~Eval('state').in_(['assigned'])
                        | ~Eval('is_inventory_move_out', False)),
                    'depends': ['state', 'is_inventory_move_out'],
                    },
                })

    @fields.depends('shipment')
    def on_change_with_is_inventory_move_out(self, name=None):
        if self.shipment:
            if getattr(self.shipment, 'warehouse_output', None):
                return self.shipment.warehouse_output == self.to_location
        return False

    @classmethod
    @ModelView.button_action('preciball.wizard_pick')
    def pick(cls, moves):
        pass

Everything seems ok except sometimes button stays ‘readonly’ (Pyson conditions are ok) and user has to close-reopen the customer shipment tab to get the button active.

Any idea why sometimes ‘readonly’ state of button is incorrect ?

Do I need to use another method to test if the current move is an inventory move out ? (I guess this is the function field which makes the condition sometimes wrong).

I guess the button is readonly once you modified something from the parent record.
This is the spected behaviour as you need to save first the parent record before triggering the button on some of the moves.

The inventory moves is a One2Many that is not using the shipment as field so the on_change is not called if something change on the parent and the warehouse_output is not sent (because it is not a _parent_shipment.warehouse_output).

So I guess it will be more stable if you could use another criteria than the shipment.

What criteria could I use to distinct different moves ?

It’s the inverse, the button is readonly by default and become active once the move is assigned. So the major condition to change the state of the button is the state of the move. My function field allows me to identify what kind of move to prevent to have an active button everywhere moves are displayed. Once the move is linked to the shipment, shipment field stays unchanged.

Why not just the type of locations?

If “From Location” type is “Storage” then it’s always an inventory move ?

The “To Location” must also be a storage.

1 Like

And in my case “To Location” must be the “Output Zone”, so self.to_location == self.to_location.warehouse.output_zone

Is it really necessary? I think you can pick also for internal moves.

Indeed. Thanks. :slight_smile: