Skip to Content
Меню
Вам необхідно зареєструватися, щоб взаємодіяти зі спільнотою.
Це запитання позначене
11 Відповіді
33848 Переглядів

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.

Аватар
Відмінити
Найкраща відповідь

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.


Аватар
Відмінити
Найкраща відповідь

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)

Аватар
Відмінити
Автор

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

Найкраща відповідь

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

Аватар
Відмінити
Автор

Thanks it also good solution, I will test it.

Автор

Tested, Working Perfectly.

Найкраща відповідь

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

Аватар
Відмінити
Автор Найкраща відповідь

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.

Аватар
Відмінити
Найкраща відповідь

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>})
Аватар
Відмінити

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

Найкраща відповідь


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

Аватар
Відмінити
Related Posts Відповіді Переглядів Дія
1
серп. 25
182
0
лип. 25
354
2
черв. 25
1378
1
трав. 25
1391
0
бер. 25
1240