按位置提供的数量(再次)
Qty available by location (again)
我知道之前有人讨论过这个问题,但它对我来说并不适用。我如何才能获得我的产品现在所在位置或仓库的可用数量。
(大部分答案都是旧的 API,而这个对我来说并不适用)
class ProductProduct(models.Model):
_inherit = 'product.template'
available_qty = fields.Integer(
string='Qty By Loc',
compute='product_qty_location_check',
)
def product_qty_location_check(self):
if self:
self.available_qty = self.with_context({'location' : self.source_location.id}).qty_available
AttributeError: 'product.template' object has no attribute 'source_location'
要按位置获取数量,您需要在 中使用 product_id 搜索 位置 stock.quant
在您的计算函数中使用以下示例:
quant_sr = self.env["stock.quant"].search([('location_id','=',self.source_location.id),('product_id','=',self.product_id.id)])
qty = 0.0
for quant in quant_sr:
qty += quant.qty
print qty
首先你必须找出你所在的 and/or 仓库。有足够的可能性这样做:
- 使用具有字段的向导
- 通过参考搜索,例如
self.env.ref('my_module.my_location')
- 使用其他来源
现在您可以在 product.product
的 _product_available()
上使用它们了。只需在一个或多个产品(记录集)上调用该方法并评估该方法的结果。要过滤位置 and/or 个仓库,请使用上下文。例如:
# get a recordset of all products
products = self.env['product.product'].search([])
# get a special location (my_location)
location = self.env.ref('my_module.my_location')
quantities = products.with_context(location=location.id)._product_available()
# print the quantities
for product_id, quantity_dict in quantities.iteritems():
print product_id
print quantity_dict
with_context(warehouse=warehouse.id)
甚至 ID 或名称列表也是如此:with_context(location=[1,2,3])
、with_context(location="My Location")
我知道之前有人讨论过这个问题,但它对我来说并不适用。我如何才能获得我的产品现在所在位置或仓库的可用数量。
(大部分答案都是旧的 API,而这个对我来说并不适用)
class ProductProduct(models.Model):
_inherit = 'product.template'
available_qty = fields.Integer(
string='Qty By Loc',
compute='product_qty_location_check',
)
def product_qty_location_check(self):
if self:
self.available_qty = self.with_context({'location' : self.source_location.id}).qty_available
AttributeError: 'product.template' object has no attribute 'source_location'
要按位置获取数量,您需要在 中使用 product_id 搜索 位置 stock.quant
在您的计算函数中使用以下示例:
quant_sr = self.env["stock.quant"].search([('location_id','=',self.source_location.id),('product_id','=',self.product_id.id)])
qty = 0.0
for quant in quant_sr:
qty += quant.qty
print qty
首先你必须找出你所在的 and/or 仓库。有足够的可能性这样做:
- 使用具有字段的向导
- 通过参考搜索,例如
self.env.ref('my_module.my_location')
- 使用其他来源
现在您可以在 product.product
的 _product_available()
上使用它们了。只需在一个或多个产品(记录集)上调用该方法并评估该方法的结果。要过滤位置 and/or 个仓库,请使用上下文。例如:
# get a recordset of all products
products = self.env['product.product'].search([])
# get a special location (my_location)
location = self.env.ref('my_module.my_location')
quantities = products.with_context(location=location.id)._product_available()
# print the quantities
for product_id, quantity_dict in quantities.iteritems():
print product_id
print quantity_dict
with_context(warehouse=warehouse.id)
甚至 ID 或名称列表也是如此:with_context(location=[1,2,3])
、with_context(location="My Location")