Search code examples
pythonpython-3.xodooodoo-11

How to get products available quantity for individual stock (Odoo11)


I am using odoo11(python3) and develop a custom module.

I would like to get products quantity (by hand or forecast) for an individual stock house.

Just I select any individual stockhouse and show product quantity for this stockhouse. Actually, my target is two things 1.Products total available quantity 2.Products available quantity based on location This is my code

class Threshold(models.Model):
    _name="threshold.threshold"
    main_location=fields.Many2many("stock.warehouse", string="Main Location")
    product=fields.Many2many("product.template", string="Product")
    category=fields.Many2one("product.category", string="Category")
    attribute=fields.Many2many("product.attribute", string="Attribute")
    threshold_value=fields.Integer(string="Threshold Value")
    transfer_quantity=fields.Integer(string="Transfer Quantity")
    status_button=fields.Selection([('0','Active'),('1','Dissmiss')], default='0', index=True, string="Status")
    threshold_selection=fields.Selection([('0','Product'),('1','Category'),], default= '0', index=True, string="Threshold Selection")

product_quantity=fields.Integer(compute="_product_based_on_warehouse", store=True)

@api.depends('main_location','product_quantity')
def _product_based_on_warehouse(self):
    count_products = 0 
self.env['product.template'].with_context(warehouse=self.main_location).search([], limit=1)
self.product_quantity=products print(f'Product Quantity: {self.product_quantity}')


Solution

  • You have to load/browse the products with a special context value warehouse to get all the quantity values for all, one or multiple warehouses.

    # load all products and use a warehouse id
    products = self.env['product.product'].with_context(warehouse=1).search([])
    # load one product and use a warehouse name
    products = self.env['product.product'].with_context(warehouse="Main Warehouse").search([], limit=1)
    # load product with ID 1 and use a list of warehouse IDs
    products = self.env['product.product'].with_context(warehouse=[1,2]).browse([1])
    

    You can use the 4 quantity fields of products to get the quantity values you need. There is also an option to use location as context value in the same manner as with warehouse.

    If you want to know where this is coming from look into the methods _compute_quantities, _compute_quantities_dict and the most important one _get_domain_locations.