From 5db33bfa439cdd7a4df3ee9b9379eccee1022457 Mon Sep 17 00:00:00 2001 From: pedrambiria Date: Wed, 13 Jul 2022 06:52:40 +0000 Subject: [PATCH] [FIX] point_of_sale,pos_sale: load the missing partners Before this commit: if the "Limited Partners Loading" were enabled, the partner wouldn't set when using the "Quotation/Order" functionality for unsynced partners. Steps to reproduce the issue on the runbot: Enable developer mode. Go to the Point of Sale. Open the config of any pos. Enable "Limited Partners Loading" and set "Number of Partners Loaded" to a low number (like 5 or 10) Disable "Load all remaining partners in the background." Open a new PoS session for the configured PoS Use the "Quotation/Order" functionality. Select any order, for example, S00043. The customer is not set for the current pos order. Solution The solution is to load the partner. Also, there was an issue when using the limited number of partner loading when it tried to load the cached and paid orders' partners. I added a function to load missing partners. opw-2883686 closes odoo/odoo#97442 X-original-commit: 4d7c05346e0c253c8b11736b9ee9f8426a87f2af Signed-off-by: Joseph Caburnay (jcb) Signed-off-by: Pedram Bi Ria (pebr) --- .../js/Screens/TicketScreen/TicketScreen.js | 3 ++- addons/point_of_sale/static/src/js/models.js | 27 +++++++++++++++++++ .../SaleOrderManagementScreen.js | 15 ++++++++++- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/addons/point_of_sale/static/src/js/Screens/TicketScreen/TicketScreen.js b/addons/point_of_sale/static/src/js/Screens/TicketScreen/TicketScreen.js index 66a23aa76d2..3dd8aaf1883 100644 --- a/addons/point_of_sale/static/src/js/Screens/TicketScreen/TicketScreen.js +++ b/addons/point_of_sale/static/src/js/Screens/TicketScreen/TicketScreen.js @@ -573,8 +573,9 @@ odoo.define('point_of_sale.TicketScreen', function (require) { args: [idsNotInCache], context: this.env.session.user_context, }); - // Check for missing products and load them in the PoS + // Check for missing products and partners and load them in the PoS await this.env.pos._loadMissingProducts(fetchedOrders); + await this.env.pos._loadMissingPartners(fetchedOrders); // Cache these fetched orders so that next time, no need to fetch // them again, unless invalidated. See `_onInvoiceOrder`. fetchedOrders.forEach((order) => { diff --git a/addons/point_of_sale/static/src/js/models.js b/addons/point_of_sale/static/src/js/models.js index 0a55beab69e..488ad056cf3 100644 --- a/addons/point_of_sale/static/src/js/models.js +++ b/addons/point_of_sale/static/src/js/models.js @@ -434,6 +434,7 @@ class PosGlobalState extends PosModel { async load_orders(){ var jsons = this.db.get_unpaid_orders(); await this._loadMissingProducts(jsons); + await this._loadMissingPartners(jsons); var orders = []; for (var i = 0; i < jsons.length; i++) { @@ -479,6 +480,32 @@ class PosGlobalState extends PosModel { }); this._loadProductProduct(products); } + // load the partners based on the ids + async _loadPartners(partnerIds) { + if (partnerIds.length > 0) { + var domain = [['id','in', partnerIds]]; + const fetchedPartners = await this.env.services.rpc({ + model: 'pos.session', + method: 'get_pos_ui_res_partner_by_params', + args: [[odoo.pos_session_id], {domain}], + }, { + timeout: 3000, + shadow: true, + }); + this.env.pos.db.add_partners(fetchedPartners); + } + } + async _loadMissingPartners(orders) { + const missingPartnerIds = new Set([]); + for (const order of orders) { + const partnerId = order.partner_id; + if(missingPartnerIds.has(partnerId)) continue; + if (partnerId && !this.db.get_partner_by_id(partnerId)) { + missingPartnerIds.add(partnerId); + } + } + await this._loadPartners([...missingPartnerIds]); + } async loadProductsBackground() { let page = 0; let products = []; diff --git a/addons/pos_sale/static/src/js/OrderManagementScreen/SaleOrderManagementScreen.js b/addons/pos_sale/static/src/js/OrderManagementScreen/SaleOrderManagementScreen.js index 9d9755527c3..2773e978fbc 100644 --- a/addons/pos_sale/static/src/js/OrderManagementScreen/SaleOrderManagementScreen.js +++ b/addons/pos_sale/static/src/js/OrderManagementScreen/SaleOrderManagementScreen.js @@ -90,7 +90,20 @@ odoo.define('pos_sale.SaleOrderManagementScreen', function (require) { } catch (_error){ } - currentPOSOrder.set_partner(this.env.pos.db.get_partner_by_id(sale_order.partner_id[0])); + let order_partner = this.env.pos.db.get_partner_by_id(sale_order.partner_id[0]) + if(order_partner){ + currentPOSOrder.set_client(order_partner); + } else { + try { + await this.env.pos._loadPartners([sale_order.partner_id[0]]); + } + catch (_error){ + const title = this.env._t('Customer loading error'); + const body = _.str.sprintf(this.env._t('There was a problem in loading the %s customer.'), sale_order.partner_id[1]); + await this.showPopup('ErrorPopup', { title, body }); + } + currentPOSOrder.set_partner(this.env.pos.db.get_partner_by_id(sale_order.partner_id[0])); + } let orderFiscalPos = sale_order.fiscal_position_id ? this.env.pos.fiscal_positions.find( (position) => position.id === sale_order.fiscal_position_id[0] )