From dff352acdb8b15b59bb4257814e62c4e46798739 Mon Sep 17 00:00:00 2001 From: Diep Huu Hoang Date: Tue, 24 Jan 2017 04:01:14 +0700 Subject: [PATCH] [FIX] point_of_sale: prevent losing orders when validating during sync When validating a new order in between the create_from_ui RPC call and the corresponding callback this new order was lost. There were two reasons for this. First of all, the Mutex supposed to prevent multiple create_from_ui RPC calls from happening at the same time did not return a Deferred. This is problematic because this function gets passed to jQuery.when() which will consider functions that do not return a Deferred to be resolved upon return. Secondly, the resolved callback of the create_from_ui RPC call naively deleted all paid orders, assuming they were all successfully sent to the backend. This is not the case when an order is added to the orders array in between the RPC call and the callback. Closes #15190 --- addons/point_of_sale/static/src/js/models.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/addons/point_of_sale/static/src/js/models.js b/addons/point_of_sale/static/src/js/models.js index 3880049e6d3..819dd4a5ee8 100644 --- a/addons/point_of_sale/static/src/js/models.js +++ b/addons/point_of_sale/static/src/js/models.js @@ -682,6 +682,8 @@ exports.PosModel = Backbone.Model.extend({ flushed.always(function(ids){ pushed.resolve(); }); + + return flushed; }); return pushed; }, @@ -782,6 +784,11 @@ exports.PosModel = Backbone.Model.extend({ var self = this; var timeout = typeof options.timeout === 'number' ? options.timeout : 7500 * orders.length; + // Keep the order ids that are about to be sent to the + // backend. In between create_from_ui and the success callback + // new orders may have been added to it. + var order_ids_to_sync = _.pluck(orders, 'id'); + // we try to send the order. shadow prevents a spinner if it takes too long. (unless we are sending an invoice, // then we want to notify the user that we are waiting on something ) var posOrderModel = new Model('pos.order'); @@ -796,8 +803,8 @@ exports.PosModel = Backbone.Model.extend({ timeout: timeout } ).then(function (server_ids) { - _.each(orders, function (order) { - self.db.remove_order(order.id); + _.each(order_ids_to_sync, function (order_id) { + self.db.remove_order(order_id); }); self.set('failed',false); return server_ids;