odooodoo-12

How to get a product's multiple locations in odoo?


I want to get a product's location and display it on a custom report table: custom report table

and on the "Warehouse" cell it should be all the product's location, so if that product has multiple it should be displayed there. Just for clarification this is the location I'm talking about: enter image description here

In order to put that there I tried this code:

class StockInventoryValuationReport(models.TransientModel):
    _name = 'report.stock.inventory.valuation.report'
    _description = 'Stock Inventory Valuation Report'

    location_id = fields.Many2one('stock.location')


    # filter domain wizard
    @api.multi
    def _compute_results(self):
        self.ensure_one()

        stockquant_obj = self.env['stock.quant'].search([("location_id", "=", self.location_id.id)])
        print(stockquant_obj.location_id)

            line = {
                'name': product.name,
                'reference': product.default_code,
                'barcode': product.barcode,
                'qty_at_date': product.qty_at_date,
                'uom_id': product.uom_id,
                'currency_id': product.currency_id,
                'cost_currency_id': product.cost_currency_id,
                'standard_price': standard_price,
                'stock_value': product.qty_at_date * standard_price,
                'cost_method': product.cost_method,
                'taxes_id': product.taxes_id,
                'location_id': stockquant_obj.location_id,
            }
            if product.qty_at_date != 0:
                self.results += ReportLine.new(line)


but when I'm printing stockquant_obj.location_id it is an empty recordset basically its not finding any locations. Can someone please hint me on anything?


Solution

  • I actually managed to get the product's locations using this code:

    class StockInventoryValuationReport(models.TransientModel):
        _name = 'report.stock.inventory.valuation.report'
        _description = 'Stock Inventory Valuation Report'
    
        location_id = fields.Many2one('stock.location')
    
    
        # filter domain wizard
        @api.multi
        def _compute_results(self):
            self.ensure_one()
    
            stockquant_obj = self.env['stock.quant'].search([("location_id", "=", self.location_id.id)])
    
               for xyz in stockquant_obj:
                    line = {
                        'name': product.name,
                        'reference': product.default_code,
                        'barcode': product.barcode,
                        'qty_at_date': product.qty_at_date,
                        'uom_id': product.uom_id,
                        'currency_id': product.currency_id,
                        'cost_currency_id': product.cost_currency_id,
                        'standard_price': standard_price,
                        'stock_value': product.qty_at_date * standard_price,
                        'cost_method': product.cost_method,
                        'taxes_id': product.taxes_id,
                        'location_id': xyz.location_id,
                    }
                    if product.qty_at_date != 0:
                        self.results += ReportLine.new(line)
    

    I debugged further discovering that now stock.quant() could get some record-set but odoo was expecting a singleton when on my old code was stockquant_obj.location_id so since I have seen from other people that the solution to singleton is a for loop and for that reason I added it.

    The problem with this is that now not only the warehouse would be added but the same product would repeat as times as long the recordset is. How can I dodge this? How to tell python that I only need to loop through stockquant_obj and xyz should be inside the line variable?