Search code examples
odoo-8odooodoo-9

Qty available by location (again)


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'

Solution

  • 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")