import { fields, models, onRpc } from "@web/../tests/web_test_helpers"; import { projectModels } from "@project/../tests/project_models"; import { defineTimesheetModels as defineHRTimesheetModels, hrTimesheetModels, } from "@hr_timesheet/../tests/hr_timesheet_models"; import { timerModels } from "@timer/../tests/timer_models"; export class ProjectProject extends projectModels.ProjectProject { allow_timesheets = fields.Boolean(); _records = [ { id: 1, name: "P1", allow_timesheets: true }, { id: 2, name: "Webocalypse Now", allow_timesheets: true }, ]; } export class ProjectTask extends projectModels.ProjectTask { allow_timesheets = fields.Boolean(); _records = [ { id: 1, name: "BS task", project_id: 1 }, { id: 2, name: "Another BS task", project_id: 2 }, { id: 3, name: "yet another task", project_id: 2 }, ]; } export class HREmployeePublic extends models.Model { _name = "hr.employee.public"; name = fields.Char(); _records = [ { id: 1, name: "Mario" }, { id: 2, name: "Luigi" }, { id: 3, name: "Yoshi" }, { id: 4, name: "Toad" }, ]; } export class HRTimesheet extends hrTimesheetModels.HRTimesheet { timer_start = fields.Datetime(); timer_pause = fields.Datetime(); is_timer_running = fields.Boolean({ compute: "_compute_is_timer_running" }); company_id = fields.Many2one({ relation: "res.company" }); employee_id = fields.Many2one({ relation: "hr.employee.public" }); date = fields.Date({ default: "2017-01-31" }); display_timer = fields.Boolean(); selection_field = fields.Selection({ selection: [ ["abc", "ABC"], ["def", "DEF"], ["ghi", "GHI"], ], }); _compute_is_timer_running() { for (const record of this) { record.is_timer_running = record.timer_start && !record.timer_pause; } } action_start_new_timesheet_timer(vals = {}) { if (!vals.project_id) { vals.project_id = this.env["project.project"].search([], null, 1)[0]; } const timesheetId = this.create(vals); // Creating a timer to run usally done "timer.mixin" const timer = this.env["timer.timer"].create({ timer_start: false, timer_pause: false, is_timer_running: false, res_model: this._name, res_id: timesheetId, user_id: this.env.user.id, }); const Timer = this.env["timer.timer"]; Timer.action_timer_start(timer); const timerData = Timer.read(timer, ["timer_start"])[0]; this.write(timesheetId, { timer_start: timerData.timer_start }); // should be computed instead return this.read(timesheetId, this._get_timesheet_timer_field_names())[0]; } grid_unavailability(dateStart, dateEnd) { const { res_ids: employeeIds } = arguments[2]; const unavailabilityDates = Object.fromEntries( employeeIds.map((employee) => [employee, [dateStart, dateEnd]]) ); unavailabilityDates.false = [dateStart, dateEnd]; return unavailabilityDates; } action_add_time_to_timer() { return false; } action_timer_start() { this.action_start_new_timesheet_timer(); return false; } action_timer_unlink(timesheetId) { const timerId = this.env["timer.timer"].search([ ["res_model", "=", this._name], ["res_id", "=", timesheetId], ])[0]; if (timerId) { this.env["timer.timer"].unlink(timerId); } const timesheetData = this.read(timesheetId)[0]; if (!timesheetData.unit_amount) { this.unlink(timesheetId); } } action_timer_stop(timesheetId) { const timerId = this.env["timer.timer"].search([ ["res_model", "=", this._name], ["res_id", "=", timesheetId], ])[0]; if (timerId) { let duration = this.env["timer.timer"].action_timer_stop(timerId); if (duration < 0.25) { duration = 0.25; } this.write(timesheetId, { unit_amount: duration, timer_start: false }); this.env["timer.timer"].unlink(timerId); return duration; } return 0; } get_running_timer() { const result = { step_timer: 30 }; const timesheetId = this.env["timer.timer"].search_read( [ ["res_model", "=", this._name], ["user_id", "=", this.env.user.id], ], ["res_id"] )[0]; if (timesheetId) { Object.assign(result, { ...this.read(timesheetId, this._get_timesheet_timer_field_names()), }); } return result; } _get_timesheet_timer_field_names() { return ["date", "name", "project_id", "task_id", "unit_amount", "timer_start"]; } _records = [ { name: "youpi", id: 1, project_id: 1, employee_id: 2, date: "2017-01-24", unit_amount: 2.5, display_timer: true, }, { name: "bop", id: 2, project_id: 1, task_id: 1, employee_id: 1, date: "2017-01-25", unit_amount: 25, }, { name: "Sabaton", id: 3, project_id: 1, task_id: 1, employee_id: 3, date: "2017-01-25", unit_amount: 5.5, }, { name: "chaos", id: 4, project_id: 2, task_id: 3, employee_id: 1, date: "2017-01-27", unit_amount: 10, }, { name: "sakamoto", id: 5, project_id: 2, task_id: 2, employee_id: 2, date: "2017-01-27", unit_amount: -3.5, }, { name: "frieren", id: 6, project_id: 2, task_id: 1, employee_id: 4, date: "2017-01-26", unit_amount: 4, }, ]; _views = { form: `
`, list: ` `, kanban: `
`, grid: `