This question has been flagged
11 Replies
30446 Views

By the code,

How may I get product quantity in particular stock location ?

I have two different warehouse & internal stock location for each warehouse, Now I want to get product quantity from particular stock location. How ???

right code with example.

Avatar
Discard
Best Answer

This is the simple way to get product on hand qty based on location:

code :

quant_obj = self.env['stock.quant']

qty_available = quant_obj._get_available_quantity(product_id, loc_id)


Here we can get exact qty based on location.


Avatar
Discard
Best Answer

Hello Zahin,

This method will return the quantity on hand on the location which is passed on method for passed product as argument on method.

def quantity_by_loc(self, product_id, location_id, context)

     #if you can't access context from method as argument than you can define here.
     product_obj = self.pool.get('product.product')
     qty = 0.0
     if context is None:
         context={}
     context.update({
        'states': ['done'],
        'what': ('in', 'out'),
        'location':location_id,
    })

    avail_product_details = product_obj.get_product_available(cr, uid, product_id, context=context)
    if avail_product_defails.values():
        qty = avail_product_defails.values()[0]
    return qty

I hope this will helps you.

Best Regards, Anil (SerpentCS)

Avatar
Discard
Author

Hello SerpentCS, your this code is buggy, its seems issue in your code. Please correct here.

Best Answer

Below I've written a function that could be helpful for you:

def get_quantity_at_location(self,cr,uid,lid,p):
    ls = ['stock_real','stock_virtual','stock_real_value','stock_virtual_value']
    move_avail = self.pool.get('stock.location')._product_value(cr,uid,[lid],ls,0,{'product_id':p})
    return move_avail[lid]['stock_real']

Basically, you are using the function _product_value() from the 'stock.location' class, and taking the 'stock_real' of the return statement.

Hope this helps!

-Tim

Avatar
Discard
Author

Thanks it also good solution, I will test it.

Author

Tested, Working Perfectly.

Best Answer


Hi

I think one can also use the select statement

select sum(product_qty) from report_stock_inventory where product_id = p1 and location_id = w1 and state = 'done'

where p1 is the product id , w1 is the location id, this gives you the actual stock

if you needed the unallocated stock only you can drop the "state='done'" from the where clause

Avatar
Discard
Best Answer

There is a function in product.product called get_product_available. You can get the stock value in location by passing the location id in context. So you can modify your code as:

def product_qty_by_location(self, cr, uid, product_id, warehouse_stock_location, context=None):
    if context is None:
        context = {}
    product_pool = self.pool.get('product.product')
    context.update({'states': ('done',), 'what': ('in', 'out'), 'location': warehouse_stock_location})
    result = product_pool.get_product_available(cr, uid, [product_id], context=context)
    qty = result.get(product_id, 0.00)
    return qty

You can also get the stock level of product based on warehouse by passing warehouse_id insted of location_id as:

context.update({'states': ('done',), 'what': ('in', 'out'), 'warehouse': <warehouse_id>})
Avatar
Discard

context is frozendict, hence cannot be modified. I think it has become more complex in odoo 8

Author Best Answer

I also get one alternate solution.

    def product_qty_by_location(self, cr, uid, product_id, warehouse_stock_location, context=None):
        sql = """select ((select sum(product_qty) from stock_move where product_id = %s and state in ('done') and location_dest_id = %s group by product_id) - (select sum(product_qty) from stock_move where product_id = %s and state in ('done') and location_id = %s group by product_id) ) as total;""" % (product_id, warehouse_stock_location, product_id, warehouse_stock_location)
        cr.execute(sql)
        return cr.fetchall()[0][0]

This gives me exact qty on hand by location. It fulfill my requirement.

Avatar
Discard
Best Answer

How would one use these functions in the following context:

    def _get_raw_qty(self, cr, uid, ids, field_name, args, context=None):
        
        res = {}
        lid = 13
        
        for record in self.browse(cr, uid, ids,context=context):
        
            pid = record.product_id
            lid = 13


            qty = self.product_qty_by_location(cr, uid, pid, lid) # I need to get the qty in a specific location per record.
            
            res[record.id] = 3.0
        return res

Avatar
Discard