Skip to content

Commit c57d066

Browse files
Jaime Arroyochandni299
Jaime Arroyo
authored andcommitted
[12.0][MIG] hr_calendar_rest_time
1 parent 3c53eeb commit c57d066

File tree

5 files changed

+70
-36
lines changed

5 files changed

+70
-36
lines changed

resource_hook/README.rst

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ Resource Hook
1414
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
1515
:alt: License: AGPL-3
1616
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fhr-lightgray.png?logo=github
17-
:target: https://github.com/OCA/hr/tree/11.0/resource_hook
17+
:target: https://github.com/OCA/hr/tree/12.0/resource_hook
1818
:alt: OCA/hr
1919
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
20-
:target: https://translation.odoo-community.org/projects/hr-11-0/hr-11-0-resource_hook
20+
:target: https://translation.odoo-community.org/projects/hr-12-0/hr-12-0-resource_hook
2121
:alt: Translate me on Weblate
2222
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
23-
:target: https://runbot.odoo-community.org/runbot/116/11.0
23+
:target: https://runbot.odoo-community.org/runbot/116/12.0
2424
:alt: Try me on Runbot
2525

2626
|badge1| |badge2| |badge3| |badge4| |badge5|
@@ -51,7 +51,7 @@ Bug Tracker
5151
Bugs are tracked on `GitHub Issues <https://github.com/OCA/hr/issues>`_.
5252
In case of trouble, please check there if your issue has already been reported.
5353
If you spotted it first, help us smashing it by providing a detailed and welcomed
54-
`feedback <https://github.com/OCA/hr/issues/new?body=module:%20resource_hook%0Aversion:%2011.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
54+
`feedback <https://github.com/OCA/hr/issues/new?body=module:%20resource_hook%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
5555

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

@@ -82,6 +82,6 @@ OCA, or the Odoo Community Association, is a nonprofit organization whose
8282
mission is to support the collaborative development of Odoo features and
8383
promote its widespread use.
8484

85-
This module is part of the `OCA/hr <https://github.com/OCA/hr/tree/11.0/resource_hook>`_ project on GitHub.
85+
This module is part of the `OCA/hr <https://github.com/OCA/hr/tree/12.0/resource_hook>`_ project on GitHub.
8686

8787
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

resource_hook/__manifest__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
'name': 'Resource Hook',
66
'summary': """
77
Extends the resource with hooks to standard methods.""",
8-
'version': '11.0.1.0.0',
8+
'version': '12.0.1.0.0',
99
'license': 'AGPL-3',
1010
'author': 'Creu Blanca, Odoo Community Association (OCA)',
1111
'website': 'https://github.com/OCA/hr',

resource_hook/hooks.py

+58-24
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,72 @@
1-
from odoo.addons.resource.models.resource_mixin import ResourceMixin
1+
from odoo.addons.resource.models.resource_mixin import ResourceMixin, ROUNDING_FACTOR
22
from datetime import timedelta
33
from odoo.tools import float_utils
4+
from pytz import utc
5+
from collections import defaultdict
46

57

68
def post_load_hook():
79
if not hasattr(ResourceMixin, 'get_work_days_data_original'):
810
ResourceMixin.get_work_days_data_original = \
911
ResourceMixin.get_work_days_data
1012

11-
def __new_get_work_days_data(self, from_datetime, to_datetime,
12-
calendar=None):
13-
if not hasattr(self, '_get_work_hours'):
14-
return self.get_day_work_hours_count_original(
15-
from_datetime, to_datetime, calendar=calendar)
16-
days_count = 0.0
17-
total_work_time = timedelta()
13+
def __new_get_work_days_data(
14+
self, from_datetime, to_datetime, compute_leaves=True,
15+
calendar=None, domain=None
16+
):
17+
"""
18+
By default the resource calendar is used, but it can be
19+
changed using the `calendar` argument.
20+
21+
`domain` is used in order to recognise the leaves to take,
22+
None means default value ('time_type', '=', 'leave')
23+
24+
Returns a dict {'days': n, 'hours': h} containing the
25+
quantity of working time expressed as days and as hours.
26+
"""
27+
resource = self.resource_id
1828
calendar = calendar or self.resource_calendar_id
19-
calendar = calendar.with_context(
20-
no_tz_convert=self.env.context.get('no_tz_convert', False))
21-
for day_intervals in calendar._iter_work_intervals(
22-
from_datetime, to_datetime, self.resource_id.id,
23-
compute_leaves=True):
24-
theoric_hours = self.get_day_work_hours_count(
25-
day_intervals[0][0].date(), calendar=calendar)
26-
# Here we introduce the hook method '_get_work_hours'.
27-
work_time = sum((self._get_work_hours(interval)
28-
for interval in day_intervals), timedelta())
29-
total_work_time += work_time
30-
if theoric_hours:
31-
days_count += float_utils.round(
32-
(work_time.total_seconds() / 3600 / theoric_hours) * 4) / 4
29+
30+
# naive datetimes are made explicit in UTC
31+
if not from_datetime.tzinfo:
32+
from_datetime = from_datetime.replace(tzinfo=utc)
33+
if not to_datetime.tzinfo:
34+
to_datetime = to_datetime.replace(tzinfo=utc)
35+
36+
# total hours per day: retrieve attendances with one extra day margin,
37+
# in order to compute the total hours on the first and last days
38+
from_full = from_datetime - timedelta(days=1)
39+
to_full = to_datetime + timedelta(days=1)
40+
intervals = calendar._attendance_intervals(
41+
from_full, to_full, resource
42+
)
43+
day_total = defaultdict(float)
44+
for start, stop, meta in intervals:
45+
day_total[start.date()] += (stop - start).total_seconds() / 3600
46+
47+
# actual hours per day
48+
if compute_leaves:
49+
intervals = calendar._work_intervals(
50+
from_datetime, to_datetime, resource, domain
51+
)
52+
else:
53+
intervals = calendar._attendance_intervals(
54+
from_datetime, to_datetime, resource
55+
)
56+
day_hours = defaultdict(float)
57+
for start, stop, meta in intervals:
58+
day_hours[start.date()] += self._get_work_hours(start, stop, meta)
59+
60+
# compute number of days as quarters
61+
days = sum(
62+
float_utils.round(
63+
ROUNDING_FACTOR * day_hours[day] / day_total[day]
64+
) / ROUNDING_FACTOR
65+
for day in day_hours
66+
)
3367
return {
34-
'days': days_count,
35-
'hours': total_work_time.total_seconds() / 3600,
68+
'days': days,
69+
'hours': sum(day_hours.values()),
3670
}
3771

3872
ResourceMixin.get_work_days_data = __new_get_work_days_data

resource_hook/models/resource_mixin.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
class ResourceMixin(models.AbstractModel):
88
_inherit = 'resource.mixin'
99

10-
def _get_work_hours(self, interval):
10+
def _get_work_hours(self, start, stop, meta):
1111
"""
1212
This method now returns the hours between the two ends of the
1313
interval. Extend this method if you want to alter the logic.
1414
:param interval:
1515
:return: float representing the time worked.
1616
"""
17-
return interval[1] - interval[0]
17+
return (stop - start).total_seconds() / 3600

resource_hook/static/description/index.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
44
<head>
55
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6-
<meta name="generator" content="Docutils 0.15.1: http://docutils.sourceforge.net/" />
6+
<meta name="generator" content="Docutils 0.14: http://docutils.sourceforge.net/" />
77
<title>Resource Hook</title>
88
<style type="text/css">
99

@@ -367,7 +367,7 @@ <h1 class="title">Resource Hook</h1>
367367
!! This file is generated by oca-gen-addon-readme !!
368368
!! changes will be overwritten. !!
369369
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
370-
<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/hr/tree/11.0/resource_hook"><img alt="OCA/hr" src="https://img.shields.io/badge/github-OCA%2Fhr-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/hr-11-0/hr-11-0-resource_hook"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runbot.odoo-community.org/runbot/116/11.0"><img alt="Try me on Runbot" src="https://img.shields.io/badge/runbot-Try%20me-875A7B.png" /></a></p>
370+
<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/hr/tree/12.0/resource_hook"><img alt="OCA/hr" src="https://img.shields.io/badge/github-OCA%2Fhr-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/hr-12-0/hr-12-0-resource_hook"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runbot.odoo-community.org/runbot/116/12.0"><img alt="Try me on Runbot" src="https://img.shields.io/badge/runbot-Try%20me-875A7B.png" /></a></p>
371371
<p>This module includes additional hooks to the standard resource methods,
372372
so that it is possible to alter the behaviour of methods, where inheritance
373373
is not an option.</p>
@@ -404,7 +404,7 @@ <h1><a class="toc-backref" href="#id4">Bug Tracker</a></h1>
404404
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/hr/issues">GitHub Issues</a>.
405405
In case of trouble, please check there if your issue has already been reported.
406406
If you spotted it first, help us smashing it by providing a detailed and welcomed
407-
<a class="reference external" href="https://github.com/OCA/hr/issues/new?body=module:%20resource_hook%0Aversion:%2011.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
407+
<a class="reference external" href="https://github.com/OCA/hr/issues/new?body=module:%20resource_hook%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
408408
<p>Do not contact contributors directly about support or help with technical issues.</p>
409409
</div>
410410
<div class="section" id="credits">
@@ -435,7 +435,7 @@ <h2><a class="toc-backref" href="#id8">Maintainers</a></h2>
435435
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose
436436
mission is to support the collaborative development of Odoo features and
437437
promote its widespread use.</p>
438-
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/hr/tree/11.0/resource_hook">OCA/hr</a> project on GitHub.</p>
438+
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/hr/tree/12.0/resource_hook">OCA/hr</a> project on GitHub.</p>
439439
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
440440
</div>
441441
</div>

0 commit comments

Comments
 (0)