diff --git a/addons/point_of_sale/models/pos_config.py b/addons/point_of_sale/models/pos_config.py index 147b60bcca1..97c092a0f5b 100644 --- a/addons/point_of_sale/models/pos_config.py +++ b/addons/point_of_sale/models/pos_config.py @@ -668,29 +668,39 @@ class PosConfig(models.Model): else: pos_config.write({'module_account': False}) - def get_limited_products_loading(self): - self.env.cr.execute(""" - WITH pm AS - ( - SELECT product_id, - Max(write_date) date - FROM stock_quant - GROUP BY product_id) - SELECT p.id - FROM product_product p - LEFT JOIN product_template t - ON ( - product_tmpl_id=t.id) - LEFT JOIN pm - ON ( - p.id=pm.product_id) - WHERE p.active - AND t.available_in_pos - ORDER BY t.priority DESC, + def get_limited_products_loading(self, fields): + query = """ + WITH pm AS ( + SELECT product_id, + Max(write_date) date + FROM stock_quant + GROUP BY product_id + ) + SELECT p.id + FROM product_product p + LEFT JOIN product_template t ON product_tmpl_id=t.id + LEFT JOIN pm ON p.id=pm.product_id + WHERE ( + t.available_in_pos + AND t.sale_ok + AND (t.company_id=%(company_id)s OR t.company_id IS NULL) + AND %(available_categ_ids)s IS NULL OR t.pos_categ_id=ANY(%(available_categ_ids)s) + ) OR p.id=%(tip_product_id)s + ORDER BY t.priority DESC, t.detailed_type DESC, - COALESCE(pm.date,p.write_date) DESC limit %s - """, [str(self.limited_products_amount)]) - return self.env.cr.fetchall() + COALESCE(pm.date,p.write_date) DESC + LIMIT %(limit)s + """ + params = { + 'company_id': self.company_id.id, + 'available_categ_ids': self.iface_available_categ_ids.mapped('id') if self.iface_available_categ_ids else None, + 'tip_product_id': self.tip_product_id.id if self.tip_product_id else None, + 'limit': self.limited_products_amount + } + self.env.cr.execute(query, params) + product_ids = self.env.cr.fetchall() + products = self.env['product.product'].search_read([('id', 'in', product_ids)], fields=fields) + return products def get_limited_partners_loading(self): self.env.cr.execute(""" diff --git a/addons/point_of_sale/static/src/js/models.js b/addons/point_of_sale/static/src/js/models.js index 7c0bc09e352..b42c74c9fc5 100644 --- a/addons/point_of_sale/static/src/js/models.js +++ b/addons/point_of_sale/static/src/js/models.js @@ -160,7 +160,7 @@ exports.PosModel = Backbone.Model.extend({ return this.connect_to_proxy(); } if(this.config.limited_products_loading) { - await this.env.pos._addProducts(await this.loadProductsFromOffset(0)); + await this.loadLimitedProducts(); if(this.config.product_load_background) this.loadProductsBackground(); } @@ -930,29 +930,37 @@ exports.PosModel = Backbone.Model.extend({ partnerModel.loaded(this, partners); } }, - loadProductsFromOffset: async function(offset) { - let ProductIds = []; + // Load the products following specific rules into the `db` + loadLimitedProducts: async function() { let product_model = _.find(this.models, (model) => model.model === 'product.product'); - ProductIds = await this.rpc({ - model: 'product.product', - method: 'search', - args: [product_model.domain(this)], - kwargs: { - offset: this.env.pos.config.limited_products_amount * offset, - limit: this.env.pos.config.limited_products_amount, - }, - context: this.env.session.user_context, + const products = await this.rpc({ + model: 'pos.config', + method: 'get_limited_products_loading', + args: [this.config_id, product_model.fields], + context: { ...this.session.user_context, ...product_model.context() }, }); - return ProductIds; + product_model.loaded(this, products); + return products.length }, loadProductsBackground: async function() { - let offset = 0 - let ProductIds = []; + let page = 0; + let product_model = _.find(this.models, (model) => model.model === 'product.product'); + let products = []; do { - ProductIds = await this.loadProductsFromOffset(offset) - this.env.pos._addProducts(ProductIds); - offset += 1; - } while(ProductIds.length); + products = await this.rpc({ + model: 'product.product', + method: 'search_read', + kwargs: { + 'domain': product_model.domain(this), + 'fields': product_model.fields, + 'offset': page * this.env.pos.config.limited_products_amount, + 'limit': this.env.pos.config.limited_products_amount + }, + context: { ...this.session.user_context, ...product_model.context() }, + }); + product_model.loaded(this, products); + page += 1; + } while(products.length == this.config.limited_products_amount); }, loadPartnersBackground: async function() { let i = 1;