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] resource_calendar_get_days #129

Closed
wants to merge 7 commits into from
Closed
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ addon | version | maintainers | summary
--- | --- | --- | ---
[hr_holidays_public](hr_holidays_public/) | 17.0.1.0.1 | | Manage Public Holidays
[hr_holidays_public_city](hr_holidays_public_city/) | 17.0.1.0.0 | [![victoralmau](https://github.com/victoralmau.png?size=30px)](https://github.com/victoralmau) | HR Holidays Public City
[resource_calendar_get_days](resource_calendar_get_days/) | 17.0.1.0.0 | | Calculate number of leave days relative to company working hours.

[//]: # (end addons)

Expand Down
86 changes: 86 additions & 0 deletions resource_calendar_get_days/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
==========================
Resource Calendar Get Days
==========================

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

.. |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--holidays-lightgray.png?logo=github
:target: https://github.com/OCA/hr-holidays/tree/17.0/resource_calendar_get_days
:alt: OCA/hr-holidays
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/hr-holidays-17-0/hr-holidays-17-0-resource_calendar_get_days
: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-holidays&target_branch=17.0
:alt: Try me on Runboat

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

Show remaining leaves per employee in allocation overview.

**Table of contents**

.. contents::
:local:

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

Bugs are tracked on `GitHub Issues <https://github.com/OCA/hr-holidays/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-holidays/issues/new?body=module:%20resource_calendar_get_days%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]
- Son Ho [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/tree/15.0/resource_calendar_get_days

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-holidays <https://github.com/OCA/hr-holidays/tree/17.0/resource_calendar_get_days>`_ 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 resource_calendar_get_days/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
20 changes: 20 additions & 0 deletions resource_calendar_get_days/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2024 Janik von Rotz <[email protected]>
# Copyright 2024 Camptocamp
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
{
"name": "Resource Calendar Get Days",
"summary": """
Calculate number of leave days relative to company working hours.
""",
"author": "Mint System GmbH, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/hr-holidays",
"category": "Human Resources",
"version": "17.0.1.0.0",
"license": "AGPL-3",
"demo": ["demo/resource_data.xml"],
"depends": ["hr_holidays"],
"installable": True,
"application": False,
"auto_install": False,
"images": ["images/screen.png"],
}
43 changes: 43 additions & 0 deletions resource_calendar_get_days/demo/resource_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo noupdate="1">

<record id="resource_calendar_std_42h" model="resource.calendar">
<field name="name">Standard 42 hours/week</field>
<field name="company_id" ref="base.main_company" />
<field name="hours_per_day">8.4</field>
<field
name="attendance_ids"
eval="[(5, 0, 0),
(0, 0, {'name': 'Monday Morning', 'dayofweek': '0', 'hour_from': 7.5, 'hour_to': 11.75, 'day_period': 'morning'}),
(0, 0, {'name': 'Monday Afternoon', 'dayofweek': '0', 'hour_from': 13.33333, 'hour_to': 17.66666, 'day_period': 'afternoon'}),
(0, 0, {'name': 'Tuesday Morning', 'dayofweek': '1', 'hour_from': 7.5, 'hour_to': 11.75, 'day_period': 'morning'}),
(0, 0, {'name': 'Tuesday Afternoon', 'dayofweek': '1', 'hour_from': 13.33333, 'hour_to': 17.66666, 'day_period': 'afternoon'}),
(0, 0, {'name': 'Wednesday Morning', 'dayofweek': '2', 'hour_from': 7.5, 'hour_to': 12, 'day_period': 'morning'}),
(0, 0, {'name': 'Wednesday Afternoon', 'dayofweek': '2', 'hour_from': 13.25, 'hour_to': 17.75, 'day_period': 'afternoon'}),
(0, 0, {'name': 'Thursday Morning', 'dayofweek': '3', 'hour_from': 7.5, 'hour_to': 11.75, 'day_period': 'morning'}),
(0, 0, {'name': 'Thursday Afternoon', 'dayofweek': '3', 'hour_from': 13.33333, 'hour_to': 17.66666, 'day_period': 'afternoon'}),
(0, 0, {'name': 'Friday Morning', 'dayofweek': '4', 'hour_from': 7.5, 'hour_to': 11.75, 'day_period': 'morning'}),
(0, 0, {'name': 'Friday Afternoon', 'dayofweek': '4', 'hour_from': 13.16666, 'hour_to': 16.16666, 'day_period': 'afternoon'})
]"
/>
</record>

<record id="resource_calendar_std_25_2h" model="resource.calendar">
<field name="name">Standard 25.2 hours/week</field>
<field name="company_id" ref="base.main_company" />
<field name="hours_per_day">5.04</field>
<field
name="attendance_ids"
eval="[(5, 0, 0),
(0, 0, {'name': 'Tuesday Morning', 'dayofweek': '1', 'hour_from': 7, 'hour_to': 11.7, 'day_period': 'morning'}),
(0, 0, {'name': 'Wednesday Morning', 'dayofweek': '2', 'hour_from': 10.5, 'hour_to': 12, 'day_period': 'morning'}),
(0, 0, {'name': 'Wednesday Afternoon', 'dayofweek': '2', 'hour_from': 12, 'hour_to': 15.5, 'day_period': 'afternoon'}),
(0, 0, {'name': 'Thursday Morning', 'dayofweek': '3', 'hour_from': 7, 'hour_to': 11.5, 'day_period': 'morning'}),
(0, 0, {'name': 'Thursday Afternoon', 'dayofweek': '3', 'hour_from': 13.5, 'hour_to': 17.5, 'day_period': 'afternoon'}),
(0, 0, {'name': 'Friday Morning', 'dayofweek': '4', 'hour_from': 7, 'hour_to': 11.5, 'day_period': 'morning'}),
(0, 0, {'name': 'Friday Afternoon', 'dayofweek': '4', 'hour_from': 13.5, 'hour_to': 16, 'day_period': 'afternoon'})
]"
/>
</record>

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

from odoo.addons.resource.models.utils import HOURS_PER_DAY


class HolidaysAllocation(models.Model):
_inherit = "hr.leave.allocation"

@api.depends("number_of_days", "holiday_status_id", "employee_id", "holiday_type")
def _compute_number_of_hours_display(self):
for allocation in self:
allocation_calendar = (
allocation.holiday_status_id.company_id.resource_calendar_id
)
if allocation.holiday_type == "employee" and allocation.employee_id:
allocation_calendar = (
allocation.employee_id.sudo().resource_id.calendar_id
)
allocation.number_of_hours_display = allocation.number_of_days * (
allocation_calendar.hours_per_day or HOURS_PER_DAY
)
38 changes: 38 additions & 0 deletions resource_calendar_get_days/models/resource_calendar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2024 Janik von Rotz <[email protected]>
# Copyright 2024 Camptocamp
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from collections import defaultdict

from odoo import models
from odoo.tools import float_utils

HOURS_PER_DAY = 8
ROUNDING_FACTOR = 9600


class ResourceCalendar(models.Model):
_inherit = "resource.calendar"

def _get_days_data(self, intervals, day_total):
"""
OVERWRITE: Calculate number of days against company hours per day.
"""
day_hours = defaultdict(float)
for start, stop, _ in intervals:
day_hours[start.date()] += (stop - start).total_seconds() / 3600

# compute number of days as quarters
hours_per_day = self.company_id.resource_calendar_id.hours_per_day
days = sum(
float_utils.round(
ROUNDING_FACTOR * day_hours[day] / hours_per_day or day_total[day]
)
/ ROUNDING_FACTOR
if day_total[day]
else 0
for day in day_hours
)
return {
"days": days,
"hours": sum(day_hours.values()),
}
3 changes: 3 additions & 0 deletions resource_calendar_get_days/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 resource_calendar_get_days/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Janik von Rotz <[email protected]>
* Son Ho <[email protected]>
5 changes: 5 additions & 0 deletions resource_calendar_get_days/readme/CREDITS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

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/tree/15.0/resource_calendar_get_days

This module has been ported to the OCA with their agreement.
1 change: 1 addition & 0 deletions resource_calendar_get_days/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Show remaining leaves per employee in allocation overview.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions resource_calendar_get_days/static/description/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body><a href="http://www.gnu.org/licenses/gpl-3.0-standalone.html">
<img alt="License: GPL-3" src="https://img.shields.io/badge/licence-GPL--3-blue.svg" />
</a>
<section id="resource-calendar-get-days">
<h1>Resource Calendar Get Days</h1>
<p>Calculate number of leave days relative to company working hours.</p>
<p>For a detailed documentation have a look at <a href="https://www.odoo-wiki.org/">https://www.odoo-wiki.org/</a>.</p>
<img height="100" width="100" alt="Icon" src="https://raw.githubusercontent.com/Mint-System/Wiki/master/assets/icon-box.png" />
<section id="configuration">
<h2>Configuration</h2>
<ul>
<li>No additional configurations needed</li>
</ul>
<section id="maintainer">
<h3>Maintainer</h3>
<a href="https://www.mint-system.ch">
<img src="https://raw.githubusercontent.com/Mint-System/Wiki/master/assets/mint-system-logo.png" alt="" />
</a>
<p>This module is maintained by Mint System GmbH.</p>
<p>For support and more information, please visit <a href="https://www.mint-system.ch">our Website</a>.</p>
</section>
</section>
</section>
</body>
</html>
1 change: 1 addition & 0 deletions resource_calendar_get_days/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_resource_calendar_get_days
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2024 Camptocamp
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo.addons.hr_holidays.tests.common import TestHrHolidaysCommon


class TestResourceCalendarGetDays(TestHrHolidaysCommon):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.leave_type = cls.env["hr.leave.type"].create(
{
"name": "Time Off with no validation for approval",
"time_type": "leave",
"requires_allocation": "yes",
"allocation_validation_type": "no",
}
)

cls.employee_emp.resource_calendar_id = cls.env.ref(
"resource_calendar_get_days.resource_calendar_std_42h"
).id

def test_resource_calendar_get_days_effect_allocation(self):
emp_allocation = self.env["hr.leave.allocation"].create(
{
"name": "Bank Holiday",
"holiday_type": "employee",
"employee_ids": [(4, self.employee_emp.id)],
"employee_id": self.employee_emp.id,
"date_from": "2024-06-17",
"holiday_status_id": self.leave_type.id,
"number_of_days": 3,
"allocation_type": "regular",
}
)
emp_allocation.action_validate()
# 3 days * 8.4 hour/day ~ 25.2
self.assertEqual(round(emp_allocation.number_of_hours_display, 2), 25.2)
Loading