diff --git a/addons/point_of_sale/static/src/js/models.js b/addons/point_of_sale/static/src/js/models.js index 92276db60de..17278ed1bd9 100644 --- a/addons/point_of_sale/static/src/js/models.js +++ b/addons/point_of_sale/static/src/js/models.js @@ -324,20 +324,21 @@ function openerp_pos_models(instance, module){ //module is instance.point_of_sal // it returns a deferred that succeeds after having tried to send the order and all the other pending orders. push_order: function(order) { var self = this; - this.proxy.log('push_order',order.export_as_JSON()); - var order_id = this.db.add_order(order.export_as_JSON()); + + if(order){ + this.proxy.log('push_order',order.export_as_JSON()); + this.db.add_order(order.export_as_JSON()); + } + var pushed = new $.Deferred(); - this.set('synch',{state:'connecting', pending:self.db.get_orders().length}); - this.flush_mutex.exec(function(){ - var flushed = self._flush_all_orders(); + var flushed = self._flush_orders(self.db.get_orders()); - flushed.always(function(){ + flushed.always(function(ids){ + console.log('pushed orders:',ids); pushed.resolve(); }); - - return flushed; }); return pushed; }, @@ -361,8 +362,6 @@ function openerp_pos_models(instance, module){ //module is instance.point_of_sal var order_id = this.db.add_order(order.export_as_JSON()); - this.set('synch',{state:'connecting', pending:self.db.get_orders().length}); - this.flush_mutex.exec(function(){ var done = new $.Deferred(); // holds the mutex @@ -373,7 +372,7 @@ function openerp_pos_models(instance, module){ //module is instance.point_of_sal // things will happen as a duplicate will be sent next time // so we must make sure the server detects and ignores duplicated orders - var transfer = self._flush_order(order_id, {timeout:30000, to_invoice:true}); + var transfer = self._flush_orders([self.db.get_order(order_id)], {timeout:30000, to_invoice:true}); transfer.fail(function(){ invoiced.reject('error-transfer'); @@ -382,10 +381,12 @@ function openerp_pos_models(instance, module){ //module is instance.point_of_sal // on success, get the order id generated by the server transfer.pipe(function(order_server_id){ + // generate the pdf and download it self.pos_widget.do_action('point_of_sale.pos_invoice_report',{additional_context:{ active_ids:order_server_id, }}); + invoiced.resolve(); done.resolve(); }); @@ -397,62 +398,33 @@ function openerp_pos_models(instance, module){ //module is instance.point_of_sal return invoiced; }, - // attemps to send all pending orders ( stored in the pos_db ) to the server, - // and remove the successfully sent ones from the db once - // it has been confirmed that they have been sent correctly. - flush: function() { + // wrapper around the _save_to_server that updates the synch status widget + _flush_orders: function(orders, options) { var self = this; - var flushed = new $.Deferred(); - this.flush_mutex.exec(function(){ - var done = new $.Deferred(); + this.set('synch',{ state: 'connecting', pending: orders.length}); - self._flush_all_orders() - .done( function(){ flushed.resolve();}) - .fail( function(){ flushed.reject(); }) - .always(function(){ done.resolve(); }); - - return done; - }); - - return flushed; - }, - - // attempts to send the locally stored order of id 'order_id' - // the sending is asynchronous and can take some time to decide if it is successful or not - // it is therefore important to only call this method from inside a mutex - // this method returns a deferred indicating wether the sending was successful or not - // there is a timeout parameter which is set to 2 seconds by default. - _flush_order: function( order_id, options) { - return this._flush_all_orders([this.db.get_order(order_id)], options); - }, - - // attempts to send all the locally stored orders. As with _flush_order, it should only be - // called from within a mutex. - // this method returns a deferred that always succeeds when all orders have been tried to be sent, - // even if none of them could actually be sent. - _flush_all_orders: function () { - var self = this; - self.set('synch', { - state: 'connecting', - pending: self.get('synch').pending - }); - return self._save_to_server(self.db.get_orders()).done(function () { + return self._save_to_server(orders, options).done(function (server_ids) { var pending = self.db.get_orders().length; + self.set('synch', { state: pending ? 'connecting' : 'connected', pending: pending }); + + return server_ids; }); }, // send an array of orders to the server // available options: // - timeout: timeout for the rpc call in ms + // returns a deferred that resolves with the list of + // server generated ids for the sent orders _save_to_server: function (orders, options) { if (!orders || !orders.length) { var result = $.Deferred(); - result.resolve(); + result.resolve([]); return result; } @@ -474,10 +446,11 @@ function openerp_pos_models(instance, module){ //module is instance.point_of_sal shadow: !options.to_invoice, timeout: timeout } - ).then(function () { + ).then(function (server_ids) { _.each(orders, function (order) { self.db.remove_order(order.id); }); + return server_ids; }).fail(function (unused, event){ // prevent an error popup creation by the rpc failure // we want the failure to be silent as we send the orders in the background diff --git a/addons/point_of_sale/static/src/js/widgets.js b/addons/point_of_sale/static/src/js/widgets.js index 5db12165737..66130a7740c 100644 --- a/addons/point_of_sale/static/src/js/widgets.js +++ b/addons/point_of_sale/static/src/js/widgets.js @@ -840,7 +840,7 @@ function openerp_pos_widgets(instance, module){ //module is instance.point_of_sa self.set_status(synch.state, synch.pending); }); this.$el.click(function(){ - self.pos.flush(); + self.pos.push_order(); }); }, }); @@ -1005,7 +1005,7 @@ function openerp_pos_widgets(instance, module){ //module is instance.point_of_sa self.$('.loader').animate({opacity:0},1500,'swing',function(){self.$('.loader').addClass('oe_hidden');}); - self.pos.flush(); + self.pos.push_order(); }).fail(function(){ // error when loading models data from the backend return new instance.web.Model("ir.model.data").get_func("search_read")([['name', '=', 'action_pos_session_opening']], ['res_id'])