From 68998a428a347fcb86e21f7e253a02e9fb988249 Mon Sep 17 00:00:00 2001 From: "Laurent Stukkens (LTU)" Date: Fri, 23 Sep 2022 12:58:27 +0000 Subject: [PATCH] [REF] hr_timesheet: convert kanban and list views to owl Tests adaptations will come in a following PR, but the current focus is set on owl conversion. task-2936932 closes odoo/odoo#101208 X-original-commit: 22c8e0ec8cd0ffc6a14302fc548bb5fffc5c4e11 Related: odoo/enterprise#31820 Signed-off-by: Xavier --- .../task_with_hours/task_with_hours.js | 49 +++++++++++++++ .../timesheet_float_factor_field.js | 10 ---- .../timesheet_float_time_field.js | 6 -- .../timesheet_float_toggle.js | 10 ---- .../components/timesheet_uom/timesheet_uom.js | 56 ++++++++++++++++++ .../timesheet_uom/timesheet_uom.xml | 8 +++ .../static/src/js/timesheet_uom.js | 2 +- .../timesheet_uom_multi_company_mixin.js | 15 ----- .../src/services/timesheet_uom_service.js | 59 ------------------- .../timesheet_graph}/timesheet_graph_model.js | 0 .../timesheet_graph}/timesheet_graph_view.js | 0 .../static/tests/timesheet_uom_common.js | 4 +- .../tests/timesheet_uom_widgets_tests.js | 2 - .../web/static/src/views/fields/formatters.js | 3 +- 14 files changed, 118 insertions(+), 106 deletions(-) create mode 100644 addons/hr_timesheet/static/src/components/task_with_hours/task_with_hours.js delete mode 100644 addons/hr_timesheet/static/src/components/timesheet_float_factor/timesheet_float_factor_field.js delete mode 100644 addons/hr_timesheet/static/src/components/timesheet_float_time/timesheet_float_time_field.js delete mode 100644 addons/hr_timesheet/static/src/components/timesheet_float_toggle/timesheet_float_toggle.js create mode 100644 addons/hr_timesheet/static/src/components/timesheet_uom/timesheet_uom.js create mode 100644 addons/hr_timesheet/static/src/components/timesheet_uom/timesheet_uom.xml delete mode 100644 addons/hr_timesheet/static/src/mixins/timesheet_uom_multi_company_mixin.js delete mode 100644 addons/hr_timesheet/static/src/services/timesheet_uom_service.js rename addons/hr_timesheet/static/src/{js => views/timesheet_graph}/timesheet_graph_model.js (100%) rename addons/hr_timesheet/static/src/{js => views/timesheet_graph}/timesheet_graph_view.js (100%) diff --git a/addons/hr_timesheet/static/src/components/task_with_hours/task_with_hours.js b/addons/hr_timesheet/static/src/components/task_with_hours/task_with_hours.js new file mode 100644 index 00000000000..22e1464d48b --- /dev/null +++ b/addons/hr_timesheet/static/src/components/task_with_hours/task_with_hours.js @@ -0,0 +1,49 @@ +/** @odoo-module **/ + +import { registry } from "@web/core/registry"; +import { Many2OneField } from "@web/views/fields/many2one/many2one_field"; + + +class TaskWithHours extends Many2OneField { + + setup() { + super.setup(); + const activeActions = this.state.activeActions; + activeActions.canCreate = activeActions.canCreate && this.canCreate; + activeActions.canQuickCreate = activeActions.canQuickCreate || this.canCreate; + activeActions.canCreateEdit = activeActions.canCreate; + } + + get canCreate() { + return Boolean(this.context.default_project_id); + } + + /** + * @override + */ + get displayName() { + const displayName = super.displayName; + return displayName ? displayName.split('\u00A0')[0] : displayName; + } + + /** + * @override + */ + get context() { + return {...super.context, hr_timesheet_display_remaining_hours: true}; + } + + /** + * @override + */ + get Many2XAutocompleteProps() { + const props = super.Many2XAutocompleteProps; + if (!this.canCreate) { + props.quickCreate = null; + } + return props; + } + +} + +registry.category("fields").add("task_with_hours", TaskWithHours); diff --git a/addons/hr_timesheet/static/src/components/timesheet_float_factor/timesheet_float_factor_field.js b/addons/hr_timesheet/static/src/components/timesheet_float_factor/timesheet_float_factor_field.js deleted file mode 100644 index be89019df4b..00000000000 --- a/addons/hr_timesheet/static/src/components/timesheet_float_factor/timesheet_float_factor_field.js +++ /dev/null @@ -1,10 +0,0 @@ -/** @odoo-module */ - -import { FloatFactorField } from "@web/views/fields/float_factor/float_factor_field"; -import { TimesheetUOMMultiCompanyMixin } from "../../mixins/timesheet_uom_multi_company_mixin"; - -export class TimesheetFloatFactorField extends TimesheetUOMMultiCompanyMixin(FloatFactorField) { - get factor() { - return this.currentCompanyTimesheetUOMFactor || super.factor; - } -} diff --git a/addons/hr_timesheet/static/src/components/timesheet_float_time/timesheet_float_time_field.js b/addons/hr_timesheet/static/src/components/timesheet_float_time/timesheet_float_time_field.js deleted file mode 100644 index 77090956688..00000000000 --- a/addons/hr_timesheet/static/src/components/timesheet_float_time/timesheet_float_time_field.js +++ /dev/null @@ -1,6 +0,0 @@ -/** @odoo-module */ - -import { FloatTimeField } from '@web/views/fields/float_time/float_time_field'; -import { TimesheetUOMMultiCompanyMixin } from '../../mixins/timesheet_uom_multi_company_mixin'; - -export class TimesheetFloatTimeField extends TimesheetUOMMultiCompanyMixin(FloatTimeField) {} diff --git a/addons/hr_timesheet/static/src/components/timesheet_float_toggle/timesheet_float_toggle.js b/addons/hr_timesheet/static/src/components/timesheet_float_toggle/timesheet_float_toggle.js deleted file mode 100644 index 5a665701bf9..00000000000 --- a/addons/hr_timesheet/static/src/components/timesheet_float_toggle/timesheet_float_toggle.js +++ /dev/null @@ -1,10 +0,0 @@ -/** @odoo-module */ - -import { FloatToggleField } from '@web/views/fields/float_toggle/float_toggle_field'; -import { TimesheetUOMMultiCompanyMixin } from '../../mixins/timesheet_uom_multi_company_mixin'; - -export class TimesheetFloatToggleField extends TimesheetUOMMultiCompanyMixin(FloatToggleField) { - get factor() { - return this.currentCompanyTimesheetUOMFactor || super.factor; - } -} diff --git a/addons/hr_timesheet/static/src/components/timesheet_uom/timesheet_uom.js b/addons/hr_timesheet/static/src/components/timesheet_uom/timesheet_uom.js new file mode 100644 index 00000000000..21c63d2f9ff --- /dev/null +++ b/addons/hr_timesheet/static/src/components/timesheet_uom/timesheet_uom.js @@ -0,0 +1,56 @@ +/** @odoo-module */ + +import { useService } from "@web/core/utils/hooks"; +import { session } from "@web/session"; +import { registry } from "@web/core/registry"; +import { FloatFactorField } from "@web/views/fields/float_factor/float_factor_field"; +import { FloatToggleField } from "@web/views/fields/float_toggle/float_toggle_field"; +import { FloatTimeField } from "@web/views/fields/float_time/float_time_field"; +import { standardFieldProps } from "@web/views/fields/standard_field_props"; + +const { Component } = owl; + + +export class TimesheetUOM extends Component { + + setup() { + this.companyService = useService("company"); + } + + get timesheetUOMId() { + return this.companyService.currentCompany.timesheet_uom_id; + } + + get timesheetWidget() { + let timesheet_widget = "float_factor"; + if (this.timesheetUOMId in session.uom_ids) { + timesheet_widget = session.uom_ids[this.timesheetUOMId].timesheet_widget; + } + return timesheet_widget; + } + + get timesheetComponent() { + return registry.category("fields").get(this.timesheetWidget, FloatFactorField); + } + + get timesheetComponentProps() { + const factorDependantComponents = ["float_toggle", "float_factor"]; + return factorDependantComponents.includes(this.timesheetWidget) ? this.FactorCompanyDependentProps : this.props; + } + + get FactorCompanyDependentProps() { + const factor = this.companyService.currentCompany.timesheet_uom_factor || this.props.factor; + return { ...this.props, factor }; + } + +} + +TimesheetUOM.props = { + ...standardFieldProps, +}; + +TimesheetUOM.template = "hr_timesheet.TimesheetUOM"; + +TimesheetUOM.components = { FloatFactorField, FloatToggleField, FloatTimeField }; + +registry.category("fields").add("timesheet_uom", TimesheetUOM); diff --git a/addons/hr_timesheet/static/src/components/timesheet_uom/timesheet_uom.xml b/addons/hr_timesheet/static/src/components/timesheet_uom/timesheet_uom.xml new file mode 100644 index 00000000000..1307235d26c --- /dev/null +++ b/addons/hr_timesheet/static/src/components/timesheet_uom/timesheet_uom.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/addons/hr_timesheet/static/src/js/timesheet_uom.js b/addons/hr_timesheet/static/src/js/timesheet_uom.js index 1622d09dc01..8ceac631fb9 100644 --- a/addons/hr_timesheet/static/src/js/timesheet_uom.js +++ b/addons/hr_timesheet/static/src/js/timesheet_uom.js @@ -178,7 +178,7 @@ const timesheetUomService = { return timesheetUomInfo; }, }; -registry.category("services").add("legacy_timesheet_uom", timesheetUomService); +registry.category("services").add("timesheet_uom", timesheetUomService); return { FieldTimesheetFactor, diff --git a/addons/hr_timesheet/static/src/mixins/timesheet_uom_multi_company_mixin.js b/addons/hr_timesheet/static/src/mixins/timesheet_uom_multi_company_mixin.js deleted file mode 100644 index a520127aa7c..00000000000 --- a/addons/hr_timesheet/static/src/mixins/timesheet_uom_multi_company_mixin.js +++ /dev/null @@ -1,15 +0,0 @@ -/** @odoo-module */ - -import { useService } from "@web/core/utils/hooks"; - -const { onWillStart } = owl; - -export const TimesheetUOMMultiCompanyMixin = (component) => class extends component { - setup() { - super.setup(); - this.companyService = useService('company'); - onWillStart(() => { - this.currentCompanyTimesheetUOMFactor = this.companyService.currentCompany.timesheet_uom_factor || 1; - }); - } -}; diff --git a/addons/hr_timesheet/static/src/services/timesheet_uom_service.js b/addons/hr_timesheet/static/src/services/timesheet_uom_service.js deleted file mode 100644 index 940f7b6feef..00000000000 --- a/addons/hr_timesheet/static/src/services/timesheet_uom_service.js +++ /dev/null @@ -1,59 +0,0 @@ -/** @odoo-module */ - -import { session } from "@web/session"; -import { registry } from "@web/core/registry"; - -import { TimesheetFloatFactorField } from "../components/timesheet_float_factor/timesheet_float_factor_field"; -import { TimesheetFloatTimeField } from "../components/timesheet_float_time/timesheet_float_time_field"; -import { TimesheetFloatToggleField } from "../components/timesheet_float_toggle/timesheet_float_toggle"; -import {FloatFactorField} from "@web/views/fields/float_factor/float_factor_field"; - -export const timesheetUomService = { - dependencies: ["company"], - start(env, { company }) { - const timesheetUomWidget = { widget: "float_factor" }; - if (company.allowedCompanyIds.length) { - const currentCompanyTimesheetUOMId = company.currentCompany.timesheet_uom_id || false; - if (currentCompanyTimesheetUOMId) { - timesheetUomWidget.widget = session.uom_ids[currentCompanyTimesheetUOMId].timesheet_widget; - } - } - - /** - * Binding depending on Company Preference - * - * determine which widget will be the timesheet one. - * Simply match the 'timesheet_uom' widget key with the correct - * implementation (float_time, float_toggle, ...). The default - * value will be 'float_factor'. - **/ - const fieldRegistry = registry.category("fields"); - - let TimesheetUoMField = null; - - if (timesheetUomWidget.widget === "float_toggle") { - TimesheetUoMField = TimesheetFloatToggleField; - } else if (timesheetUomWidget.widget === "float_time") { - TimesheetUoMField = TimesheetFloatTimeField; - } else { - let fieldRegistryWidget = fieldRegistry.get(timesheetUomWidget.widget); - if (fieldRegistryWidget === FloatFactorField) { - fieldRegistryWidget = TimesheetFloatFactorField; - } - TimesheetUoMField = fieldRegistryWidget; - } - fieldRegistry.add("timesheet_uom", TimesheetUoMField); - - // widget timesheet_uom_no_toggle is the same as timesheet_uom but without toggle. - // We can modify easly huge amount of days. - let TimesheetUoMWithoutToggleField = TimesheetUoMField; - if (timesheetUomWidget.widget === "float_toggle") { - TimesheetUoMWithoutToggleField = TimesheetFloatFactorField; - } - fieldRegistry.add("timesheet_uom_no_toggle", TimesheetUoMWithoutToggleField); - - return timesheetUomWidget.widget; - }, -}; - -registry.category("services").add("timesheet_uom", timesheetUomService); diff --git a/addons/hr_timesheet/static/src/js/timesheet_graph_model.js b/addons/hr_timesheet/static/src/views/timesheet_graph/timesheet_graph_model.js similarity index 100% rename from addons/hr_timesheet/static/src/js/timesheet_graph_model.js rename to addons/hr_timesheet/static/src/views/timesheet_graph/timesheet_graph_model.js diff --git a/addons/hr_timesheet/static/src/js/timesheet_graph_view.js b/addons/hr_timesheet/static/src/views/timesheet_graph/timesheet_graph_view.js similarity index 100% rename from addons/hr_timesheet/static/src/js/timesheet_graph_view.js rename to addons/hr_timesheet/static/src/views/timesheet_graph/timesheet_graph_view.js diff --git a/addons/hr_timesheet/static/tests/timesheet_uom_common.js b/addons/hr_timesheet/static/tests/timesheet_uom_common.js index ce0d779cc0a..702d68e8c20 100644 --- a/addons/hr_timesheet/static/tests/timesheet_uom_common.js +++ b/addons/hr_timesheet/static/tests/timesheet_uom_common.js @@ -167,9 +167,9 @@ function SetupTimesheetUOMWidgetsTestEnvironment () { session.uid = sessionToApply.uid; } const serviceRegistry = registry.category("services"); - if (!serviceRegistry.contains("legacy_timesheet_uom")) { + if (!serviceRegistry.contains("timesheet_uom")) { // Remove dependency on legacy_session since we're patching the session directly - serviceRegistry.add("legacy_timesheet_uom", Object.assign({}, timesheetUomService, { dependencies: [] })); + serviceRegistry.add("timesheet_uom", Object.assign({}, timesheetUomService, { dependencies: [] })); } await makeTestEnv(); // Start services }; diff --git a/addons/hr_timesheet/static/tests/timesheet_uom_widgets_tests.js b/addons/hr_timesheet/static/tests/timesheet_uom_widgets_tests.js index 6b4f1227b98..18ebe885244 100644 --- a/addons/hr_timesheet/static/tests/timesheet_uom_widgets_tests.js +++ b/addons/hr_timesheet/static/tests/timesheet_uom_widgets_tests.js @@ -3,7 +3,6 @@ import { registry } from "@web/core/registry"; import { session } from "@web/session"; import { companyService } from "@web/webclient/company_service"; -import { timesheetUomService } from "../src/services/timesheet_uom_service"; import { uiService } from "@web/core/ui/ui_service"; import { makeView, setupViewRegistries} from "@web/../tests/views/helpers"; import { click, getFixture, patchWithCleanup } from "@web/../tests/helpers/utils"; @@ -36,7 +35,6 @@ QUnit.module("Timesheet UOM Widgets", (hooks) => { }, }; serviceRegistry.add("ui", uiService); - serviceRegistry.add("timesheet_uom", timesheetUomService); serviceRegistry.add("company", companyService, { force: true }); patchWithCleanup(session, { user_companies: { diff --git a/addons/web/static/src/views/fields/formatters.js b/addons/web/static/src/views/fields/formatters.js index 0235776d7b1..a7e010fbca4 100644 --- a/addons/web/static/src/views/fields/formatters.js +++ b/addons/web/static/src/views/fields/formatters.js @@ -198,7 +198,8 @@ export function formatFloatTime(value, options = {}) { value = Math.abs(value); } let hour = Math.floor(value); - // This ensures that we do not have float issues while still considering that 59s is 00:00. + // Although looking quite overkill, the following line ensures that we do + // not have float issues while still considering that 59s is 00:00. let min = Math.floor(Math.round((value % 1) * 100) / 100 * 60); if (min === 60) { min = 0;