Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
11 Răspunsuri
33639 Vizualizări

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.

Imagine profil
Abandonează
Cel mai bun răspuns

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.


Imagine profil
Abandonează
Cel mai bun răspuns

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)

Imagine profil
Abandonează
Autor

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

Cel mai bun răspuns

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

Imagine profil
Abandonează
Autor

Thanks it also good solution, I will test it.

Autor

Tested, Working Perfectly.

Cel mai bun răspuns

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

Imagine profil
Abandonează
Autor Cel mai bun răspuns

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.

Imagine profil
Abandonează
Cel mai bun răspuns

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>})
Imagine profil
Abandonează

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

Cel mai bun răspuns


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

Imagine profil
Abandonează
Related Posts Răspunsuri Vizualizări Activitate
0
iul. 25
354
2
iun. 25
1200
1
mai 25
1247
0
mar. 25
1125
1
feb. 25
1782