diff options
| author | Cedric Snauwaert <csn@openerp.com> | 2017-06-29 10:17:42 +0200 |
|---|---|---|
| committer | Cedric Snauwaert <csn@openerp.com> | 2017-06-29 16:37:30 +0200 |
| commit | 07fdcad3439716f4fbe91cadf31ccfca3a49b05f (patch) | |
| tree | 7e79d645331f22cbbe0af4ab93d1674a354ded01 /addons/web/static/src/js | |
| parent | 8473bde80e6609d552af8b00396c72a20bd3c47f (diff) | |
[ADD] web: add parse monetary method
In field_utils, the parse monetary method was remapped to parsefloat. In this commit, we add a specific parseMonetary method to handle currency specific formatting.
Diffstat (limited to 'addons/web/static/src/js')
| -rw-r--r-- | addons/web/static/src/js/fields/field_utils.js | 48 |
1 files changed, 47 insertions, 1 deletions
diff --git a/addons/web/static/src/js/fields/field_utils.js b/addons/web/static/src/js/fields/field_utils.js index becb1e124ef..ac4c06454ea 100644 --- a/addons/web/static/src/js/fields/field_utils.js +++ b/addons/web/static/src/js/fields/field_utils.js @@ -427,6 +427,52 @@ function parseFloat(value) { return parsed; } +/** + * Parse a String containing currency symbol and returns amount + * + * @param {string} value + * The string to be parsed + * We assume that a monetary is always a pair (symbol, amount) separated + * by a non breaking space. A simple float can also be accepted as value + * @param {Object} [field] + * a description of the field (returned by fields_get for example). + * @param {Object} [options] additional options. + * @param {Object} [options.currency] - the description of the currency to use + * @param {integer} [options.currency_id] + * the id of the 'res.currency' to use (ignored if options.currency) + * @param {string} [options.currency_field] + * the name of the field whose value is the currency id + * (ignore if options.currency or options.currency_id) + * Note: if not given it will default to the field currency_field value + * or to 'currency_id'. + * @param {Object} [options.data] + * a mapping of field name to field value, required with + * options.currency_field + * + * @returns {float} the float value contained in the string representation + * @throws {Error} if no float is found or if parameter does not respect monetary condition + */ +function parseMonetary(value, field, options) { + var values = value.split(' '); + if (values.length === 1) { + return parseFloat(value); + } + else if (values.length !== 2) { + throw new Error(_.str.sprintf(core._t("'%s' is not a correct monetary field"), value)); + } + options = options || {}; + var currency = options.currency; + if (!currency) { + var currency_id = options.currency_id; + if (!currency_id && options.data) { + var currency_field = options.currency_field || field.currency_field || 'currency_id'; + currency_id = options.data[currency_field] && options.data[currency_field].res_id; + } + currency = session.get_currency(currency_id); + } + return parseFloat(values[0] === currency.symbol ? values[1] : values[0]); +} + function parseFloatTime(value) { var factor = 1; if (value[0] === '-') { @@ -510,7 +556,7 @@ return { integer: parseInteger, many2many: _.identity, // todo many2one: parseMany2one, - monetary: parseFloat, + monetary: parseMonetary, one2many: _.identity, reference: parseMany2one, selection: _.identity, // todo |
