// 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());
- var pushed = new $.Deferred();
- this.set('synch',{state:'connecting', pending:self.db.get_orders().length});
+ if(order){
+ this.proxy.log('push_order',order.export_as_JSON());
+ this.db.add_order(order.export_as_JSON());
+ }
+
+ var pushed = new $.Deferred();
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;
},
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
// 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');
// 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();
});
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();
- self._flush_all_orders()
- .done( function(){ flushed.resolve();})
- .fail( function(){ flushed.reject(); })
- .always(function(){ done.resolve(); });
+ this.set('synch',{ state: 'connecting', pending: orders.length});
- 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;
}
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