From 91678b3cf34be96843aedc900d31ecb4c2b97471 Mon Sep 17 00:00:00 2001 From: "Xavier BOL (xbo)" Date: Tue, 23 Aug 2022 14:03:20 +0000 Subject: [PATCH] [IMP] web,hr_timesheet: convert timesheet_uom service and components in OWL Before this commit, a service called `timesheet_uom` is created to add in the legacy registry, the right component to use when `timesheet_uom` is searched in the registry because it depends on the UoM set on the current company. This commit converts all the widgets in which be used in the `timesheet_uom` registry and also adapt the service to use the session instead of the legacy one. task-2944742 closes odoo/odoo#98380 Related: odoo/upgrade#3787 Related: odoo/enterprise#30565 Signed-off-by: Laurent Stukkens (ltu) --- .../timesheet_float_factor_field.js | 10 ++++ .../timesheet_float_time_field.js | 6 +++ .../timesheet_float_toggle.js | 10 ++++ .../static/src/js/timesheet_uom.js | 2 +- .../timesheet_uom_multi_company_mixin.js | 15 ++++++ .../src/services/timesheet_uom_service.js | 54 +++++++++++++++++++ .../static/tests/timesheet_uom_common.js | 4 +- .../fields/float_factor/float_factor_field.js | 8 ++- 8 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 addons/hr_timesheet/static/src/components/timesheet_float_factor/timesheet_float_factor_field.js create mode 100644 addons/hr_timesheet/static/src/components/timesheet_float_time/timesheet_float_time_field.js create mode 100644 addons/hr_timesheet/static/src/components/timesheet_float_toggle/timesheet_float_toggle.js create mode 100644 addons/hr_timesheet/static/src/mixins/timesheet_uom_multi_company_mixin.js create mode 100644 addons/hr_timesheet/static/src/services/timesheet_uom_service.js 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 new file mode 100644 index 00000000000..be89019df4b --- /dev/null +++ b/addons/hr_timesheet/static/src/components/timesheet_float_factor/timesheet_float_factor_field.js @@ -0,0 +1,10 @@ +/** @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 new file mode 100644 index 00000000000..77090956688 --- /dev/null +++ b/addons/hr_timesheet/static/src/components/timesheet_float_time/timesheet_float_time_field.js @@ -0,0 +1,6 @@ +/** @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 new file mode 100644 index 00000000000..5a665701bf9 --- /dev/null +++ b/addons/hr_timesheet/static/src/components/timesheet_float_toggle/timesheet_float_toggle.js @@ -0,0 +1,10 @@ +/** @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/js/timesheet_uom.js b/addons/hr_timesheet/static/src/js/timesheet_uom.js index 8ceac631fb9..1622d09dc01 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("timesheet_uom", timesheetUomService); +registry.category("services").add("legacy_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 new file mode 100644 index 00000000000..a520127aa7c --- /dev/null +++ b/addons/hr_timesheet/static/src/mixins/timesheet_uom_multi_company_mixin.js @@ -0,0 +1,15 @@ +/** @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 new file mode 100644 index 00000000000..5e3a99ecfeb --- /dev/null +++ b/addons/hr_timesheet/static/src/services/timesheet_uom_service.js @@ -0,0 +1,54 @@ +/** @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"; + +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 { + TimesheetUoMField = fieldRegistry.get(timesheetUomWidget.widget) || TimesheetFloatFactorField; + } + 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/tests/timesheet_uom_common.js b/addons/hr_timesheet/static/tests/timesheet_uom_common.js index 702d68e8c20..ce0d779cc0a 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("timesheet_uom")) { + if (!serviceRegistry.contains("legacy_timesheet_uom")) { // Remove dependency on legacy_session since we're patching the session directly - serviceRegistry.add("timesheet_uom", Object.assign({}, timesheetUomService, { dependencies: [] })); + serviceRegistry.add("legacy_timesheet_uom", Object.assign({}, timesheetUomService, { dependencies: [] })); } await makeTestEnv(); // Start services }; diff --git a/addons/web/static/src/views/fields/float_factor/float_factor_field.js b/addons/web/static/src/views/fields/float_factor/float_factor_field.js index 0bf1bd4a967..f8a156e8f82 100644 --- a/addons/web/static/src/views/fields/float_factor/float_factor_field.js +++ b/addons/web/static/src/views/fields/float_factor/float_factor_field.js @@ -5,11 +5,15 @@ import { FloatField } from "../float/float_field"; const { Component } = owl; export class FloatFactorField extends Component { + get factor() { + return this.props.factor; + } + get floatFieldProps() { const result = { ...this.props, - value: this.props.value * this.props.factor, - update: (value) => this.props.update(value / this.props.factor), + value: this.props.value * this.factor, + update: (value) => this.props.update(value / this.factor), }; delete result.factor; return result;