From cd353e2b52560d468d321c7b878f395256f16558 Mon Sep 17 00:00:00 2001 From: "Jacky (trj)" Date: Thu, 6 Oct 2022 11:44:43 +0000 Subject: [PATCH] [FIX] pos_restaurant: orders table synchronization Due to how we change the reactivity in the UI and the printing changes that kept being computed and set when an order object is created, fetched orders became part of the `ordersToUpdateSet` which is wrong. Only updated orders should be tracked and send to the back end. Also fixed how the transferred order should act when offline then online. closes odoo/odoo#102472 X-original-commit: db553434a4a07599bcefb628ef672807f89db863 Signed-off-by: Heinz Robin (rhe) Signed-off-by: Trinh Jacky (trj) --- addons/pos_restaurant/static/src/js/models.js | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/addons/pos_restaurant/static/src/js/models.js b/addons/pos_restaurant/static/src/js/models.js index d4692c6db16..db1bc1b9138 100644 --- a/addons/pos_restaurant/static/src/js/models.js +++ b/addons/pos_restaurant/static/src/js/models.js @@ -16,6 +16,8 @@ const PosRestaurantPosGlobalState = (PosGlobalState) => class PosRestaurantPosGl super(obj); this.orderToTransfer = null; // table transfer feature this.ordersToUpdateSet = new Set(); // used to know which orders need to be sent to the back end when syncing + this.transferredOrdersSet = new Set(); // used to know which orders has been transferred but not sent to the back end yet + this.loadingOrderState = false; // used to prevent orders fetched to be put in the update set during the reactive change } //@override async _processData(loadedData) { @@ -31,7 +33,7 @@ const PosRestaurantPosGlobalState = (PosGlobalState) => class PosRestaurantPosGl //@override _onReactiveOrderUpdated(order) { super._onReactiveOrderUpdated(...arguments) - if (this.config.iface_floorplan) { + if (this.config.iface_floorplan && !this.loadingOrderState) { this.ordersToUpdateSet.add(order); } } @@ -83,6 +85,12 @@ const PosRestaurantPosGlobalState = (PosGlobalState) => class PosRestaurantPosGl } return reactiveOrder; } + //@override + async load_orders() { + this.loadingOrderState = true; + await super.load_orders(); + this.loadingOrderState = false; + } _loadRestaurantPrinter(printers) { this.unwatched.printers = []; // list of product categories that belong to one or more order printer @@ -124,6 +132,7 @@ const PosRestaurantPosGlobalState = (PosGlobalState) => class PosRestaurantPosGl await this._removeOrdersFromServer(); // This need to be called here otherwise _onReactiveOrderUpdated() will be called after the set is being cleared this.ordersToUpdateSet.clear(); + this.transferredOrdersSet.clear(); } /** * Send the orders to be saved to the back end @@ -196,15 +205,20 @@ const PosRestaurantPosGlobalState = (PosGlobalState) => class PosRestaurantPosGl tableOrders.forEach(order => { // We don't remove the validated orders because we still want to see them in the ticket screen. // Orders in 'ReceiptScreen' or 'TipScreen' are validated orders. - if (order.server_id && !order.finalized){ + if (order.server_id && !order.finalized && !this.transferredOrdersSet.has(order)){ this.removeOrder(order, false); } }); ordersJsons.forEach(json => { // Because of the offline feature, some draft orders fetched from the backend will appear // to belong in different table, but in fact they are already moved. - const potentiallyTransferredOrder = [...this.ordersToUpdateSet].find(order => order.uid === json.uid) - if (!potentiallyTransferredOrder || potentiallyTransferredOrder.tableId === tableId) { + const transferredOrder = [...this.transferredOrdersSet].find(order => order.uid === json.uid) + const isSameTable = transferredOrder && transferredOrder.tableId === tableId; + if (isSameTable) { + // this means we transferred back to the original table, we'll prioritize the server state + this.removeOrder(transferredOrder, false); + } + if (!transferredOrder || isSameTable) { const order = this.createReactiveOrder(json); this.orders.add(order); } @@ -229,10 +243,12 @@ const PosRestaurantPosGlobalState = (PosGlobalState) => class PosRestaurantPosGl async setTable(table, orderUid=null) { this.table = table; try { + this.loadingOrderState = true; await this._syncTableOrdersFromServer(table.id); } catch (error) { throw error; } finally { + this.loadingOrderState = false; const currentOrder = this.getTableOrders(table.id).find(order => orderUid ? order.uid === orderUid : !order.finalized); if (currentOrder) { this.set_order(currentOrder); @@ -255,12 +271,15 @@ const PosRestaurantPosGlobalState = (PosGlobalState) => class PosRestaurantPosGl async transferTable(table) { this.table = table; try { + this.loadingOrderState = true; await this._syncTableOrdersFromServer(table.id); } catch (error) { throw error; } finally { + this.loadingOrderState = false; this.orderToTransfer.tableId = table.id; this.set_order(this.orderToTransfer); + this.transferredOrdersSet.add(this.orderToTransfer); this.orderToTransfer = null; } }