|
|
"""
Track how long a record stays in different stages.
This method calculates elapsed time since the last stage change and stores the
information in the `duration_tracking` dictionary.
The dictionary keys are:
* d (datetime):
The exact UTC datetime the record added the current state.
* s (stage id):
The unique ID of the *current* stage the record is in.
<stage_id> (int):
A running total of accumulated time (in minutes) spent in previous states, mapping
stage IDs to minutes.
Example:
{"d":"2025-11-21 15:34:28","s":3, "1":172814,"2":86401 , '0': 12 }
"""
now = self.env.cr.now()
for record in self:
duration_dict = record.duration_tracking or {}
last_stage_id = duration_dict.get('s')
if last_stage_id is not None and duration_dict.get('d'):
old_update = fields.Datetime.to_datetime(duration_dict['d'])
duration_dict[str(last_stage_id)] = duration_dict.get(str(last_stage_id), 0) + int((now - old_update) / timedelta(minutes=1))
# update last stage / datetime
duration_dict['s'] = record[record._track_duration_field].id or 0
duration_dict['d'] = self.env.cr.now()
record.duration_tracking = duration_dict
|
|