Skip to content

Commit

Permalink
Merge PR #142 into 18.0
Browse files Browse the repository at this point in the history
Signed-off-by pedrobaeza
  • Loading branch information
OCA-git-bot committed Feb 13, 2025
2 parents 05c1e3a + d54b079 commit f04ea38
Show file tree
Hide file tree
Showing 23 changed files with 1,388 additions and 0 deletions.
96 changes: 96 additions & 0 deletions calendar_public_holiday/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
========================
Calendar Holidays Public
========================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:323fe101af3ba04c9322fb644c273896c1fb66484ed662a3ca0984279d457570
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |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%2Fcalendar-lightgray.png?logo=github
:target: https://github.com/OCA/calendar/tree/18.0/calendar_public_holiday
:alt: OCA/calendar
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/calendar-18-0/calendar-18-0-calendar_public_holiday
: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/calendar&target_branch=18.0
:alt: Try me on Runboat

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

This module handles public holidays.

**Table of contents**

.. contents::
:local:

Usage
=====

For adding public holidays:

1. Go to the menu *Calendar > Public Holidays > Public Holidays*.
2. Create your public holidays.

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

Bugs are tracked on `GitHub Issues <https://github.com/OCA/calendar/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/calendar/issues/new?body=module:%20calendar_public_holiday%0Aversion:%2018.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
-------

* Michael Telahun Makonnen
* Tecnativa
* Fekete Mihai (Forest and Biomass Services Romania)
* Druidoo
* Camptocamp
*

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

- [Trobz](https://trobz.com):

- Do Anh Duy <<[email protected]>>

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

The creation of this module was financially supported by Camptocamp

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/calendar <https://github.com/OCA/calendar/tree/18.0/calendar_public_holiday>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
5 changes: 5 additions & 0 deletions calendar_public_holiday/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from . import models
from . import wizards
from .hooks import pre_init_hook
30 changes: 30 additions & 0 deletions calendar_public_holiday/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright 2015 2011,2013 Michael Telahun Makonnen <[email protected]>
# Copyright 2020 InitOS Gmbh
# Copyright 2024 Camptocamp
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

{
"name": "Calendar Holidays Public",
"summary": """
Manage Public Holidays
""",
"version": "18.0.1.0.0",
"license": "AGPL-3",
"category": "HR/Calendar",
"author": "Michael Telahun Makonnen, "
"Tecnativa, "
"Fekete Mihai (Forest and Biomass Services Romania), "
"Druidoo, "
"Odoo Community Association (OCA), "
"Camptocamp,",
"website": "https://github.com/OCA/calendar",
"depends": ["calendar"],
"external_dependencies": {"python": ["openupgradelib"]},
"data": [
"data/data.xml",
"security/ir.model.access.csv",
"views/calendar_public_holiday_view.xml",
"wizards/calendar_public_holiday_next_year_wizard.xml",
],
"pre_init_hook": "pre_init_hook",
}
8 changes: 8 additions & 0 deletions calendar_public_holiday/data/data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8" ?>
<!--Copyright 2024 Camptocamp
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).-->
<odoo>
<record id="event_type_holiday" model="calendar.event.type">
<field name="name">Public Holidays</field>
</record>
</odoo>
55 changes: 55 additions & 0 deletions calendar_public_holiday/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright 2024 Camptocamp
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from openupgradelib import openupgrade


def migrate_rename_xmlid_event_type_holiday(env):
if not openupgrade.is_module_installed(env.cr, "hr_holidays_public"):
return
xmlid_renames = [
(
"hr_holidays_public.event_type_holiday",
"calendar_public_holiday.event_type_holiday",
),
]
openupgrade.rename_xmlids(env.cr, xmlid_renames)


def migrate_rename_field_model_hr_holidays_public_line(env):
field_renames = [
(
"hr.holidays.public.line",
"hr_holidays_public_line",
"year_id",
"public_holiday_id",
),
]
openupgrade.rename_fields(env, field_renames, no_deep=True)


def migrate_rename_model_hr_holidays_public_line(env):
if not openupgrade.table_exists(env.cr, "hr_holidays_public_line"):
return
model_renames = [("hr.holidays.public.line", "calendar.public.holiday.line")]
openupgrade.rename_models(env.cr, model_renames)
tables_renames = [("hr_holidays_public_line", "calendar_public_holiday_line")]
openupgrade.rename_tables(env.cr, tables_renames)


def migrate_rename_model_hr_holidays_public(env):
if not openupgrade.table_exists(env.cr, "hr_holidays_public"):
return
model_renames = [
("hr.holidays.public", "calendar.public.holiday"),
]
openupgrade.rename_models(env.cr, model_renames)
tables_renames = [("hr_holidays_public", "calendar_public_holiday")]
openupgrade.rename_tables(env.cr, tables_renames)


def pre_init_hook(env):
migrate_rename_xmlid_event_type_holiday(env)
migrate_rename_field_model_hr_holidays_public_line(env)
migrate_rename_model_hr_holidays_public_line(env)
migrate_rename_model_hr_holidays_public(env)
4 changes: 4 additions & 0 deletions calendar_public_holiday/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from . import calendar_public_holiday
from . import calendar_public_holiday_line
128 changes: 128 additions & 0 deletions calendar_public_holiday/models/calendar_public_holiday.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# Copyright 2015 2011,2013 Michael Telahun Makonnen <[email protected]>
# Copyright 2020 InitOS Gmbh
# Copyright 2024 Camptocamp
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import datetime

from odoo import api, fields, models
from odoo.exceptions import ValidationError


class ResourceCalendarPublicHoliday(models.Model):
_name = "calendar.public.holiday"
_description = "Calendar Public Holiday"
_rec_name = "year"
_order = "year desc"

year = fields.Integer(
"Calendar Year",
required=True,
default=lambda self: fields.Date.context_today(self).year,
)
line_ids = fields.One2many(
"calendar.public.holiday.line",
"public_holiday_id",
"Holiday Dates",
)
country_id = fields.Many2one("res.country", "Country")

@api.constrains("year", "country_id")
def _check_year(self):
for line in self:
line._check_year_one()

def _check_year_one(self):
if self.search_count(
[
("year", "=", self.year),
("country_id", "=", self.country_id.id),
("id", "!=", self.id),
]
):
raise ValidationError(
self.env._(
"You can't create duplicate public holiday per year and/or"
" country"
)
)
return True

@api.depends("country_id")
def _compute_display_name(self):
for line in self:
if line.country_id:
line.display_name = f"{line.year} ({line.country_id.name})"
else:
line.display_name = line.year

def _get_domain_states_filter(self, pholidays, start_dt, end_dt, partner_id=None):
partner = self.env["res.partner"].browse(partner_id)
states_filter = [
("public_holiday_id", "in", pholidays.ids),
("date", ">=", start_dt),
("date", "<=", end_dt),
]
if partner and partner.state_id:
states_filter.extend(
[
"|",
("state_ids", "in", partner.state_id.ids),
("state_ids", "=", False),
]
)
else:
states_filter.append(("state_ids", "=", False))
return states_filter

@api.model
@api.returns("calendar.public.holiday.line")
def get_holidays_list(self, year=None, start_dt=None, end_dt=None, partner_id=None):
"""Returns recordset of calendar.public.holiday.line
for the specified year and employee
:param year: year as string (optional if start_dt and end_dt defined)
:param start_dt: start_dt as date
:param end_dt: end_dt as date
:param partner_id: ID of the partner
:return: recordset of calendar.public.holiday.line
"""
partner = self.env["res.partner"].browse(partner_id)
if not start_dt and not end_dt:
start_dt = datetime.date(year, 1, 1)
end_dt = datetime.date(year, 12, 31)
years = list(range(start_dt.year, end_dt.year + 1))
holidays_filter = [("year", "in", years)]
if partner:
if partner.country_id:
holidays_filter.append(
("country_id", "in", (False, partner.country_id.id))
)
else:
holidays_filter.append(("country_id", "=", False))
public_holidays = self.search(holidays_filter)
public_holiday_line = self.env["calendar.public.holiday.line"]
if not public_holidays:
return public_holiday_line
states_filter = self._get_domain_states_filter(
public_holidays, start_dt, end_dt, partner_id=partner.id
)
return public_holiday_line.search(states_filter)

@api.model
def is_public_holiday(self, selected_date, partner_id=None):
"""
Returns True if selected_date is a public holiday for the employee
:param selected_date: datetime object
:param partner_id: ID of the partner
:return: bool
"""
partner = self.env["res.partner"].browse(partner_id)
partner_id = partner.id if partner else None
holidays_lines = self.get_holidays_list(
year=selected_date.year, partner_id=partner_id
)
if holidays_lines:
hol_date = holidays_lines.filtered(lambda r: r.date == selected_date)
if hol_date:
return True
return False
Loading

0 comments on commit f04ea38

Please sign in to comment.