[FIX] mrp: fix batch version of _set_dates

`_set_dates` didn't work properly with multiple records.
In fact, it only used the `date_start` and `date_finished` of the
 first record. The assumption was that `date_start`/`date_finished`
 is always the same for each record in self, because it usually
 comes from a write call (writing the same values to a batch of
 records). But inverse methods are also called by the create
 method, and then the previous assumption isn't true anymore.

This bug leads to several inconsistencies between the `date_start`
and the `leave_id`. The `test_replan_mo_without_bom` was fixed in
the previous version, but with the new onchange, it breaks again
because of these inconsistencies.

closes odoo/odoo#135635

Signed-off-by: Raphael Collet <rco@odoo.com>
This commit is contained in:
Rémy Voet (ryv)
2023-09-18 22:17:07 +00:00
parent a62baec7f6
commit 82b3fe05ea
+10 -16
View File
@@ -238,30 +238,24 @@ class MrpWorkorder(models.Model):
workorder.date_finished = workorder.leave_id.date_to
def _set_dates(self):
if not self[0].date_start or not self[0].date_finished:
if not self.leave_id:
return
raise UserError(_("It is not possible to unplan one single Work Order. "
"You should unplan the Manufacturing Order instead in order to unplan all the linked operations."))
date_from = self[0].date_start
date_to = self[0].date_finished
to_write = self.env['mrp.workorder']
for wo in self.sudo():
if wo.leave_id:
to_write |= wo
else:
if (not wo.date_start or not wo.date_finished):
raise UserError(_("It is not possible to unplan one single Work Order. "
"You should unplan the Manufacturing Order instead in order to unplan all the linked operations."))
wo.leave_id.write({
'date_from': wo.date_start,
'date_to': wo.date_finished,
})
elif wo.date_start and wo.date_finished:
wo.leave_id = wo.env['resource.calendar.leaves'].create({
'name': wo.display_name,
'calendar_id': wo.workcenter_id.resource_calendar_id.id,
'date_from': date_from,
'date_to': date_to,
'date_from': wo.date_start,
'date_to': wo.date_finished,
'resource_id': wo.workcenter_id.resource_id.id,
'time_type': 'other',
})
to_write.leave_id.write({
'date_from': date_from,
'date_to': date_to,
})
@api.constrains('blocked_by_workorder_ids')
def _check_no_cyclic_dependencies(self):