Product._get_quantity performance

Dear comunity

I have an issue on a implementation done on Tryton 5.0 related to stock quantity by wharehouse. The implementation was done on a SaleLine Class implementation, and the issue is related to performance.

The use case is on sales, on each sale, when the user add a line, he / her needs to know the current stock in order to know wich is the max item to sell. Yes, when the sale is done, the sale verifies the stocks, but usually the user sell more than 36 lines, and more than 10 item per lines, then wait to know the stock ant the end of sale is not a good idea. Coder implements the way on verify items number on each line adding, the problem is the performance, response is too slow on _get_quantity method. Ihave tried to improve it on a database level, but was not enough.

Any suggestabout it?

Bests

class SaleLine(metaclass=PoolMeta):
    __name__ = 'sale.line'
    ...
    def get_warehouse_qty(self, name=None):

        if self.sale:
            if not self.sale.warehouse:
                self.raise_user_error('La venta necesita un almacén seleccionado')
            else:
                if self.product:
                    Product = Pool().get('product.product')
                    location_ids = [self.sale.warehouse.id]
                    product_quantity = Product._get_quantity([self.product], 'quantity', location_ids)
                    qty = product_quantity[self.product.id]
                    return Decimal(qty)
        return Decimal('0')

First the quantity is displayed on the product searcher. This is the fastest way and it provide an UI to search for other available product.

Now about this getter, the problem is that it does not scale because it is an instance method. So there is not possibility for the framework to group the computation.
So the first thing to do is to re-implement it as class method and take advantage of the quantity computation for a list of products at once.
It is also better to avoid to call private method like Product._get_quantity. Instead you could just re-browse the products with the warehouse in the context.
Also you should consider to put stock_date_end to sale_date, stock_skip_warehouse to True, etc. Indeed the same context as the search_context of the sale line product field.

1 Like