如何获取产品可用数量(Odoo v8 和 v9)

How to get products available quantity (Odoo v8 and v9)

我需要从 odoo 库存中获取产品的可用数量。

我有几个模型 stock_quant, stock_move, stock_location.

我想要实现的是两件事:

  1. 产品总可用数量
  2. 基于位置的产品可用数量

有人可以指导我吗?

库存相关字段在产品(功能字段)中定义,您可以直接从产品中获取所有仓库/位置或单个位置/仓库的库存。

示例:

所有仓库/位置

product = self.env['product.product'].browse(PRODUCT_ID)
available_qty = product.qty_available

对于个别位置/仓库(WAREHOUSE_ID/LOCATION_ID应替换为实际id)

product = self.env['product.product'].browse(PRODUCT_ID)
available_qty = product.with_context({'warehouse' : WAREHOUSE_ID}).qty_available

available_qty = product.with_context({'location' : LOCATION_ID}).qty_available

其他字段也有

Forecasted Stock => virtual_available
Incoming Stock => incoming
Outgoing Stock => outgoing

您可以通过类似的方式访问所有这些字段。如果您不在上下文中传递任何仓库/位置,那么它将 returns 所有仓库的库存加在一起。

For more details you may refer product.py in stock module.

解法:

@api.onchange('product_id','source_location') 
def product_qty_location_check(self): 
    if self.product_id and self.source_location: 
        product = self.product_id
        available_qty = product.with_context({'location' : self.source_location.id}).qty_‌​available 
        print available_qty

对于 Odoo 8,9 和 10:

with
  uitstock as (
    select
      t.name product, sum(product_qty) sumout, m.product_id, m.product_uom 
    from stock_move m 
      left join product_product p on m.product_id = p.id 
      left join product_template t on p.product_tmpl_id = t.id
    where
      m.state like 'done' 
      and m.location_id in (select id from stock_location where complete_name like '%Stock%') 
      and m.location_dest_id not in (select id from stock_location where complete_name like '%Stock%') 
    group by product_id,product_uom, t.name order by t.name asc
  ),
  instock as (
    select
      t.list_price purchaseprice, t.name product, sum(product_qty) sumin, m.product_id, m.product_uom
    from stock_move m
      left join product_product p on m.product_id = p.id
      left join product_template t on p.product_tmpl_id = t.id
    where 
      m.state like 'done' and m.location_id not in (select id from stock_location where complete_name like '%Stock%')
      and m.location_dest_id in (select id from stock_location where complete_name like '%Stock%')
    group by product_id,product_uom, t.name, t.list_price order by t.name asc
  ) 
select
  i.product, sumin-coalesce(sumout,0) AS stock, sumin, sumout, purchaseprice, ((sumin-coalesce(sumout,0)) * purchaseprice) as stockvalue
from uitstock u 
  full outer join instock i on u.product = i.product