Explorar el Código

fix: Use Odoo Planning's exact logic for allocated hours calculation

- Added pytz import for timezone handling
- Now uses _get_duration_over_period() method like Odoo Planning does
- Calculates intersection between slot and working intervals
- Uses _get_valid_work_intervals() to get resource work schedules
- This should match exactly how Odoo Planning calculates 99h33 vs our 181.25
- Implements the same logic as Odoo Planning's _compute_allocated_hours method
root hace 5 meses
padre
commit
a34892612f
Se han modificado 1 ficheros con 19 adiciones y 1 borrados
  1. 19 1
      hr_efficiency/models/hr_efficiency.py

+ 19 - 1
hr_efficiency/models/hr_efficiency.py

@@ -2,6 +2,7 @@
 # Part of Odoo. See LICENSE file for full copyright and licensing details.
 
 import logging
+import pytz
 from datetime import datetime, date
 from dateutil.relativedelta import relativedelta
 from odoo import api, fields, models, _
@@ -413,7 +414,24 @@ class HrEfficiency(models.Model):
         total_non_billable = 0.0
         
         for slot in planning_slots:
-            hours = slot.allocated_hours or 0.0
+            # Use the same logic as Odoo Planning to calculate allocated hours
+            if slot.start_datetime and slot.end_datetime and slot.resource_id:
+                # Calculate the intersection between slot and working intervals
+                start_utc = pytz.utc.localize(slot.start_datetime)
+                end_utc = pytz.utc.localize(slot.end_datetime)
+                
+                # Get working intervals for the resource
+                resource_work_intervals, calendar_work_intervals = slot.resource_id._get_valid_work_intervals(
+                    start_utc, end_utc, calendars=slot.company_id.resource_calendar_id
+                )
+                
+                # Calculate duration over working periods
+                hours = slot._get_duration_over_period(
+                    start_utc, end_utc,
+                    resource_work_intervals, calendar_work_intervals, has_allocated_hours=False
+                )
+            else:
+                hours = slot.allocated_hours or 0.0
             
             # Check if the slot is linked to a billable project
             if slot.project_id and slot.project_id.allow_billable: