Rafael Carrillo
Rafael Carrillo

Reputation: 2883

Override api.onchange method

I'm trying to override the _onchange_product_id_check_availability method from the sale.order.line class in order to change the availavility calculus.

But odoo is ignoring the overriden method and still uses the original implementation, my implementation is called only if I rename the method.

this is the original method:

@api.onchange('product_uom_qty', 'product_uom', 'route_id')
    def _onchange_product_id_check_availability(self):
        if not self.product_id or not self.product_uom_qty or not self.product_uom:
            self.product_packaging = False
            return {}
        if self.product_id.type == 'product':
            precision = self.env['decimal.precision'].precision_get('Product Unit of Measure')
            product = self.product_id.with_context(warehouse=self.order_id.warehouse_id.id)
            product_qty = self.product_uom._compute_quantity(self.product_uom_qty, self.product_id.uom_id)
            if float_compare(product.virtual_available, product_qty, precision_digits=precision) == -1:
                is_available = self._check_routing()
                if not is_available:
                    message =  _('You plan to sell %s %s but you only have %s %s available in %s warehouse.') % \
                            (self.product_uom_qty, self.product_uom.name, product.virtual_available, product.uom_id.name, self.order_id.warehouse_id.name)
                    # We check if some products are available in other warehouses.
                    if float_compare(product.virtual_available, self.product_id.virtual_available, precision_digits=precision) == -1:
                        message += _('\nThere are %s %s available accross all warehouses.') % \
                                (self.product_id.virtual_available, product.uom_id.name)

                    warning_mess = {
                        'title': _('Not enough inventory!'),
                        'message' : message
                    }
                    return {'warning': warning_mess}
        return {}

and this is what I'm trying to implement:

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


    @api.onchange('product_uom_qty', 'product_uom', 'route_id')
    def _onchange_product_id_check_availability(self):
        if not self.product_id or not self.product_uom_qty or not self.product_uom:
            self.product_packaging = False
            return {}
        if self.product_id.type == 'product':
            precision = self.env['decimal.precision'].precision_get('Product Unit of Measure')
            product = self.product_id.with_context(warehouse=self.order_id.warehouse_id.id)
            product_qty = self._calculate_availabilty()
            if float_compare(product.virtual_available, product_qty, precision_digits=precision) == -1:
                is_available = self._check_routing()
                if not is_available:
                    message =  _('You plan to sell %s %s but you only have %s %s available in %s warehouse.') % \
                            (product_qty, self.product_uom.name, product.virtual_available, product.uom_id.name, self.order_id.warehouse_id.name)
                    # We check if some products are available in other warehouses.
                    if float_compare(product.virtual_available, self.product_id.virtual_available, precision_digits=precision) == -1:
                        message += _('\nThere are %s %s available accross all warehouses.') % \
                                (self.product_id.virtual_available, product.uom_id.name)

                    warning_mess = {
                        'title': _('Not enough inventory!'),
                        'message' : message
                    }
                    return {'warning': warning_mess}
        return {}


    def _calculate_availabilty(self):
        pdb.set_trace()
        if self.product_uom.name == "Piece(s)":
            return self.product_uom_qty
        if self.product_uom.name == "Kilogram":
            if(self.product_id.theoric_weight == 0 or self.product_uom_qty==0):
                return self.product_uom_qty
            return self.product_uom_qty/self.product_id.theoric_weight
        pass

Upvotes: 1

Views: 1210

Answers (1)

forvas
forvas

Reputation: 10189

The method _onchange_product_id_check_availability is defined in sale_stock module.

If you want to replace its behaviour don't forget to add the sale_stock module in the depends array of your __manifest__.py. Otherwise your method won't be called and the source code will be executed as always.

'depends': [
    'sale_stock',
    ...
],

Upvotes: 2

Related Questions