跳至內容
選單
此問題已被標幟
11 回覆
33652 瀏覽次數

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

頭像
捨棄
相關帖文 回覆 瀏覽次數 活動
0
7月 25
354
2
6月 25
1219
1
5月 25
1266
0
3月 25
1132
1
2月 25
1841