From ca200e2ba58c8f8e4024367163aa2eb6576b61a0 Mon Sep 17 00:00:00 2001 From: Eleftherios Kritsotakis Date: Wed, 13 Apr 2022 14:31:44 +0000 Subject: [PATCH] [IMP] point_of_sale: disable backend querying when all items loaded In this commit, we improve the load-in-the-background feature of partners/products. If there are items that are not loaded yet, (either because the background loading is still running or because the user did not want to load any prodcts at all at the start of the session) the option to fetch items from the database is enabled: every time the user interacts with the search bar (pressing "Enter", "Search database" or the "Search" icon at the search bar) the backend is queried and new items (products/partners) are fetched and added to the frontend. Before the fetching of products from the backend was done using the "database" icon / "Load Customers" button which are now dropped. In this commit, the feature of querrying the database is automatically disabled once all the partners or products (available_in_pos) have been loaded in PoS. Then, the querying of the backend stops. task-2755384 closes odoo/odoo#81226 Related: odoo/enterprise#23544 Signed-off-by: Masereel Pierre --- addons/point_of_sale/static/src/js/Chrome.js | 9 ++++++-- .../PartnerListScreen/PartnerListScreen.js | 11 +++++----- .../Screens/ProductScreen/ProductsWidget.js | 4 ++-- .../ProductsWidgetControlPanel.js | 2 ++ addons/point_of_sale/static/src/js/models.js | 21 +++++++++++++++++++ .../Screens/ProductScreen/ProductsWidget.xml | 5 ++--- 6 files changed, 40 insertions(+), 12 deletions(-) diff --git a/addons/point_of_sale/static/src/js/Chrome.js b/addons/point_of_sale/static/src/js/Chrome.js index 2b39a3dcf1d..6adb7a72bff 100644 --- a/addons/point_of_sale/static/src/js/Chrome.js +++ b/addons/point_of_sale/static/src/js/Chrome.js @@ -198,10 +198,15 @@ odoo.define('point_of_sale.Chrome', function(require) { // Basically, preload the images in the background. this._preloadImages(); if (this.env.pos.config.limited_partners_loading && this.env.pos.config.partner_load_background) { - this.env.pos.loadPartnersBackground(); + this.env.pos.loadPartnersBackground().then(() => { + this.env.pos.isEveryPartnerLoaded = true; + }); } if (this.env.pos.config.limited_products_loading && this.env.pos.config.product_load_background) { - this.env.pos.loadProductsBackground().then(() => this.render(true)); + this.env.pos.loadProductsBackground().then(() => { + this.env.pos.isEveryProductLoaded = true; + this.render(true); + }); } } diff --git a/addons/point_of_sale/static/src/js/Screens/PartnerListScreen/PartnerListScreen.js b/addons/point_of_sale/static/src/js/Screens/PartnerListScreen/PartnerListScreen.js index 0ebffa4efde..2ee190afd3c 100644 --- a/addons/point_of_sale/static/src/js/Screens/PartnerListScreen/PartnerListScreen.js +++ b/addons/point_of_sale/static/src/js/Screens/PartnerListScreen/PartnerListScreen.js @@ -8,7 +8,7 @@ odoo.define('point_of_sale.PartnerListScreen', function(require) { const { debounce } = require("@web/core/utils/timing"); const { useListener } = require("@web/core/utils/hooks"); - const { onWillUnmount, useRef } = owl; + const { onWillUnmount, useRef, onMounted } = owl; /** * Render this screen using `showTempScreen` to select partner. @@ -47,6 +47,10 @@ odoo.define('point_of_sale.PartnerListScreen', function(require) { }; this.updatePartnerList = debounce(this.updatePartnerList, 70); onWillUnmount(this.updatePartnerList.cancel); + onMounted(() => { + if(!this.env.pos.config.limited_partners_loading) + this.env.pos.isEveryPartnerLoaded = true; + }); } // Lifecycle hooks @@ -93,9 +97,6 @@ odoo.define('point_of_sale.PartnerListScreen', function(require) { } return res } - get isEveryPartnerLoaded() { - return !this.env.pos.config.limited_partners_loading || this.env.pos.config.partner_load_background; - } get isBalanceDisplayed() { return this.env.pos.config.module_pos_loyalty; } @@ -107,7 +108,7 @@ odoo.define('point_of_sale.PartnerListScreen', function(require) { async _onPressEnterKey() { if (!this.state.query) return; - if (!this.isEveryPartnerLoaded) { + if (!this.env.pos.isEveryPartnerLoaded) { const result = await this.searchPartner(); this.showNotification( _.str.sprintf(this.env._t('%s customer(s) found for "%s".'), diff --git a/addons/point_of_sale/static/src/js/Screens/ProductScreen/ProductsWidget.js b/addons/point_of_sale/static/src/js/Screens/ProductScreen/ProductsWidget.js index 8da8e5304f9..17b9988f3ce 100644 --- a/addons/point_of_sale/static/src/js/Screens/ProductScreen/ProductsWidget.js +++ b/addons/point_of_sale/static/src/js/Screens/ProductScreen/ProductsWidget.js @@ -59,8 +59,8 @@ odoo.define('point_of_sale.ProductsWidget', function(require) { get hasNoCategories() { return this.env.pos.db.get_category_childs_ids(0).length === 0; } - get isEveryProductLoaded() { - return !this.env.pos.config.limited_products_loading || this.env.pos.config.product_load_background; + get shouldShowButton() { + return this.productsToDisplay.length === 0 && this.searchWord && !this.env.pos.isEveryProductLoaded; } _switchCategory(event) { this.env.pos.setSelectedCategoryId(event.detail); diff --git a/addons/point_of_sale/static/src/js/Screens/ProductScreen/ProductsWidgetControlPanel.js b/addons/point_of_sale/static/src/js/Screens/ProductScreen/ProductsWidgetControlPanel.js index af16a2ec450..67d0e6c5e26 100644 --- a/addons/point_of_sale/static/src/js/Screens/ProductScreen/ProductsWidgetControlPanel.js +++ b/addons/point_of_sale/static/src/js/Screens/ProductScreen/ProductsWidgetControlPanel.js @@ -17,6 +17,8 @@ odoo.define('point_of_sale.ProductsWidgetControlPanel', function(require) { onMounted(() => { this.env.posbus.on('search-product-from-info-popup', this, this.searchProductFromInfo) + if(!this.env.pos.config.limited_products_loading) + this.env.pos.isEveryProductLoaded = true; }); onWillUnmount(() => { diff --git a/addons/point_of_sale/static/src/js/models.js b/addons/point_of_sale/static/src/js/models.js index ea264e44832..81fb4432426 100644 --- a/addons/point_of_sale/static/src/js/models.js +++ b/addons/point_of_sale/static/src/js/models.js @@ -112,6 +112,9 @@ class PosGlobalState extends PosModel { this.numpadMode = 'quantity'; + this.isEveryPartnerLoaded = false; + this.isEveryProductLoaded = false; + // Record this.toRefundLines = {}; this.TICKET_SCREEN_STATE = { @@ -349,6 +352,24 @@ class PosGlobalState extends PosModel { }); } + async updateIsEveryPartnerLoaded() { + let partnersCount = await this.env.services.rpc({ + model: 'res.partner', + method: 'search_count', + args: [[]], + }); + this.isEveryPartnerLoaded = partnersCount === this.db.partner_sorted.length; + } + + async updateIsEveryProductLoaded() { + let productsCount = await this.env.services.rpc({ + model: 'product.product', + method: 'search_count', + args: [[['available_in_pos', '=', true]]], + }); + this.isEveryProductLoaded = productsCount === this.db.get_product_by_category(this.db.root_category_id).length; + } + setSelectedCategoryId(categoryId) { this.selectedCategoryId = categoryId; } diff --git a/addons/point_of_sale/static/src/xml/Screens/ProductScreen/ProductsWidget.xml b/addons/point_of_sale/static/src/xml/Screens/ProductScreen/ProductsWidget.xml index df3f0fb2c68..4111c0b54fc 100644 --- a/addons/point_of_sale/static/src/xml/Screens/ProductScreen/ProductsWidget.xml +++ b/addons/point_of_sale/static/src/xml/Screens/ProductScreen/ProductsWidget.xml @@ -4,7 +4,7 @@
+ subcategories="subcategories" hasNoCategories="hasNoCategories" />
@@ -19,8 +19,7 @@ There are no products in this category.

-
+