Skip to content

Commit

Permalink
[FIX] Fix employee_id related stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
astirpe committed Mar 21, 2019
1 parent 22a26de commit 044654d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 17 deletions.
4 changes: 2 additions & 2 deletions hr_timesheet_sheet/models/account_analytic_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def _compute_sheet(self):
('date_start', '<=', timesheet.date),
('employee_id', '=', timesheet.employee_id.id),
('company_id', 'in', [timesheet.company_id.id, False]),
('state', '=', 'draft'),
('state', 'in', ['new', 'draft']),
], limit=1)
if timesheet.sheet_id != sheet:
timesheet.sheet_id = sheet
Expand Down Expand Up @@ -81,7 +81,7 @@ def _check_state(self):
if self.env.context.get('skip_check_state'):
return
for line in self:
if line.sheet_id and line.sheet_id.state != 'draft':
if line.sheet_id and line.sheet_id.state not in ['new', 'draft']:
raise UserError(
_('You cannot modify an entry in a confirmed '
'timesheet sheet.'))
Expand Down
5 changes: 3 additions & 2 deletions hr_timesheet_sheet/models/hr_employee.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ class HrEmployee(models.Model):

@api.multi
def _compute_timesheet_count(self):
Sheet = self.env['hr_timesheet.sheet']
for employee in self:
employee.timesheet_count = employee.env['hr_timesheet.sheet'].\
search_count([('employee_id', '=', employee.id)])
employee.timesheet_count = Sheet.search_count([
('employee_id', '=', employee.id)])

@api.constrains('company_id')
def _check_company_id(self):
Expand Down
27 changes: 17 additions & 10 deletions hr_timesheet_sheet/models/hr_timesheet_sheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ def _default_date_end(self):
return today

def _default_employee(self):
emp_ids = self.env['hr.employee'].search(
[('user_id', '=', self.env.uid)])
return emp_ids and emp_ids[0] or False
company = self.env['res.company']._company_default_get()
employee = self.env['hr.employee'].search([
('user_id', '=', self.env.uid),
('company_id', '=', company.id),
], limit=1)
return employee

name = fields.Char(
string="Note",
Expand All @@ -70,6 +73,8 @@ def _default_employee(self):
string='Employee',
default=lambda self: self._default_employee(),
required=True,
readonly=True,
states={'new': [('readonly', False)]},
)
user_id = fields.Many2one(
comodel_name='res.users',
Expand All @@ -84,22 +89,23 @@ def _default_employee(self):
required=True,
index=True,
readonly=True,
states={'draft': [('readonly', False)]},
states={'new': [('readonly', False)]},
)
date_end = fields.Date(
string='Date To',
default=lambda self: self._default_date_end(),
required=True,
index=True,
readonly=True,
states={'draft': [('readonly', False)]},
states={'new': [('readonly', False)]},
)
timesheet_ids = fields.One2many(
comodel_name='account.analytic.line',
inverse_name='sheet_id',
string='Timesheets',
readonly=True,
states={
'new': [('readonly', False)],
'draft': [('readonly', False)],
}
)
Expand All @@ -109,14 +115,16 @@ def _default_employee(self):
string='Timesheet Sheet Lines',
readonly=True,
states={
'new': [('readonly', False)],
'draft': [('readonly', False)],
}
)
state = fields.Selection([
('new', 'New'),
('draft', 'Open'),
('confirm', 'Waiting Approval'),
('done', 'Approved')],
default='draft', track_visibility='onchange',
default='new', track_visibility='onchange',
string='Status', required=True, readonly=True, index=True,
)
company_id = fields.Many2one(
Expand Down Expand Up @@ -235,7 +243,6 @@ def _check_company_id(self):
def _onchange_employee_id(self):
if self.employee_id:
self.department_id = self.employee_id.department_id
self.user_id = self.employee_id.user_id

def _get_timesheet_sheet_lines_domain(self):
self.ensure_one()
Expand Down Expand Up @@ -291,7 +298,7 @@ def _create_data_matrix_lines(self, data_matrix):
self.clean_timesheets(data_matrix[item]['timesheets'])
return lines

@api.onchange('date_start', 'date_end')
@api.onchange('date_start', 'date_end', 'employee_id')
def _onchange_dates(self):
domain = self._get_timesheet_sheet_lines_domain()
timesheets = self.env['account.analytic.line'].search(domain)
Expand Down Expand Up @@ -415,7 +422,7 @@ def action_timesheet_refuse(self):
@api.multi
def button_add_line(self):
for rec in self:
if rec.state == 'draft':
if rec.state in ['new', 'draft']:
rec.add_line()
rec.add_line_task_id = False
rec.add_line_project_id = False
Expand Down Expand Up @@ -496,7 +503,7 @@ def add_line(self):

def link_timesheets_to_sheet(self, timesheets):
self.ensure_one()
if self.id and self.state == 'draft':
if self.id and self.state in ['new', 'draft']:
for aal in timesheets.filtered(lambda a: not a.sheet_id):
aal.write({'sheet_id': self.id})

Expand Down
2 changes: 1 addition & 1 deletion hr_timesheet_sheet/tests/test_hr_timesheet_sheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ def setUp(self):

def test_1(self):
sheet = self.sheet_model.sudo(self.user).create({
'employee_id': self.employee.id,
'company_id': self.user.company_id.id,
})
self.assertEqual(len(sheet.timesheet_ids), 0)
self.assertEqual(len(sheet.line_ids), 0)
self.assertTrue(sheet.employee_id)

sheet.add_line_project_id = self.project_1
sheet.onchange_add_project_id()
Expand Down
4 changes: 2 additions & 2 deletions hr_timesheet_sheet/views/hr_timesheet_sheet_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<notebook>
<page string="Summary">
<group name="Sheet">
<field name="line_ids" nolabel="1" attrs="{'readonly': [('state', '!=', 'draft')]}"
<field name="line_ids" nolabel="1" attrs="{'readonly': [('state', 'not in', ['new', 'draft'])]}"
widget="x2many_2d_matrix"
field_x_axis="value_x"
field_y_axis="value_y"
Expand All @@ -71,7 +71,7 @@
<field name="task_id"/>
</tree>
</field>
<group class="oe_edit_only" attrs="{'invisible': [('state', '!=', 'draft')]}">
<group class="oe_edit_only" attrs="{'invisible': [('state', 'not in', ['new', 'draft'])]}">
<field name="add_line_project_id" domain="[('company_id', '=', company_id), ('allow_timesheets', '=', True)]"/>
<field name="add_line_task_id" attrs="{'invisible': [('add_line_project_id', '=', False)]}"
context="{'default_project_id': add_line_project_id}"/>
Expand Down

0 comments on commit 044654d

Please sign in to comment.