summaryrefslogtreecommitdiff
path: root/addons/hr_holidays/static/src/js
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
commit3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch)
treea44932296ef4a9b71d5f010906253d8c53727726 /addons/hr_holidays/static/src/js
parent0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff)
initial commit 2
Diffstat (limited to 'addons/hr_holidays/static/src/js')
-rw-r--r--addons/hr_holidays/static/src/js/leave_stats_widget.js153
-rw-r--r--addons/hr_holidays/static/src/js/time_off_calendar.js182
2 files changed, 335 insertions, 0 deletions
diff --git a/addons/hr_holidays/static/src/js/leave_stats_widget.js b/addons/hr_holidays/static/src/js/leave_stats_widget.js
new file mode 100644
index 00000000..2d7f5d64
--- /dev/null
+++ b/addons/hr_holidays/static/src/js/leave_stats_widget.js
@@ -0,0 +1,153 @@
+odoo.define('hr_holidays.LeaveStatsWidget', function (require) {
+ "use strict";
+
+ var time = require('web.time');
+ var Widget = require('web.Widget');
+ var widget_registry = require('web.widget_registry');
+
+ var LeaveStatsWidget = Widget.extend({
+ template: 'hr_holidays.leave_stats',
+
+ /**
+ * @override
+ * @param {Widget|null} parent
+ * @param {Object} params
+ */
+ init: function (parent, params) {
+ this._setState(params);
+ this._super(parent);
+ },
+
+ //--------------------------------------------------------------------------
+ // Public
+ //--------------------------------------------------------------------------
+
+ /**
+ * @override to fetch data before rendering.
+ */
+ willStart: function () {
+ return Promise.all([this._super(), this._fetchLeaveTypesData(), this._fetchDepartmentLeaves()]);
+ },
+
+ /**
+ * Fetch new data if needed (according to updated fields) and re-render the widget.
+ * Called by the basic renderer when the view changes.
+ * @param {Object} state
+ * @returns {Promise}
+ */
+ updateState: function (state) {
+ var self = this;
+ var to_await = [];
+ var updatedFields = this._setState(state);
+
+ if (_.intersection(updatedFields, ['employee', 'date']).length) {
+ to_await.push(this._fetchLeaveTypesData());
+ }
+ if (_.intersection(updatedFields, ['department', 'date']).length) {
+ to_await.push(this._fetchDepartmentLeaves());
+ }
+ return Promise.all(to_await).then(function () {
+ self.renderElement();
+ });
+ },
+
+ //--------------------------------------------------------------------------
+ // Private
+ //--------------------------------------------------------------------------
+
+ /**
+ * Update the state
+ * @param {Object} state
+ * @returns {String[]} list of updated fields
+ */
+ _setState: function (state) {
+ var updatedFields = [];
+ if (state.data.employee_id.res_id !== (this.employee && this.employee.res_id)) {
+ updatedFields.push('employee');
+ this.employee = state.data.employee_id;
+ }
+ if (state.data.department_id.res_id !== (this.department && this.department.res_id)) {
+ updatedFields.push('department');
+ this.department = state.data.department_id;
+ }
+ if (state.data.date_from !== this.date) {
+ updatedFields.push('date');
+ this.date = state.data.date_from;
+ }
+ return updatedFields;
+ },
+
+ /**
+ * Fetch leaves taken by members of ``this.department`` in the
+ * month of ``this.date``.
+ * Three fields are fetched for each leave, namely: employee_id, date_from
+ * and date_to.
+ * The resulting data is assigned to ``this.departmentLeaves``
+ * @private
+ * @returns {Promise}
+ */
+ _fetchDepartmentLeaves: function () {
+ if (!this.date || !this.department) {
+ this.departmentLeaves = null;
+ return Promise.resolve();
+ }
+ var self = this;
+ var month_date_from = this.date.clone().startOf('month');
+ var month_date_to = this.date.clone().endOf('month');
+ return this._rpc({
+ model: 'hr.leave',
+ method: 'search_read',
+ args: [
+ [['department_id', '=', this.department.res_id],
+ ['state', '=', 'validate'],
+ ['holiday_type', '=', 'employee'],
+ ['date_from', '<=', month_date_to],
+ ['date_to', '>=', month_date_from]],
+ ['employee_id', 'date_from', 'date_to', 'number_of_days'],
+ ],
+ }).then(function (data) {
+ var dateFormat = time.getLangDateFormat();
+ self.departmentLeaves = data.map(function (leave) {
+ // Format datetimes to date (in the user's format)
+ return _.extend(leave, {
+ date_from: moment(leave.date_from).format(dateFormat),
+ date_to: moment(leave.date_to).format(dateFormat),
+ number_of_days: leave.number_of_days,
+ });
+ });
+ });
+ },
+
+ /**
+ * Fetch the number of leaves, grouped by leave type, taken by ``this.employee``
+ * in the year of ``this.date``.
+ * The resulting data is assigned to ``this.leavesPerType``
+ * @private
+ * @returns {Promise}
+ */
+ _fetchLeaveTypesData: function () {
+ if (!this.date || !this.employee) {
+ this.leavesPerType = null;
+ return Promise.resolve();
+ }
+ var self = this;
+ var year_date_from = this.date.clone().startOf('year');
+ var year_date_to = this.date.clone().endOf('year');
+ return this._rpc({
+ model: 'hr.leave',
+ method: 'read_group',
+ kwargs: {
+ domain: [['employee_id', '=', this.employee.res_id], ['state', '=', 'validate'], ['date_from', '<=', year_date_to], ['date_to', '>=', year_date_from]],
+ fields: ['holiday_status_id', 'number_of_days:sum'],
+ groupby: ['holiday_status_id'],
+ },
+ }).then(function (data) {
+ self.leavesPerType = data;
+ });
+ }
+ });
+
+ widget_registry.add('hr_leave_stats', LeaveStatsWidget);
+
+ return LeaveStatsWidget;
+});
diff --git a/addons/hr_holidays/static/src/js/time_off_calendar.js b/addons/hr_holidays/static/src/js/time_off_calendar.js
new file mode 100644
index 00000000..ef9f262d
--- /dev/null
+++ b/addons/hr_holidays/static/src/js/time_off_calendar.js
@@ -0,0 +1,182 @@
+odoo.define('hr_holidays.dashboard.view_custo', function(require) {
+ 'use strict';
+
+ var core = require('web.core');
+ var CalendarPopover = require('web.CalendarPopover');
+ var CalendarController = require("web.CalendarController");
+ var CalendarRenderer = require("web.CalendarRenderer");
+ var CalendarView = require("web.CalendarView");
+ var viewRegistry = require('web.view_registry');
+
+ var _t = core._t;
+ var QWeb = core.qweb;
+
+ var TimeOffCalendarPopover = CalendarPopover.extend({
+ template: 'hr_holidays.calendar.popover',
+
+ init: function (parent, eventInfo) {
+ this._super.apply(this, arguments);
+ const state = this.event.extendedProps.record.state;
+ this.canDelete = state && ['validate', 'refuse'].indexOf(state) === -1;
+ this.canEdit = state !== undefined;
+ this.displayFields = [];
+
+ if (this.modelName === "hr.leave.report.calendar") {
+ const duration = this.event.extendedProps.record.display_name.split(':').slice(-1);
+ this.display_name = _.str.sprintf(_t("Time Off : %s"), duration);
+ } else {
+ this.display_name = this.event.extendedProps.record.display_name;
+ }
+ },
+ });
+
+ var TimeOffCalendarController = CalendarController.extend({
+ events: _.extend({}, CalendarController.prototype.events, {
+ 'click .btn-time-off': '_onNewTimeOff',
+ 'click .btn-allocation': '_onNewAllocation',
+ }),
+
+ /**
+ * @override
+ */
+ start: function () {
+ this.$el.addClass('o_timeoff_calendar');
+ return this._super(...arguments);
+ },
+
+ //--------------------------------------------------------------------------
+ // Public
+ //--------------------------------------------------------------------------
+
+ /**
+ * Render the buttons and add new button about
+ * time off and allocations request
+ *
+ * @override
+ */
+
+ renderButtons: function ($node) {
+ this._super.apply(this, arguments);
+
+ $(QWeb.render('hr_holidays.dashboard.calendar.button', {
+ time_off: _t('New Time Off'),
+ request: _t('New Allocation'),
+ })).appendTo(this.$buttons);
+
+ if ($node) {
+ this.$buttons.appendTo($node);
+ } else {
+ this.$('.o_calendar_buttons').replaceWith(this.$buttons);
+ }
+ },
+
+ //--------------------------------------------------------------------------
+ // Handlers
+ //--------------------------------------------------------------------------
+
+ /**
+ * Action: create a new time off request
+ *
+ * @private
+ */
+ _onNewTimeOff: function () {
+ var self = this;
+
+ this.do_action('hr_holidays.hr_leave_action_my_request', {
+ on_close: function () {
+ self.reload();
+ }
+ });
+ },
+
+ /**
+ * Action: create a new allocation request
+ *
+ * @private
+ */
+ _onNewAllocation: function () {
+ var self = this;
+ this.do_action({
+ type: 'ir.actions.act_window',
+ res_model: 'hr.leave.allocation',
+ name: 'New Allocation Request',
+ views: [[false,'form']],
+ context: {'form_view_ref': 'hr_holidays.hr_leave_allocation_view_form_dashboard'},
+ target: 'new',
+ }, {
+ on_close: function () {
+ self.reload();
+ }
+ });
+ },
+ });
+
+ var TimeOffPopoverRenderer = CalendarRenderer.extend({
+ config: _.extend({}, CalendarRenderer.prototype.config, {
+ CalendarPopover: TimeOffCalendarPopover,
+ }),
+
+ _getPopoverParams: function (eventData) {
+ let params = this._super.apply(this, arguments);
+ let calendarIcon;
+ let state = eventData.extendedProps.record.state;
+
+ if (state === 'validate') {
+ calendarIcon = 'fa-calendar-check-o';
+ } else if (state === 'refuse') {
+ calendarIcon = 'fa-calendar-times-o';
+ } else if(state) {
+ calendarIcon = 'fa-calendar-o';
+ }
+
+ params['title'] = eventData.extendedProps.record.display_name.split(':').slice(0, -1).join(':');
+ params['template'] = QWeb.render('hr_holidays.calendar.popover.placeholder', {color: this.getColor(eventData.color_index), calendarIcon: calendarIcon});
+ return params;
+ },
+
+ _render: function () {
+ var self = this;
+ return this._super.apply(this, arguments).then(function () {
+ self.$el.parent().find('.o_calendar_mini').hide();
+ });
+ },
+ });
+
+ var TimeOffCalendarRenderer = TimeOffPopoverRenderer.extend({
+ _render: function () {
+ var self = this;
+ return this._super.apply(this, arguments).then(function () {
+ return self._rpc({
+ model: 'hr.leave.type',
+ method: 'get_days_all_request',
+ context: self.context,
+ });
+ }).then(function (result) {
+ self.$el.parent().find('.o_calendar_mini').hide();
+ self.$el.parent().find('.o_timeoff_container').remove();
+ var elem = QWeb.render('hr_holidays.dashboard_calendar_header', {
+ timeoffs: result,
+ });
+ self.$el.before(elem);
+ });
+ },
+ });
+ var TimeOffCalendarView = CalendarView.extend({
+ config: _.extend({}, CalendarView.prototype.config, {
+ Controller: TimeOffCalendarController,
+ Renderer: TimeOffCalendarRenderer,
+ }),
+ });
+
+ /**
+ * Calendar shown in the "Everyone" menu
+ */
+ var TimeOffCalendarAllView = CalendarView.extend({
+ config: _.extend({}, CalendarView.prototype.config, {
+ Renderer: TimeOffPopoverRenderer,
+ }),
+ });
+
+ viewRegistry.add('time_off_calendar', TimeOffCalendarView);
+ viewRegistry.add('time_off_calendar_all', TimeOffCalendarAllView);
+});