2018-01-16 06:58:15 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2018-01-16 11:34:37 +01:00
|
|
|
# Part of Odoo, Flectra. See LICENSE file for full copyright and licensing details.
|
2018-01-16 06:58:15 +01:00
|
|
|
|
2018-01-16 11:34:37 +01:00
|
|
|
from flectra import api, fields, models
|
2018-01-16 06:58:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
class AccountAnalyticLine(models.Model):
|
|
|
|
_inherit = 'account.analytic.line'
|
|
|
|
|
|
|
|
@api.model
|
|
|
|
def default_get(self, field_list):
|
|
|
|
result = super(AccountAnalyticLine, self).default_get(field_list)
|
|
|
|
if 'employee_id' in field_list and result.get('user_id'):
|
|
|
|
result['employee_id'] = self.env['hr.employee'].search([('user_id', '=', result['user_id'])], limit=1).id
|
|
|
|
return result
|
|
|
|
|
2018-04-05 10:25:40 +02:00
|
|
|
task_id = fields.Many2one('project.task', 'Task', index=True)
|
2018-01-16 06:58:15 +01:00
|
|
|
project_id = fields.Many2one('project.project', 'Project', domain=[('allow_timesheets', '=', True)])
|
|
|
|
|
|
|
|
employee_id = fields.Many2one('hr.employee', "Employee")
|
2018-04-05 10:25:40 +02:00
|
|
|
department_id = fields.Many2one('hr.department', "Department", compute='_compute_department_id', store=True)
|
2018-01-16 06:58:15 +01:00
|
|
|
|
|
|
|
@api.onchange('project_id')
|
|
|
|
def onchange_project_id(self):
|
2018-04-05 10:25:40 +02:00
|
|
|
# reset task when changing project
|
2018-01-16 06:58:15 +01:00
|
|
|
self.task_id = False
|
2018-04-05 10:25:40 +02:00
|
|
|
# force domain on task when project is set
|
|
|
|
if self.project_id:
|
|
|
|
return {'domain': {
|
|
|
|
'task_id': [('project_id', '=', self.project_id.id)]
|
|
|
|
}}
|
2018-01-16 06:58:15 +01:00
|
|
|
|
|
|
|
@api.onchange('employee_id')
|
|
|
|
def _onchange_employee_id(self):
|
|
|
|
self.user_id = self.employee_id.user_id
|
|
|
|
|
2018-04-05 10:25:40 +02:00
|
|
|
@api.depends('employee_id')
|
|
|
|
def _compute_department_id(self):
|
|
|
|
for line in self:
|
|
|
|
line.department_id = line.employee_id.department_id
|
|
|
|
|
2018-01-16 06:58:15 +01:00
|
|
|
@api.model
|
|
|
|
def create(self, vals):
|
|
|
|
vals = self._timesheet_preprocess(vals)
|
|
|
|
return super(AccountAnalyticLine, self).create(vals)
|
|
|
|
|
|
|
|
@api.multi
|
|
|
|
def write(self, vals):
|
|
|
|
vals = self._timesheet_preprocess(vals)
|
|
|
|
return super(AccountAnalyticLine, self).write(vals)
|
|
|
|
|
|
|
|
def _timesheet_preprocess(self, vals):
|
|
|
|
""" Deduce other field values from the one given.
|
|
|
|
Overrride this to compute on the fly some field that can not be computed fields.
|
|
|
|
:param values: dict values for `create`or `write`.
|
|
|
|
"""
|
|
|
|
# project implies analytic account
|
|
|
|
if vals.get('project_id') and not vals.get('account_id'):
|
|
|
|
project = self.env['project.project'].browse(vals.get('project_id'))
|
|
|
|
vals['account_id'] = project.analytic_account_id.id
|
|
|
|
# employee implies user
|
|
|
|
if vals.get('employee_id') and not vals.get('user_id'):
|
|
|
|
employee = self.env['hr.employee'].browse(vals['employee_id'])
|
|
|
|
vals['user_id'] = employee.user_id.id
|
|
|
|
# compute employee only for timesheet lines, makes no sense for other lines
|
|
|
|
if not vals.get('employee_id') and vals.get('project_id'):
|
|
|
|
if vals.get('user_id'):
|
|
|
|
ts_user_id = vals['user_id']
|
|
|
|
else:
|
|
|
|
ts_user_id = self._default_user()
|
|
|
|
vals['employee_id'] = self.env['hr.employee'].search([('user_id', '=', ts_user_id)], limit=1).id
|
|
|
|
return vals
|