diff --git a/addons/hr_work_entry_contract/static/src/js/work_entries_calendar.js b/addons/hr_work_entry_contract/static/src/js/work_entries_calendar.js deleted file mode 100644 index 404f9e3c6d1..00000000000 --- a/addons/hr_work_entry_contract/static/src/js/work_entries_calendar.js +++ /dev/null @@ -1,76 +0,0 @@ -odoo.define('hr_work_entry_contract.work_entries_calendar', function(require) { - 'use strict'; - - var core = require('web.core'); - var WorkEntryControllerMixin = require('hr_work_entry_contract.WorkEntryControllerMixin'); - var CalendarController = require("web.CalendarController"); - var CalendarModel = require('web.CalendarModel'); - var CalendarRenderer = require('web.CalendarRenderer'); - var CalendarView = require('web.CalendarView'); - var viewRegistry = require('web.view_registry'); - - var _t = core._t; - - - var WorkEntryCalendarController = CalendarController.extend(WorkEntryControllerMixin, { - events: _.extend({}, WorkEntryControllerMixin.events, CalendarController.prototype.events), - - // Returns the records from the model - _fetchRecords: function () { - var self = this; - var records = _.filter(this.model.data.data, function (data) { - // Filter records that are not inside the current month - // (because in calendar view some days of prev. and next month are visible) - return data.record.date_start.isBefore(self.lastDay) && data.record.date_stop.isAfter(self.firstDay); - }); - return _.pluck(records, 'record'); - }, - _fetchFirstDay: function () { - return this.model.data.target_date.clone().startOf('month'); - }, - _fetchLastDay: function () { - return this.model.data.target_date.clone().endOf('month'); - }, - _displayWarning: function ($warning) { - this.$('.o_calendar_container').before($warning); - }, - }); - - var WorkEntryCalendarModel = CalendarModel.extend({ - /** - * Display everybody's work entries if no employee filter exists - * @private - * @override - * @param {any} filter - * @returns {Deferred} - */ - _loadFilter: function (filter) { - return this._super.apply(this, arguments).then(function () { - var filters = filter.filters; - var all_filter = filters[filters.length - 1]; - - if (all_filter) { - all_filter.label = _t("Everybody's work entries"); - - if (filter.write_model && filter.filters.length <= 1 && all_filter.active === undefined) { - filter.all = true; - all_filter.active = true; - } - } - - }); - } - }); - - var WorkEntryCalendarView = CalendarView.extend({ - config: _.extend({}, CalendarView.prototype.config, { - Controller: WorkEntryCalendarController, - Model: WorkEntryCalendarModel, - Renderer: CalendarRenderer, - }), - }); - - viewRegistry.add('work_entries_calendar', WorkEntryCalendarView); - - return WorkEntryCalendarController; -}); diff --git a/addons/hr_work_entry_contract/static/src/views/work_entry_calendar/work_entry_calendar.xml b/addons/hr_work_entry_contract/static/src/views/work_entry_calendar/work_entry_calendar.xml new file mode 100644 index 00000000000..90ef5e7bc1d --- /dev/null +++ b/addons/hr_work_entry_contract/static/src/views/work_entry_calendar/work_entry_calendar.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + diff --git a/addons/hr_work_entry_contract/static/src/views/work_entry_calendar/work_entry_calendar_controller.js b/addons/hr_work_entry_contract/static/src/views/work_entry_calendar/work_entry_calendar_controller.js new file mode 100644 index 00000000000..2b3acc05876 --- /dev/null +++ b/addons/hr_work_entry_contract/static/src/views/work_entry_calendar/work_entry_calendar_controller.js @@ -0,0 +1,31 @@ +/** @odoo-module **/ + +import { registry } from "@web/core/registry"; +import { CalendarController } from '@web/views/calendar/calendar_controller'; +import { WorkEntryCalendarModel } from "@hr_work_entry_contract/views/work_entry_calendar/work_entry_calendar_model"; +import { calendarView } from "@web/views/calendar/calendar_view"; +import { useWorkEntry } from "@hr_work_entry_contract/views/work_entry_hook"; + +export class WorkEntryCalendarController extends CalendarController { + setup() { + super.setup(...arguments); + const { onRegenerateWorkEntries } = useWorkEntry({ + getEmployeeIds: this.getEmployeeIds.bind(this), + getRange: this.model.computeRange.bind(this.model), + }); + this.onRegenerateWorkEntries = onRegenerateWorkEntries; + } + + getEmployeeIds() { + return [...new Set(Object.values(this.model.records).map(rec => rec.rawRecord.employee_id[0]))]; + } +} + +export const WorkEntryCalendarView = { + ...calendarView, + Controller: WorkEntryCalendarController, + Model: WorkEntryCalendarModel, + buttonTemplate: "hr_work_entry_contract.calendar.controlButtons", +} + +registry.category("views").add("work_entries_calendar", WorkEntryCalendarView); diff --git a/addons/hr_work_entry_contract/static/src/views/work_entry_calendar/work_entry_calendar_model.js b/addons/hr_work_entry_contract/static/src/views/work_entry_calendar/work_entry_calendar_model.js new file mode 100644 index 00000000000..17587d58c2d --- /dev/null +++ b/addons/hr_work_entry_contract/static/src/views/work_entry_calendar/work_entry_calendar_model.js @@ -0,0 +1,38 @@ +/** @odoo-module **/ + +import { CalendarModel } from "@web/views/calendar/calendar_model"; +import { useWorkEntry } from "@hr_work_entry_contract/views/work_entry_hook"; + +const { DateTime } = luxon; + +export class WorkEntryCalendarModel extends CalendarModel { + setup() { + super.setup(...arguments); + const { generateWorkEntries } = useWorkEntry({ getRange: this.computeRange.bind(this) }); + this.generateWorkEntries = generateWorkEntries; + } + + computeRange() { + const { scale, date } = this.meta; + const start = date.startOf(scale); + const end = date.endOf(scale); + return { start, end }; + } + + makeFilterAll() { + return { + ...super.makeFilterAll(...arguments), + label: this.env._t("Everybody's work entries"), + active: true, + }; + } + + async updateData(data) { + const { start } = this.computeRange(); + const shouldGenerateWorkEntries = start <= DateTime.now(); + if (shouldGenerateWorkEntries) { + await this.generateWorkEntries(); + } + await super.updateData(data); + } +} diff --git a/addons/hr_work_entry_contract/static/src/views/work_entry_hook.js b/addons/hr_work_entry_contract/static/src/views/work_entry_hook.js new file mode 100644 index 00000000000..f34170ca9e9 --- /dev/null +++ b/addons/hr_work_entry_contract/static/src/views/work_entry_hook.js @@ -0,0 +1,30 @@ +/** @odoo-module **/ + +import { serializeDate } from "@web/core/l10n/dates"; +import { useService } from "@web/core/utils/hooks"; + +export function useWorkEntry({ getEmployeeIds, getRange }) { + const orm = useService("orm"); + const action = useService("action"); + + return { + onRegenerateWorkEntries: () => { + const { start, end } = getRange(); + action.doAction('hr_work_entry_contract.hr_work_entry_regeneration_wizard_action', { + additionalContext: { + default_employee_ids: getEmployeeIds(), + date_start: serializeDate(start), + date_end: serializeDate(end), + } + }); + }, + generateWorkEntries: () => { + const { start, end } = getRange(); + return orm.call( + 'hr.employee', + 'generate_work_entries', + [[], serializeDate(start), serializeDate(end)] + ); + }, + } +}