7

I need to get products available quantity from odoo stock.

There are several models I stock_quant, stock_move, stock_location.

What I am trying to achieve are two things:

  1. Products total available quantity
  2. Products available quantity based on location

Can anyone please guide me?

Tanzil Khan
  • 942
  • 1
  • 9
  • 20

2 Answers2

6

Stock related fields are defines in products (functional field) and directly from the product you can get the stock for all warehouses / locations or for individual location / warehouse.

Example:

For all warehouses / locations

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

For individual location / warehouse (WAREHOUSE_ID / LOCATION_ID should be replaced by actual 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

Other fields are also there.

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

You can access all those fields in similar manner. If you will not pass any warehouse / location in context then it will returns the stock of the all warehouses together.

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

Solution:

@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
SiHa
  • 7,830
  • 13
  • 34
  • 43
  • I have written following piece of code but it is giving error. – Tanzil Khan Oct 24 '16 at 09:07
  • ' @api.onchange('product_id','source_location') def product_qty_location_check(self): if self.product_id and self.source_location: product = self.env['product.product'].browse(self.product_id.id) available_qty = product.with_context(warehouse=self.source_location.id).qty_available print available_qty' the error is **MissingError One of the documents you are trying to access has been deleted, please try again after refreshing.** – Tanzil Khan Oct 24 '16 at 09:08
  • **Thanks.** I should have sent dictionary. – Tanzil Khan Oct 24 '16 at 09:36
  • can you please help me with this query - http://stackoverflow.com/questions/40215992/how-to-make-odoo-custom-stock-move-odoo-v8-and-v9 – Tanzil Khan Oct 25 '16 at 05:15
-3

For Odoo 8,9 and 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
emulbreh
  • 3,421
  • 23
  • 27