# HG changeset patch
# User Asier Martirena <asier.martirena@openbravo.com>
# Date 1529937109 -7200
#      Mon Jun 25 16:31:49 2018 +0200
# Node ID f7ce58487545ed4474edb9955d09539245b1d97d
# Parent  148bdaa50dd982c3f7378d2c192ade8163d79db0
Fixed issue 38825: Ensure that an order is going to be saved before setting the id

In Web POS, when doing a save action, if the order doesn't have an ID, the ID is generated and then the order is saved. But there can be the situation in which the ID is set and the order is not saved, so that order won't be saved again. Now, before setting the ID, it is verified that the order will be saved.

diff --git a/web/org.openbravo.retail.posterminal/js/model/order.js b/web/org.openbravo.retail.posterminal/js/model/order.js
--- a/web/org.openbravo.retail.posterminal/js/model/order.js
+++ b/web/org.openbravo.retail.posterminal/js/model/order.js
@@ -419,32 +419,33 @@
     },
 
     save: function (callback) {
-      var undoCopy = this.get('undo'),
-          me = this,
-          forceInsert = false;
-
-      if (this.get('isBeingClosed')) {
-        var diffReceipt = OB.UTIL.diffJson(this.serializeToJSON(), this.get('json'));
-        var error = new Error();
-        OB.error('The receipt is being save during the closing: ' + diffReceipt);
-        OB.error('The stack trace is: ' + error.stack);
-      }
-
-      var now = new Date();
-      this.set('timezoneOffset', now.getTimezoneOffset());
-
-      if (!this.get('id') || !this.id) {
-        var uuid = OB.UTIL.get_UUID();
-        this.set('id', uuid);
-        this.id = uuid;
-        forceInsert = true;
-      }
-
-      this.set('json', JSON.stringify(this.serializeToJSON()));
-      if (callback === undefined || !callback instanceof Function) {
-        callback = function () {};
-      }
       if (!OB.MobileApp.model.get('preventOrderSave') && !this.pendingCalculateReceipt) {
+        var undoCopy = this.get('undo'),
+            me = this,
+            forceInsert = false;
+
+        if (this.get('isBeingClosed')) {
+          var diffReceipt = OB.UTIL.diffJson(this.serializeToJSON(), this.get('json'));
+          var error = new Error();
+          OB.error('The receipt is being save during the closing: ' + diffReceipt);
+          OB.error('The stack trace is: ' + error.stack);
+        }
+
+        var now = new Date();
+        this.set('timezoneOffset', now.getTimezoneOffset());
+
+        if (!this.get('id') || !this.id) {
+          var uuid = OB.UTIL.get_UUID();
+          this.set('id', uuid);
+          this.id = uuid;
+          forceInsert = true;
+        }
+
+        this.set('json', JSON.stringify(this.serializeToJSON()));
+        if (callback === undefined || !callback instanceof Function) {
+          callback = function () {};
+        }
+
         OB.Dal.save(this, function () {
           if (callback) {
             callback();
@@ -452,12 +453,13 @@
         }, function () {
           OB.error(arguments);
         }, forceInsert);
+
+        this.setUndo('SaveOrder', undoCopy);
       } else {
         if (callback) {
           callback();
         }
       }
-      this.setUndo('SaveOrder', undoCopy);
     },
 
     calculateTaxes: function (callback) {
