Skip to content

Commit

Permalink
Merge pull request #223 from onesteinbv/11_fix_hr_timesheet_sheet2
Browse files Browse the repository at this point in the history
[11.0][FIX] hr_timesheet_sheet: case of employee's company not set
  • Loading branch information
pedrobaeza authored Apr 5, 2019
2 parents e4cc192 + 27dca8b commit 411d72f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
21 changes: 14 additions & 7 deletions hr_timesheet_sheet/models/hr_timesheet_sheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,10 @@ def _default_date_end(self):

def _default_employee(self):
company = self.env['res.company']._company_default_get()
employee = self.env['hr.employee'].search([
return self.env['hr.employee'].search([
('user_id', '=', self.env.uid),
('company_id', '=', company.id),
], limit=1)
return employee
('company_id', 'in', [company.id, False]),
], limit=1, order="company_id ASC")

name = fields.Char(
string="Note",
Expand Down Expand Up @@ -126,6 +125,7 @@ def _default_employee(self):
comodel_name='res.company',
string='Company',
default=lambda self: self.env['res.company']._company_default_get(),
required=True,
readonly=True,
)
department_id = fields.Many2one(
Expand Down Expand Up @@ -222,11 +222,19 @@ def _check_company_id_add_line_task_id(self):
_('The Company in the Timesheet Sheet and in '
'the Task must be the same.'))

def _get_timesheet_sheet_company(self):
self.ensure_one()
employee = self.employee_id
company = employee.company_id or employee.department_id.company_id
if not company:
company = employee.user_id.company_id
return company

@api.onchange('employee_id')
def _onchange_employee_id(self):
if self.employee_id:
self.department_id = self.employee_id.department_id
self.company_id = self.employee_id.company_id
self.company_id = self._get_timesheet_sheet_company()

def _get_timesheet_sheet_lines_domain(self):
self.ensure_one()
Expand All @@ -235,7 +243,7 @@ def _get_timesheet_sheet_lines_domain(self):
('date', '<=', self.date_end),
('date', '>=', self.date_start),
('employee_id', '=', self.employee_id.id),
('company_id', '=', self.employee_id.company_id.id),
('company_id', '=', self._get_timesheet_sheet_company().id),
]

@api.multi
Expand Down Expand Up @@ -312,7 +320,6 @@ def create(self, vals):
raise UserError(
_('In order to create a sheet for this employee, '
'you must link him/her to an user.'))
vals['company_id'] = employee.company_id.id
res = super(Sheet, self).create(vals)
res.write({'state': 'draft'})
self.delete_empty_lines(True)
Expand Down
35 changes: 35 additions & 0 deletions hr_timesheet_sheet/tests/test_hr_timesheet_sheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,3 +634,38 @@ def test_13(self):

with self.assertRaises(ValidationError):
analytic_account.company_id = self.company_2

def test_14(self):
department = self.department_model.create({
'name': "Department test",
'company_id': False,
})
new_employee = self.employee_model.create({
'name': "Test User",
'user_id': self.user.id,
'company_id': False,
'department_id': department.id,
})
self.assertFalse(new_employee.company_id)
sheet_no_department = self.sheet_model.sudo(self.user).create({
'employee_id': new_employee.id,
'department_id': False,
'date_start': self.sheet_model._default_date_start(),
'date_end': self.sheet_model._default_date_end(),
})
self.assertFalse(sheet_no_department.department_id)
sheet_no_department._onchange_employee_id()
self.assertTrue(sheet_no_department.department_id)
self.assertEqual(sheet_no_department.department_id, department)
self.assertTrue(sheet_no_department.company_id)

sheet_no_department.unlink()
sheet_no_employee = self.sheet_model.sudo(self.user).create({
'date_start': self.sheet_model._default_date_start(),
'date_end': self.sheet_model._default_date_end(),
})
self.assertTrue(sheet_no_employee.employee_id)
self.assertFalse(sheet_no_employee.department_id)
sheet_no_employee._onchange_employee_id()
self.assertFalse(sheet_no_employee.department_id)
self.assertTrue(sheet_no_employee.company_id)
2 changes: 1 addition & 1 deletion hr_timesheet_sheet/views/hr_timesheet_sheet_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<div style="display: inline;"><field name="date_start" class="oe_inline"/> to <field name="date_end" class="oe_inline"/></div>
<field name="name" invisible="1"/>
<field name="department_id" invisible="1"/>
<field name="company_id" groups="base.group_multi_company"/>
<field name="company_id" groups="base.group_multi_company" force_save="1"/>
</group>
</group>
<notebook>
Expand Down

0 comments on commit 411d72f

Please sign in to comment.