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

时间:2016-10-24 04:15:16

标签: python-2.7 openerp odoo-8 odoo-9 stock

我需要从odoo stock获得产品可用数量。

我有stock_quant,stock_move,stock_location。

我想要实现的是两件事:

  1. 产品总可用数量
  2. 基于位置的产品可用数量
  3. 有人可以指导我吗?

2 个答案:

答案 0 :(得分:3)

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

示例:

适用于所有仓库/地点

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

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

  

有关详细信息,请参阅库存模块中的product.py。

<强>解决方案:

@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

答案 1 :(得分:-2)

对于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