From d9fa0d1554ad51128b33ceae5b43f4c521ff6e11 Mon Sep 17 00:00:00 2001
From: PNO <pno@odoo.com>
Date: Thu, 11 May 2023 08:20:15 +0000
Subject: [PATCH] [FIX] mrp: compute interval duration with multiple loss_id

When working on an operation in workcenter that requires login, we can add multiple timers.
However, if multiple timers do not have an `end_date` and have different `loss_id`, we will get a traceback (expected singleton) when computing the interval duration.
After this fix, `_convert_to_duration` can be called with multiple productivity loss.

OPW-3292374

closes odoo/odoo#121112

Signed-off-by: William Henrotin (whe) <whe@odoo.com>
---
 addons/mrp/models/mrp_workcenter.py | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/addons/mrp/models/mrp_workcenter.py b/addons/mrp/models/mrp_workcenter.py
index d3ca0044bfaa..88c94e293b42 100644
--- a/addons/mrp/models/mrp_workcenter.py
+++ b/addons/mrp/models/mrp_workcenter.py
@@ -359,11 +359,14 @@ class MrpWorkcenterProductivityLoss(models.Model):
         and the workcenter has a calendar, convert the dates into a duration based on
         working hours.
         """
-        if (self.loss_type not in ('productive', 'performance')) and workcenter and workcenter.resource_calendar_id:
-            r = workcenter._get_work_days_data_batch(date_start, date_stop)[workcenter.id]['hours']
-            return round(r * 60, 2)
-        else:
-            return round((date_stop - date_start).total_seconds() / 60.0, 2)
+        duration = 0
+        for productivity_loss in self:
+            if (productivity_loss.loss_type not in ('productive', 'performance')) and workcenter and workcenter.resource_calendar_id:
+                r = workcenter._get_work_days_data_batch(date_start, date_stop)[workcenter.id]['hours']
+                duration = max(duration, r * 60)
+            else:
+                duration = max(duration, (date_stop - date_start).total_seconds() / 60.0)
+        return round(duration, 2)
 
 class MrpWorkcenterProductivity(models.Model):
     _name = "mrp.workcenter.productivity"
-- 
GitLab