Files
odoo_source/addons/hr_work_entry_holidays/tests/common.py
T
Rob Coekaerts 6074d86b20 [IMP] hr_holidays: refactor leave duration
The idea of current date_{from,to} computations is as follows:

The user selects the request_date_{from,end} (and optionally
request_hour_{from,to} and these inputs are then processed into a
date_{to,from}, taking into account the type of leave, the work schedule
(resource_calendar) and time zone (since date_{to,from} are saved in UTC
while the request_dates are stored in the user's timezone.

However, in practice this computation is very messy, resulting in
date_{to,from} needing to be specified in all demo data and test cases,
even though it should be derived from the request dates. Various
superfluous or poorly named methods also exist in this flow
(eg _get_start_or_end_from_attendance which really performs a timezone
conversion, the logic of which resource calendar to use is scattered
across the whole model etc).

date_{to,from} are used many times as inputs throughout the code, with
code being present te inverse compute request_date_{from,to} from these
values. However in reality this is not possible to do consistently.

Therefore with this commit, we restore request_date_{from,to} as the
sole possible inputs, with date_{from,to} being derived from them. In
addition, the timezone and resource calendar are consolidated into their
own fields, with a single computation method computing them.

task-3081565

Part-of: odoo/odoo#119317
2023-09-26 09:32:27 +00:00

92 lines
4.8 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from datetime import datetime
from odoo import Command
from odoo.fields import Datetime
from odoo.addons.hr_work_entry_contract.tests.common import TestWorkEntryBase
class TestWorkEntryHolidaysBase(TestWorkEntryBase):
@classmethod
def setUpClass(cls):
super(TestWorkEntryHolidaysBase, cls).setUpClass()
cls.leave_type = cls.env['hr.leave.type'].create({
'name': 'Legal Leaves',
'time_type': 'leave',
'requires_allocation': 'no',
'responsible_ids': [Command.link(cls.env.ref('base.user_admin').id)],
'work_entry_type_id': cls.work_entry_type_leave.id
})
cls.env.ref('base.user_admin').notification_type = 'inbox'
# I create a new employee "Jules"
cls.jules_emp = cls.env['hr.employee'].create({
'name': 'Jules',
'gender': 'male',
'birthday': '1984-05-01',
'country_id': cls.env.ref('base.be').id,
'department_id': cls.dep_rd.id,
})
cls.calendar_35h = cls.env['resource.calendar'].create({
'name': '35h calendar',
'attendance_ids': [
(0, 0, {'name': 'Monday Morning', 'dayofweek': '0', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
(0, 0, {'name': 'Monday Lunch', 'dayofweek': '0', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
(0, 0, {'name': 'Monday Evening', 'dayofweek': '0', 'hour_from': 13, 'hour_to': 16, 'day_period': 'afternoon'}),
(0, 0, {'name': 'Tuesday Morning', 'dayofweek': '1', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
(0, 0, {'name': 'Tuesday Lunch', 'dayofweek': '1', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
(0, 0, {'name': 'Tuesday Evening', 'dayofweek': '1', 'hour_from': 13, 'hour_to': 16, 'day_period': 'afternoon'}),
(0, 0, {'name': 'Wednesday Morning', 'dayofweek': '2', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
(0, 0, {'name': 'Wednesday Lunch', 'dayofweek': '2', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
(0, 0, {'name': 'Wednesday Evening', 'dayofweek': '2', 'hour_from': 13, 'hour_to': 16, 'day_period': 'afternoon'}),
(0, 0, {'name': 'Thursday Morning', 'dayofweek': '3', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
(0, 0, {'name': 'Thursday Lunch', 'dayofweek': '3', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
(0, 0, {'name': 'Thursday Evening', 'dayofweek': '3', 'hour_from': 13, 'hour_to': 16, 'day_period': 'afternoon'}),
(0, 0, {'name': 'Friday Morning', 'dayofweek': '4', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
(0, 0, {'name': 'Friday Lunch', 'dayofweek': '4', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
(0, 0, {'name': 'Friday Evening', 'dayofweek': '4', 'hour_from': 13, 'hour_to': 16, 'day_period': 'afternoon'})
]
})
cls.calendar_40h = cls.env['resource.calendar'].create({'name': 'Default calendar'})
# This contract ends at the 15th of the month
cls.contract_cdd = cls.env['hr.contract'].create({ # Fixed term contract
'date_end': datetime.strptime('2015-11-15', '%Y-%m-%d'),
'date_start': datetime.strptime('2015-01-01', '%Y-%m-%d'),
'name': 'First CDD Contract for Jules',
'resource_calendar_id': cls.calendar_40h.id,
'wage': 5000.0,
'employee_id': cls.jules_emp.id,
'state': 'open',
'kanban_state': 'blocked',
'date_generated_from': datetime.strptime('2015-11-16', '%Y-%m-%d'),
'date_generated_to': datetime.strptime('2015-11-16', '%Y-%m-%d'),
})
# This contract starts the next day
cls.contract_cdi = cls.env['hr.contract'].create({
'date_start': datetime.strptime('2015-11-16', '%Y-%m-%d'),
'name': 'Contract for Jules',
'resource_calendar_id': cls.calendar_35h.id,
'wage': 5000.0,
'employee_id': cls.jules_emp.id,
'state': 'open',
'kanban_state': 'normal',
'date_generated_from': datetime.strptime('2015-11-15', '%Y-%m-%d'),
'date_generated_to': datetime.strptime('2015-11-15', '%Y-%m-%d'),
})
@classmethod
def create_leave(cls, date_from=None, date_to=None):
return cls.env['hr.leave'].create({
'name': 'Holiday!!!',
'employee_id': cls.richard_emp.id,
'holiday_status_id': cls.leave_type.id,
'request_date_to': date_to or Datetime.today(),
'request_date_from': date_from or Datetime.today(),
})