Apply factor on domain parser

issue7642
review68301002
This commit is contained in:
Cédric Krier
2018-08-30 16:05:38 +02:00
parent 2bdac9492b
commit 0f2ecee3c4
4 changed files with 60 additions and 29 deletions
+1
View File
@@ -1,3 +1,4 @@
* Apply factor on domain parser
* Use tab name in CSV Export/Import
* Add bus management
* Load custom CSS and Javascript
+13 -7
View File
@@ -1779,28 +1779,31 @@
}
},
'float': function() {
var factor = Number(field.factor || 1);
var result = Number(value);
if (isNaN(result) || value === '' || value === null) {
return null;
} else {
return result;
return result / factor;
}
},
'integer': function() {
var factor = Number(field.factor || 1, 10);
var result = parseInt(value, 10);
if (isNaN(result)) {
return null;
} else {
return result;
return result / factor;
}
},
'numeric': function() {
var result = new Sao.Decimal(value);
var factor = Number(field.factor || 1);
var result = Number(value);
if (isNaN(result.valueOf()) ||
value === '' || value === null) {
return null;
} else {
return result;
return new Sao.Decimal(result / factor);
}
},
'selection': convert_selection,
@@ -1855,13 +1858,15 @@
if (!value && value !== 0 && value !== new Sao.Decimal(0)) {
return '';
}
var digit = String(value).split('.')[1];
var factor = Number(field.factor || 1);
var digit = String(value * factor)
.replace(/0+$/, '').split('.')[1];
if (digit) {
digit = digit.length;
} else {
digit = 0;
}
return value.toFixed(digit);
return (value * factor).toFixed(digit);
};
var format_selection = function() {
for (var i = 0; i < field.selection.length; i++) {
@@ -1894,8 +1899,9 @@
}
},
'integer': function() {
var factor = Number(field.factor || 1);
if (value || value === 0) {
return '' + parseInt(value, 10);
return '' + parseInt(parseInt(value, 10) * factor, 10);
} else {
return '';
}
+14 -8
View File
@@ -1493,15 +1493,21 @@
if ('arch' in view_tree) {
// Filter only fields in XML view
var xml_view = jQuery(jQuery.parseXML(view_tree.arch));
var xml_fields = xml_view.find('tree').children()
.filter(function(i, node) {
return node.tagName == 'field';
}).map(function(i, node) {
return node.getAttribute('name');
});
var dom_fields = {};
xml_fields.each(function(k, name) {
dom_fields[name] = fields[name];
xml_view.find('tree').children().each(function(i, node) {
if (node.tagName == 'field') {
var name = node.getAttribute('name');
// If a field is defined multiple times in the XML,
// take only the first definition
if (!(name in dom_fields)) {
dom_fields[name] = fields[name];
['string', 'factor'].forEach(function(attr) {
if (node.getAttribute(attr)) {
dom_fields[name][attr] = node.getAttribute(attr);
}
});
}
}
});
fields = dom_fields;
}
+32 -14
View File
@@ -1309,6 +1309,8 @@
[null, null]
].forEach(test_func, field);
test_func.call({'type': 'float', 'factor': '100'}, ['42', 0.42]);
field = {
'type': 'integer'
};
@@ -1320,6 +1322,23 @@
[null, null]
].forEach(test_func, field);
test_func.call({'type': 'integer', 'factor': '2'}, ['6', 3]);
var test_func_numeric = function(test) {
var value = test[0];
var result = test[1];
value = parser.convert_value(this, value);
if (value !== null) {
value = value.toString();
}
if (result !== null) {
result = result.toString();
}
QUnit.strictEqual(value, result,
'convert_value(' + JSON.stringify(this) + ', ' +
JSON.stringify(test[0]) + ')');
};
field = {
'type': 'numeric'
};
@@ -1329,20 +1348,11 @@
['', null],
['test', null],
[null, null]
].forEach(function(test) {
var value = test[0];
var result = test[1];
value = parser.convert_value(field, value);
if (value !== null) {
value = value.toString();
}
if (result !== null) {
result = result.toString();
}
QUnit.strictEqual(value, result,
'convert_value(' + JSON.stringify(field) + ', ' +
JSON.stringify(test[0]) + ')');
});
].forEach(test_func_numeric, field);
test_func_numeric.call(
{'type': 'numeric', 'factor': '5'},
['1', new Sao.Decimal('0.2')]);
field = {
'type': 'selection',
@@ -1523,6 +1533,8 @@
[null, '']
].forEach(test_func, field);
test_func.call({'type': 'integer', 'factor': '2'}, [3, '6']);
field = {
'type': 'float'
};
@@ -1537,6 +1549,8 @@
[null, '']
].forEach(test_func, field);
test_func.call({'type': 'float', 'factor': '100'}, [0.42, '42']);
field = {
'type': 'numeric'
};
@@ -1551,6 +1565,10 @@
[null, '']
].forEach(test_func, field);
test_func.call(
{'type': 'numeric', 'factor': '5'},
[new Sao.Decimal('0.2'), '1']);
field = {
'type': 'selection',
'selection': [