diff --git a/addons/hr/populate/hr.py b/addons/hr/populate/hr.py index f71e521034b..b0cc09926a8 100644 --- a/addons/hr/populate/hr.py +++ b/addons/hr/populate/hr.py @@ -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 diff --git a/addons/resource/__init__.py b/addons/resource/__init__.py index 23a96e9133a..44785a95139 100644 --- a/addons/resource/__init__.py +++ b/addons/resource/__init__.py @@ -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 diff --git a/addons/resource/populate/__init__.py b/addons/resource/populate/__init__.py new file mode 100644 index 00000000000..d310a34daa0 --- /dev/null +++ b/addons/resource/populate/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from . import resource_calendar diff --git a/addons/resource/populate/resource_calendar.py b/addons/resource/populate/resource_calendar.py new file mode 100644 index 00000000000..0eea78ff840 --- /dev/null +++ b/addons/resource/populate/resource_calendar.py @@ -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