Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[17.0][ADD] hr_leave_dashboard_extra_time #177

Open
wants to merge 5 commits into
base: 17.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions hr_leave_dashboard_extra_time/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
=============================
HR Leave Dashboard Extra Time
=============================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:2c90bc9e12f014d49678e4e4a81f627ba6965799a6fc44915f10fb0c1bb1539a
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fhr--attendance-lightgray.png?logo=github
:target: https://github.com/OCA/hr-attendance/tree/17.0/hr_leave_dashboard_extra_time
:alt: OCA/hr-attendance
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/hr-attendance-17-0/hr-attendance-17-0-hr_leave_dashboard_extra_time
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/hr-attendance&target_branch=17.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

Display an additional card at the top of the Time Off Dashboard: Extra
Hours, informing the employee about the number of extra hours he has
been working and thus the number of hours available for him to take as a
compensation leave.

**Table of contents**

.. contents::
:local:

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/hr-attendance/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/hr-attendance/issues/new?body=module:%20hr_leave_dashboard_extra_time%0Aversion:%2017.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
-------

* Mint System GmbH

Contributors
------------

- Janik von Rotz [email protected]
- Do Anh Duy [email protected]

Other credits
-------------

The original development of this module has been done by Mint System. It
can be found in:
https://github.com/Mint-System/Odoo-Apps-HR/blob/15.0/hr_attendance_overtime_negative_hours

This module has been ported to the OCA with their agreement.

Maintainers
-----------

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/hr-attendance <https://github.com/OCA/hr-attendance/tree/17.0/hr_leave_dashboard_extra_time>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions hr_leave_dashboard_extra_time/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
22 changes: 22 additions & 0 deletions hr_leave_dashboard_extra_time/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright 2023 Janik von Rotz <[email protected]>
# Copyright 2024 Camptocamp
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
{
"name": "HR Leave Dashboard Extra Time",
"summary": """
Display an additional card at the top of the Time Off Dashboard:
Extra Hours, informing the employee about the number of extra hours
he has been working and thus the number of hours available
for him to take as a compensation leave
""",
"author": "Mint System GmbH, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/hr-attendance",
"category": "Human Resources",
"version": "17.0.1.0.0",
"license": "AGPL-3",
"depends": ["hr_holidays_attendance"],
"installable": True,
"application": False,
"auto_install": False,
"images": ["images/screen.png"],
}
Binary file added hr_leave_dashboard_extra_time/images/screen.png

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove this image. Its our default background image for the Odoo app store.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions hr_leave_dashboard_extra_time/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import hr_leave_type
170 changes: 170 additions & 0 deletions hr_leave_dashboard_extra_time/models/hr_leave_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
# Copyright 2023 Janik von Rotz <[email protected]>
# Copyright 2024 Camptocamp
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from datetime import datetime, time

import pytz

from odoo import api, fields, models
from odoo.tools import format_date


class HrLeaveType(models.Model):
_inherit = "hr.leave.type"

@api.model
def get_allocation_data_request(self, target_date=None):
"""Always include the extra hours leave type in the allocation data."""
extra_hours_time_off_type = self.env.ref(
"hr_holidays_attendance.holiday_status_extra_hours",
raise_if_not_found=False,
)

allocation_data = super().get_allocation_data_request(target_date)

extra_hours_in_data = any(
leave_type[3] == extra_hours_time_off_type.id
for leave_type in allocation_data
)

if extra_hours_time_off_type and not extra_hours_in_data:
employee = extra_hours_time_off_type.env[
"hr.employee"
]._get_contextual_employee()
extra_hours_data = extra_hours_time_off_type._prepare_extra_hours_data(
employee, target_date
)
allocation_data.append(extra_hours_data)
return allocation_data

def _prepare_lt_info_detail(self):
return {
"remaining_leaves": 0,
"virtual_remaining_leaves": 0,
"max_leaves": 0,
"accrual_bonus": 0,
"leaves_taken": 0,
"virtual_leaves_taken": 0,
"leaves_requested": 0,
"leaves_approved": 0,
"closest_allocation_remaining": 0,
"closest_allocation_expire": False,
"holds_changes": False,
"total_virtual_excess": 0,
"virtual_excess_data": {},
"exceeding_duration": 0,
"request_unit": self.request_unit,
"icon": self.sudo().icon_id.url,
"allows_negative": self.allows_negative,
"max_allowed_negative": self.max_allowed_negative,
}

def _prepare_extra_hours_data(self, employee, target_date):
if target_date and isinstance(target_date, str):
target_date = datetime.fromisoformat(target_date).date()

Check warning on line 65 in hr_leave_dashboard_extra_time/models/hr_leave_type.py

View check run for this annotation

Codecov / codecov/patch

hr_leave_dashboard_extra_time/models/hr_leave_type.py#L65

Added line #L65 was not covered by tests
elif target_date and isinstance(target_date, datetime):
target_date = target_date.date()

Check warning on line 67 in hr_leave_dashboard_extra_time/models/hr_leave_type.py

View check run for this annotation

Codecov / codecov/patch

hr_leave_dashboard_extra_time/models/hr_leave_type.py#L67

Added line #L67 was not covered by tests
elif not target_date:
target_date = fields.Date.today()

Check warning on line 69 in hr_leave_dashboard_extra_time/models/hr_leave_type.py

View check run for this annotation

Codecov / codecov/patch

hr_leave_dashboard_extra_time/models/hr_leave_type.py#L69

Added line #L69 was not covered by tests
allocations_leaves_consumed, extra_data = employee.with_context(
ignored_leave_ids=self.env.context.get("ignored_leave_ids")
)._get_consumed_leaves(self, target_date)
lt_info = self._prepare_lt_info_detail()
lt_info["exceeding_duration"] = extra_data[employee][self]["exceeding_duration"]
for excess_date, excess_days in extra_data[employee][self][
"excess_days"
].items():
amount = excess_days["amount"]
lt_info["virtual_excess_data"][

Check warning on line 79 in hr_leave_dashboard_extra_time/models/hr_leave_type.py

View check run for this annotation

Codecov / codecov/patch

hr_leave_dashboard_extra_time/models/hr_leave_type.py#L78-L79

Added lines #L78 - L79 were not covered by tests
excess_date.strftime("%Y-%m-%d")
] = excess_days
lt_info["total_virtual_excess"] += amount

Check warning on line 82 in hr_leave_dashboard_extra_time/models/hr_leave_type.py

View check run for this annotation

Codecov / codecov/patch

hr_leave_dashboard_extra_time/models/hr_leave_type.py#L82

Added line #L82 was not covered by tests
if not self.allows_negative:
continue
lt_info["virtual_leaves_taken"] += amount
lt_info["virtual_remaining_leaves"] -= amount

Check warning on line 86 in hr_leave_dashboard_extra_time/models/hr_leave_type.py

View check run for this annotation

Codecov / codecov/patch

hr_leave_dashboard_extra_time/models/hr_leave_type.py#L84-L86

Added lines #L84 - L86 were not covered by tests
if excess_days["is_virtual"]:
lt_info["leaves_requested"] += amount

Check warning on line 88 in hr_leave_dashboard_extra_time/models/hr_leave_type.py

View check run for this annotation

Codecov / codecov/patch

hr_leave_dashboard_extra_time/models/hr_leave_type.py#L88

Added line #L88 was not covered by tests
else:
lt_info["leaves_approved"] += amount
lt_info["leaves_taken"] += amount
lt_info["remaining_leaves"] -= amount

Check warning on line 92 in hr_leave_dashboard_extra_time/models/hr_leave_type.py

View check run for this annotation

Codecov / codecov/patch

hr_leave_dashboard_extra_time/models/hr_leave_type.py#L90-L92

Added lines #L90 - L92 were not covered by tests
allocations_now = self.env["hr.leave.allocation"]
allocations_date = self.env["hr.leave.allocation"]
allocations_with_remaining_leaves = self.env["hr.leave.allocation"]
today = fields.Date.today()
for allocation, data in allocations_leaves_consumed[employee][self].items():
if allocation:
if allocation.date_from <= today and (
not allocation.date_to or allocation.date_to >= today
):
allocations_now |= allocation

Check warning on line 102 in hr_leave_dashboard_extra_time/models/hr_leave_type.py

View check run for this annotation

Codecov / codecov/patch

hr_leave_dashboard_extra_time/models/hr_leave_type.py#L102

Added line #L102 was not covered by tests
if allocation.date_from <= target_date and (
not allocation.date_to or allocation.date_to >= target_date
):
allocations_date |= allocation

Check warning on line 106 in hr_leave_dashboard_extra_time/models/hr_leave_type.py

View check run for this annotation

Codecov / codecov/patch

hr_leave_dashboard_extra_time/models/hr_leave_type.py#L106

Added line #L106 was not covered by tests
if allocation.date_from > target_date:
continue

Check warning on line 108 in hr_leave_dashboard_extra_time/models/hr_leave_type.py

View check run for this annotation

Codecov / codecov/patch

hr_leave_dashboard_extra_time/models/hr_leave_type.py#L108

Added line #L108 was not covered by tests
if allocation.date_to and allocation.date_to < target_date:
continue

Check warning on line 110 in hr_leave_dashboard_extra_time/models/hr_leave_type.py

View check run for this annotation

Codecov / codecov/patch

hr_leave_dashboard_extra_time/models/hr_leave_type.py#L110

Added line #L110 was not covered by tests
lt_info["remaining_leaves"] += data["remaining_leaves"]
lt_info["virtual_remaining_leaves"] += data["virtual_remaining_leaves"]
lt_info["max_leaves"] += data["max_leaves"]
lt_info["accrual_bonus"] += data["accrual_bonus"]
lt_info["leaves_taken"] += data["leaves_taken"]
lt_info["virtual_leaves_taken"] += data["virtual_leaves_taken"]
lt_info["leaves_requested"] += (
data["virtual_leaves_taken"] - data["leaves_taken"]
)
lt_info["leaves_approved"] += data["leaves_taken"]
if data["virtual_remaining_leaves"] > 0:
allocations_with_remaining_leaves |= allocation

Check warning on line 122 in hr_leave_dashboard_extra_time/models/hr_leave_type.py

View check run for this annotation

Codecov / codecov/patch

hr_leave_dashboard_extra_time/models/hr_leave_type.py#L122

Added line #L122 was not covered by tests
closest_allocation = (
allocations_with_remaining_leaves[0]
if allocations_with_remaining_leaves
else self.env["hr.leave.allocation"]
)
closest_allocations = allocations_with_remaining_leaves.filtered(
lambda ca: ca.date_to == closest_allocation.date_to
)
closest_allocation_remaining = sum(
allocations_leaves_consumed[employee][self][ca]["virtual_remaining_leaves"]
for ca in closest_allocations
)
if closest_allocation.date_to:
closest_allocation_expire = format_date(

Check warning on line 136 in hr_leave_dashboard_extra_time/models/hr_leave_type.py

View check run for this annotation

Codecov / codecov/patch

hr_leave_dashboard_extra_time/models/hr_leave_type.py#L136

Added line #L136 was not covered by tests
self.env, closest_allocation.date_to
)
calendar = (

Check warning on line 139 in hr_leave_dashboard_extra_time/models/hr_leave_type.py

View check run for this annotation

Codecov / codecov/patch

hr_leave_dashboard_extra_time/models/hr_leave_type.py#L139

Added line #L139 was not covered by tests
employee.resource_calendar_id
or employee.company_id.resource_calendar_id
)
closest_allocation_duration = (

Check warning on line 143 in hr_leave_dashboard_extra_time/models/hr_leave_type.py

View check run for this annotation

Codecov / codecov/patch

hr_leave_dashboard_extra_time/models/hr_leave_type.py#L143

Added line #L143 was not covered by tests
calendar._attendance_intervals_batch(
datetime.combine(closest_allocation.date_to, time.min).replace(
tzinfo=pytz.UTC
),
datetime.combine(target_date, time.max).replace(tzinfo=pytz.UTC),
)
if self.request_unit == "hour"
else (closest_allocation.date_to - target_date).days + 1
)
else:
closest_allocation_expire = False
closest_allocation_duration = False
holds_changes = (
lt_info["accrual_bonus"] > 0
or bool(allocations_date - allocations_now)
or bool(allocations_now - allocations_date)
) and target_date != fields.Date.today()

lt_info.update(
{
"closest_allocation_remaining": closest_allocation_remaining,
"closest_allocation_expire": closest_allocation_expire,
"closest_allocation_duration": closest_allocation_duration,
"holds_changes": holds_changes,
}
)
return self.name, lt_info, self.requires_allocation, self.id
3 changes: 3 additions & 0 deletions hr_leave_dashboard_extra_time/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["whool"]
build-backend = "whool.buildapi"
2 changes: 2 additions & 0 deletions hr_leave_dashboard_extra_time/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Janik von Rotz <[email protected]>
* Do Anh Duy <[email protected]>
4 changes: 4 additions & 0 deletions hr_leave_dashboard_extra_time/readme/CREDITS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
The original development of this module has been done by Mint System.
It can be found in: https://github.com/Mint-System/Odoo-Apps-HR/blob/15.0/hr_attendance_overtime_negative_hours

This module has been ported to the OCA with their agreement.
1 change: 1 addition & 0 deletions hr_leave_dashboard_extra_time/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Display an additional card at the top of the Time Off Dashboard: Extra Hours, informing the employee about the number of extra hours he has been working and thus the number of hours available for him to take as a compensation leave.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading