[FIX] mrp: adapt mrp_time_counter fields widget

This widget behaviour hadn't correctly implemented with the new views.
This commit also introduces some tests.
This commit is contained in:
Martin Geubelle
2017-04-27 16:56:48 +02:00
parent 5a78f4bad7
commit 1690f317ba
2 changed files with 105 additions and 30 deletions
+70 -30
View File
@@ -152,46 +152,86 @@ var SetBulletStatus = AbstractField.extend({
});
var TimeCounter = AbstractField.extend({
start_time_counter: function () {
supportedFieldTypes: [],
/**
* @override
*/
willStart: function () {
var self = this;
var def = this._rpc({
model: 'mrp.workcenter.productivity',
method: 'search_read',
domain: [
['workorder_id', '=', this.record.data.id],
['user_id', '=', this.getSession().uid],
],
}).then(function (result) {
if (self.mode === 'readonly') {
var currentDate = new Date();
self.duration = 0;
_.each(result, function (data) {
self.duration += data.date_end ?
self._getDateDifference(data.date_start, data.date_end) :
self._getDateDifference(time.auto_str_to_date(data.date_start), currentDate);
});
}
});
return $.when(this._super.apply(this, arguments), def);
},
destroy: function () {
this._super.apply(this, arguments);
clearTimeout(this.timer);
},
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
/**
* @override
*/
isSet: function () {
return true;
},
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* Compute the difference between two dates.
*
* @private
* @param {string} dateStart
* @param {string} dateEnd
* @returns {integer} the difference in millisecond
*/
_getDateDifference: function (dateStart, dateEnd) {
return moment(dateEnd).diff(moment(dateStart));
},
/**
* @override
*/
_render: function () {
this._startTimeCounter();
},
/**
* @private
*/
_startTimeCounter: function () {
var self = this;
clearTimeout(this.timer);
if (this.record.data.is_user_working) {
this.duration += 1000;
this.timer = setTimeout(function () {
self.start_time_counter();
self.duration += 1000;
self._startTimeCounter();
}, 1000);
} else {
clearTimeout(this.timer);
}
this.$el.html($('<span>' + moment.utc(this.duration).format("HH:mm:ss") + '</span>'));
},
render: function () {
var self = this;
this._super.apply(this, arguments);
var productivity_domain = [['workorder_id', '=', this.record.data.id], ['user_id', '=', self.session.uid]];
this.trigger_up('perform_model_rpc', {
method: 'search_read',
model: 'mrp.workcenter.productivity',
args: [productivity_domain, []],
on_success: function (result) {
if (self.mode === "readonly") {
var current_date = new Date();
self.duration = 0;
_.each(result, function (data) {
self.duration += data.date_end ? self.get_date_difference(data.date_start, data.date_end) : self.get_date_difference(time.auto_str_to_date(data.date_start), current_date);
});
self.start_time_counter();
}
},
});
},
get_date_difference: function (date_start, date_end) {
var difference = moment(date_end).diff(moment(date_start));
return moment.duration(difference);
},
has_no_value: function () {
return false;
}
});
field_registry
+35
View File
@@ -17,11 +17,13 @@ QUnit.module('mrp', {
selection: [['waiting', 'Waiting'], ['chilling', 'Chilling']],
},
document: {string: "Document", type: "binary"},
duration: {string: "Duration", type: "float"},
},
records: [{
id: 1,
document: 'coucou==\n',
state: 'waiting',
duration: 6000,
}],
onchanges: {},
},
@@ -103,5 +105,38 @@ QUnit.module('mrp', {
form.destroy();
});
QUnit.test("mrp_time_counter: basic rendering", function (assert) {
assert.expect(2);
var form = createView({
View: FormView,
model: 'partner',
data: this.data,
res_id: 1,
arch:
'<form>' +
'<field name="duration" widget="mrp_time_counter"/>' +
'</form>',
mockRPC: function (route, args) {
if (args.method === 'search_read' && args.model === 'mrp.workcenter.productivity') {
assert.ok(true, "the widget should fetch the mrp.workcenter.productivity");
return $.when([{
date_start: '2017-01-01 08:00:00',
date_end: '2017-01-01 10:00:00',
}, {
date_start: '2017-01-01 12:00:00',
date_end: '2017-01-01 12:30:00',
}]);
}
return this._super.apply(this, arguments);
},
});
assert.strictEqual(form.$('.o_form_field[name="duration"]').text(), "02:30:00",
"the timer should be correctly set");
form.destroy();
});
});
});