We are porting some code to v8. For this we are rewriting our old on_change_xxx handlers with the new @api.onchange methods.
In the HR module we'd like to:
1. Set the 'date_from' value of a new entry to the latest time of all entries of the current day.
2. Check for overlapping time-entries dynamically (not when finally writing them).
For this to achieve we need to be able to access both, entries in the DB and entries in the 'cache' e.g. entries not yet persisted to the DB, within the @api.onchange decorated method. Accessing the DB is no problem. But how can we access all edited/added lines which have not yet been persited (no write done)?
PS: With v7, the on change handlers have been registered in the views and where passed the correct model/cache. E.g. on_change="on_change_hours(hour_from, hour_to, parent.timesheet_ids, date)"...
To make the problem clear here is a screenshot and some code:
https://s3-eu-west-1.amazonaws.com/uploads-eu.hipchat.com/198549/2302468/47Td6X8wAxkEBoR/problem.png
During ONE edit session, the user adds these three entries. We want to be able to check for overlapping entries during editing and NOT when saving. So we like to report an error to the user, that there are overlapping lines (line 1 and line2).
We tried the following code:
@api.onchange('date', 'hour_from', 'hour_to')
def on_change_hour_values(self):
# self._origin contains the analytic_timesheet which is being changed right now
# find the current timesheet and get all entries for this timesheet
analytic_timesheets = self._origin.sheet_id.timesheet_ids
timesheets_to_check = []
for entry in analytic_timesheets:
timesheet_data = {}
if entry == self._origin:
# this is the currently changeing line. Don't take the DB values but the changed ones.
timesheet_data['hour_from'] = self.hour_from
timesheet_data['hour_to'] = self.hour_to
timesheet_data['date'] = self.date
timesheet_data['unit_amount'] = self.unit_amount
else:
timesheet_data['hour_from'] = entry.hour_from
timesheet_data['hour_to'] = entry.hour_to
timesheet_data['date'] = entry.date
timesheet_data['unit_amount'] = entry.unit_amount
timesheets_to_check.append(timesheet_data)
self.env['hr_timesheet_sheet.sheet'].duration_validation(timesheets_to_check)
...
but it does not behave as expected.
If a user adds multiple new lines during one edit sessions, these entries can not be accessed.
So the question is: In the screenshot below, how can we access the values from the three entries during editing, just when the onchange handler fires?