[IMP] hr+resource: populate: link users and resource calendars

Previously for `appointment`, now for `appointment_hr`, the computation of
users' availability for appointment slots can be dependent on that person's
work schedule if:

* The user is linked to an employee
* The user's employee has working hours defined (resource calendar)

This commit provides working hours to all employees and a user to some, within
a multi-company setup.

Task-2728093
See odoo/enterprise#22642

closes odoo/odoo#80688

Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
This commit is contained in:
Florian Charlier
2022-02-01 14:37:37 +00:00
parent 26009fec93
commit ca0f936dd1
4 changed files with 80 additions and 7 deletions
+31 -7
View File
@@ -78,7 +78,8 @@ class HrEmployee(models.Model):
_inherit = 'hr.employee'
_populate_sizes = {'small': 100, 'medium': 2000, 'large': 20000}
_populate_dependencies = ['hr.department', 'hr.job', 'hr.work.location', 'hr.employee.category']
_populate_dependencies = ['res.company', 'res.users', 'resource.calendar', 'hr.department',
'hr.job', 'hr.work.location', 'hr.employee.category']
def _populate(self, size):
employees = super()._populate(size)
@@ -86,11 +87,32 @@ class HrEmployee(models.Model):
return employees
def _populate_factories(self):
company_id = self.env.ref('base.main_company').id
company_ids = self.env['res.company'].browse(self.env.registry.populated_models['res.company'])
company_calendars = {}
for company_id in company_ids:
company_calendars[company_id.id] = company_id.resource_calendar_ids.filtered_domain([
('name', 'not like', 'Standard')]).ids
department_ids = self.env.registry.populated_models['hr.department']
job_ids = self.env.registry.populated_models['hr.job']
work_location_ids = self.env.registry.populated_models['hr.work.location']
tag_ids = self.env.registry.populated_models['hr.employee.category']
user_ids = self.env['res.users'].browse(self.env.registry.populated_models['res.users'])
def _compute_user_and_company(iterator, *args):
# First users
for values, user_id in zip(iterator, user_ids):
yield {'company_id': user_id.company_id.id,
'user_id': user_id.id,
**values}
# then as many as required non - users
for values in iterator:
yield {'company_id': populate.random.choice(company_ids).id,
'user_id': False,
**values}
def get_resource_calendar_id(values, random, **kwargs):
return random.choice(company_calendars[values['company_id']])
def get_tag_ids(values, counter, random):
return [
@@ -101,23 +123,25 @@ class HrEmployee(models.Model):
return [
('active', populate.iterate([True, False], [0.9, 0.1])),
('company_id', populate.constant(company_id)),
('name', populate.constant("employee_{counter}")),
('_user_and_company', _compute_user_and_company),
('department_id', populate.randomize(department_ids)),
('job_id', populate.randomize(job_ids)),
('work_location_id', populate.randomize(work_location_ids)),
('category_ids', populate.compute(get_tag_ids)),
('resource_calendar_id', populate.compute(get_resource_calendar_id)),
]
def _populate_set_manager(self, employees):
manager_ids = []
manager_ids = defaultdict(list)
rand = populate.Random('hr.employee+manager_generator')
for employee in employees:
if rand.random() >= 0.85: # 15% of employees are managers
manager_ids.append(employee.id)
# 15% of employees are managers, at least one per company
if rand.random() >= 0.85 or not manager_ids.get(employee.company_id):
manager_ids[employee.company_id].append(employee.id)
for employee in employees:
manager = rand.choice(manager_ids)
manager = rand.choice(manager_ids[employee.company_id])
if manager != employee.id:
employee.parent_id = manager
+1
View File
@@ -2,4 +2,5 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import models
from . import populate
from .tests import test_resource_model
+4
View File
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import resource_calendar
@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import random
from odoo import models
from odoo.tools import populate
class ResourceCalendar(models.Model):
_inherit = "resource.calendar"
_populate_dependencies = ["res.company"] # multi-company setup
_populate_sizes = {
"small": 10, # 1-2 per company
"medium": 30, # 3 per company
"large": 250 # 5 per company
}
def _populate_factories(self):
company_ids = self.env.registry.populated_models["res.company"]
return [
("company_id", populate.iterate(company_ids)),
("name", populate.iterate(["A little {counter}", "A lot {counter}"])),
]
def _populate(self, size):
records = super()._populate(size)
# Randomly remove 1 half day from schedule
a_lot = records.filtered_domain([("name", "like", "A lot")])
for record in a_lot:
att_id = record.attendance_ids[random.randint(0, 9)]
record.write({
'attendance_ids': [(3, att_id.id)],
})
# Randomly remove 3 to 5 half days from schedule
a_little = records - a_lot
for record in a_little:
to_pop = random.sample(range(10), random.randint(3, 5))
record.write({
'attendance_ids': [(3, record.attendance_ids[idx].id) for idx in to_pop],
})
return records