[ADD]hr_employee_stats_sheet
This commit is contained in:
55
hr_employee_stats_sheet/README.rst
Normal file
55
hr_employee_stats_sheet/README.rst
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
=======================
|
||||||
|
hr_employee_stats_sheet
|
||||||
|
=======================
|
||||||
|
|
||||||
|
Summary
|
||||||
|
=======
|
||||||
|
|
||||||
|
Add global sheet for employee stats
|
||||||
|
|
||||||
|
Description
|
||||||
|
===========
|
||||||
|
|
||||||
|
This module add a global sheet stats in timesheet sheet
|
||||||
|
It compare planified working hours (from calendar ressouce) and worked hours form timesheets
|
||||||
|
|
||||||
|
Installation
|
||||||
|
============
|
||||||
|
|
||||||
|
Use Odoo normal module installation procedure to install
|
||||||
|
``hr_employee_stats_sheet``.
|
||||||
|
|
||||||
|
Known issues / Roadmap
|
||||||
|
======================
|
||||||
|
|
||||||
|
None yet.
|
||||||
|
|
||||||
|
Bug Tracker
|
||||||
|
===========
|
||||||
|
|
||||||
|
Bugs are tracked on `our issues website <https://github.com/elabore-coop/allow_negative_leave_and_allocation/issues>`_. In case of
|
||||||
|
trouble, please check there if your issue has already been
|
||||||
|
reported. If you spotted it first, help us smashing it by providing a
|
||||||
|
detailed and welcomed feedback.
|
||||||
|
|
||||||
|
Credits
|
||||||
|
=======
|
||||||
|
|
||||||
|
Contributors
|
||||||
|
------------
|
||||||
|
|
||||||
|
* `Alusage : Nicolas JEUDY`
|
||||||
|
* `Elabore <mailto:laetitia.dacosta@elabore.coop>`
|
||||||
|
|
||||||
|
Funders
|
||||||
|
-------
|
||||||
|
|
||||||
|
The development of this module has been financially supported by:
|
||||||
|
* Alusage (https://github.com/Alusage)
|
||||||
|
* Elabore (https://elabore.coop)
|
||||||
|
|
||||||
|
|
||||||
|
Maintainer
|
||||||
|
----------
|
||||||
|
|
||||||
|
This module is maintained by Elabore.
|
1
hr_employee_stats_sheet/__init__.py
Executable file
1
hr_employee_stats_sheet/__init__.py
Executable file
@@ -0,0 +1 @@
|
|||||||
|
from . import models
|
27
hr_employee_stats_sheet/__manifest__.py
Executable file
27
hr_employee_stats_sheet/__manifest__.py
Executable file
@@ -0,0 +1,27 @@
|
|||||||
|
{
|
||||||
|
"name": "hr_employee_stats_sheet",
|
||||||
|
"version": "16.0.1.0.1",
|
||||||
|
"description": "Add global sheet for employee stats",
|
||||||
|
"summary": "Add global sheet for employee stats",
|
||||||
|
"author": "Nicolas JEUDY",
|
||||||
|
"website": "https://github.com/Alusage/odoo-hr-addons",
|
||||||
|
"license": "LGPL-3",
|
||||||
|
"category": "Human Resources",
|
||||||
|
"depends": [
|
||||||
|
"allow_negative_leave_and_allocation",
|
||||||
|
"base",
|
||||||
|
"hr",
|
||||||
|
"hr_holidays",
|
||||||
|
"hr_timesheet",
|
||||||
|
"hr_timesheet_sheet",
|
||||||
|
"resource",
|
||||||
|
],
|
||||||
|
"data": [
|
||||||
|
"security/ir.model.access.csv",
|
||||||
|
"views/hr_employee_stats.xml",
|
||||||
|
"views/hr_timesheet_sheet.xml",
|
||||||
|
"views/res_config_settings_views.xml",
|
||||||
|
],
|
||||||
|
"installable": True,
|
||||||
|
"application": False,
|
||||||
|
}
|
5
hr_employee_stats_sheet/models/__init__.py
Normal file
5
hr_employee_stats_sheet/models/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
from . import hr_employee_stats
|
||||||
|
from . import hr_timesheet_sheet
|
||||||
|
from . import res_config
|
||||||
|
from . import res_company
|
||||||
|
from . import hr_leave_allocation
|
209
hr_employee_stats_sheet/models/hr_employee_stats.py
Normal file
209
hr_employee_stats_sheet/models/hr_employee_stats.py
Normal file
@@ -0,0 +1,209 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
|
from odoo import api, fields, models, _
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
|
_logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class HrEmployeeStats(models.Model):
|
||||||
|
_name = "hr.employee.stats"
|
||||||
|
_description = "Employee Stats"
|
||||||
|
_order = "date desc"
|
||||||
|
_inherit = ["mail.thread", "mail.activity.mixin"]
|
||||||
|
|
||||||
|
name = fields.Char("Name", compute="_compute_name", store=True)
|
||||||
|
dayofweek = fields.Integer("Day of Week", compute="_compute_dayofweek")
|
||||||
|
is_public_holiday = fields.Boolean("Public Holiday", compute="_compute_dayofweek")
|
||||||
|
employee_id = fields.Many2one("hr.employee", "Employee", required=True)
|
||||||
|
department_id = fields.Many2one("hr.department", "Department")
|
||||||
|
timesheet_line_ids = fields.One2many(
|
||||||
|
"account.analytic.line",
|
||||||
|
"employee_id",
|
||||||
|
"Timesheet lines",
|
||||||
|
compute="_compute_timesheet_line_ids",
|
||||||
|
)
|
||||||
|
date = fields.Date("Date", required=True)
|
||||||
|
company_id = fields.Many2one(
|
||||||
|
"res.company",
|
||||||
|
"Company",
|
||||||
|
default=lambda self: self.env.company,
|
||||||
|
required=True,
|
||||||
|
)
|
||||||
|
sheet_id = fields.Many2one("hr_timesheet.sheet", "Timesheet")
|
||||||
|
total_hours = fields.Float("Total Hours", compute="_compute_hours")
|
||||||
|
total_planned_hours = fields.Float("Total Planning Hours", compute="_compute_hours")
|
||||||
|
total_leave_hours = fields.Float("Total Leave Hours", compute="_compute_hours")
|
||||||
|
total_recovery_hours = fields.Float(
|
||||||
|
"Total Recovery Hours", compute="_compute_hours"
|
||||||
|
)
|
||||||
|
gap_hours = fields.Float("Gap Hours", compute="_compute_hours")
|
||||||
|
|
||||||
|
def _get_holiday_status_id(self):
|
||||||
|
recovery_type_id = self.env.company.recovery_type_id
|
||||||
|
if recovery_type_id:
|
||||||
|
return recovery_type_id.id
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def _compute_timesheet_line_ids(self):
|
||||||
|
for stat in self:
|
||||||
|
stat.timesheet_line_ids = self.env["account.analytic.line"].search(
|
||||||
|
[
|
||||||
|
("employee_id", "=", stat.employee_id.id),
|
||||||
|
("date", "=", stat.date),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
def _get_intersects(
|
||||||
|
self, datetime1_start, datetime1_end, datetime2_start, datetime2_end
|
||||||
|
):
|
||||||
|
latest_start = max(datetime1_start, datetime2_start)
|
||||||
|
earliest_end = min(datetime1_end, datetime2_end)
|
||||||
|
delta = (earliest_end - latest_start).total_seconds() / 3600
|
||||||
|
return max(0, delta)
|
||||||
|
|
||||||
|
def get_total_hours_domain(self):
|
||||||
|
return [
|
||||||
|
("employee_id", "=", self.employee_id.id),
|
||||||
|
("date", "=", self.date),
|
||||||
|
]
|
||||||
|
|
||||||
|
@api.depends("timesheet_line_ids")
|
||||||
|
def _get_total_hours(self):
|
||||||
|
timesheet_line = self.env["account.analytic.line"]
|
||||||
|
for stat in self:
|
||||||
|
if stat.date and stat.employee_id:
|
||||||
|
timesheet_line_ids = timesheet_line.search(
|
||||||
|
stat.get_total_hours_domain()
|
||||||
|
)
|
||||||
|
total_hours = sum(timesheet_line_ids.mapped("unit_amount"))
|
||||||
|
else:
|
||||||
|
total_hours = 0
|
||||||
|
return total_hours
|
||||||
|
|
||||||
|
def _get_total_planned_hours(self):
|
||||||
|
for stat in self:
|
||||||
|
if stat.employee_id and stat.date and not stat.is_public_holiday:
|
||||||
|
dayofweek = int(stat.date.strftime("%u")) - 1
|
||||||
|
calendar_id = stat.employee_id.resource_calendar_id
|
||||||
|
week_number = stat.date.isocalendar()[1] % 2
|
||||||
|
if calendar_id.two_weeks_calendar:
|
||||||
|
hours = calendar_id.attendance_ids.search(
|
||||||
|
[
|
||||||
|
("dayofweek", "=", dayofweek),
|
||||||
|
("calendar_id", "=", calendar_id.id),
|
||||||
|
("week_type", "=", week_number),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
hours = calendar_id.attendance_ids.search(
|
||||||
|
[
|
||||||
|
("dayofweek", "=", dayofweek),
|
||||||
|
("calendar_id", "=", calendar_id.id),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
total_planned_hours = sum(
|
||||||
|
hours.mapped(lambda r: r.hour_to - r.hour_from)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
total_planned_hours = 0
|
||||||
|
return total_planned_hours
|
||||||
|
|
||||||
|
def _get_total_recovery_hours(self):
|
||||||
|
recovery = self.env["hr.leave"]
|
||||||
|
for stat in self:
|
||||||
|
if stat.date and stat.employee_id and stat._get_holiday_status_id():
|
||||||
|
recovery_ids = recovery.search(
|
||||||
|
[
|
||||||
|
("employee_id", "=", stat.employee_id.id),
|
||||||
|
("request_date_from", ">=", stat.date),
|
||||||
|
("request_date_from", "<=", stat.date),
|
||||||
|
("holiday_status_id", "=", stat._get_holiday_status_id()),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
total_recovery_hours = sum(
|
||||||
|
recovery_ids.mapped("number_of_hours_display")
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
total_recovery_hours = 0
|
||||||
|
return total_recovery_hours
|
||||||
|
|
||||||
|
def _get_total_leave_hours(self):
|
||||||
|
leave = self.env["hr.leave"]
|
||||||
|
for stat in self:
|
||||||
|
if stat.date and stat.employee_id:
|
||||||
|
leave_ids = leave.search(
|
||||||
|
[
|
||||||
|
("employee_id", "=", stat.employee_id.id),
|
||||||
|
("holiday_status_id", "!=", stat._get_holiday_status_id()),
|
||||||
|
("request_date_from", ">=", stat.date),
|
||||||
|
("request_date_to", "<=", stat.date),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
# retire des congés les jours où j'ai travaillé (car présence dans l'app présence) alors que j'étais noté en congé
|
||||||
|
# TODO faire pareil avec les feuilles de temps?
|
||||||
|
intersect_hours = sum(leave_ids.mapped("number_of_hours_display"))
|
||||||
|
# for leave_id in leave_ids:
|
||||||
|
# for attendance_id in stat.attendance_ids:
|
||||||
|
# intersect_hours -= stat._get_intersects(
|
||||||
|
# leave_id.date_from,
|
||||||
|
# leave_id.date_to,
|
||||||
|
# attendance_id.check_in,
|
||||||
|
# attendance_id.check_out,
|
||||||
|
# )
|
||||||
|
total_leave_hours = intersect_hours
|
||||||
|
else:
|
||||||
|
total_leave_hours = 0
|
||||||
|
return total_leave_hours
|
||||||
|
|
||||||
|
@api.depends("employee_id", "date")
|
||||||
|
def _compute_name(self):
|
||||||
|
for stat in self:
|
||||||
|
stat.name = "%s - %s" % (stat.employee_id.name, stat.date)
|
||||||
|
|
||||||
|
@api.depends("date","employee_id")
|
||||||
|
def _compute_dayofweek(self):
|
||||||
|
for stat in self:
|
||||||
|
if not stat.date:
|
||||||
|
stat.dayofweek = None
|
||||||
|
stat.is_public_holiday = False
|
||||||
|
continue
|
||||||
|
stat.dayofweek = int(stat.date.strftime("%u")) - 1
|
||||||
|
stat.is_public_holiday = bool(stat.sheet_id.employee_id._get_public_holidays(stat.date, stat.date - timedelta(days=1)))
|
||||||
|
|
||||||
|
def _get_gap_hours(self, total_hours, total_recovery_hours, total_leave_hours, total_planned_hours):
|
||||||
|
self.ensure_one()
|
||||||
|
balance = (
|
||||||
|
total_hours
|
||||||
|
+ total_recovery_hours
|
||||||
|
+ total_leave_hours
|
||||||
|
- total_planned_hours
|
||||||
|
)
|
||||||
|
return balance
|
||||||
|
|
||||||
|
@api.depends(
|
||||||
|
"employee_id",
|
||||||
|
"date",
|
||||||
|
"total_hours",
|
||||||
|
"total_planned_hours",
|
||||||
|
"timesheet_line_ids",
|
||||||
|
)
|
||||||
|
def _compute_hours(self):
|
||||||
|
for stat in self:
|
||||||
|
total_hours = stat._get_total_hours()
|
||||||
|
total_recovery_hours = stat._get_total_recovery_hours()
|
||||||
|
total_planned_hours = stat._get_total_planned_hours()
|
||||||
|
total_leave_hours = stat._get_total_leave_hours()
|
||||||
|
# balance = (
|
||||||
|
# total_hours
|
||||||
|
# + total_recovery_hours
|
||||||
|
# + total_leave_hours
|
||||||
|
# - total_planned_hours
|
||||||
|
# )
|
||||||
|
stat.total_hours = total_hours
|
||||||
|
stat.total_planned_hours = total_planned_hours
|
||||||
|
#stat.gap_hours = balance
|
||||||
|
stat.gap_hours = stat._get_gap_hours(total_hours, total_recovery_hours, total_leave_hours, total_planned_hours)
|
||||||
|
stat.total_recovery_hours = total_recovery_hours
|
||||||
|
stat.total_leave_hours = total_leave_hours
|
9
hr_employee_stats_sheet/models/hr_leave_allocation.py
Normal file
9
hr_employee_stats_sheet/models/hr_leave_allocation.py
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||||
|
|
||||||
|
from odoo import models, fields
|
||||||
|
|
||||||
|
class HrLeaveAllocation(models.Model):
|
||||||
|
_inherit = "hr.leave.allocation"
|
||||||
|
|
||||||
|
timesheet_sheet_id = fields.Many2one("hr_timesheet.sheet", string="Timesheet Sheet", readonly=True)
|
318
hr_employee_stats_sheet/models/hr_timesheet_sheet.py
Normal file
318
hr_employee_stats_sheet/models/hr_timesheet_sheet.py
Normal file
@@ -0,0 +1,318 @@
|
|||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
|
from odoo import api, fields, models, _
|
||||||
|
from odoo.exceptions import UserError
|
||||||
|
from odoo.addons.resource.models.resource import HOURS_PER_DAY
|
||||||
|
|
||||||
|
|
||||||
|
class HrTimesheetSheet(models.Model):
|
||||||
|
_inherit = "hr_timesheet.sheet"
|
||||||
|
|
||||||
|
employee_stats_ids = fields.One2many(
|
||||||
|
"hr.employee.stats", "sheet_id", "Employee Stats"
|
||||||
|
)
|
||||||
|
total_recovery_hours = fields.Float(
|
||||||
|
compute="_compute_total_recovery_hours", string="Total Recovery Hours"
|
||||||
|
)
|
||||||
|
timesheet_sheet_gap_hours = fields.Float(
|
||||||
|
"Timesheet sheet gap hours", compute="_compute_hours"
|
||||||
|
)
|
||||||
|
timesheet_sheet_recovery_hours = fields.Float(
|
||||||
|
"Timesheet sheet recovery hours", compute="_compute_hours"
|
||||||
|
)
|
||||||
|
|
||||||
|
recovery_allocation_ids = fields.One2many(
|
||||||
|
'hr.leave.allocation',
|
||||||
|
'timesheet_sheet_id',
|
||||||
|
string='Recovery Allocations',
|
||||||
|
)
|
||||||
|
|
||||||
|
@api.depends("employee_stats_ids.gap_hours", "employee_id")
|
||||||
|
def _compute_hours(self):
|
||||||
|
for sheet in self:
|
||||||
|
sheet.timesheet_sheet_gap_hours = sheet._get_timesheet_sheet_gap_hours()
|
||||||
|
sheet.timesheet_sheet_recovery_hours = (
|
||||||
|
sheet._get_timesheet_sheet_recovery_hours()
|
||||||
|
)
|
||||||
|
|
||||||
|
def _get_timesheet_sheet_gap_hours(self):
|
||||||
|
for sheet in self:
|
||||||
|
timesheet_sheet_gap_hours = sum(
|
||||||
|
sheet.employee_stats_ids.filtered(
|
||||||
|
lambda stat: stat.date <= fields.Date.today()
|
||||||
|
).mapped("gap_hours")
|
||||||
|
)
|
||||||
|
return timesheet_sheet_gap_hours
|
||||||
|
|
||||||
|
def _get_timesheet_sheet_recovery_hours(self):
|
||||||
|
self.ensure_one()
|
||||||
|
coef = self.env.company.coef
|
||||||
|
# apply coef only if hours to recovery are positive and coef is existing
|
||||||
|
if self.timesheet_sheet_gap_hours > 0 and coef > 0:
|
||||||
|
timesheet_sheet_recovery_hours = self.timesheet_sheet_gap_hours + (
|
||||||
|
self.timesheet_sheet_gap_hours * coef / 100
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
timesheet_sheet_recovery_hours = self.timesheet_sheet_gap_hours
|
||||||
|
return timesheet_sheet_recovery_hours
|
||||||
|
|
||||||
|
def search_and_create_employee_stats(self):
|
||||||
|
for sheet in self:
|
||||||
|
if sheet.employee_id:
|
||||||
|
for day in range((sheet.date_end - sheet.date_start).days + 1):
|
||||||
|
date = sheet.date_start + timedelta(days=day)
|
||||||
|
stats = self.env["hr.employee.stats"].search(
|
||||||
|
[
|
||||||
|
("employee_id", "=", sheet.employee_id.id),
|
||||||
|
("date", "=", date),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
if stats and not stats.sheet_id:
|
||||||
|
stats.write({"sheet_id": sheet.id})
|
||||||
|
if not stats:
|
||||||
|
self.env["hr.employee.stats"].create(
|
||||||
|
{
|
||||||
|
"employee_id": sheet.employee_id.id,
|
||||||
|
"date": date,
|
||||||
|
"sheet_id": sheet.id,
|
||||||
|
"company_id": sheet.company_id.id,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return True
|
||||||
|
|
||||||
|
@api.model
|
||||||
|
def create(self, vals):
|
||||||
|
res = super().create(vals)
|
||||||
|
res.search_and_create_employee_stats()
|
||||||
|
return res
|
||||||
|
|
||||||
|
def write(self, vals):
|
||||||
|
res = super().write(vals)
|
||||||
|
if "date_end" in vals or "date_start" in vals or "employee_id" in vals:
|
||||||
|
self.search_and_create_employee_stats()
|
||||||
|
return res
|
||||||
|
|
||||||
|
def unlink(self):
|
||||||
|
for sheet in self:
|
||||||
|
sheet.employee_stats_ids.unlink()
|
||||||
|
return super().unlink()
|
||||||
|
|
||||||
|
def action_timesheet_draft(self):
|
||||||
|
res = super().action_timesheet_draft()
|
||||||
|
for sheet in self:
|
||||||
|
# set the state of the recovery allocation to refuse if the timesheet sheet is set to draft
|
||||||
|
if sheet.state == "draft":
|
||||||
|
sheet.recovery_allocation_ids.write({"state": "refuse"})
|
||||||
|
return res
|
||||||
|
|
||||||
|
@api.depends("employee_stats_ids.gap_hours", "employee_id")
|
||||||
|
def _compute_total_recovery_hours(self):
|
||||||
|
recovery_type_id = self.env.company.recovery_type_id
|
||||||
|
for sheet in self:
|
||||||
|
recovery_ids = self.env["hr.leave"].search(
|
||||||
|
[
|
||||||
|
("holiday_status_id", "=", recovery_type_id.id),
|
||||||
|
("employee_id", "=", sheet.employee_id.id),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
recovery_allocation_ids = self.env["hr.leave.allocation"].search(
|
||||||
|
[
|
||||||
|
("holiday_status_id", "=", recovery_type_id.id),
|
||||||
|
("employee_id", "=", sheet.employee_id.id),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
total_allocation_ids = sum(
|
||||||
|
recovery_allocation_ids.mapped("number_of_hours_display")
|
||||||
|
)
|
||||||
|
total_recovery_ids = sum(recovery_ids.mapped("number_of_hours_display"))
|
||||||
|
sheet.total_recovery_hours = (
|
||||||
|
total_allocation_ids
|
||||||
|
+ total_recovery_ids
|
||||||
|
+ sum(
|
||||||
|
sheet.employee_stats_ids.filtered(
|
||||||
|
lambda stat: stat.date <= fields.Date.today()
|
||||||
|
).mapped("gap_hours")
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
def _get_contract_in_progress_during_timesheet_sheet_time_period(self):
|
||||||
|
"""
|
||||||
|
get the contract which was in progress during the timesheet sheet range time :
|
||||||
|
- the contrat start date must be before the timesheet sheet start date
|
||||||
|
- the contrat end date can be not defined or must be after the timesheet sheet end date
|
||||||
|
- the state can be closed today or still open
|
||||||
|
"""
|
||||||
|
contract_in_progress_during_timesheet = self.env["hr.contract"].search(
|
||||||
|
[
|
||||||
|
("employee_id", "=", self.employee_id.id),
|
||||||
|
("state", "in", ("open","close")),
|
||||||
|
("date_start", "<=", self.date_start),
|
||||||
|
("date_end", ">=", self.date_end or None),
|
||||||
|
],
|
||||||
|
order="date_start desc",
|
||||||
|
limit=1,
|
||||||
|
)
|
||||||
|
return contract_in_progress_during_timesheet
|
||||||
|
|
||||||
|
def _check_new_contract_starting_during_timesheet(self):
|
||||||
|
"""
|
||||||
|
check if there was a contract starting during the timesheet sheet range time
|
||||||
|
:return: raise error if there is a contract starting during the timesheet sheet range time
|
||||||
|
"""
|
||||||
|
contract_starting_during_timesheet_ids = self.env["hr.contract"].search(
|
||||||
|
[
|
||||||
|
("employee_id", "=", self.employee_id.id),
|
||||||
|
("state", "in", ("open","close")),
|
||||||
|
("date_start", ">", self.date_start),
|
||||||
|
("date_start", "<=", self.date_end),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
if contract_starting_during_timesheet_ids:
|
||||||
|
raise UserError(
|
||||||
|
_("There is a contract starting during the timesheet sheet time period for the employee %s"
|
||||||
|
"Please create a new timesheet sheet starting from the new contract start date")
|
||||||
|
% self.employee_id.display_name
|
||||||
|
)
|
||||||
|
|
||||||
|
def _get_calendar_in_progress_during_timesheet_time_period(self):
|
||||||
|
"""
|
||||||
|
get the ressource calendar which was used during the timesheet sheet time period
|
||||||
|
"""
|
||||||
|
# get work contrat in progress for the employe during the timesheet sheet time period
|
||||||
|
contract_during_timesheet_sheet = self._get_contract_in_progress_during_timesheet_sheet_time_period()
|
||||||
|
if contract_during_timesheet_sheet:
|
||||||
|
# check if a new contract start during timesheet sheet time period. If yes, raise an error
|
||||||
|
self._check_new_contract_starting_during_timesheet()
|
||||||
|
# get the ressource calendar id according to the work contract
|
||||||
|
return contract_during_timesheet_sheet.resource_calendar_id
|
||||||
|
elif self.employee_id.resource_calendar_id:
|
||||||
|
return self.employee_id.resource_calendar_id
|
||||||
|
elif self.env.company.resource_calendar_id:
|
||||||
|
return self.env.company.resource_calendar_id
|
||||||
|
return None
|
||||||
|
|
||||||
|
def _get_working_hours_per_week(self):
|
||||||
|
"""
|
||||||
|
Get the weekly working hours for the employee, which is defined by:
|
||||||
|
- the employee's work contract,
|
||||||
|
- or their resource calendar,
|
||||||
|
- or the company's resource calendar,
|
||||||
|
- or the default value of 40 hours per week.
|
||||||
|
:return: limit recovery hours
|
||||||
|
"""
|
||||||
|
# get ressource calendar id used during the timesheet sheet time period
|
||||||
|
ressource_calendar_id = self._get_calendar_in_progress_during_timesheet_time_period()
|
||||||
|
if ressource_calendar_id:
|
||||||
|
resource_calendar_attendance_ids = self.env[
|
||||||
|
"resource.calendar.attendance"
|
||||||
|
].search([("calendar_id", "=", ressource_calendar_id.id)])
|
||||||
|
# calculate working hours per week according to the employee's resource calendar
|
||||||
|
weekly_working_hours = 0
|
||||||
|
for day in resource_calendar_attendance_ids:
|
||||||
|
weekly_working_hours += day.hour_to - day.hour_from
|
||||||
|
return weekly_working_hours
|
||||||
|
return HOURS_PER_DAY * 5
|
||||||
|
|
||||||
|
def _get_working_hours_per_day(self):
|
||||||
|
"""
|
||||||
|
Get the hours per day for the employee according to:
|
||||||
|
- the employee's work contract,
|
||||||
|
- or their resource calendar,
|
||||||
|
- or the company's resource calendar,
|
||||||
|
- or the default value of 8 hours per day.
|
||||||
|
:return: hours per day
|
||||||
|
"""
|
||||||
|
# get ressource calendar id used during the timesheet sheet time period
|
||||||
|
ressource_calendar_id = self._get_calendar_in_progress_during_timesheet_time_period()
|
||||||
|
if ressource_calendar_id:
|
||||||
|
return ressource_calendar_id.hours_per_day
|
||||||
|
return HOURS_PER_DAY
|
||||||
|
|
||||||
|
def _get_max_allowed_recovery_hours(self):
|
||||||
|
"""
|
||||||
|
Get the maximum number of hours beyond which new recovery allowances cannot be created
|
||||||
|
"""
|
||||||
|
return self._get_working_hours_per_week()
|
||||||
|
|
||||||
|
def action_generate_recovery_allocation(self):
|
||||||
|
# check if the user has the right to review the timesheet sheet
|
||||||
|
self.ensure_one()
|
||||||
|
self._check_can_review()
|
||||||
|
recovery_type_id = self.env.company.recovery_type_id
|
||||||
|
employee_id = self.employee_id
|
||||||
|
|
||||||
|
if not employee_id or not recovery_type_id:
|
||||||
|
raise UserError(
|
||||||
|
_("Employe not defined for the timesheet sheet or recovery type not defined in settings")
|
||||||
|
)
|
||||||
|
|
||||||
|
# check if allocation already exists for this timesheet sheet, if yes, refuse it
|
||||||
|
allocations = (
|
||||||
|
self.env["hr.leave.allocation"]
|
||||||
|
.sudo()
|
||||||
|
.search(
|
||||||
|
[
|
||||||
|
("timesheet_sheet_id.id", "=", self.id),
|
||||||
|
("state", "!=", "refuse"),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
if allocations:
|
||||||
|
allocations.write({"state": "refuse"})
|
||||||
|
|
||||||
|
# get recovery hours from total gap hours of the timesheet sheet
|
||||||
|
recovery_hours = self._get_timesheet_sheet_recovery_hours()
|
||||||
|
|
||||||
|
# get recovery hours cap
|
||||||
|
max_allowed_recovery_hours = self._get_max_allowed_recovery_hours()
|
||||||
|
if max_allowed_recovery_hours:
|
||||||
|
# find recovery remaining leaves for the employee
|
||||||
|
recovery_type_id = self.env.company.recovery_type_id
|
||||||
|
# get virtual remaining leaves for the employee and the recovery leaves type
|
||||||
|
data_days = recovery_type_id.get_employees_days([employee_id.id])[employee_id.id]
|
||||||
|
total_recovery_type_leaves = data_days.get(recovery_type_id.id,{})
|
||||||
|
total_virtual_remaining_recovery_type_leaves = total_recovery_type_leaves.get('virtual_remaining_leaves', 0)
|
||||||
|
# TODO vérifier si ça marche avec un recovery type dont l'unité de mesure est le jour
|
||||||
|
# add the recovery hours to the total remaining leaves recovery type, and check if the limit of recovery hours is exceeded
|
||||||
|
exceeded_hours = total_virtual_remaining_recovery_type_leaves + recovery_hours - max_allowed_recovery_hours
|
||||||
|
# if limit recovery hours is exceeded, don't create a new allocation
|
||||||
|
if exceeded_hours > 0:
|
||||||
|
raise UserError(
|
||||||
|
_(
|
||||||
|
"The number of recovery hours exceeds the authorized limit (%s h) by %s hours"
|
||||||
|
)
|
||||||
|
% (max_allowed_recovery_hours, exceeded_hours)
|
||||||
|
)
|
||||||
|
# convert recovery hours into days
|
||||||
|
recovery_days = recovery_hours / self._get_working_hours_per_day()
|
||||||
|
|
||||||
|
# create an allocation (positive or negative) according to the total gap hours for the timesheet sheet range time
|
||||||
|
new_alloc = self.env["hr.leave.allocation"].create(
|
||||||
|
{
|
||||||
|
"private_name": "Allocation : %s - %s" % (recovery_type_id.name, employee_id.display_name),
|
||||||
|
"holiday_status_id": recovery_type_id.id,
|
||||||
|
"employee_id": employee_id.id,
|
||||||
|
"number_of_days": recovery_days,
|
||||||
|
"timesheet_sheet_id": self.id,
|
||||||
|
"allocation_type": 'accrual',
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
# set the allocation to validate or not
|
||||||
|
if self.env.company.auto_validate_recovery_allocation:
|
||||||
|
new_alloc.write({"state": "validate"})
|
||||||
|
|
||||||
|
return {
|
||||||
|
"type": "ir.actions.act_window",
|
||||||
|
"res_model": "hr.leave.allocation",
|
||||||
|
"res_id": new_alloc.id,
|
||||||
|
"view_mode": "form",
|
||||||
|
"target": "current",
|
||||||
|
"views": [
|
||||||
|
(
|
||||||
|
self.env.ref("hr_holidays.hr_leave_allocation_view_form").id,
|
||||||
|
"form",
|
||||||
|
)
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
12
hr_employee_stats_sheet/models/res_company.py
Normal file
12
hr_employee_stats_sheet/models/res_company.py
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
from odoo import fields, models
|
||||||
|
|
||||||
|
|
||||||
|
class ResCompany(models.Model):
|
||||||
|
_inherit = "res.company"
|
||||||
|
|
||||||
|
recovery_type_id = fields.Many2one(
|
||||||
|
"hr.leave.type", string="Leave recovery type"
|
||||||
|
)
|
||||||
|
coef = fields.Float("Coef", default=25)
|
||||||
|
|
||||||
|
auto_validate_recovery_allocation = fields.Boolean("Auto validate recovery allocation", default=True)
|
28
hr_employee_stats_sheet/models/res_config.py
Normal file
28
hr_employee_stats_sheet/models/res_config.py
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
from odoo import fields, models
|
||||||
|
|
||||||
|
|
||||||
|
class ResConfigSettings(models.TransientModel):
|
||||||
|
_inherit = "res.config.settings"
|
||||||
|
|
||||||
|
recovery_type_id = fields.Many2one(
|
||||||
|
"hr.leave.type",
|
||||||
|
related="company_id.recovery_type_id",
|
||||||
|
string="Leave recovery type",
|
||||||
|
readonly=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
coef = fields.Float(
|
||||||
|
related="company_id.coef",
|
||||||
|
required=True,
|
||||||
|
string="Coef (in %)",
|
||||||
|
domain="[('company_id', '=', company_id)]",
|
||||||
|
readonly=False,
|
||||||
|
help="The coef is applied to recovery hours"
|
||||||
|
"Example : an employe make 1h overtime, if coef is set to 25%, the recovery hours allocated will be 1,25h",
|
||||||
|
)
|
||||||
|
|
||||||
|
auto_validate_recovery_allocation = fields.Boolean(
|
||||||
|
related="company_id.auto_validate_recovery_allocation",
|
||||||
|
readonly=False,
|
||||||
|
domain="[('company_id', '=', company_id)]",
|
||||||
|
)
|
3
hr_employee_stats_sheet/security/ir.model.access.csv
Normal file
3
hr_employee_stats_sheet/security/ir.model.access.csv
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
|
||||||
|
access_hr_employee_stats_manager,hr.employee.stats,model_hr_employee_stats,hr_timesheet.group_timesheet_manager,1,1,1,1
|
||||||
|
access_hr_employee_stats_user,hr.employee.stats,model_hr_employee_stats,hr_timesheet.group_hr_timesheet_user,1,0,1,0
|
|
20
hr_employee_stats_sheet/views/hr_employee_stats.xml
Normal file
20
hr_employee_stats_sheet/views/hr_employee_stats.xml
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<odoo>
|
||||||
|
<record id="view_hr_employee_stats_tree" model="ir.ui.view">
|
||||||
|
<field name="name">hr.employee.stats.tree</field>
|
||||||
|
<field name="model">hr.employee.stats</field>
|
||||||
|
<field name="arch" type="xml">
|
||||||
|
<tree name="Employee time stats" editable="top" decoration-muted="dayofweek in [5,6] or is_public_holiday">
|
||||||
|
<field name="name"/>
|
||||||
|
<field name="date"/>
|
||||||
|
<field name="employee_id"/>
|
||||||
|
<field name="total_hours" widget="float_time" sum="Total"/>
|
||||||
|
<field name="total_leave_hours" widget="float_time" sum="Total"/>
|
||||||
|
<field name="total_recovery_hours" widget="float_time" sum="Total"/>
|
||||||
|
<field name="total_planned_hours" widget="float_time" sum="Total"/>
|
||||||
|
<field name="gap_hours" widget="float_time" sum="Total"/>
|
||||||
|
<field name="dayofweek" invisible="1"/>
|
||||||
|
<field name="is_public_holiday" invisible="1"/>
|
||||||
|
</tree>
|
||||||
|
</field>
|
||||||
|
</record>
|
||||||
|
</odoo>
|
40
hr_employee_stats_sheet/views/hr_timesheet_sheet.xml
Normal file
40
hr_employee_stats_sheet/views/hr_timesheet_sheet.xml
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
<odoo>
|
||||||
|
<data>
|
||||||
|
<record id="view_hr_timesheet_sheet_form_inherit" model="ir.ui.view">
|
||||||
|
<field name="name">hr.timesheet.sheet.form.inherit</field>
|
||||||
|
<field name="model">hr_timesheet.sheet</field>
|
||||||
|
<field name="inherit_id" ref="hr_timesheet_sheet.hr_timesheet_sheet_form"/>
|
||||||
|
<field name="arch" type="xml">
|
||||||
|
<group name="dates" position="inside">
|
||||||
|
<field name="timesheet_sheet_gap_hours" string="Total gap hours in this timesheet sheet time range" widget="float_time"/>
|
||||||
|
<field name="timesheet_sheet_recovery_hours" string="Total recovery hours in this timesheet sheet time range" widget="float_time" style="font-weight: bold"/>
|
||||||
|
<button
|
||||||
|
colspan="2"
|
||||||
|
class="oe_highlight"
|
||||||
|
name="action_generate_recovery_allocation"
|
||||||
|
string='Create recovery allocation'
|
||||||
|
type='object'
|
||||||
|
attrs="{'invisible': ['|', ('can_review', '=', False), ('state', '!=', 'done')]}"
|
||||||
|
/>
|
||||||
|
</group>
|
||||||
|
<xpath expr="//notebook" position="inside">
|
||||||
|
<page string="Employee Stats">
|
||||||
|
<field name="employee_stats_ids" no_label="1" readonly="1"/>
|
||||||
|
</page>
|
||||||
|
<page string="Generated recovery allocations">
|
||||||
|
<field name="recovery_allocation_ids" no_label="1" readonly="1"/>
|
||||||
|
</page>
|
||||||
|
</xpath>
|
||||||
|
</field>
|
||||||
|
</record>
|
||||||
|
<record id="hr_timesheet_sheet_stats_overview_pivot_view" model="ir.ui.view">
|
||||||
|
<field name="name">hr.timesheet.sheet.stats.overview.pivot.view</field>
|
||||||
|
<field name="model">hr_timesheet.sheet</field>
|
||||||
|
<field name="arch" type="xml">
|
||||||
|
<pivot string="Employee time stats pivot" sample="1">
|
||||||
|
<field name="employee_id" type="row"/>
|
||||||
|
</pivot>
|
||||||
|
</field>
|
||||||
|
</record>
|
||||||
|
</data>
|
||||||
|
</odoo>
|
60
hr_employee_stats_sheet/views/res_config_settings_views.xml
Normal file
60
hr_employee_stats_sheet/views/res_config_settings_views.xml
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
<odoo>
|
||||||
|
<record id="res_config_settings_view_form" model="ir.ui.view">
|
||||||
|
<field name="name">res.config.settings.view.form.inherit.hr.timesheet</field>
|
||||||
|
<field name="model">res.config.settings</field>
|
||||||
|
<field name="priority" eval="55" />
|
||||||
|
<field name="inherit_id" ref="hr_timesheet.res_config_settings_view_form" />
|
||||||
|
<field name="arch" type="xml">
|
||||||
|
<xpath expr="//div[@name='hr_timesheet_sheet']" position="inside">
|
||||||
|
<div class="col-xs-12 col-md-6 o_setting_box">
|
||||||
|
<div class="o_setting_right_pane">
|
||||||
|
<label for="recovery_type_id"/>
|
||||||
|
<span
|
||||||
|
class="fa fa-lg fa-building"
|
||||||
|
title="Values set here are company-specific."
|
||||||
|
aria-label="Values set here are company-specific."
|
||||||
|
groups="base.group_multi_company"
|
||||||
|
role="img"
|
||||||
|
/>
|
||||||
|
<div class="text-muted">
|
||||||
|
Choose the recovery type.
|
||||||
|
</div>
|
||||||
|
<div class="content-group">
|
||||||
|
<div class="mb16">
|
||||||
|
<field
|
||||||
|
name="recovery_type_id"
|
||||||
|
class="o_light_label"
|
||||||
|
widget="selection"
|
||||||
|
required="1"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="text-muted">
|
||||||
|
Choose the coef to apply to recovery hours.
|
||||||
|
</div >
|
||||||
|
<div class="content-group">
|
||||||
|
<div class="mb16">
|
||||||
|
<field
|
||||||
|
name="coef"
|
||||||
|
class="o_light_label"
|
||||||
|
widget="float"
|
||||||
|
/> %
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="text-muted">
|
||||||
|
Choose to auto-validate the recovery allocation or not
|
||||||
|
</div>
|
||||||
|
<div class="content-group">
|
||||||
|
<div class="mb16">
|
||||||
|
<field
|
||||||
|
name="auto_validate_recovery_allocation"
|
||||||
|
widget="boolean_toggle"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</xpath>
|
||||||
|
</field>
|
||||||
|
</record>
|
||||||
|
</odoo>
|
@@ -0,0 +1,52 @@
|
|||||||
|
==================================================
|
||||||
|
hr_employee_stats_sheet_project_timesheet_holidays
|
||||||
|
==================================================
|
||||||
|
|
||||||
|
Summary
|
||||||
|
=======
|
||||||
|
|
||||||
|
When holidays creates timesheets, exclude thoses timesheets to calculated total hours
|
||||||
|
|
||||||
|
Description
|
||||||
|
===========
|
||||||
|
|
||||||
|
Installation
|
||||||
|
============
|
||||||
|
|
||||||
|
``hr_employee_stats_sheet_project_timesheet_holidays`` self-installs
|
||||||
|
when ``hr_employee_stats_sheet``
|
||||||
|
and ``project_timesheet_holidays`` are installed
|
||||||
|
|
||||||
|
Known issues / Roadmap
|
||||||
|
======================
|
||||||
|
|
||||||
|
None yet.
|
||||||
|
|
||||||
|
Bug Tracker
|
||||||
|
===========
|
||||||
|
|
||||||
|
Bugs are tracked on `our issues website <https://github.com/elabore-coop/allow_negative_leave_and_allocation/issues>`_. In case of
|
||||||
|
trouble, please check there if your issue has already been
|
||||||
|
reported. If you spotted it first, help us smashing it by providing a
|
||||||
|
detailed and welcomed feedback.
|
||||||
|
|
||||||
|
Credits
|
||||||
|
=======
|
||||||
|
|
||||||
|
Contributors
|
||||||
|
------------
|
||||||
|
|
||||||
|
* `Alusage : Nicolas JEUDY`
|
||||||
|
* `Elabore <mailto:laetitia.dacosta@elabore.coop>`
|
||||||
|
|
||||||
|
Funders
|
||||||
|
-------
|
||||||
|
|
||||||
|
The development of this module has been financially supported by:
|
||||||
|
* Elabore (https://elabore.coop)
|
||||||
|
|
||||||
|
|
||||||
|
Maintainer
|
||||||
|
----------
|
||||||
|
|
||||||
|
This module is maintained by Elabore.
|
1
hr_employee_stats_sheet_project_timesheet_holidays/__init__.py
Executable file
1
hr_employee_stats_sheet_project_timesheet_holidays/__init__.py
Executable file
@@ -0,0 +1 @@
|
|||||||
|
from . import models
|
19
hr_employee_stats_sheet_project_timesheet_holidays/__manifest__.py
Executable file
19
hr_employee_stats_sheet_project_timesheet_holidays/__manifest__.py
Executable file
@@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"name": "hr_employee_stats_sheet_project_timesheet_holidays",
|
||||||
|
"version": "16.0.1.0.1",
|
||||||
|
"description": "Add global sheet for employee stats",
|
||||||
|
"summary": "Add global sheet for employee stats",
|
||||||
|
"author": "Nicolas JEUDY",
|
||||||
|
"website": "https://github.com/Alusage/odoo-hr-addons",
|
||||||
|
"license": "LGPL-3",
|
||||||
|
"category": "Human Resources",
|
||||||
|
"depends": [
|
||||||
|
"hr_employee_stats_sheet",
|
||||||
|
"project_timesheet_holidays",
|
||||||
|
],
|
||||||
|
"data": [
|
||||||
|
],
|
||||||
|
"installable": True,
|
||||||
|
"application": False,
|
||||||
|
"auto_install": True,
|
||||||
|
}
|
@@ -0,0 +1 @@
|
|||||||
|
from . import hr_employee_stats
|
@@ -0,0 +1,14 @@
|
|||||||
|
from odoo import models
|
||||||
|
|
||||||
|
class HrEmployeeStats(models.Model):
|
||||||
|
_inherit = "hr.employee.stats"
|
||||||
|
|
||||||
|
def get_total_hours_domain(self):
|
||||||
|
'''
|
||||||
|
if project_timesheet_holidays is installed, exclude timesheet generated from holidays to calculated total hours
|
||||||
|
'''
|
||||||
|
domain = super().get_total_hours_domain()
|
||||||
|
if self.env.company.leave_timesheet_task_id:
|
||||||
|
domain.append(("task_id", "!=", self.env.company.leave_timesheet_task_id.id),)
|
||||||
|
return domain
|
||||||
|
|
Reference in New Issue
Block a user