覆盖 api.onchange 方法
Override api.onchange method
我正在尝试覆盖 sale.order.line class 中的 _onchange_product_id_check_availability 方法以更改可用性计算。
但是odoo忽略了重写的方法并仍然使用原来的实现,只有当我重命名方法时才会调用我的实现。
这是原来的方法:
@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 {}
这就是我要实现的:
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
方法 _onchange_product_id_check_availability
在 sale_stock
模块中定义。
如果您想替换它的行为,请不要忘记在 __manifest__.py
的 depends
数组中添加 sale_stock
模块。否则您的方法将不会被调用,源代码将一如既往地执行。
'depends': [
'sale_stock',
...
],
我正在尝试覆盖 sale.order.line class 中的 _onchange_product_id_check_availability 方法以更改可用性计算。
但是odoo忽略了重写的方法并仍然使用原来的实现,只有当我重命名方法时才会调用我的实现。
这是原来的方法:
@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 {}
这就是我要实现的:
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
方法 _onchange_product_id_check_availability
在 sale_stock
模块中定义。
如果您想替换它的行为,请不要忘记在 __manifest__.py
的 depends
数组中添加 sale_stock
模块。否则您的方法将不会被调用,源代码将一如既往地执行。
'depends': [
'sale_stock',
...
],