[REM] hr_timesheet: remove legacy code

This commit removes the legacy code in hr_timesheet module since
everything is converted to OWL.

task-2702636

closes odoo/odoo#108605

Related: odoo/upgrade#4410
Related: odoo/enterprise#35217
Signed-off-by: Xavier Bol (xbo) <xbo@odoo.com>
This commit is contained in:
Xavier BOL (xbo)
2023-03-09 20:52:52 +01:00
committed by Xavier Bol (xbo)
parent b4448d35a8
commit 87bdb372bc
5 changed files with 0 additions and 625 deletions
@@ -1,50 +0,0 @@
/** @odoo-module alias=hr_timesheet.task_with_hours **/
import field_registry from 'web.field_registry';
import { FieldMany2One } from 'web.relational_fields';
const TaskWithHours = FieldMany2One.extend({
/**
* @override
*/
init: function () {
this._super.apply(this, arguments);
this.additionalContext.hr_timesheet_display_remaining_hours = true;
// By default, we keep the no_quick_create value or we set to false.
this.nodeOptions.no_quick_create = this.nodeOptions.no_quick_create || false;
},
/**
* @override
*/
_getDisplayNameWithoutHours: function (value) {
return value && value.split('\u00A0')[0];
},
/**
* @override
* @private
*/
_onInputClick: function () {
const context = Object.assign(
this.record.getContext(this.recordParams),
this.additionalContext
);
// We don't want to quick create if no project is set in the timesheet
const canCreate = 'default_project_id' in context && context.default_project_id;
this.nodeOptions.no_quick_create =
this.nodeOptions.no_quick_create || !canCreate;
this.can_create = this.can_create && canCreate;
this._super.apply(this, arguments);
},
/**
* @override
* @private
*/
_renderEdit: function (){
this.m2o_value = this._getDisplayNameWithoutHours(this.m2o_value);
this._super.apply(this, arguments);
},
});
field_registry.add('task_with_hours', TaskWithHours);
export default TaskWithHours;
@@ -1,186 +0,0 @@
odoo.define('hr_timesheet.timesheet_uom', function (require) {
'use strict';
const { registry } = require("@web/core/registry");
const basicFields = require('web.basic_fields');
const fieldUtils = require('web.field_utils');
const fieldRegistry = require('web.field_registry');
const session = require('web.session');
const TimesheetUOMMultiCompanyMixin = {
init: function(parent, name, record, options) {
this._super(parent, name, record, options);
const currentCompanyId = session.user_context.allowed_company_ids[0];
const currentCompany = session.user_companies.allowed_companies[currentCompanyId];
this.currentCompanyTimesheetUOMFactor = currentCompany.timesheet_uom_factor || 1;
}
};
/**
* Extend the float factor widget to set default value for timesheet
* use case. The 'factor' is forced to be the UoM timesheet
* conversion from the session info.
**/
const FieldTimesheetFactor = basicFields.FieldFloatFactor.extend(TimesheetUOMMultiCompanyMixin).extend({
formatType: 'float_factor',
/**
* Override init to tweak options depending on the session info
*
* @constructor
* @override
*/
init: function(parent, name, record, options) {
this._super(parent, name, record, options);
// force factor in format and parse options
this.nodeOptions.factor = this.currentCompanyTimesheetUOMFactor;
this.parseOptions.factor = this.currentCompanyTimesheetUOMFactor;
},
});
/**
* Extend the float toggle widget to set default value for timesheet
* use case. The 'range' is different from the default one of the
* native widget, and the 'factor' is forced to be the UoM timesheet
* conversion.
**/
const FieldTimesheetToggle = basicFields.FieldFloatToggle.extend(TimesheetUOMMultiCompanyMixin).extend({
formatType: 'float_factor',
/**
* Override init to tweak options depending on the session info
*
* @constructor
* @override
*/
init: function(parent, name, record, options) {
options = options || {};
var fieldsInfo = record.fieldsInfo[options.viewType || 'default'];
var attrs = options.attrs || (fieldsInfo && fieldsInfo[name]) || {};
var hasRange = _.contains(_.keys(attrs.options || {}), 'range');
this._super(parent, name, record, options);
// Set the timesheet widget options: the range can be customized
// by setting the option on the field in the view. The factor
// is forced to be the UoM conversion factor.
if (!hasRange) {
this.nodeOptions.range = [0.00, 1.00, 0.50];
}
this.nodeOptions.factor = this.currentCompanyTimesheetUOMFactor;
},
});
/**
* Extend float time widget
*/
const FieldTimesheetTime = basicFields.FieldFloatTime.extend(TimesheetUOMMultiCompanyMixin).extend({
init: function () {
this._super.apply(this, arguments);
this.nodeOptions.factor = this.currentCompanyTimesheetUOMFactor;
this.parseOptions.factor = this.currentCompanyTimesheetUOMFactor;
}
});
const timesheetUomService = {
dependencies: ["legacy_session"],
start() {
const timesheetUomInfo = {
widget: null,
factor: 1,
};
if (session.user_context &&
session.user_context.allowed_company_ids &&
session.user_context.allowed_company_ids.length) {
const currentCompanyId = session.user_context.allowed_company_ids[0];
const currentCompany = session.user_companies.allowed_companies[currentCompanyId];
const currentCompanyTimesheetUOMId = currentCompany.timesheet_uom_id || false;
timesheetUomInfo.factor = currentCompany.timesheet_uom_factor || 1;
if (currentCompanyTimesheetUOMId) {
timesheetUomInfo.widget = session.uom_ids[currentCompanyTimesheetUOMId].timesheet_widget;
}
}
/**
* Binding depending on Company Preference
*
* determine wich 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 widgetName = timesheetUomInfo.widget || 'float_factor';
let FieldTimesheetUom = null;
if (widgetName === 'float_toggle') {
FieldTimesheetUom = FieldTimesheetToggle;
} else if (widgetName === 'float_time') {
FieldTimesheetUom = FieldTimesheetTime;
} else {
FieldTimesheetUom = (
fieldRegistry.get(widgetName) &&
fieldRegistry.get(widgetName).extend({ })
) || FieldTimesheetFactor;
}
fieldRegistry.add('timesheet_uom', FieldTimesheetUom);
// widget timesheet_uom_no_toggle is the same as timesheet_uom but without toggle.
// We can modify easly huge amount of days.
let FieldTimesheetUomWithoutToggle = null;
if (widgetName === 'float_toggle') {
FieldTimesheetUomWithoutToggle = FieldTimesheetFactor;
} else {
FieldTimesheetUomWithoutToggle = FieldTimesheetTime;
}
fieldRegistry.add('timesheet_uom_no_toggle', FieldTimesheetUomWithoutToggle);
// bind the formatter and parser method, and tweak the options
const _tweak_options = (options) => {
if (!_.contains(options, 'factor')) {
options.factor = timesheetUomInfo.factor;
}
return options;
};
fieldUtils.format.timesheet_uom = function(value, field, options) {
options = _tweak_options(options || { });
const formatter = fieldUtils.format[FieldTimesheetUom.prototype.formatType];
return formatter(value, field, options);
};
fieldUtils.parse.timesheet_uom = function(value, field, options) {
options = _tweak_options(options || { });
const parser = fieldUtils.parse[FieldTimesheetUom.prototype.formatType];
return parser(value, field, options);
};
fieldUtils.format.timesheet_uom_no_toggle = function(value, field, options) {
options = _tweak_options(options || { });
const formatter = fieldUtils.format[FieldTimesheetUom.prototype.formatType];
return formatter(value, field, options);
};
fieldUtils.parse.timesheet_uom_no_toggle = function(value, field, options) {
options = _tweak_options(options || { });
const parser = fieldUtils.parse[FieldTimesheetUom.prototype.formatType];
return parser(value, field, options);
};
return timesheetUomInfo;
},
};
registry.category("services").add("timesheet_uom", timesheetUomService);
return {
FieldTimesheetFactor,
FieldTimesheetTime,
FieldTimesheetToggle,
timesheetUomService,
};
});
@@ -1,190 +0,0 @@
odoo.define("hr_timesheet.timesheet_uom_tests_env", function (require) {
"use strict";
const session = require('web.session');
const { createView } = require("web.test_utils");
const { timesheetUomService } = require('hr_timesheet.timesheet_uom');
const { makeTestEnv } = require("@web/../tests/helpers/mock_env");
const { registry } = require("@web/core/registry");
/**
* Sets the timesheet related widgets testing environment up.
*/
function SetupTimesheetUOMWidgetsTestEnvironment () {
this.allowedCompanies = {
1: {
id: 1,
name: 'Company 1',
timesheet_uom_id: 1,
timesheet_uom_factor: 1,
},
2: {
id: 2,
name: 'Company 2',
timesheet_uom_id: 2,
timesheet_uom_factor: 0.125,
},
3: {
id: 3,
name: 'Company 3',
timesheet_uom_id: 2,
timesheet_uom_factor: 0.125,
},
};
this.uomIds = {
1: {
id: 1,
name: 'hour',
rounding: 0.01,
timesheet_widget: 'float_time',
},
2: {
id: 2,
name: 'day',
rounding: 0.01,
timesheet_widget: 'float_toggle',
},
};
this.singleCompanyHourUOMUser = {
allowed_company_ids: [this.allowedCompanies[1].id],
};
this.singleCompanyDayUOMUser = {
allowed_company_ids: [this.allowedCompanies[2].id],
};
this.multiCompanyHourUOMUser = {
allowed_company_ids: [
this.allowedCompanies[1].id,
this.allowedCompanies[3].id,
],
};
this.multiCompanyDayUOMUser = {
allowed_company_ids: [
this.allowedCompanies[3].id,
this.allowedCompanies[1].id,
],
};
this.session = {
uid: 0, // In order to avoid bbqState and cookies to be taken into account in AbstractWebClient.
user_companies: {
current_company: 1,
allowed_companies: this.allowedCompanies,
},
user_context: this.singleCompanyHourUOMUser,
uom_ids: this.uomIds,
};
this.data = {
'account.analytic.line': {
fields: {
project_id: {
string: "Project",
type: "many2one",
relation: "project.project",
},
task_id: {
string:"Task",
type: "many2one",
relation: "project.task",
},
date: {
string: "Date",
type: "date",
},
unit_amount: {
string: "Unit Amount",
type: "float",
},
},
records: [
{
id: 1,
project_id: 1,
task_id: 1,
date: "2021-01-12",
unit_amount: 8,
},
],
},
'project.project': {
fields: {
name: {
string: "Project Name",
type: "char",
},
},
records: [
{
id: 1,
display_name: "P1",
},
],
},
'project.task': {
fields: {
name: {
string: "Task Name",
type: "char",
},
project_id: {
string: "Project",
type: "many2one",
relation: "project.project",
},
},
records: [
{
id: 1,
display_name: "T1",
project_id: 1,
},
],
},
};
this.patchSessionAndStartServices = async function (sessionToApply, doNotUseEnvSession = false) {
/*
Adds the timesheet_uom to the fieldRegistry by setting the session and
starting the timesheet_uom service which registers the widget in the registry.
*/
session.user_companies = Object.assign(
{ },
!doNotUseEnvSession && this.session.user_companies || { },
sessionToApply && sessionToApply.user_companies);
if (Object.keys(session.user_companies).length === 0) {
delete session.user_companies;
}
session.user_context = Object.assign(
{ },
!doNotUseEnvSession && this.session.user_context || { },
sessionToApply && sessionToApply.user_context);
session.uom_ids = Object.assign(
{ },
!doNotUseEnvSession && this.session.uom_ids || { },
sessionToApply && sessionToApply.uom_ids);
if (!doNotUseEnvSession && 'uid' in this.session) {
session.uid = this.session.uid;
}
if (sessionToApply && 'uid' in sessionToApply) {
session.uid = sessionToApply.uid;
}
const serviceRegistry = registry.category("services");
if (!serviceRegistry.contains("timesheet_uom")) {
// Remove dependency on legacy_session since we're patching the session directly
serviceRegistry.add("timesheet_uom", Object.assign({}, timesheetUomService, { dependencies: [] }));
}
await makeTestEnv(); // Start services
};
this.createView = async function (options) {
const sessionToApply = options && options.session || { };
await this.patchSessionAndStartServices(sessionToApply);
return await createView(Object.assign(
{
data: this.data,
model: 'account.analytic.line',
},
options || { },
));
};
};
return SetupTimesheetUOMWidgetsTestEnvironment;
});
@@ -1,74 +0,0 @@
odoo.define("hr_timesheet.timesheet_uom_tests", function (require) {
"use strict";
const session = require('web.session');
const SetupTimesheetUOMWidgetsTestEnvironment = require('hr_timesheet.timesheet_uom_tests_env');
const fieldUtils = require('web.field_utils');
QUnit.module('Timesheet UOM Widgets', function (hooks) {
let env;
let sessionUserCompaniesBackup;
let sessionUserContextBackup;
let sessionUOMIdsBackup;
let sessionUIDBackup;
hooks.beforeEach(async function (assert) {
env = new SetupTimesheetUOMWidgetsTestEnvironment();
// Backups session parts that this testing module will alter in order to restore it at the end.
sessionUserCompaniesBackup = session.user_companies || false;
sessionUserContextBackup = session.user_context || false;
sessionUOMIdsBackup = session.uom_ids || false;
sessionUIDBackup = session.uid || false;
});
hooks.afterEach(async function (assert) {
// Restores the session
const sessionToApply = Object.assign(
{ },
sessionUserCompaniesBackup && {
user_companies: sessionUserCompaniesBackup,
} || { },
sessionUserContextBackup && {
user_context: sessionUserContextBackup,
} || { },
sessionUOMIdsBackup && {
uom_ids: sessionUOMIdsBackup,
} || { },
sessionUIDBackup && {
uid: sessionUIDBackup,
} || { });
await env.patchSessionAndStartServices(sessionToApply, true);
});
QUnit.module('timesheet_uom', function (hooks) {
QUnit.module('timesheet_uom_factor', function (hooks) {
QUnit.test('the timesheet_uom_factor usage in formatters and parsers is company related', async function (assert) {
assert.expect(4);
await env.patchSessionAndStartServices();
assert.strictEqual(fieldUtils.format.timesheet_uom(1), '01:00', 'The format is taking the timesheet_uom_factor into account');
assert.strictEqual(fieldUtils.parse.timesheet_uom('01:00'), 1, 'The parsing is taking the timesheet_uom_factor into account');
const sessionToApply = {
user_context: env.singleCompanyDayUOMUser,
};
await env.patchSessionAndStartServices(sessionToApply);
assert.strictEqual(fieldUtils.format.timesheet_uom(8), '1.00', 'The format is taking the timesheet_uom_factor into account');
assert.strictEqual(fieldUtils.parse.timesheet_uom('1.00'), 8, 'The parsing is taking the timesheet_uom_factor into account');
});
QUnit.test('the timesheet_uom_factor taken into account in a multi company environment is the current company', async function (assert) {
assert.expect(4);
let sessionToApply = {
user_context: env.multiCompanyHourUOMUser,
};
await env.patchSessionAndStartServices(sessionToApply);
assert.strictEqual(fieldUtils.format.timesheet_uom(1), '01:00', 'The format is taking the timesheet_uom_factor into account');
assert.strictEqual(fieldUtils.parse.timesheet_uom('01:00'), 1, 'The parsing is taking the timesheet_uom_factor into account');
sessionToApply.user_context = env.singleCompanyDayUOMUser;
await env.patchSessionAndStartServices(sessionToApply);
assert.strictEqual(fieldUtils.format.timesheet_uom(8), '1.00', 'The format is taking the timesheet_uom_factor into account');
assert.strictEqual(fieldUtils.parse.timesheet_uom('1.00'), 8, 'The parsing is taking the timesheet_uom_factor into account');
});
});
});
});
});
@@ -1,125 +0,0 @@
/** @odoo-module */
import { registry } from "@web/core/registry";
import { session } from "@web/session";
import { companyService } from "@web/webclient/company_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";
const serviceRegistry = registry.category("services");
QUnit.module("Timesheet UOM Widgets", (hooks) => {
let serverData;
let target;
hooks.beforeEach(async function (assert) {
setupViewRegistries();
target = getFixture();
serverData = {
models: {
'account.analytic.line': {
fields: {
unit_amount: { string: "Unit Amount", type: "float" },
},
records: [
{ id: 1, unit_amount: 8 }
],
},
},
views: {
"account.analytic.line,false,list": `
<tree>
<field name="unit_amount" widget="timesheet_uom"/>
</tree>
`,
},
};
serviceRegistry.add("ui", uiService);
serviceRegistry.add("company", companyService, { force: true });
patchWithCleanup(session, {
user_companies: {
current_company: 1,
allowed_companies: {
1: {
id: 1,
name: 'Company',
timesheet_uom_id: 2,
timesheet_uom_factor: 0.125,
},
},
},
user_context: {
allowed_company_ids: [1],
},
uom_ids: {
1: {
id: 1,
name: 'hour',
rounding: 0.01,
timesheet_widget: 'float_time',
},
2: {
id: 2,
name: 'day',
rounding: 0.01,
timesheet_widget: 'float_toggle',
},
}
});
});
QUnit.module("TimesheetFloatToggleField");
QUnit.test("factor is applied in TimesheetFloatToggleField", async function (assert) {
await makeView({
serverData,
type: "list",
resModel: "account.analytic.line",
});
assert.containsOnce(target, 'div[name="unit_amount"]:contains("1")', "TimesheetFloatToggleField should take `timesheet_uom_factor` into account");
});
QUnit.test("ranges are working properly in TimesheetFloatToggleField", async function (assert) {
serverData.models["account.analytic.line"].records[0].unit_amount = 1;
serverData.views["account.analytic.line,false,list"] = serverData.views["account.analytic.line,false,list"].replace('<tree', '<tree editable="bottom"')
await makeView({
serverData,
type: "list",
resModel: "account.analytic.line",
});
// Enter edit mode
await click(target, 'div[name="unit_amount"]');
await click(target, 'div[name="unit_amount"] .o_field_float_toggle');
assert.containsOnce(target, 'div[name="unit_amount"]:contains("0.00")', "ranges are working properly in TimesheetFloatToggleField");
await click(target, 'div[name="unit_amount"] .o_field_float_toggle');
assert.containsOnce(target, 'div[name="unit_amount"]:contains("0.50")', "ranges are working properly in TimesheetFloatToggleField");
await click(target, 'div[name="unit_amount"] .o_field_float_toggle');
assert.containsOnce(target, 'div[name="unit_amount"]:contains("1.00")', "ranges are working properly in TimesheetFloatToggleField");
});
QUnit.module("TimesheetFloatTimeField");
QUnit.test("factor is applied in TimesheetFloatTimeField", async function (assert) {
patchWithCleanup(session.user_companies.allowed_companies[1], {timesheet_uom_id: 1});
await makeView({
serverData,
type: "list",
resModel: "account.analytic.line",
});
assert.containsOnce(target, 'div[name="unit_amount"]:contains("08:00")', "TimesheetFloatTimeField should not take `timesheet_uom_factor` into account");
});
QUnit.module("TimesheetFloatFactorField");
QUnit.test("factor is applied in TimesheetFloatFactorField", async function (assert) {
patchWithCleanup(session.uom_ids[2], {timesheet_widget: 'float_factor'});
await makeView({
serverData,
type: "list",
resModel: "account.analytic.line",
});
assert.containsOnce(target, 'div[name="unit_amount"]:contains("1")', "TimesheetFloatFactorField should take `timesheet_uom_factor` into account");
});
});