Skip to content

Commit

Permalink
[FIX] project_timesheet_holidays: add default project and task on status
Browse files Browse the repository at this point in the history
When using this module, the default behavior is to generate timesheet.
This timsheet is linked to a project and a task and there is a
constrain that verify that the task and project are set on model.
If another module (like l10n_fr_hr_payroll for example) tries to
create holidays.status without a project and task, it causes a
traceback.

This commit adds default values for these fields in order fullfill
the constrains.

Notice that the order of the imports in the __init__ is important
because the columns have to be created on the res.company before the
status is initialized.
  • Loading branch information
d-fence committed Nov 24, 2017
1 parent f347865 commit e0a4d18
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion addons/project_timesheet_holidays/models/__init__.py
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from . import res_company # has to be before hr_holidays to create needed columns on res.company
from . import account_analytic
from . import hr_holidays
from . import res_config_settings
from . import res_company
12 changes: 10 additions & 2 deletions addons/project_timesheet_holidays/models/hr_holidays.py
Expand Up @@ -8,9 +8,17 @@
class HolidaysType(models.Model):
_inherit = "hr.holidays.status"

def _default_project_id(self):
company = self.company_id if self.company_id else self.env.user.company_id
return company.leave_timesheet_project_id.id

def _default_task_id(self):
company = self.company_id if self.company_id else self.env.user.company_id
return company.leave_timesheet_task_id.id

timesheet_generate = fields.Boolean('Generate Timesheet', default=True, help="If checked, when validating a leave, timesheet will be generated in the Vacation Project of the company.")
timesheet_project_id = fields.Many2one('project.project', string="Internal Project", help="The project will contain the timesheet generated when a leave is validated.")
timesheet_task_id = fields.Many2one('project.task', string="Internal Task for timesheet", domain="[('project_id', '=', timesheet_project_id)]")
timesheet_project_id = fields.Many2one('project.project', string="Internal Project", default=_default_project_id, help="The project will contain the timesheet generated when a leave is validated.")
timesheet_task_id = fields.Many2one('project.task', string="Internal Task for timesheet", default=_default_task_id, domain="[('project_id', '=', timesheet_project_id)]")

@api.onchange('timesheet_generate')
def _onchange_timesheet_generate(self):
Expand Down
15 changes: 15 additions & 0 deletions addons/project_timesheet_holidays/tests/test_timesheet_holidays.py
Expand Up @@ -6,9 +6,24 @@

from odoo import fields

from odoo.tests import common
from odoo.addons.hr_timesheet.tests.test_timesheet import TestTimesheet


class TestTimesheetHolidaysCreate(common.TransactionCase):

def test_status_create(self):
"""Ensure that when a status is created, it fullfills the project and task constrains"""
status = self.env['hr.holidays.status'].create({
'name': 'A nice Leave Type',
'limit': True
})

company = self.env.user.company_id
self.assertEqual(status.timesheet_project_id, company.leave_timesheet_project_id, 'The default project linked to the status should be the same as the company')
self.assertEqual(status.timesheet_task_id, company.leave_timesheet_task_id, 'The default task linked to the status should be the same as the company')


class TestTimesheetHolidays(TestTimesheet):

def setUp(self):
Expand Down

0 comments on commit e0a4d18

Please sign in to comment.