Skip to content

Commit

Permalink
[ADD] resource_leaves_geographic
Browse files Browse the repository at this point in the history
  • Loading branch information
Shide committed Aug 2, 2024
1 parent 05881b1 commit 1824c91
Show file tree
Hide file tree
Showing 20 changed files with 1,037 additions and 0 deletions.
139 changes: 139 additions & 0 deletions resource_leaves_geographic/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
==========================
Resource Leaves Geographic
==========================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:e5409e484959f22489b0c0976ce8231c697b28572e9d29285501c0e20bc49179
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Alpha-red.png
:target: https://odoo-community.org/page/development-status
:alt: Alpha
.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
:alt: License: LGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fhr--holidays-lightgray.png?logo=github
:target: https://github.com/OCA/hr-holidays/tree/16.0/resource_leaves_geographic
: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-16-0/hr-holidays-16-0-resource_leaves_geographic
: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=16.0
:alt: Try me on Runboat

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

This module extends the functionality of Public Holidays to support
States and allow you to define Local Holidays more easily in the Company
Calendar.

Shows on Time Off all the holidays (with/without State) in the Public
Holidays section. Public Holidays (without State) always will have grey
background. Local holidays displayed with a grey background in the
calendar view belong to the Employee currently viewing the calendar or
to the single Employee selected from the Employee Form view.

This module is compatible with Attendances and Contracts (but those
modules are not required).

.. IMPORTANT::
This is an alpha version, the data model and design can change at any time without warning.
Only for development or testing purpose, do not use in production.
`More details on development status <https://odoo-community.org/page/development-status>`_

**Table of contents**

.. contents::
:local:

Use Cases / Context
===================

This module was developed because in order to differentiate local
Employee holidays, they must be added to their calendars instead of
being able to mark them on the general company calendar.

This will be useful for you if you want to be able to create holidays by
State in the company calendar and have them automatically applied to all
employees based on their workplace.

Usage
=====

To use this module, you need to:

1. Go to Time Off app > Configuration > Public Holidays
2. Create a Public Holiday without State with name *Local Holiday*
3. Create an *Employee 1* and set his Work Address in the same State
than your *Local Holiday*
4. Create an *Employee 2* and set his Work Address in other State than
your *Local Holiday*
5. Go to Time Off > Overview and check the grey background is only on
*Employee 1* on the days used in *Local Holiday*
6. Create a Time Off for *Employee 1* that involves *Local Holiday* and
check the days used are computed correctly
7. Create a Time Off for *Employee 2* that involves *Local Holiday* and
check the days used are computed correctly (more days than the Time
Off of *Employee 1*)
8. Check both Employee Calendar views from the Employee form to view the
grey background on Local and Public Holidays.

If you have installed Attendances and Contracts apps, and create
attenances for both employees on the *Local Holiday* you can also check
Extra Hours are computed correctly in both employees.

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_leaves_geographic%0Aversion:%2016.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
-------

* Moduon

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

- Eduardo de Miguel (`Moduon <https://www.moduon.team/>`__)

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.

.. |maintainer-Shide| image:: https://github.com/Shide.png?size=40px
:target: https://github.com/Shide
:alt: Shide
.. |maintainer-rafaelbn| image:: https://github.com/rafaelbn.png?size=40px
:target: https://github.com/rafaelbn
:alt: rafaelbn

Current `maintainers <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-Shide| |maintainer-rafaelbn|

This module is part of the `OCA/hr-holidays <https://github.com/OCA/hr-holidays/tree/16.0/resource_leaves_geographic>`_ 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_leaves_geographic/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
23 changes: 23 additions & 0 deletions resource_leaves_geographic/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2024 Moduon Team S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0)

{
"name": "Resource Leaves Geographic",
"summary": "Add geographic State to Resource Calendar Leaves",
"version": "16.0.1.0.0",
"development_status": "Alpha",
"category": "Human Resources/Time Off",
"website": "https://github.com/OCA/hr-holidays",
"author": "Moduon, Odoo Community Association (OCA)",
"maintainers": ["Shide", "rafaelbn"],
"license": "LGPL-3",
"application": False,
"installable": True,
"depends": [
"resource",
"hr_holidays",
],
"data": [
"views/resource_calendar_leaves_views.xml",
],
}
38 changes: 38 additions & 0 deletions resource_leaves_geographic/i18n/es.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * resource_leaves_geographic
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 16.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-07-31 07:50+0000\n"
"PO-Revision-Date: 2024-07-31 09:52+0200\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: es\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Generator: Poedit 3.4.4\n"

#. module: resource_leaves_geographic
#: model:ir.model,name:resource_leaves_geographic.model_resource_calendar_leaves
msgid "Resource Time Off Detail"
msgstr "Detalle de las ausencias del recurso"

#. module: resource_leaves_geographic
#: model:ir.model,name:resource_leaves_geographic.model_resource_calendar
msgid "Resource Working Time"
msgstr "Tiempo de Trabajo de Recursos"

#. module: resource_leaves_geographic
#: model:ir.model.fields,field_description:resource_leaves_geographic.field_resource_calendar_leaves__state_ids
msgid "States"
msgstr "Estados"

#. module: resource_leaves_geographic
#: model:ir.model.fields,help:resource_leaves_geographic.field_resource_calendar_leaves__state_ids
msgid "States for which this leave is applicable"
msgstr "Estados para los que se aplica esta ausencia"
36 changes: 36 additions & 0 deletions resource_leaves_geographic/i18n/resource_leaves_geographic.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * resource_leaves_geographic
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 16.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-07-31 07:50+0000\n"
"PO-Revision-Date: 2024-07-31 07:50+0000\n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"

#. module: resource_leaves_geographic
#: model:ir.model,name:resource_leaves_geographic.model_resource_calendar_leaves
msgid "Resource Time Off Detail"
msgstr ""

#. module: resource_leaves_geographic
#: model:ir.model,name:resource_leaves_geographic.model_resource_calendar
msgid "Resource Working Time"
msgstr ""

#. module: resource_leaves_geographic
#: model:ir.model.fields,field_description:resource_leaves_geographic.field_resource_calendar_leaves__state_ids
msgid "States"
msgstr ""

#. module: resource_leaves_geographic
#: model:ir.model.fields,help:resource_leaves_geographic.field_resource_calendar_leaves__state_ids
msgid "States for which this leave is applicable"
msgstr ""
3 changes: 3 additions & 0 deletions resource_leaves_geographic/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import resource_calendar_leaves
from . import resource_calendar
from . import hr_employee
51 changes: 51 additions & 0 deletions resource_leaves_geographic/models/hr_employee.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright 2024 Moduon Team S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0)

from datetime import datetime, time

from dateutil.rrule import DAILY, rrule
from pytz import UTC

from odoo import fields, models


class HrEmployee(models.Model):
_inherit = "hr.employee"

def _get_unusual_days(self, date_from, date_to=None):
"""Get the geographical unusual days belongs to employees"""
res = super()._get_unusual_days(date_from, date_to=date_to)

def dt_combiner(dt, tx):
"""Return a datetime object with the date converted to datetime
:param dt: date string
:param tx: time.min or time.max
"""
return datetime.combine(fields.Date.from_string(dt), tx).replace(tzinfo=UTC)

calendars = (
self.mapped("resource_calendar_id") or self.env.company.resource_calendar_id
)
state_interval_map = calendars._get_general_time_off_intervals_by_state(
domain=[
("state_ids", "in", self.mapped("address_id.state_id").ids),
("date_from", "<=", dt_combiner(date_to, time.max)),
("date_to", ">=", dt_combiner(date_from, time.min)),
],
any_calendar=False,
)
local_leave_days = set()
for state_interval_map in state_interval_map.values():
for start, end, _rcl in state_interval_map:
local_leave_days.update(
{
fields.Date.to_string(day.date())
for day in rrule(DAILY, start.date(), until=end.date())
}
)

for day in res.keys():
if day in local_leave_days:
res[day] = True
return res
82 changes: 82 additions & 0 deletions resource_leaves_geographic/models/resource_calendar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Copyright 2024 Moduon Team S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0)

from collections import defaultdict

from odoo import models

from odoo.addons.resource.models.resource import Intervals, make_aware


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

def _get_general_time_off_intervals_by_state(self, domain=None, any_calendar=False):
"""Get a map of State to Intervals of leaves for the given domain."""
domain = domain or []
# Respect the any_calendar option
if not any_calendar:
domain = [("calendar_id", "in", self.ids + [False])] + domain
# Ensure that the leaves are not related to a specific resource
domain = [("resource_id", "=", False)] + domain
# Iterate all matching leaves
state_interval_map = defaultdict(Intervals)
for res_cal_leave in self.env["resource.calendar.leaves"].search(domain):
dttf, _f = make_aware(res_cal_leave.date_from)
dttt, _f = make_aware(res_cal_leave.date_to)
# Add the interval to the corresponding State or False if no state
if res_cal_leave.state_ids:
for state in res_cal_leave.state_ids:
state_interval_map[state.id] |= Intervals(
[(dttf, dttt, res_cal_leave)]
)
else:
state_interval_map[False] |= Intervals([(dttf, dttt, res_cal_leave)])
return state_interval_map

def _leave_intervals_batch(
self, start_dt, end_dt, resources=None, domain=None, tz=None, any_calendar=False
):
# All interval batches (with/out State)
res = super()._leave_intervals_batch(
start_dt,
end_dt,
resources=resources,
domain=domain,
tz=tz,
any_calendar=any_calendar,
)
# Overwrite the result if resources are specified, but only with
# the global leaves (skipping the local leaves) using the domain
for resource in resources or []:
res.update(
super()._leave_intervals_batch(
start_dt,
end_dt,
resources=resource,
domain=[("state_ids", "=", False)], # Force global leaves
tz=tz,
any_calendar=any_calendar,
)
)
# Get Local interval batches by State
state_interval_map = self._get_general_time_off_intervals_by_state(
domain=domain, any_calendar=any_calendar
)
# Post-process generated intervals
for resource_id in res.keys():
if not resource_id:
# If no resource is specified, only consider Global leaves
res[resource_id] = state_interval_map[False]
else:
# If resource is specified, add Local Leaves to the result if
# the State of its Work Address has Local leaves
resource_state_id = (
self.env["hr.employee"]
.sudo()
.search([("resource_id", "=", resource_id)], limit=1)
.address_id.state_id.id
)
if resource_state_id in state_interval_map:
res[resource_id] |= state_interval_map[resource_state_id]
return res
14 changes: 14 additions & 0 deletions resource_leaves_geographic/models/resource_calendar_leaves.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2024 Moduon Team S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0)

from odoo import fields, models


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

state_ids = fields.Many2many(
comodel_name="res.country.state",
string="States",
help="States for which this leave is applicable",
)
3 changes: 3 additions & 0 deletions resource_leaves_geographic/readme/CONTEXT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This module was developed because in order to differentiate local Employee holidays, they must be added to their calendars instead of being able to mark them on the general company calendar.

This will be useful for you if you want to be able to create holidays by State in the company calendar and have them automatically applied to all employees based on their workplace.
Loading

0 comments on commit 1824c91

Please sign in to comment.