diff --git a/addons/hr_contract/__openerp__.py b/addons/hr_contract/__openerp__.py index b4361b7c234..2750480674b 100644 --- a/addons/hr_contract/__openerp__.py +++ b/addons/hr_contract/__openerp__.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. - { 'name': 'Employee Contracts', 'version': '1.0', @@ -21,9 +20,9 @@ You can assign several contracts per employee. 'depends': ['base_action_rule', 'hr'], 'data': [ 'security/ir.model.access.csv', - 'views/hr_contract_views.xml', 'data/hr_contract_data.xml', 'views/base_action_rule_views.xml', + 'views/hr_contract_views.xml', ], 'demo': [], 'test': ['test/test_hr_contract.yml'], diff --git a/addons/hr_contract/data/hr_contract_data.xml b/addons/hr_contract/data/hr_contract_data.xml index 9bf558d5148..edaa93b16c0 100644 --- a/addons/hr_contract/data/hr_contract_data.xml +++ b/addons/hr_contract/data/hr_contract_data.xml @@ -1,5 +1,5 @@ - + @@ -109,4 +109,4 @@ - + diff --git a/addons/hr_contract/models/base_action_rule.py b/addons/hr_contract/models/base_action_rule.py index 462e92e8daf..8998c9d2288 100644 --- a/addons/hr_contract/models/base_action_rule.py +++ b/addons/hr_contract/models/base_action_rule.py @@ -1,36 +1,27 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from openerp.addons.base_action_rule.base_action_rule import get_datetime -from openerp.osv import fields, osv +from odoo import api, fields, models +from odoo.fields import Datetime -class base_action_rule(osv.Model): +class BaseActionRule(models.Model): """ Add resource and calendar for time-based conditions """ - _name = 'base.action.rule' - _inherit = ['base.action.rule'] - _columns = { - 'trg_date_resource_field_id': fields.many2one( - 'ir.model.fields', 'Use employee work schedule', - help='Use the user\'s working schedule.', - ), - } + _inherit = 'base.action.rule' - def _check_delay(self, cr, uid, action, record, record_dt, context=None): + trg_date_resource_field_id = fields.Many2one('ir.model.fields', string='Use employee work schedule', help='Use the user\'s working schedule.') + + @api.model + def _check_delay(self, action, record, record_dt): """ Override the check of delay to try to use a user-related calendar. - If no calendar is found, fallback on the default behavior. """ + If no calendar is found, fallback on the default behavior. + """ if action.trg_date_calendar_id and action.trg_date_range_type == 'day' and action.trg_date_resource_field_id: user = record[action.trg_date_resource_field_id.name] - if user.employee_ids and user.employee_ids[0].contract_id \ - and user.employee_ids[0].contract_id.working_hours: + if user.employee_ids and user.employee_ids[0].contract_id and user.employee_ids[0].contract_id.working_hours: calendar = user.employee_ids[0].contract_id.working_hours - start_dt = get_datetime(record_dt) + start_dt = Datetime.from_string(record_dt) resource_id = user.employee_ids[0].resource_id.id - action_dt = self.pool['resource.calendar'].schedule_days_get_date( - cr, uid, calendar.id, action.trg_date_range, - day_date=start_dt, compute_leaves=True, resource_id=resource_id, - context=context - ) - return action_dt - return super(base_action_rule, self)._check_delay(cr, uid, action, record, record_dt, context=context) + return calendar.schedule_days_get_date(action.trg_date_range, day_date=start_dt, compute_leaves=True, resource_id=resource_id) + return super(BaseActionRule, self)._check_delay(action, record, record_dt) diff --git a/addons/hr_contract/models/hr_contract.py b/addons/hr_contract/models/hr_contract.py index f649fb86636..e6a4a9b85c9 100644 --- a/addons/hr_contract/models/hr_contract.py +++ b/addons/hr_contract/models/hr_contract.py @@ -1,130 +1,101 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -import time +from odoo import api, fields, models, _ +from odoo.exceptions import ValidationError -from openerp import SUPERUSER_ID -from openerp.osv import fields, osv -class hr_employee(osv.osv): - _name = "hr.employee" - _description = "Employee" +class Employee(models.Model): + _inherit = "hr.employee" - def _get_latest_contract(self, cr, uid, ids, field_name, args, context=None): - res = {} - obj_contract = self.pool.get('hr.contract') - for emp in self.browse(cr, uid, ids, context=context): - contract_ids = obj_contract.search(cr, uid, [('employee_id', '=', emp.id)], order='date_start', context=context) - if contract_ids: - res[emp.id] = contract_ids[-1:][0] - else: - res[emp.id] = False - return res + manager = fields.Boolean(string='Is a Manager') + medic_exam = fields.Date(string='Medical Examination Date') + place_of_birth = fields.Char('Place of Birth') + children = fields.Integer(string='Number of Children') + vehicle = fields.Char(string='Company Vehicle') + vehicle_distance = fields.Integer(string='Home-Work Dist.', help="In kilometers") + contract_ids = fields.One2many('hr.contract', 'employee_id', string='Contracts') + contract_id = fields.Many2one('hr.contract', compute='_compute_contract_id', string='Current Contract', help='Latest contract of the employee') + contracts_count = fields.Integer(compute='_compute_contracts_count', string='Contracts') - def _contracts_count(self, cr, uid, ids, field_name, arg, context=None): - Contract = self.pool['hr.contract'] - return { - employee_id: Contract.search_count(cr, SUPERUSER_ID, [('employee_id', '=', employee_id)], context=context) - for employee_id in ids - } + def _compute_contract_id(self): + """ get the lastest contract """ + Contract = self.env['hr.contract'] + for employee in self: + employee.contract_id = Contract.search([('employee_id', '=', employee.id)], order='date_start', limit=1) - _columns = { - 'manager': fields.boolean('Is a Manager'), - 'medic_exam': fields.date('Medical Examination Date'), - 'place_of_birth': fields.char('Place of Birth'), - 'children': fields.integer('Number of Children'), - 'vehicle': fields.char('Company Vehicle'), - 'vehicle_distance': fields.integer('Home-Work Dist.', help="In kilometers"), - 'contract_ids': fields.one2many('hr.contract', 'employee_id', 'Contracts'), - 'contract_id': fields.function(_get_latest_contract, string='Current Contract', type='many2one', relation="hr.contract", help='Latest contract of the employee'), - 'contracts_count': fields.function(_contracts_count, type='integer', string='Contracts'), - } + def _compute_contracts_count(self): + # read_group as sudo, since contract count is displayed on form view + contract_data = self.env['hr.contract'].sudo().read_group([('employee_id', 'in', self.ids)], ['employee_id'], ['employee_id']) + result = dict((data['employee_id'][0], data['employee_id_count']) for data in contract_data) + for employee in self: + employee.contracts_count = result.get(employee.id, 0) -class hr_contract_type(osv.osv): +class ContractType(models.Model): + _name = 'hr.contract.type' _description = 'Contract Type' _order = 'sequence, id' - _columns = { - 'name': fields.char('Contract Type', required=True), - 'sequence': fields.integer('Sequence', help="Gives the sequence when displaying a list of Contract."), - } - defaults = { - 'sequence': 10 - } + name = fields.Char(string='Contract Type', required=True) + sequence = fields.Integer(help="Gives the sequence when displaying a list of Contract.", default=10) -class hr_contract(osv.osv): +class Contract(models.Model): + _name = 'hr.contract' _description = 'Contract' _inherit = ['mail.thread', 'ir.needaction_mixin'] - _columns = { - 'name': fields.char('Contract Reference', required=True), - 'employee_id': fields.many2one('hr.employee', "Employee", required=True), - 'department_id': fields.many2one('hr.department', string="Department"), - 'type_id': fields.many2one('hr.contract.type', "Contract Type", required=True), - 'job_id': fields.many2one('hr.job', 'Job Title'), - 'date_start': fields.date('Start Date', required=True), - 'date_end': fields.date('End Date'), - 'trial_date_start': fields.date('Trial Start Date'), - 'trial_date_end': fields.date('Trial End Date'), - 'working_hours': fields.many2one('resource.calendar', 'Working Schedule'), - 'wage': fields.float('Wage', digits=(16, 2), required=True, help="Basic Salary of the employee"), - 'advantages': fields.text('Advantages'), - 'notes': fields.text('Notes'), - 'permit_no': fields.char('Work Permit No', required=False, readonly=False), - 'visa_no': fields.char('Visa No', required=False, readonly=False), - 'visa_expire': fields.date('Visa Expire Date'), - 'state': fields.selection( - [('draft', 'New'), ('open', 'Running'), ('pending', 'To Renew'), ('close', 'Expired')], - string='Status', track_visibility='onchange', - help='Status of the contract'), - } + name = fields.Char('Contract Reference', required=True) + employee_id = fields.Many2one('hr.employee', string='Employee', required=True) + department_id = fields.Many2one('hr.department', string="Department") + type_id = fields.Many2one('hr.contract.type', string="Contract Type", required=True, default=lambda self: self.env['hr.contract.type'].search([], limit=1)) + job_id = fields.Many2one('hr.job', string='Job Title') + date_start = fields.Date('Start Date', required=True, default=fields.Date.today) + date_end = fields.Date('End Date') + trial_date_start = fields.Date('Trial Start Date') + trial_date_end = fields.Date('Trial End Date') + working_hours = fields.Many2one('resource.calendar', string='Working Schedule') + wage = fields.Float('Wage', digits=(16, 2), required=True, help="Basic Salary of the employee") + advantages = fields.Text('Advantages') + notes = fields.Text('Notes') + permit_no = fields.Char('Work Permit No') + visa_no = fields.Char('Visa No') + visa_expire = fields.Date('Visa Expire Date') + state = fields.Selection([ + ('draft', 'New'), + ('open', 'Running'), + ('pending', 'To Renew'), + ('close', 'Expired'), + ], string='Status', track_visibility='onchange', help='Status of the contract', default='draft') - def _get_type(self, cr, uid, context=None): - type_ids = self.pool.get('hr.contract.type').search(cr, uid, [], limit=1) - return type_ids and type_ids[0] or False + @api.onchange('employee_id') + def _onchange_employee_id(self): + if self.employee_id: + self.job_id = self.employee_id.job_id + self.department_id = self.employee_id.department_id - _defaults = { - 'date_start': lambda *a: time.strftime("%Y-%m-%d"), - 'type_id': _get_type, - 'state': 'draft', - } + @api.constrains('date_start', 'date_end') + def _check_dates(self): + if self.filtered(lambda c: c.date_end and c.date_start > c.date_end): + raise ValidationError(_('Contract start date must be less than contract end date.')) - def onchange_employee_id(self, cr, uid, ids, employee_id, context=None): - if not employee_id: - return {'value': {'job_id': False, 'department_id': False}} - emp_obj = self.pool.get('hr.employee').browse(cr, uid, employee_id, context=context) - job_id = dept_id = False - if emp_obj.job_id: - job_id = emp_obj.job_id.id - if emp_obj.department_id: - dept_id = emp_obj.department_id.id - return {'value': {'job_id': job_id, 'department_id': dept_id}} + @api.multi + def set_as_pending(self): + return self.write({'state': 'pending'}) - def _check_dates(self, cr, uid, ids, context=None): - for contract in self.read(cr, uid, ids, ['date_start', 'date_end'], context=context): - if contract['date_start'] and contract['date_end'] and contract['date_start'] > contract['date_end']: - return False - return True + @api.multi + def set_as_close(self): + return self.write({'state': 'close'}) - _constraints = [ - (_check_dates, 'Error! Contract start-date must be less than contract end-date.', ['date_start', 'date_end']) - ] - - def set_as_pending(self, cr, uid, ids, context=None): - return self.write(cr, uid, ids, {'state': 'pending'}, context=context) - - def set_as_close(self, cr, uid, ids, context=None): - return self.write(cr, uid, ids, {'state': 'close'}, context=context) - - def _track_subtype(self, cr, uid, ids, init_values, context=None): - record = self.browse(cr, uid, ids[0], context=context) - if 'state' in init_values and record.state == 'pending': + @api.multi + def _track_subtype(self, init_values): + self.ensure_one() + if 'state' in init_values and self.state == 'pending': return 'hr_contract.mt_contract_pending' - elif 'state' in init_values and record.state == 'close': + elif 'state' in init_values and self.state == 'close': return 'hr_contract.mt_contract_close' - return super(hr_contract, self)._track_subtype(cr, uid, ids, init_values, context=context) + return super(Contract, self)._track_subtype(init_values) diff --git a/addons/hr_contract/views/base_action_rule_views.xml b/addons/hr_contract/views/base_action_rule_views.xml index c37628b8c37..6f9a2f3414e 100644 --- a/addons/hr_contract/views/base_action_rule_views.xml +++ b/addons/hr_contract/views/base_action_rule_views.xml @@ -1,7 +1,5 @@ - - - + base.action.rule.form base.action.rule @@ -14,6 +12,4 @@ - - - + diff --git a/addons/hr_contract/views/hr_contract_views.xml b/addons/hr_contract/views/hr_contract_views.xml index 541f4a05854..20ab4adf15b 100644 --- a/addons/hr_contract/views/hr_contract_views.xml +++ b/addons/hr_contract/views/hr_contract_views.xml @@ -1,33 +1,29 @@ - - - - - + + Contracts hr.contract form - Contracts tree,form {'search_default_employee_id': [active_id], 'default_employee_id': active_id} - + hr.hr.employee.view.form2 hr.employee -
- -
+ @@ -38,21 +34,21 @@ - + - - + + - - + + - +
- hr.contract.view.search + hr.contract.search hr.contract @@ -67,7 +63,7 @@ - + @@ -78,7 +74,7 @@ - hr.contract.view.form + hr.contract.form hr.contract
@@ -94,7 +90,7 @@ - + @@ -142,7 +138,7 @@ - hr.contract.view.tree + hr.contract.tree hr.contract @@ -167,10 +163,15 @@ [] {'search_default_current':1} + +

+ Click here to create new contracts. +

+
- hr.contract.type.view.form + hr.contract.type.form hr.contract.type @@ -181,23 +182,24 @@ - - hr.contract.type.view.tree + + hr.contract.type.tree hr.contract.type + - hr.contract.type.view.search + hr.contract.type.search hr.contract.type - + @@ -210,8 +212,19 @@ - - + - - + + +