简体   繁体   English

覆盖api.onchange方法

[英]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. 我试图从sale.order.line类覆盖_onchange_product_id_check_availability方法,以更改可用性演算。

But odoo is ignoring the overriden method and still uses the original implementation, my implementation is called only if I rename the method. 但是odoo忽略了重写的方法,仍然使用原始的实现,仅当我重命名该方法时,我的实现才会被调用。

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

The method _onchange_product_id_check_availability is defined in sale_stock module. 方法_onchange_product_id_check_availabilitysale_stock模块中定义。

If you want to replace its behaviour don't forget to add the sale_stock module in the depends array of your __manifest__.py . 如果您想更换自己的行为,不要忘记添加sale_stock模块中的depends你的阵列__manifest__.py Otherwise your method won't be called and the source code will be executed as always. 否则,将不会调用您的方法,并且源代码将一如既往地执行。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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