Skip to content

Commit

Permalink
[IMP] Extra code simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
astirpe committed Mar 21, 2019
1 parent 4dc6093 commit b2c5b30
Showing 1 changed file with 20 additions and 26 deletions.
46 changes: 20 additions & 26 deletions hr_timesheet_sheet/models/hr_timesheet_sheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,29 +255,25 @@ def _compute_line_ids(self):
for sheet in self:
if not all([sheet.date_start, sheet.date_end]):
continue
dates = sheet._get_dates()
if not dates:
continue
data_matrix = {}
projects = sheet.timesheet_ids.mapped('project_id')
for date in dates:
for project in projects:
project_timesheets = sheet.timesheet_ids.filtered(
lambda x: x.project_id == project)
tasks = [project_timesheets.mapped('task_id')]
if not project_timesheets or not all(
[t.task_id for t in project_timesheets]):
tasks += [self.env['project.task']]
for task in tasks:
timesheets = project_timesheets.filtered(
lambda t: date == t.date
and t.task_id == task)
unit_amount = sum(t.unit_amount for t in timesheets)
data_matrix[(date, project, task)] = {
'unit_amount': unit_amount,
'timesheets': timesheets
}
sheet.line_ids = sheet._create_data_matrix_lines(data_matrix)
matrix = sheet._get_data_matrix()
sheet.line_ids = sheet._create_data_matrix_lines(matrix)

def _get_data_matrix(self):
self.ensure_one()
data_matrix = {}
empty_line = self.env['account.analytic.line']
init_item = {'unit_amount': 0.0, 'timesheets': empty_line}
for line in self.timesheet_ids:
data_key = (line.date, line.project_id, line.task_id)
if data_key not in data_matrix:
data_matrix[data_key] = init_item.copy()
data_matrix[data_key]['unit_amount'] += line.unit_amount
data_matrix[data_key]['timesheets'] += line
for date in self._get_dates():
for item in data_matrix.copy():
if (date, item[1], item[2]) not in data_matrix:
data_matrix[(date, item[1], item[2])] = init_item.copy()
return data_matrix

def _create_data_matrix_lines(self, data_matrix):
self.ensure_one()
Expand Down Expand Up @@ -456,9 +452,7 @@ def _get_default_sheet_line(self, date, project, task, unit_amount):
'unit_amount': unit_amount,
}
if self.id:
values.update({
'sheet_id': self.id,
})
values.update({'sheet_id': self.id})
return values

@api.model
Expand Down

0 comments on commit b2c5b30

Please sign in to comment.