# -*- coding: utf-8 -*- # Copyright 2026 Nexa Systems Inc. # License OPL-1 (Odoo Proprietary License v1.0) from odoo import api, fields, models class PlanningSlot(models.Model): _inherit = 'planning.slot' # Bulk-create helper: when set on a NEW slot, the system also creates # one identical copy of the shift for each of these resources. # Combined with the recurrence config, this lets a manager schedule # the same shift across many employees in a single Save. x_fc_additional_resource_ids = fields.Many2many( 'resource.resource', 'fc_planning_slot_extra_resource_rel', 'slot_id', 'resource_id', string='Apply Also To', help="Also create this same shift for each of these employees. " "Useful when scheduling the same shift across many people in one go.", ) @api.model_create_multi def create(self, vals_list): # 1. Auto-publish: every new shift is born published so employees # see it immediately. Manager keeps full control via the existing # Publish/Unpublish buttons after the fact. # 2. Multi-resource expansion: if a manager fills the Apply Also To # field, build one extra vals dict per additional resource. expanded_vals_list = [] for vals in vals_list: vals.setdefault('state', 'published') extra_ids = self._fc_pop_additional_resource_ids(vals) primary_rid = vals.get('resource_id') expanded_vals_list.append(vals) for rid in extra_ids: if rid == primary_rid: continue copy_vals = dict(vals) copy_vals['resource_id'] = rid copy_vals.pop('x_fc_additional_resource_ids', None) expanded_vals_list.append(copy_vals) return super().create(expanded_vals_list) @api.model def _fc_pop_additional_resource_ids(self, vals): """Pop and flatten the m2m vals into a plain list of resource ids.""" cmds = vals.pop('x_fc_additional_resource_ids', None) or [] ids = [] for cmd in cmds: # Odoo m2m commands: (6, 0, ids) replace, (4, id) link, (1, id, vals) update if isinstance(cmd, (list, tuple)): if cmd[0] == 6 and len(cmd) >= 3: ids.extend(cmd[2]) elif cmd[0] == 4 and len(cmd) >= 2: ids.append(cmd[1]) elif isinstance(cmd, int): ids.append(cmd) return ids