Fixes bug 28747,related with bug 28427: Number entered with formula is rounded
authorAugusto Mauch <augusto.mauch@openbravo.com>
Mon, 26 Jan 2015 12:39:25 +0100
changeset 24122 e18688c19880
parent 24121 3a018e1f7069
child 24123 d6b21dfcf62c
Fixes bug 28747,related with bug 28427: Number entered with formula is rounded

The problem was that if a number was entered using a formula (i.e. by entering =1/3 in the form item), the value was not rounded using the numeric field mask. For non-formula inputs the validation is done at the beginning of the blur function. For formula inputs it is not possible to do it at that point, because the value of the field is still the formula, not the result of evaluating the formula. The formula is evaluated in the validate function(), so the rounding for formula inputs is done after invoking it.
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/form/formitem/ob-formitem-number.js
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/form/formitem/ob-formitem-number.js	Thu Jan 22 11:14:24 2015 +0100
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/form/formitem/ob-formitem-number.js	Mon Jan 26 12:39:25 2015 +0100
@@ -11,7 +11,7 @@
  * under the License.
  * The Original Code is Openbravo ERP.
  * The Initial Developer of the Original Code is Openbravo SLU
- * All portions are Copyright (C) 2011-2013 Openbravo SLU
+ * All portions are Copyright (C) 2011-2015 Openbravo SLU
  * All Rights Reserved.
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -429,7 +429,7 @@
   },
 
   blur: function () {
-    var value, roundedValue, textRoundedValue;
+    var value, roundedValue, textRoundedValue, isFormula = false;
 
     // Make sure the number is rounded using the number of decimal digits specified in the number typeInstance
     if (isc.isA.String(this.getValue())) {
@@ -438,12 +438,15 @@
         value = OB.Utilities.Number.OBPlainToOBMasked(this.getValue(), this.typeInstance.maskNumeric, this.typeInstance.decSeparator, this.typeInstance.groupSeparator, OB.Format.defaultGroupingSize);
       } else {
         value = this.getValue();
+	isFormula = true;
       }
       this.setValue(OB.Utilities.Number.OBMaskedToJS(value, this.typeInstance.decSeparator, this.typeInstance.groupSeparator));
       if (this.form.setTextualValue) {
         this.form.setTextualValue(this.name, value, this.typeInstance);
       }
-    }
+    } else {
+      value = this.roundJsNumberUsingTypeInstance(this.getValue(), this.typeInstance);
+      this.setValue(value);
 
     if (this.grid && this.grid.isEditing && this.grid.isEditing()) {
       this.grid.setEditValue(this.grid.getEditRow(), this.name, this.getValue(), true, true);
@@ -463,7 +466,11 @@
       this.validate();
 
       value = this.getValue();
-
+      if (isFormula) {
+        // the formula is evaluated in the validate function, so until then it is not possible to round it, do it now
+        value = this.roundJsNumberUsingTypeInstance(value, this.typeInstance);
+        this.setValue(value);
+      }
       // first check if the number is valid
       if (!isc.isA.String(value)) {
         // format the value to be displayed.
@@ -472,6 +479,11 @@
       }
     }
     return this.Super('blur', arguments);
+  },
+
+  roundJsNumberUsingTypeInstance: function (jsNumber, typeInstance) {
+    var roundedStringNumber = OB.Utilities.Number.JSToOBMasked(jsNumber, typeInstance.maskNumeric, typeInstance.decSeparator, typeInstance.groupSeparator);
+    return OB.Utilities.Number.OBMaskedToJS(roundedStringNumber, typeInstance.decSeparator, typeInstance.groupSeparator);
   }
 });
 
@@ -638,4 +650,4 @@
     }
     return value;
   }
-});
\ No newline at end of file
+});