summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/fields/field_utils.js
diff options
context:
space:
mode:
authorChristophe Simonis <chs@odoo.com>2017-07-07 14:30:30 +0200
committerChristophe Simonis <chs@odoo.com>2017-07-07 14:30:30 +0200
commitbf51fe55a149fb642a410f579dc4b6dcbd794eac (patch)
treef4f208f06094bcef6541fd1fc034e0265de4ad21 /addons/web/static/src/js/fields/field_utils.js
parentbf32940142beb879211b7034bb77a7845c9d0555 (diff)
parent0f2b2c4e2b99df8d68456016a4bea86e26c1c80e (diff)
[MERGE] forward port branch saas-16 up to 0f2b2c4e2b9
Diffstat (limited to 'addons/web/static/src/js/fields/field_utils.js')
-rw-r--r--addons/web/static/src/js/fields/field_utils.js52
1 files changed, 49 insertions, 3 deletions
diff --git a/addons/web/static/src/js/fields/field_utils.js b/addons/web/static/src/js/fields/field_utils.js
index 9a6734b7baf..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('&nbsp;');
+ 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] === '-') {
@@ -494,7 +540,7 @@ return {
many2one: formatMany2one,
monetary: formatMonetary,
one2many: formatX2Many,
- reference: _.identity, // todo
+ reference: formatMany2one,
selection: formatSelection,
text: formatChar,
},
@@ -510,9 +556,9 @@ return {
integer: parseInteger,
many2many: _.identity, // todo
many2one: parseMany2one,
- monetary: parseFloat,
+ monetary: parseMonetary,
one2many: _.identity,
- reference: _.identity, // todo
+ reference: parseMany2one,
selection: _.identity, // todo
text: _.identity, // todo
},