
# HG changeset patch
# User Carlos Aristu <carlos.aristu@openbravo.com>
# Date 1450684402 -3600
# Node ID 92bb52d9be4975ab944b7634fb888d9672912823
# Parent  59e1a50dd801ad4df0b2acb4d3c22bb91af0f3f2
fixes issue 31331: When add new lines in sales order, the product field is already filled

Two different flows were resulting in this problem. From one side, if we clicked directly on the new record button quickly after filling the combo value it could happen that the new form was created before the filter operation of the combo finished. To overcome this problem now a temporary id is generated every time we edit/create a new record in form. This id is checked once the combo filter operation ends, to identify if the form has changed. In that case, the flow to be executed after the filter operations is cancelled.

From other side, the same proble could happen if we clicked on the new record button quickly after clicking on the save record button. In this case, it could happen that after clearing the values a wrong entry was retrieved for the combo using the mapValueToDisplay() function. To solve this problem, now in the setEntries() function we include the value field information. This helps to retrieve the correct value on the described circumnstances.

Together with this, a new utility function has been created in order to have a common function to generate 'temporary' ids.

diff -r 59e1a50dd801 -r 92bb52d9be49 modules/org.openbravo.client.application/web/org.openbravo.client.application/js/form/formitem/ob-formitem-combo.js
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/form/formitem/ob-formitem-combo.js	Thu Dec 17 16:47:54 2015 +0000
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/form/formitem/ob-formitem-combo.js	Mon Dec 21 08:53:22 2015 +0100
@@ -28,9 +28,12 @@
   // Prevents validation of this item while filtering because real value is not 
   // set yet.
   // see issues #26189 and #28651
-  filterComplete: function () {
+  filterComplete: function (response, data, request, fromSharedPickList) {
     var ret;
 
+    if (request && request.params && request.params.recordIdInForm && request.params.recordIdInForm !== this.form.recordIdInForm) {
+      return;
+    }
     this.preventValidation = true;
     ret = this.Super('filterComplete', arguments);
     delete this.preventValidation;
@@ -62,5 +65,17 @@
       }
     }
     return false;
+  },
+
+  // This function will fall through to filterComplete() when the filter operation returns
+  filterDataBoundPickList: function (requestProperties, dropCache) {
+    if (this.form && this.form.view && this.form.view.isShowingForm) {
+      // Identify the record being currently edited in form view and include it in the request.
+      // It will be used to avoid problems when a new record is opened in form view before filterComplete() finishes
+      // See issue https://issues.openbravo.com/view.php?id=31331
+      requestProperties.params = requestProperties.params || {};
+      requestProperties.params.recordIdInForm = this.form.recordIdInForm;
+    }
+    return this.Super('filterDataBoundPickList', [requestProperties, dropCache]);
   }
 });
\ No newline at end of file
diff -r 59e1a50dd801 -r 92bb52d9be49 modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-view-grid.js
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-view-grid.js	Thu Dec 17 16:47:54 2015 +0000
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-view-grid.js	Mon Dec 21 08:53:22 2015 +0100
@@ -2986,7 +2986,7 @@
     // it MUST start with _ to identify it is a temporary id 
     var record = {
       _new: true,
-      id: '_' + new Date().getTime()
+      id: OB.Utilities.getTemporaryId()
     };
 
     this.addToCacheData(record, rowNum);
diff -r 59e1a50dd801 -r 92bb52d9be49 modules/org.openbravo.client.application/web/org.openbravo.client.application/js/main/ob-standard-view.js
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/main/ob-standard-view.js	Thu Dec 17 16:47:54 2015 +0000
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/main/ob-standard-view.js	Mon Dec 21 08:53:22 2015 +0100
@@ -1476,6 +1476,9 @@
     // at this point the time fields of the record are formatted in local time
     localTime = true;
     this.messageBar.hide();
+    // Set a temporary identifier for the record being edited in form view
+    // See issue https://issues.openbravo.com/view.php?id=31331
+    this.viewForm.recordIdInForm = OB.Utilities.getTemporaryId();
 
     if (!this.isShowingForm) {
       this.switchFormGridVisibility();
diff -r 59e1a50dd801 -r 92bb52d9be49 modules/org.openbravo.client.application/web/org.openbravo.client.application/js/utilities/ob-utilities.js
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/utilities/ob-utilities.js	Thu Dec 17 16:47:54 2015 +0000
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/utilities/ob-utilities.js	Mon Dec 21 08:53:22 2015 +0100
@@ -1310,4 +1310,11 @@
   grid.dataProperties.manualResultSet = true;
   grid.setData(grid.createDataModel());
   grid.serverDataNotLoaded = true;
+};
+
+//** {{{ OB.Utilities.getTemporaryId }}} **
+//
+// Returns a temporary id that starts with _
+OB.Utilities.getTemporaryId = function () {
+  return '_' + new Date().getTime();
 };
\ No newline at end of file
diff -r 59e1a50dd801 -r 92bb52d9be49 modules/org.openbravo.client.application/web/org.openbravo.client.application/js/utilities/ob-view-manager.js
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/utilities/ob-view-manager.js	Thu Dec 17 16:47:54 2015 +0000
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/utilities/ob-view-manager.js	Mon Dec 21 08:53:22 2015 +0100
@@ -118,7 +118,7 @@
       var layout = OB.Utilities.createLoadingLayout();
       // is used to prevent history updating
       layout.isLoadingTab = true;
-      viewTabId = viewTabId || params.currentViewTabId || '_' + new Date().getTime();
+      viewTabId = viewTabId || params.currentViewTabId || OB.Utilities.getTemporaryId();
       params.loadingTabId = viewTabId;
       this.createTab(viewId, viewTabId, layout, params);
       return params;
diff -r 59e1a50dd801 -r 92bb52d9be49 modules/org.openbravo.userinterface.selector/web/org.openbravo.userinterface.selector/js/ob-selector-item.js
--- a/modules/org.openbravo.userinterface.selector/web/org.openbravo.userinterface.selector/js/ob-selector-item.js	Thu Dec 17 16:47:54 2015 +0000
+++ b/modules/org.openbravo.userinterface.selector/web/org.openbravo.userinterface.selector/js/ob-selector-item.js	Mon Dec 21 08:53:22 2015 +0100
@@ -518,7 +518,8 @@
   setEntries: function (entries) {
     var length = entries.length,
         i, id, identifier, valueMap = {},
-        valueMapData = [];
+        valueMapElement, valueField = this.getValueFieldName(),
+        valueFieldContent, valueMapData = [];
 
     if (!this.setValueMap) {
       return;
@@ -529,10 +530,20 @@
       identifier = entries[i][OB.Constants.IDENTIFIER] || '';
       valueMap[id] = identifier;
 
-      valueMapData.push({
-        _identifier: identifier,
-        id: id
-      });
+      valueMapElement = {};
+      valueMapElement[OB.Constants.IDENTIFIER] = identifier;
+      valueMapElement[OB.Constants.ID] = id;
+
+      // We include the value field into the entry.
+      // With this we avoid to retrieve an incorrect value from mapValueToDisplay() when looking for undefined values.
+      // This could happen when creating a new record in form view, after clearing the values.
+      // See issue https://issues.openbravo.com/view.php?id=31331
+      if (valueField) {
+        valueFieldContent = entries[i][valueField] || '';
+        valueMapElement[valueField] = valueFieldContent;
+      }
+
+      valueMapData.push(valueMapElement);
     }
 
     this.wholeMapSet = true; // flag to use local filtering from now on

