0

I know this was discussed before but it's not really working for me. How can i get qty available for location or warehouse my product is now.

(Most of the answers are in old API and this one not really working for me)

class ProductProduct(models.Model):
    _inherit = 'product.template'

  available_qty = fields.Integer(
    string='Qty By Loc',
    compute='product_qty_location_check',
)


    def product_qty_location_check(self): 
        if self: 

            self.available_qty = self.with_context({'location' : self.source_location.id}).qty_‌​available 

AttributeError: 'product.template' object has no attribute 'source_location'
Chaban33
  • 1,362
  • 11
  • 38

2 Answers2

1

To get the quantity by location you need to search the location with the product_id in stock.quant

Use the below sample in your compute function:

quant_sr = self.env["stock.quant"].search([('location_id','=',self.source_location.id),('product_id','=',self.product_id.id)])
qty = 0.0
for quant in quant_sr:
    qty += quant.qty
print qty
sfx
  • 1,793
  • 17
  • 24
  • AttributeError: 'product.template' object has no attribute 'source_location' , probably i need to do it in other model or? can't find this field in any model. any solution? – Chaban33 Jun 22 '18 at 13:48
  • Already there is smart button which shows the OnHand Quantity in product.template. Then why do you need another field? – sfx Jun 22 '18 at 14:32
0

First you have to find out your location and/or warehouse. There are enough possibilities to do so:

  1. use a wizard which has a field
  2. search by reference e.g. self.env.ref('my_module.my_location')
  3. use other sources

Now you can use them on product.product's _product_available(). Just call that method on one or more products (recordssets) and evaluate the result of this method. To filter for locations and/or warehouses use the context. For example:

# get a recordset of all products
products = self.env['product.product'].search([])
# get a special location (my_location)
location = self.env.ref('my_module.my_location')
quantities = products.with_context(location=location.id)._product_available()
# print the quantities
for product_id, quantity_dict in quantities.iteritems():
    print product_id
    print quantity_dict

The same is possible with with_context(warehouse=warehouse.id) or even with list of IDs or names: with_context(location=[1,2,3]), with_context(location="My Location")

CZoellner
  • 13,553
  • 3
  • 25
  • 38