From b9d9c6e3bc8d3fd4bb547aadf002652bb7bb9db4 Mon Sep 17 00:00:00 2001 From: qsm-odoo Date: Fri, 31 Mar 2017 14:18:22 +0200 Subject: [FIX] web: monetary should reset on currency change This reverts commit 839a07fbed4720fc71c070507c591295722df350. Indeed, the system will need the `resetOnAnyFieldChange` option as the `FieldMonetary` will need to be re-rendered if the currency changes. This does not use the `_fetchSpecialData` system as it will be refactored later and the currency can be fetched without RPC. --- addons/web/static/src/js/fields/basic_fields.js | 49 ++++++++++++++++--------- 1 file changed, 32 insertions(+), 17 deletions(-) (limited to 'addons/web/static/src/js/fields/basic_fields.js') diff --git a/addons/web/static/src/js/fields/basic_fields.js b/addons/web/static/src/js/fields/basic_fields.js index ffba3158d43..1b96d585ac7 100644 --- a/addons/web/static/src/js/fields/basic_fields.js +++ b/addons/web/static/src/js/fields/basic_fields.js @@ -313,6 +313,7 @@ var FieldMonetary = InputField.extend({ className: 'o_form_field_monetary o_list_number', replace_element: true, supportedFieldTypes: ['float', 'monetary'], + resetOnAnyFieldChange: true, // Have to listen to currency changes /** * Float fields using a monetary widget have an additional currency_field @@ -331,18 +332,9 @@ var FieldMonetary = InputField.extend({ init: function () { this._super.apply(this, arguments); - // Options for _formatValue - this.nodeOptions.digits = [16,2]; - if (this.attrs.currency_field) { - this.nodeOptions.currency_field = this.attrs.currency_field; - } else { - this.nodeOptions.currency_field = this.field.currency_field || 'currency_id'; - } - if (this.record.data[this.nodeOptions.currency_field]) { - this.nodeOptions.currency_id = this.record.data[this.nodeOptions.currency_field].res_id; - } + this._setCurrency(); - if (this.mode === 'edit' && this.nodeOptions.currency_id) { + if (this.mode === 'edit' && this.currency) { this.tagName = 'div'; } }, @@ -372,7 +364,7 @@ var FieldMonetary = InputField.extend({ * @private */ _renderEdit: function () { - if (!this.nodeOptions.currency_id) { + if (!this.currency) { this._super.apply(this, arguments); return; } @@ -381,15 +373,35 @@ var FieldMonetary = InputField.extend({ // Prepare and add the input this._prepareInput().appendTo(this.$el); // prepare and add the currency symbol - var currency = session.get_currency(this.nodeOptions.currency_id); - var $currencySymbol = $('', {text: currency.symbol}); - if (currency.position === "after") { + var $currencySymbol = $('', {text: this.currency.symbol}); + if (this.currency.position === "after") { this.$el.append($currencySymbol); } else { this.$el.prepend($currencySymbol); } }, - + /** + * Re-gets the currency as its value may have changed. + * @see FieldMonetary.resetOnAnyFieldChange + * + * @override + * @private + */ + _reset: function () { + this._super.apply(this, arguments); + this._setCurrency(); + }, + /** + * Deduces the currency description from the field options and view state. + * The description is then available at this.currency. + * + * @private + */ + _setCurrency: function () { + var currencyField = this.attrs.currency_field || this.field.currency_field || 'currency_id'; + var currencyID = this.record.data[currencyField] && this.record.data[currencyField].res_id; + this.currency = session.get_currency(currencyID); + }, /** * FieldMonetary overrides _formatValue to use the format monetary method * in readonly. @@ -400,7 +412,10 @@ var FieldMonetary = InputField.extend({ */ _formatValue: function (value) { var format = field_utils.format[this.mode === 'edit' ? "float" : "monetary"]; - return format(value, this.field, this.nodeOptions); + return format(value, this.field, { + digits: [16, 2], + currency: this.currency, + }); }, }); -- cgit v1.2.3