diff options
| author | Cedric Snauwaert <csn@openerp.com> | 2019-07-03 08:58:30 +0000 |
|---|---|---|
| committer | Cedric Snauwaert <csn@openerp.com> | 2019-07-09 07:45:42 +0000 |
| commit | 9cab694bf594f6c7c8d795174d9159ac82f526b2 (patch) | |
| tree | 59c3726d041e72f92eb40d7b123d96e63eb26445 | |
| parent | 38753f95d9890171e8c9d81f29ae0b1b1eafa9cc (diff) | |
[IMP] web: add simple calculator on numeric fields
Add a new behavior to numeric fields, possibility to enter value manually or to use a simple arithmetic expression that will be evaluated.
Example: =20+3*2
In order for the computation of the arithmetic expression to be done, the value needs to starts with = and then an expression can be entered. We only support the basics mathematical operations + - * / ( ) ^
User locale and digits separator are taken into account. If the value entered can't be evaluated to a numeric, the field will be displayed in error.
closes odoo/odoo#34538
Signed-off-by: Aaron Bohy (aab) <aab@odoo.com>
| -rw-r--r-- | addons/web/static/lib/py.js/lib/py.js | 6 | ||||
| -rw-r--r-- | addons/web/static/src/js/fields/basic_fields.js | 69 | ||||
| -rw-r--r-- | addons/web/static/tests/core/py_utils_tests.js | 4 | ||||
| -rw-r--r-- | addons/web/static/tests/fields/basic_fields_tests.js | 130 |
4 files changed, 206 insertions, 3 deletions
diff --git a/addons/web/static/lib/py.js/lib/py.js b/addons/web/static/lib/py.js/lib/py.js index 60b80a7f439..645e668f5c1 100644 --- a/addons/web/static/lib/py.js/lib/py.js +++ b/addons/web/static/lib/py.js/lib/py.js @@ -882,6 +882,12 @@ var py = {}; } return py.float.fromJSON(this._value * other._value); }, + __pow__: function (other) { + if (!py.PY_isInstance(other, py.float)) { + return py.NotImplemented; + } + return py.float.fromJSON(this._value ** other._value); + }, __div__: function (other) { if (!py.PY_isInstance(other, py.float)) { return py.NotImplemented; diff --git a/addons/web/static/src/js/fields/basic_fields.js b/addons/web/static/src/js/fields/basic_fields.js index ba6fdc1aab6..715cb86d623 100644 --- a/addons/web/static/src/js/fields/basic_fields.js +++ b/addons/web/static/src/js/fields/basic_fields.js @@ -17,6 +17,7 @@ var Domain = require('web.Domain'); var DomainSelector = require('web.DomainSelector'); var DomainSelectorDialog = require('web.DomainSelectorDialog'); var framework = require('web.framework'); +var py_utils = require('web.py_utils'); var session = require('web.session'); var utils = require('web.utils'); var view_dialogs = require('web.view_dialogs'); @@ -371,6 +372,41 @@ var NumericField = InputField.extend({ // Private //-------------------------------------------------------------------------- + /** + * Evaluate a string representing a simple formula, + * a formula is composed of numbers and arithmetic operations + * (ex: 4+3*2) + * + * Supported arithmetic operations: + - * / ^ ( ) + * Since each number in the formula can be expressed in user locale, + * we parse each float value inside the formula using the user context + * This function uses py_eval to safe eval the formula. + * We assume that this function is used as a calculator so operand ^ (xor) + * is replaced by operand ** (power) so that users that are used to + * excel or libreoffice are not confused + * + * @private + * @param expr + * @return a float representing the result of the evaluated formula + * @throws error if formula can't be evaluated + */ + _evalFormula: function (expr, context) { + // remove extra space + var val = expr.replace(new RegExp(/( )/g), ''); + var safeEvalString = ''; + for (let v of val.split(new RegExp(/([-+*/()^])/g))) { + if (!['+','-','*','/','(',')','^'].includes(v) && v.length) { + // check if this is a float and take into account user delimiter preference + v = field_utils.parse.float(v); + } + if (v === '^') { + v = '**'; + } + safeEvalString += v; + }; + return py_utils.py_eval(safeEvalString, context); + }, + /** * Format numerical value (integer or float) * @@ -406,7 +442,36 @@ var NumericField = InputField.extend({ this.$input.attr({step: this.nodeOptions.step}); } return result; - } + }, + + /** + * Evaluate value set by user if starts with = + * + * @override + * @private + * @param {any} value + * @param {Object} [options] + */ + _setValue: function (value, options) { + var originalValue = value; + value = value.trim(); + if (value.startsWith('=')) { + try { + // Evaluate the formula + value = this._evalFormula(value.substr(1)); + // Format back the value in user locale + value = this._formatValue(value); + // Set the computed value in the input + this.$input.val(value); + } catch (err) { + // in case of exception, set value as the original value + // that way the Webclient will show an error as + // it is expecting a numeric value. + value = originalValue; + } + } + return this._super(value, options); + }, }); var FieldChar = InputField.extend(TranslatableFieldMixin, { @@ -665,7 +730,7 @@ var FieldDateTime = FieldDate.extend({ }, }); -var FieldMonetary = InputField.extend({ +var FieldMonetary = NumericField.extend({ description: _lt("Monetary"), className: 'o_field_monetary o_field_number', tagName: 'span', diff --git a/addons/web/static/tests/core/py_utils_tests.js b/addons/web/static/tests/core/py_utils_tests.js index 8de80fbb0d7..1a88ece088a 100644 --- a/addons/web/static/tests/core/py_utils_tests.js +++ b/addons/web/static/tests/core/py_utils_tests.js @@ -31,12 +31,14 @@ QUnit.module('core', function () { }); QUnit.test('simple arithmetic', function(assert) { - assert.expect(2); + assert.expect(3); var result = pyUtils.py_eval("1 + 2"); assert.strictEqual(result, 3, "should properly evaluate sum"); result = pyUtils.py_eval("42 % 5"); assert.strictEqual(result, 2, "should properly evaluate modulo operator"); + result = pyUtils.py_eval("2 ** 3"); + assert.strictEqual(result, 8, "should properly evaluate power operator"); }); diff --git a/addons/web/static/tests/fields/basic_fields_tests.js b/addons/web/static/tests/fields/basic_fields_tests.js index 4e5deb56780..c65873cc2c6 100644 --- a/addons/web/static/tests/fields/basic_fields_tests.js +++ b/addons/web/static/tests/fields/basic_fields_tests.js @@ -564,6 +564,86 @@ QUnit.module('basic_fields', { form.destroy(); }); + QUnit.test('float field using formula in form view', async function (assert) { + assert.expect(4); + + var form = await createView({ + View: FormView, + model: 'partner', + data: this.data, + arch:'<form string="Partners">' + + '<sheet>' + + '<field name="qux" widget="float" digits="[5,3]"/>' + + '</sheet>' + + '</form>', + res_id: 2, + }); + + // Test computation with priority of operation + await testUtils.form.clickEdit(form); + await testUtils.fields.editInput(form.$('input[name=qux]'), '=20+3*2'); + await testUtils.form.clickSave(form); + assert.strictEqual(form.$('.o_field_widget').first().text(), '26.000', + 'The new value should be calculated properly.'); + + // Test computation with ** operand + await testUtils.form.clickEdit(form); + await testUtils.fields.editInput(form.$('input[name=qux]'), '=2**3'); + await testUtils.form.clickSave(form); + assert.strictEqual(form.$('.o_field_widget').first().text(), '8.000', + 'The new value should be calculated properly.'); + + // Test computation with ^ operant which should do the same as ** + await testUtils.form.clickEdit(form); + await testUtils.fields.editInput(form.$('input[name=qux]'), '=2^3'); + await testUtils.form.clickSave(form); + assert.strictEqual(form.$('.o_field_widget').first().text(), '8.000', + 'The new value should be calculated properly.'); + + // Test computation and rounding + await testUtils.form.clickEdit(form); + await testUtils.fields.editInput(form.$('input[name=qux]'), '=100/3'); + await testUtils.form.clickSave(form); + assert.strictEqual(form.$('.o_field_widget').first().text(), '33.333', + 'The new value should be calculated properly.'); + + form.destroy(); + }); + + QUnit.test('float field using incorrect formula in form view', async function (assert) { + assert.expect(4); + + var form = await createView({ + View: FormView, + model: 'partner', + data: this.data, + arch:'<form string="Partners">' + + '<sheet>' + + '<field name="qux" widget="float" digits="[5,3]"/>' + + '</sheet>' + + '</form>', + res_id: 2, + }); + + // Test that incorrect value is not computed + await testUtils.form.clickEdit(form); + await testUtils.fields.editInput(form.$('input[name=qux]'), '=abc'); + await testUtils.form.clickSave(form); + assert.hasClass(form.$('.o_form_view'),'o_form_editable', + "form view should still be editable"); + assert.hasClass(form.$('input[name=qux]'),'o_field_invalid', + "fload field should be displayed as invalid"); + + await testUtils.fields.editInput(form.$('input[name=qux]'), '=3:2?+4'); + await testUtils.form.clickSave(form); + assert.hasClass(form.$('.o_form_view'),'o_form_editable', + "form view should still be editable"); + assert.hasClass(form.$('input[name=qux]'),'o_field_invalid', + "float field should be displayed as invalid"); + + form.destroy(); + }); + QUnit.test('float field in editable list view', async function (assert) { assert.expect(4); @@ -3390,6 +3470,35 @@ QUnit.module('basic_fields', { form.destroy(); }); + QUnit.test('monetary field rounding using formula in form view', async function (assert) { + assert.expect(1); + + var form = await createView({ + View: FormView, + model: 'partner', + data: this.data, + arch:'<form string="Partners">' + + '<sheet>' + + '<field name="qux" widget="monetary"/>' + + '<field name="currency_id" invisible="1"/>' + + '</sheet>' + + '</form>', + res_id: 5, + session: { + currencies: _.indexBy(this.data.currency.records, 'id'), + }, + }); + + // Test computation and rounding + await testUtils.form.clickEdit(form); + await testUtils.fields.editInput(form.$('.o_field_monetary input'), '=100/3'); + await testUtils.form.clickSave(form); + assert.strictEqual(form.$('.o_field_widget').first().text(), '$\u00a033.33', + 'The new value should be calculated and rounded properly.'); + + form.destroy(); + }); + QUnit.test('monetary field with currency symbol after', async function (assert) { assert.expect(5); @@ -3800,6 +3909,27 @@ QUnit.module('basic_fields', { form.destroy(); }); + QUnit.test('integer field rounding using formula in form view', async function (assert) { + assert.expect(1); + + var form = await createView({ + View: FormView, + model: 'partner', + data: this.data, + arch:'<form string="Partners"><field name="int_field"/></form>', + res_id: 2, + }); + + // Test computation and rounding + await testUtils.form.clickEdit(form); + await testUtils.fields.editInput(form.$('input[name=int_field]'), '=100/3'); + await testUtils.form.clickSave(form); + assert.strictEqual(form.$('.o_field_widget').first().text(), '33', + 'The new value should be calculated properly.'); + + form.destroy(); + }); + QUnit.test('integer field in form view with virtual id', async function (assert) { assert.expect(1); var params = { |
