Hello all,
I am using Odoo 16. I am working on a custom implementation in Odoo where I need to compute a start date (x_studio_studio_shift_start) for a task based on the end date (x_studio_studio_shift_end) and the allocated hours (allocated_hours). The requirement is that the start date should be calculated by subtracting the allocated hours from the end date.
What I’m Trying to Achieve:
- Model: planning.slot (this is the model where I'm implementing the logic)
- Fields:
- x_studio_studio_shift_start: Start date of the task (computed field)
- x_studio_studio_shift_end: End date of the task
- allocated_hours: Number of hours allocated to the task
The script below which I have tried but it is giving me errors. "ValueError: forbidden opcode(s) in 'from datetime import timedelta\n\n..."
Logic:
- Compute Start Date: The start date should be computed by subtracting the allocated hours from the end date. For example, if the end date is August 24, 2024, 08:41:28, and the allocated hours are 8, the start date should be August 23, 2024, 08:41:28.
- Planning by Resource: I'm using the Gantt view to manage tasks by resource. The idea is that if I drag and drop a task, the system will first consider the end date, then deduct the allocated hours, and finally set the start date accordingl
def _compute_shift_start(self):
- for record in self:
- if record.x_studio_studio_shift_end and record.allocated_hours:
- # Calculate start date by subtracting allocated hours from end date
- record.x_studio_studio_shift_start = fields.Datetime.subtract(
- record.x_studio_studio_shift_end, hours=record.allocated_hours
- )
- else:
- record.x_studio_studio_shift_start = record.x_studio_studio_shift_end