odoo.define('mrp.mrp_state', function (require) {
"use strict";
var AbstractField = require('web.AbstractField');
var core = require('web.core');
var field_registry = require('web.field_registry');
var time = require('web.time');
var _t = core._t;
/**
* This widget is used to display the availability on a workorder.
*/
var SetBulletStatus = AbstractField.extend({
// as this widget is based on hardcoded values, use it in another context
// probably won't work
// supportedFieldTypes: ['selection'],
/**
* @override
*/
init: function () {
this._super.apply(this, arguments);
this.classes = this.nodeOptions && this.nodeOptions.classes || {};
},
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* @private
* @override
*/
_renderReadonly: function () {
this._super.apply(this, arguments);
var bullet_class = this.classes[this.value] || 'default';
if (this.value) {
var title = this.value === 'waiting' ? _t('Waiting Materials') : _t('Ready to produce');
this.$el.attr({'title': title, 'style': 'display:inline'});
this.$el.removeClass('text-success text-danger text-default');
this.$el.html($('' + title + '').addClass('badge badge-' + bullet_class));
}
}
});
var TimeCounter = AbstractField.extend({
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 Promise.all([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.timer = setTimeout(function () {
self.duration += 1000;
self._startTimeCounter();
}, 1000);
} else {
clearTimeout(this.timer);
}
this.$el.html($('' + moment.utc(this.duration).format("HH:mm:ss") + ''));
},
});
field_registry
.add('bullet_state', SetBulletStatus)
.add('mrp_time_counter', TimeCounter);
});