From 1690f317bafe6f5293f93e329cf2d57622879fa5 Mon Sep 17 00:00:00 2001 From: Martin Geubelle Date: Wed, 26 Apr 2017 16:36:23 +0200 Subject: [PATCH] [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. --- addons/mrp/static/src/js/mrp.js | 100 +++++++++++++++++++-------- addons/mrp/static/tests/mrp_tests.js | 35 ++++++++++ 2 files changed, 105 insertions(+), 30 deletions(-) diff --git a/addons/mrp/static/src/js/mrp.js b/addons/mrp/static/src/js/mrp.js index 0b26a27a1d1..1dd3210270e 100644 --- a/addons/mrp/static/src/js/mrp.js +++ b/addons/mrp/static/src/js/mrp.js @@ -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($('' + moment.utc(this.duration).format("HH:mm:ss") + '')); }, - 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 diff --git a/addons/mrp/static/tests/mrp_tests.js b/addons/mrp/static/tests/mrp_tests.js index dd1c3c499c6..eddfafbef90 100644 --- a/addons/mrp/static/tests/mrp_tests.js +++ b/addons/mrp/static/tests/mrp_tests.js @@ -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: + '
' + + '' + + '', + 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(); + }); }); });