|
@@ -22,48 +22,34 @@ class PlanningSlot(models.Model):
|
|
|
|
|
|
|
|
real_working_days_count = fields.Float(
|
|
real_working_days_count = fields.Float(
|
|
|
string='Real Working Days',
|
|
string='Real Working Days',
|
|
|
- compute='_compute_real_working_days_count',
|
|
|
|
|
store=True,
|
|
store=True,
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
- @api.depends('start_datetime', 'end_datetime', 'resource_id')
|
|
|
|
|
- def _compute_real_working_days_count(self):
|
|
|
|
|
- slots_per_calendar = defaultdict(set)
|
|
|
|
|
- planned_dates_per_calendar_id = defaultdict(lambda: (datetime.max, datetime.min))
|
|
|
|
|
|
|
+ def _compute_working_days_count(self):
|
|
|
|
|
+ super(PlanningSlot, self)._compute_working_days_count()
|
|
|
for slot in self:
|
|
for slot in self:
|
|
|
- if not slot.employee_id:
|
|
|
|
|
|
|
+ if not slot.start_datetime:
|
|
|
slot.real_working_days_count = 0
|
|
slot.real_working_days_count = 0
|
|
|
continue
|
|
continue
|
|
|
- slots_per_calendar[slot.resource_id.calendar_id].add(slot.id)
|
|
|
|
|
- datetime_begin, datetime_end = planned_dates_per_calendar_id[slot.resource_id.calendar_id.id]
|
|
|
|
|
|
|
|
|
|
- start_datetime = slot.start_datetime
|
|
|
|
|
- first_day_of_month = datetime(start_datetime.year, start_datetime.month, 1, 0, 0, 0)
|
|
|
|
|
- last_day = calendar.monthrange(start_datetime.year, start_datetime.month)[1]
|
|
|
|
|
- last_day_of_month = datetime(start_datetime.year, start_datetime.month, last_day, 23, 59, 59)
|
|
|
|
|
|
|
+ # Obtener el primer día del mes
|
|
|
|
|
+ start_of_month = slot.start_datetime.replace(day=1, hour=0, minute=0, second=0)
|
|
|
|
|
+ # Obtener el último día del mes
|
|
|
|
|
+ end_of_month = (start_of_month + relativedelta(months=1, days=-1)).replace(hour=23, minute=59, second=59)
|
|
|
|
|
|
|
|
- datetime_begin = min(datetime_begin, first_day_of_month)
|
|
|
|
|
- datetime_end = max(datetime_end, last_day_of_month)
|
|
|
|
|
|
|
+ # Obtener el calendario de trabajo
|
|
|
|
|
+ calendar = slot.resource_id.calendar_id or slot.company_id.resource_calendar_id
|
|
|
|
|
|
|
|
- planned_dates_per_calendar_id[slot.resource_id.calendar_id.id] = datetime_begin, datetime_end
|
|
|
|
|
- for calendar_slot, slot_ids in slots_per_calendar.items():
|
|
|
|
|
- slots = self.env['planning.slot'].browse(list(slot_ids))
|
|
|
|
|
- if not calendar_slot:
|
|
|
|
|
- slots.real_working_days_count = 0
|
|
|
|
|
|
|
+ if not calendar:
|
|
|
|
|
+ slot.real_working_days_count = 0
|
|
|
continue
|
|
continue
|
|
|
- datetime_begin, datetime_end = planned_dates_per_calendar_id[calendar_slot.id]
|
|
|
|
|
- datetime_begin = timezone_datetime(datetime_begin)
|
|
|
|
|
- datetime_end = timezone_datetime(datetime_end)
|
|
|
|
|
- resources = slots.resource_id
|
|
|
|
|
- day_total = calendar_slot._get_resources_day_total(datetime_begin, datetime_end, resources)
|
|
|
|
|
- _logger.info('DAY TOTAL %s', day_total)
|
|
|
|
|
- intervals = calendar_slot._work_intervals_batch(datetime_begin, datetime_end, resources)
|
|
|
|
|
- for slot in slots:
|
|
|
|
|
- slot.real_working_days_count = calendar_slot._get_days_data(
|
|
|
|
|
- intervals[slot.resource_id.id] & Intervals([(
|
|
|
|
|
- timezone_datetime(first_day_of_month),
|
|
|
|
|
- timezone_datetime(last_day_of_month),
|
|
|
|
|
- self.env['resource.calendar.attendance']
|
|
|
|
|
- )]),
|
|
|
|
|
- day_total[slot.resource_id.id]
|
|
|
|
|
- )['days']
|
|
|
|
|
|
|
+
|
|
|
|
|
+ # Calcular los días laborables del mes completo
|
|
|
|
|
+ real_working_days = calendar.get_work_duration_data(
|
|
|
|
|
+ start_of_month,
|
|
|
|
|
+ end_of_month,
|
|
|
|
|
+ compute_leaves=True
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ # El resultado viene en horas, lo convertimos a días
|
|
|
|
|
+ slot.real_working_days_count = real_working_days['hours'] / calendar.hours_per_day
|