mirror of
https://github.com/bringout/oca-ocb-hr.git
synced 2026-04-26 08:52:06 +02:00
Initial commit: Hr packages
This commit is contained in:
commit
62531cd146
2820 changed files with 1432848 additions and 0 deletions
|
|
@ -0,0 +1,10 @@
|
|||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from . import hr_employee
|
||||
from . import hr_employee_public
|
||||
from . import hr_contract
|
||||
from . import res_users
|
||||
from . import resource
|
||||
from . import resource_calendar_leaves
|
||||
from . import resource_resource
|
||||
from . import hr_payroll_structure_type
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -0,0 +1,323 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
import threading
|
||||
|
||||
from datetime import date
|
||||
from dateutil.relativedelta import relativedelta
|
||||
|
||||
from odoo import api, fields, models, _
|
||||
from odoo.exceptions import ValidationError
|
||||
|
||||
from odoo.osv import expression
|
||||
|
||||
import logging
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Contract(models.Model):
|
||||
_name = 'hr.contract'
|
||||
_description = 'Contract'
|
||||
_inherit = ['mail.thread', 'mail.activity.mixin']
|
||||
_mail_post_access = 'read'
|
||||
|
||||
name = fields.Char('Contract Reference', required=True)
|
||||
active = fields.Boolean(default=True)
|
||||
structure_type_id = fields.Many2one('hr.payroll.structure.type', string="Salary Structure Type")
|
||||
employee_id = fields.Many2one('hr.employee', string='Employee', tracking=True, domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]")
|
||||
department_id = fields.Many2one('hr.department', compute='_compute_employee_contract', store=True, readonly=False,
|
||||
domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]", string="Department")
|
||||
job_id = fields.Many2one('hr.job', compute='_compute_employee_contract', store=True, readonly=False,
|
||||
domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]", string='Job Position')
|
||||
date_start = fields.Date('Start Date', required=True, default=fields.Date.today, tracking=True, index=True)
|
||||
date_end = fields.Date('End Date', tracking=True,
|
||||
help="End date of the contract (if it's a fixed-term contract).")
|
||||
trial_date_end = fields.Date('End of Trial Period',
|
||||
help="End date of the trial period (if there is one).")
|
||||
resource_calendar_id = fields.Many2one(
|
||||
'resource.calendar', 'Working Schedule', compute='_compute_employee_contract', store=True, readonly=False,
|
||||
default=lambda self: self.env.company.resource_calendar_id.id, copy=False, index=True,
|
||||
domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]")
|
||||
wage = fields.Monetary('Wage', required=True, tracking=True, help="Employee's monthly gross wage.")
|
||||
contract_wage = fields.Monetary('Contract Wage', compute='_compute_contract_wage')
|
||||
notes = fields.Html('Notes')
|
||||
state = fields.Selection([
|
||||
('draft', 'New'),
|
||||
('open', 'Running'),
|
||||
('close', 'Expired'),
|
||||
('cancel', 'Cancelled')
|
||||
], string='Status', group_expand='_expand_states', copy=False,
|
||||
tracking=True, help='Status of the contract', default='draft')
|
||||
company_id = fields.Many2one('res.company', compute='_compute_employee_contract', store=True, readonly=False,
|
||||
default=lambda self: self.env.company, required=True)
|
||||
company_country_id = fields.Many2one('res.country', string="Company country", related='company_id.country_id', readonly=True)
|
||||
country_code = fields.Char(related='company_country_id.code', depends=['company_country_id'], readonly=True)
|
||||
contract_type_id = fields.Many2one('hr.contract.type', "Contract Type")
|
||||
|
||||
"""
|
||||
kanban_state:
|
||||
* draft + green = "Incoming" state (will be set as Open once the contract has started)
|
||||
* open + red = "Pending" state (will be set as Closed once the contract has ended)
|
||||
* red = Shows a warning on the employees kanban view
|
||||
"""
|
||||
kanban_state = fields.Selection([
|
||||
('normal', 'Grey'),
|
||||
('done', 'Green'),
|
||||
('blocked', 'Red')
|
||||
], string='Kanban State', default='normal', tracking=True, copy=False)
|
||||
currency_id = fields.Many2one(string="Currency", related='company_id.currency_id', readonly=True)
|
||||
permit_no = fields.Char('Work Permit No', related="employee_id.permit_no", readonly=False)
|
||||
visa_no = fields.Char('Visa No', related="employee_id.visa_no", readonly=False)
|
||||
visa_expire = fields.Date('Visa Expiration Date', related="employee_id.visa_expire", readonly=False)
|
||||
|
||||
def _get_hr_responsible_domain(self):
|
||||
return "[('share', '=', False), ('company_ids', 'in', company_id), ('groups_id', 'in', %s)]" % self.env.ref('hr.group_hr_user').id
|
||||
|
||||
hr_responsible_id = fields.Many2one('res.users', 'HR Responsible', tracking=True,
|
||||
help='Person responsible for validating the employee\'s contracts.', domain=_get_hr_responsible_domain)
|
||||
calendar_mismatch = fields.Boolean(compute='_compute_calendar_mismatch', compute_sudo=True)
|
||||
first_contract_date = fields.Date(related='employee_id.first_contract_date')
|
||||
|
||||
@api.depends('employee_id.resource_calendar_id', 'resource_calendar_id')
|
||||
def _compute_calendar_mismatch(self):
|
||||
for contract in self:
|
||||
contract.calendar_mismatch = contract.resource_calendar_id != contract.employee_id.resource_calendar_id
|
||||
|
||||
def _expand_states(self, states, domain, order):
|
||||
return [key for key, val in self._fields['state'].selection]
|
||||
|
||||
@api.depends('employee_id')
|
||||
def _compute_employee_contract(self):
|
||||
for contract in self.filtered('employee_id'):
|
||||
contract.job_id = contract.employee_id.job_id
|
||||
contract.department_id = contract.employee_id.department_id
|
||||
contract.resource_calendar_id = contract.employee_id.resource_calendar_id
|
||||
contract.company_id = contract.employee_id.company_id
|
||||
|
||||
@api.onchange('company_id')
|
||||
def _onchange_company_id(self):
|
||||
if self.company_id:
|
||||
structure_types = self.env['hr.payroll.structure.type'].search([
|
||||
'|',
|
||||
('country_id', '=', self.company_id.country_id.id),
|
||||
('country_id', '=', False)])
|
||||
if structure_types:
|
||||
self.structure_type_id = structure_types[0]
|
||||
elif self.structure_type_id not in structure_types:
|
||||
self.structure_type_id = False
|
||||
|
||||
@api.onchange('structure_type_id')
|
||||
def _onchange_structure_type_id(self):
|
||||
default_calendar = self.structure_type_id.default_resource_calendar_id
|
||||
if default_calendar and default_calendar.company_id == self.company_id:
|
||||
self.resource_calendar_id = self.structure_type_id.default_resource_calendar_id
|
||||
|
||||
@api.constrains('employee_id', 'state', 'kanban_state', 'date_start', 'date_end')
|
||||
def _check_current_contract(self):
|
||||
""" Two contracts in state [incoming | open | close] cannot overlap """
|
||||
for contract in self.filtered(lambda c: (c.state not in ['draft', 'cancel'] or c.state == 'draft' and c.kanban_state == 'done') and c.employee_id):
|
||||
domain = [
|
||||
('id', '!=', contract.id),
|
||||
('employee_id', '=', contract.employee_id.id),
|
||||
('company_id', '=', contract.company_id.id),
|
||||
'|',
|
||||
('state', 'in', ['open', 'close']),
|
||||
'&',
|
||||
('state', '=', 'draft'),
|
||||
('kanban_state', '=', 'done') # replaces incoming
|
||||
]
|
||||
|
||||
if not contract.date_end:
|
||||
start_domain = []
|
||||
end_domain = ['|', ('date_end', '>=', contract.date_start), ('date_end', '=', False)]
|
||||
else:
|
||||
start_domain = [('date_start', '<=', contract.date_end)]
|
||||
end_domain = ['|', ('date_end', '>', contract.date_start), ('date_end', '=', False)]
|
||||
|
||||
domain = expression.AND([domain, start_domain, end_domain])
|
||||
if self.search_count(domain):
|
||||
raise ValidationError(
|
||||
_(
|
||||
'An employee can only have one contract at the same time. (Excluding Draft and Cancelled contracts).\n\nEmployee: %(employee_name)s',
|
||||
employee_name=contract.employee_id.name
|
||||
)
|
||||
)
|
||||
|
||||
@api.constrains('date_start', 'date_end')
|
||||
def _check_dates(self):
|
||||
for contract in self:
|
||||
if contract.date_end and contract.date_start > contract.date_end:
|
||||
raise ValidationError(_(
|
||||
'Contract %(contract)s: start date (%(start)s) must be earlier than contract end date (%(end)s).',
|
||||
contract=contract.name, start=contract.date_start, end=contract.date_end,
|
||||
))
|
||||
|
||||
def _get_employee_vals_to_update(self):
|
||||
self.ensure_one()
|
||||
vals = {'contract_id': self.id}
|
||||
if self.job_id and self.job_id != self.employee_id.job_id:
|
||||
vals['job_id'] = self.job_id.id
|
||||
if self.department_id:
|
||||
vals['department_id'] = self.department_id.id
|
||||
return vals
|
||||
|
||||
@api.model
|
||||
def update_state(self):
|
||||
from_cron = 'from_cron' in self.env.context
|
||||
contracts = self.search([
|
||||
('state', '=', 'open'), ('kanban_state', '!=', 'blocked'),
|
||||
'|',
|
||||
'&',
|
||||
('date_end', '<=', fields.Date.to_string(date.today() + relativedelta(days=7))),
|
||||
('date_end', '>=', fields.Date.to_string(date.today() + relativedelta(days=1))),
|
||||
'&',
|
||||
('visa_expire', '<=', fields.Date.to_string(date.today() + relativedelta(days=60))),
|
||||
('visa_expire', '>=', fields.Date.to_string(date.today() + relativedelta(days=1))),
|
||||
])
|
||||
|
||||
for contract in contracts:
|
||||
contract.with_context(mail_activity_quick_update=True).activity_schedule(
|
||||
'mail.mail_activity_data_todo', contract.date_end,
|
||||
_("The contract of %s is about to expire.", contract.employee_id.name),
|
||||
user_id=contract.hr_responsible_id.id or self.env.uid)
|
||||
|
||||
if contracts:
|
||||
contracts._safe_write_for_cron({'kanban_state': 'blocked'}, from_cron)
|
||||
|
||||
contracts_to_close = self.search([
|
||||
('state', '=', 'open'),
|
||||
'|',
|
||||
('date_end', '<=', fields.Date.to_string(date.today())),
|
||||
('visa_expire', '<=', fields.Date.to_string(date.today())),
|
||||
])
|
||||
|
||||
if contracts_to_close:
|
||||
contracts_to_close._safe_write_for_cron({'state': 'close'}, from_cron)
|
||||
|
||||
contracts_to_open = self.search([('state', '=', 'draft'), ('kanban_state', '=', 'done'), ('date_start', '<=', fields.Date.to_string(date.today())),])
|
||||
|
||||
if contracts_to_open:
|
||||
contracts_to_open._safe_write_for_cron({'state': 'open'}, from_cron)
|
||||
|
||||
contract_ids = self.search([('date_end', '=', False), ('state', '=', 'close'), ('employee_id', '!=', False)])
|
||||
# Ensure all closed contract followed by a new contract have a end date.
|
||||
# If closed contract has no closed date, the work entries will be generated for an unlimited period.
|
||||
for contract in contract_ids:
|
||||
next_contract = self.search([
|
||||
('employee_id', '=', contract.employee_id.id),
|
||||
('state', 'not in', ['cancel', 'draft']),
|
||||
('date_start', '>', contract.date_start)
|
||||
], order="date_start asc", limit=1)
|
||||
if next_contract:
|
||||
contract._safe_write_for_cron({'date_end': next_contract.date_start - relativedelta(days=1)}, from_cron)
|
||||
continue
|
||||
next_contract = self.search([
|
||||
('employee_id', '=', contract.employee_id.id),
|
||||
('date_start', '>', contract.date_start)
|
||||
], order="date_start asc", limit=1)
|
||||
if next_contract:
|
||||
contract._safe_write_for_cron({'date_end': next_contract.date_start - relativedelta(days=1)}, from_cron)
|
||||
|
||||
return True
|
||||
|
||||
def _safe_write_for_cron(self, vals, from_cron=False):
|
||||
if from_cron:
|
||||
auto_commit = not getattr(threading.current_thread(), 'testing', False)
|
||||
for contract in self:
|
||||
try:
|
||||
with self.env.cr.savepoint():
|
||||
contract.write(vals)
|
||||
except ValidationError as e:
|
||||
_logger.warning(e)
|
||||
else:
|
||||
if auto_commit:
|
||||
self.env.cr.commit()
|
||||
else:
|
||||
self.write(vals)
|
||||
|
||||
def _assign_open_contract(self):
|
||||
for contract in self:
|
||||
vals = contract._get_employee_vals_to_update()
|
||||
contract.employee_id.sudo().write(vals)
|
||||
|
||||
@api.depends('wage')
|
||||
def _compute_contract_wage(self):
|
||||
for contract in self:
|
||||
contract.contract_wage = contract._get_contract_wage()
|
||||
|
||||
def _get_contract_wage(self):
|
||||
if not self:
|
||||
return 0
|
||||
self.ensure_one()
|
||||
return self[self._get_contract_wage_field()]
|
||||
|
||||
def _get_contract_wage_field(self):
|
||||
return 'wage'
|
||||
|
||||
def write(self, vals):
|
||||
old_state = {c.id: c.state for c in self}
|
||||
res = super(Contract, self).write(vals)
|
||||
new_state = {c.id: c.state for c in self}
|
||||
if vals.get('state') == 'open':
|
||||
self._assign_open_contract()
|
||||
today = fields.Date.today()
|
||||
for contract in self:
|
||||
if contract == contract.employee_id.sudo().contract_id \
|
||||
and old_state[contract.id] == 'open' \
|
||||
and new_state[contract.id] != 'open':
|
||||
running_contract = self.env['hr.contract'].search([
|
||||
('employee_id', '=', contract.employee_id.id),
|
||||
('company_id', '=', contract.company_id.id),
|
||||
('state', '=', 'open'),
|
||||
]).filtered(lambda c: c.date_start <= today and (not c.date_end or c.date_end >= today))
|
||||
if running_contract:
|
||||
contract.employee_id.sudo().contract_id = running_contract[0]
|
||||
if vals.get('state') == 'close':
|
||||
for contract in self.filtered(lambda c: not c.date_end):
|
||||
contract.date_end = max(date.today(), contract.date_start)
|
||||
date_end = vals.get('date_end')
|
||||
if self.env.context.get('close_contract', True) and date_end and fields.Date.from_string(date_end) < fields.Date.context_today(self):
|
||||
for contract in self.filtered(lambda c: c.state == 'open'):
|
||||
contract.state = 'close'
|
||||
|
||||
calendar = vals.get('resource_calendar_id')
|
||||
if calendar:
|
||||
self.filtered(
|
||||
lambda c: c.state == 'open' or (c.state == 'draft' and c.kanban_state == 'done' and c.employee_id.contracts_count == 1)
|
||||
).employee_id.resource_calendar_id = calendar
|
||||
|
||||
if 'state' in vals and 'kanban_state' not in vals:
|
||||
self.write({'kanban_state': 'normal'})
|
||||
|
||||
return res
|
||||
|
||||
@api.model_create_multi
|
||||
def create(self, vals_list):
|
||||
contracts = super().create(vals_list)
|
||||
contracts.filtered(lambda c: c.state == 'open')._assign_open_contract()
|
||||
open_contracts = contracts.filtered(
|
||||
lambda c: c.state == 'open' or (c.state == 'draft' and c.kanban_state == 'done' and c.employee_id.contracts_count == 1)
|
||||
)
|
||||
# sync contract calendar -> calendar employee
|
||||
for contract in open_contracts.filtered(lambda c: c.employee_id and c.resource_calendar_id):
|
||||
contract.employee_id.resource_calendar_id = contract.resource_calendar_id
|
||||
return contracts
|
||||
|
||||
def _track_subtype(self, init_values):
|
||||
self.ensure_one()
|
||||
if 'state' in init_values and self.state == 'open' and 'kanban_state' in init_values and self.kanban_state == 'blocked':
|
||||
return self.env.ref('hr_contract.mt_contract_pending')
|
||||
elif 'state' in init_values and self.state == 'close':
|
||||
return self.env.ref('hr_contract.mt_contract_close')
|
||||
return super(Contract, self)._track_subtype(init_values)
|
||||
|
||||
def action_open_contract_form(self):
|
||||
self.ensure_one()
|
||||
action = self.env['ir.actions.actions']._for_xml_id('hr_contract.action_hr_contract')
|
||||
action.update({
|
||||
'view_mode': 'form',
|
||||
'view_id': self.env.ref('hr_contract.hr_contract_view_form').id,
|
||||
'views': [(self.env.ref('hr_contract.hr_contract_view_form').id, 'form')],
|
||||
'res_id': self.id,
|
||||
})
|
||||
return action
|
||||
|
|
@ -0,0 +1,138 @@
|
|||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from datetime import date, datetime, time
|
||||
from pytz import timezone
|
||||
|
||||
from odoo import _, api, fields, models
|
||||
from odoo.osv import expression
|
||||
from odoo.addons.resource.models.resource import Intervals
|
||||
from odoo.exceptions import UserError
|
||||
|
||||
|
||||
class Employee(models.Model):
|
||||
_inherit = "hr.employee"
|
||||
|
||||
vehicle = fields.Char(string='Company Vehicle', groups="hr.group_hr_user")
|
||||
contract_ids = fields.One2many('hr.contract', 'employee_id', string='Employee Contracts')
|
||||
contract_id = fields.Many2one(
|
||||
'hr.contract', string='Current Contract', groups="hr.group_hr_user",
|
||||
domain="[('company_id', '=', company_id), ('employee_id', '=', id)]", help='Current contract of the employee', copy=False)
|
||||
calendar_mismatch = fields.Boolean(related='contract_id.calendar_mismatch')
|
||||
contracts_count = fields.Integer(compute='_compute_contracts_count', string='Contract Count')
|
||||
contract_warning = fields.Boolean(string='Contract Warning', store=True, compute='_compute_contract_warning', groups="hr.group_hr_user")
|
||||
first_contract_date = fields.Date(compute='_compute_first_contract_date', groups="hr.group_hr_user", store=True)
|
||||
|
||||
def _get_first_contracts(self):
|
||||
self.ensure_one()
|
||||
contracts = self.sudo().contract_ids.filtered(lambda c: c.state != 'cancel')
|
||||
if self.env.context.get('before_date'):
|
||||
contracts = contracts.filtered(lambda c: c.date_start <= self.env.context['before_date'])
|
||||
return contracts
|
||||
|
||||
def _get_first_contract_date(self, no_gap=True):
|
||||
self.ensure_one()
|
||||
def remove_gap(contracts):
|
||||
# We do not consider a gap of more than 4 days to be a same occupation
|
||||
# contracts are considered to be ordered correctly
|
||||
if not contracts:
|
||||
return self.env['hr.contract']
|
||||
if len(contracts) == 1:
|
||||
return contracts
|
||||
current_contract = contracts[0]
|
||||
older_contracts = contracts[1:]
|
||||
current_date = current_contract.date_start
|
||||
for i, other_contract in enumerate(older_contracts):
|
||||
# Consider current_contract.date_end being false as an error and cut the loop
|
||||
gap = (current_date - (other_contract.date_end or date(2100, 1, 1))).days
|
||||
current_date = other_contract.date_start
|
||||
if gap >= 4:
|
||||
return older_contracts[0:i] + current_contract
|
||||
return older_contracts + current_contract
|
||||
|
||||
contracts = self._get_first_contracts().sorted('date_start', reverse=True)
|
||||
if no_gap:
|
||||
contracts = remove_gap(contracts)
|
||||
return min(contracts.mapped('date_start')) if contracts else False
|
||||
|
||||
@api.depends('contract_ids.state', 'contract_ids.date_start', 'contract_ids.active')
|
||||
def _compute_first_contract_date(self):
|
||||
for employee in self:
|
||||
employee.first_contract_date = employee._get_first_contract_date()
|
||||
|
||||
@api.depends('contract_id', 'contract_id.state', 'contract_id.kanban_state')
|
||||
def _compute_contract_warning(self):
|
||||
for employee in self:
|
||||
employee.contract_warning = not employee.contract_id or employee.contract_id.kanban_state == 'blocked' or employee.contract_id.state != 'open'
|
||||
|
||||
def _compute_contracts_count(self):
|
||||
# read_group as sudo, since contract count is displayed on form view
|
||||
contract_histories = self.env['hr.contract.history'].sudo().search([('employee_id', 'in', self.ids)])
|
||||
for employee in self:
|
||||
contract_history = contract_histories.filtered(lambda ch: ch.employee_id == employee)
|
||||
employee.contracts_count = contract_history.contract_count
|
||||
|
||||
def _get_contracts(self, date_from, date_to, states=['open'], kanban_state=False):
|
||||
"""
|
||||
Returns the contracts of the employee between date_from and date_to
|
||||
"""
|
||||
state_domain = [('state', 'in', states)]
|
||||
if kanban_state:
|
||||
state_domain = expression.AND([state_domain, [('kanban_state', 'in', kanban_state)]])
|
||||
|
||||
return self.env['hr.contract'].search(
|
||||
expression.AND([[('employee_id', 'in', self.ids)],
|
||||
state_domain,
|
||||
[('date_start', '<=', date_to),
|
||||
'|',
|
||||
('date_end', '=', False),
|
||||
('date_end', '>=', date_from)]]))
|
||||
|
||||
def _get_incoming_contracts(self, date_from, date_to):
|
||||
return self._get_contracts(date_from, date_to, states=['draft'], kanban_state=['done'])
|
||||
|
||||
@api.model
|
||||
def _get_all_contracts(self, date_from, date_to, states=['open']):
|
||||
"""
|
||||
Returns the contracts of all employees between date_from and date_to
|
||||
"""
|
||||
return self.search(['|', ('active', '=', True), ('active', '=', False)])._get_contracts(date_from, date_to, states=states)
|
||||
|
||||
def _get_expected_attendances(self, date_from, date_to, domain=None):
|
||||
self.ensure_one()
|
||||
valid_contracts = self.sudo()._get_contracts(date_from, date_to, states=['open', 'close'])
|
||||
if not valid_contracts:
|
||||
return super()._get_expected_attendances(date_from, date_to, domain)
|
||||
employee_tz = timezone(self.tz) if self.tz else None
|
||||
duration_data = Intervals()
|
||||
for contract in valid_contracts:
|
||||
contract_start = datetime.combine(contract.date_start, time.min, employee_tz)
|
||||
contract_end = datetime.combine(contract.date_end or date.max, time.max, employee_tz)
|
||||
calendar = contract.resource_calendar_id or contract.company_id.resource_calendar_id
|
||||
contract_intervals = calendar._work_intervals_batch(
|
||||
max(date_from, contract_start),
|
||||
min(date_to, contract_end),
|
||||
tz=employee_tz,
|
||||
domain=domain,
|
||||
compute_leaves=True,
|
||||
resources=self.resource_id)[self.resource_id.id]
|
||||
duration_data = duration_data | contract_intervals
|
||||
return duration_data
|
||||
|
||||
def write(self, vals):
|
||||
res = super(Employee, self).write(vals)
|
||||
if vals.get('contract_id'):
|
||||
for employee in self:
|
||||
employee.resource_calendar_id.transfer_leaves_to(employee.contract_id.resource_calendar_id, employee.resource_id)
|
||||
employee.resource_calendar_id = employee.contract_id.resource_calendar_id
|
||||
return res
|
||||
|
||||
@api.ondelete(at_uninstall=False)
|
||||
def _unlink_except_open_contract(self):
|
||||
if any(contract.state == 'open' for contract in self.contract_ids):
|
||||
raise UserError(_('You cannot delete an employee with a running contract.'))
|
||||
|
||||
def action_open_contract_history(self):
|
||||
self.ensure_one()
|
||||
action = self.env["ir.actions.actions"]._for_xml_id('hr_contract.hr_contract_history_view_form_action')
|
||||
action['res_id'] = self.id
|
||||
return action
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo import fields, models
|
||||
|
||||
|
||||
class HrEmployeePublic(models.Model):
|
||||
_inherit = "hr.employee.public"
|
||||
|
||||
first_contract_date = fields.Date(readonly=True, groups="base.group_user")
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo import fields, models
|
||||
|
||||
|
||||
class HrPayrollStructureType(models.Model):
|
||||
_name = 'hr.payroll.structure.type'
|
||||
_description = 'Salary Structure Type'
|
||||
|
||||
name = fields.Char('Salary Structure Type')
|
||||
default_resource_calendar_id = fields.Many2one(
|
||||
'resource.calendar', 'Default Working Hours',
|
||||
default=lambda self: self.env.company.resource_calendar_id)
|
||||
country_id = fields.Many2one('res.country', string='Country', default=lambda self: self.env.company.country_id)
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo import models, fields, api, _
|
||||
|
||||
|
||||
class User(models.Model):
|
||||
_inherit = ['res.users']
|
||||
|
||||
vehicle = fields.Char(related="employee_id.vehicle")
|
||||
bank_account_id = fields.Many2one(related="employee_id.bank_account_id")
|
||||
|
||||
@property
|
||||
def SELF_READABLE_FIELDS(self):
|
||||
return super().SELF_READABLE_FIELDS + ['vehicle', 'bank_account_id']
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
# -*- coding:utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
from datetime import datetime
|
||||
|
||||
from odoo import fields, models
|
||||
from odoo.osv.expression import AND
|
||||
|
||||
|
||||
class ResourceCalendar(models.Model):
|
||||
_inherit = 'resource.calendar'
|
||||
|
||||
contracts_count = fields.Integer("# Contracts using it", compute='_compute_contracts_count', groups="hr_contract.group_hr_contract_manager")
|
||||
|
||||
def transfer_leaves_to(self, other_calendar, resources=None, from_date=None):
|
||||
"""
|
||||
Transfer some resource.calendar.leaves from 'self' to another calendar 'other_calendar'.
|
||||
Transfered leaves linked to `resources` (or all if `resources` is None) and starting
|
||||
after 'from_date' (or today if None).
|
||||
"""
|
||||
from_date = from_date or fields.Datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
domain = [
|
||||
('calendar_id', 'in', self.ids),
|
||||
('date_from', '>=', from_date),
|
||||
]
|
||||
domain = AND([domain, [('resource_id', 'in', resources.ids)]]) if resources else domain
|
||||
|
||||
self.env['resource.calendar.leaves'].search(domain).write({
|
||||
'calendar_id': other_calendar.id,
|
||||
})
|
||||
|
||||
def _compute_contracts_count(self):
|
||||
count_data = self.env['hr.contract']._read_group(
|
||||
[('resource_calendar_id', 'in', self.ids), ('employee_id', '!=', False)],
|
||||
['resource_calendar_id'],
|
||||
['resource_calendar_id'])
|
||||
mapped_counts = {cd['resource_calendar_id'][0]: cd['resource_calendar_id_count'] for cd in count_data}
|
||||
for calendar in self:
|
||||
calendar.contracts_count = mapped_counts.get(calendar.id, 0)
|
||||
|
||||
def action_open_contracts(self):
|
||||
self.ensure_one()
|
||||
action = self.env["ir.actions.actions"]._for_xml_id("hr_contract.action_hr_contract")
|
||||
action.update({'domain': [('resource_calendar_id', '=', self.id), ('employee_id', '!=', False)]})
|
||||
return action
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from collections import defaultdict
|
||||
from datetime import datetime
|
||||
from pytz import timezone, utc
|
||||
|
||||
from odoo import models
|
||||
|
||||
|
||||
class ResourceCalendarLeaves(models.Model):
|
||||
_inherit = 'resource.calendar.leaves'
|
||||
|
||||
def _compute_calendar_id(self):
|
||||
def date2datetime(date, tz):
|
||||
dt = datetime.fromordinal(date.toordinal())
|
||||
return tz.localize(dt).astimezone(utc).replace(tzinfo=None)
|
||||
|
||||
Resource = self.env['resource.resource']
|
||||
CalendarLeaves = self.env['resource.calendar.leaves']
|
||||
leaves_by_resource = defaultdict(lambda: CalendarLeaves, {Resource: CalendarLeaves})
|
||||
for leave in self:
|
||||
leaves_by_resource[leave.resource_id] += leave
|
||||
# pass leaves without resource_id to super
|
||||
remaining = leaves_by_resource.pop(Resource)
|
||||
|
||||
for resource, leaves in leaves_by_resource.items():
|
||||
contract = resource.employee_id.contract_id
|
||||
if not contract:
|
||||
remaining += leaves
|
||||
continue
|
||||
tz = timezone(contract.resource_calendar_id.tz or 'UTC')
|
||||
start_dt = date2datetime(contract.date_start, tz)
|
||||
end_dt = date2datetime(contract.date_end, tz) if contract.date_end else datetime.max
|
||||
# only modify leaves that fall under the active contract
|
||||
leaves.filtered(
|
||||
lambda leave: start_dt <= leave.date_from < end_dt
|
||||
).calendar_id = contract.resource_calendar_id
|
||||
|
||||
super(ResourceCalendarLeaves, remaining)._compute_calendar_id()
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
from collections import defaultdict
|
||||
from datetime import datetime
|
||||
from pytz import timezone
|
||||
|
||||
from odoo import models
|
||||
from odoo.addons.resource.models.resource import Intervals
|
||||
|
||||
class ResourceResource(models.Model):
|
||||
_inherit = 'resource.resource'
|
||||
|
||||
def _get_calendars_validity_within_period(self, start, end, default_company=None):
|
||||
assert start.tzinfo and end.tzinfo
|
||||
if not self:
|
||||
return super()._get_calendars_validity_within_period(start, end, default_company=default_company)
|
||||
calendars_within_period_per_resource = defaultdict(lambda: defaultdict(Intervals)) # keys are [resource id:integer][calendar:self.env['resource.calendar']]
|
||||
# Employees that have ever had an active contract
|
||||
employee_ids_with_active_contracts = {
|
||||
contract['employee_id'][0] for contract in
|
||||
self.env['hr.contract']._read_group(
|
||||
domain=[
|
||||
('employee_id', 'in', self.employee_id.ids),
|
||||
'|', ('state', '=', 'open'),
|
||||
'|', ('state', '=', 'close'),
|
||||
'&', ('state', '=', 'draft'), ('kanban_state', '=', 'done')
|
||||
],
|
||||
fields=['employee_id'], groupby=['employee_id']
|
||||
)
|
||||
}
|
||||
resource_without_contract = self.filtered(
|
||||
lambda r: not r.employee_id\
|
||||
or not r.employee_id.id in employee_ids_with_active_contracts\
|
||||
or r.employee_id.employee_type not in ['employee', 'student']
|
||||
)
|
||||
if resource_without_contract:
|
||||
calendars_within_period_per_resource.update(
|
||||
super(ResourceResource, resource_without_contract)._get_calendars_validity_within_period(start, end, default_company=default_company)
|
||||
)
|
||||
resource_with_contract = self - resource_without_contract
|
||||
if not resource_with_contract:
|
||||
return calendars_within_period_per_resource
|
||||
timezones = {resource.tz for resource in resource_with_contract}
|
||||
date_start = min(start.astimezone(timezone(tz)).date() for tz in timezones)
|
||||
date_end = max(end.astimezone(timezone(tz)).date() for tz in timezones)
|
||||
contracts = resource_with_contract.employee_id._get_contracts(
|
||||
date_start, date_end, states=['open', 'draft', 'close']
|
||||
).filtered(lambda c: c.state in ['open', 'close'] or c.kanban_state == 'done')
|
||||
for contract in contracts:
|
||||
tz = timezone(contract.employee_id.tz)
|
||||
calendars_within_period_per_resource[contract.employee_id.resource_id.id][contract.resource_calendar_id] |= Intervals([(
|
||||
tz.localize(datetime.combine(contract.date_start, datetime.min.time())) if contract.date_start > start.astimezone(tz).date() else start,
|
||||
tz.localize(datetime.combine(contract.date_end, datetime.max.time())) if contract.date_end and contract.date_end < end.astimezone(tz).date() else end,
|
||||
self.env['resource.calendar.attendance']
|
||||
)])
|
||||
return calendars_within_period_per_resource
|
||||
Loading…
Add table
Add a link
Reference in a new issue