This question has been flagged

Hello, 


I have implemented this code which is being called for the other states but not cancel. I am trying to archive the order when the stock.picking record when it gets cancelled. 


I have tried the code below, but the onchange function is not being called. I can probably do this through an automated action, but if someone could tell me where I am going wrong that would be great. 


class archive_inventory(models.Model):

    _inherit = "stock.picking"


    active = fields.Boolean("active", default=True)


    @api.onchange("state")

    def archive_on_cancel(self):

        for record in self:

            if record.state == "cancel":

                record.active = False

            else:

                record.active = True


I have looked at the function which is called on the model when the cancel button is pressed and it does write the value cancel to the state field. 


def _action_cancel(self):

        if any(move.state == 'done' and not move.scrapped for move in self):

            raise UserError(_('You cannot cancel a stock move that has been set to \'Done\'.'))

        moves_to_cancel = self.filtered(lambda m: m.state != 'cancel')

        # self cannot contain moves that are either cancelled or done, therefore we can safely

        # unlink all associated move_line_ids

        moves_to_cancel._do_unreserve()


        for move in moves_to_cancel:

            siblings_states = (move.move_dest_ids.mapped('move_orig_ids') - move).mapped('state')

            if move.propagate_cancel:

                # only cancel the next move if all my siblings are also cancelled

                if all(state == 'cancel' for state in siblings_states):

                    move.move_dest_ids.filtered(lambda m: m.state != 'done')._action_cancel()

            else:

                if all(state in ('done', 'cancel') for state in siblings_states):

                    move.move_dest_ids.write({'procure_method': 'make_to_stock'})

                    move.move_dest_ids.write({'move_orig_ids': [(3, move.id, 0)]})

        self.write({'state': 'cancel', 'move_orig_ids': [(5, 0, 0)]})

        return True


So I am not sure what I am doing wrong.


Thanks, 

Avatar
Discard
Best Answer

hey Jack, were you able to solve your issue? is the `archive_on_cancel` method actually called?

Avatar
Discard