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

[18.0][ADD] calendar_public_holiday #142

Draft
wants to merge 2 commits into
base: 18.0
Choose a base branch
from
Draft
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
91 changes: 91 additions & 0 deletions calendar_public_holiday/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
========================
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
-------

* Camptocamp

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

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

- Do Anh Duy <<duyda@trobz.com>>

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
25 changes: 25 additions & 0 deletions calendar_public_holiday/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2015 2011,2013 Michael Telahun Makonnen <mmakonnen@gmail.com>
# 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": "Camptocamp," "Odoo Community Association (OCA)",
"website": "https://github.com/OCA/calendar",
"depends": ["calendar"],
"data": [
"data/data.xml",
"security/ir.model.access.csv",
"views/calendar_public_holiday_view.xml",
"wizards/calendar_public_holiday_next_year_wizard.xml",
],
"external_dependencies": {"python": ["openupgradelib"]},
"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">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 = [

Check warning on line 10 in calendar_public_holiday/hooks.py

View check run for this annotation

Codecov / codecov/patch

calendar_public_holiday/hooks.py#L10

Added line #L10 was not covered by tests
(
"hr_holidays_public.event_type_holiday",
"calendar_public_holiday.event_type_holiday",
),
]
openupgrade.rename_xmlids(env.cr, xmlid_renames)

Check warning on line 16 in calendar_public_holiday/hooks.py

View check run for this annotation

Codecov / codecov/patch

calendar_public_holiday/hooks.py#L16

Added line #L16 was not covered by tests


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)

Check warning on line 37 in calendar_public_holiday/hooks.py

View check run for this annotation

Codecov / codecov/patch

calendar_public_holiday/hooks.py#L34-L37

Added lines #L34 - L37 were not covered by tests


def migrate_rename_model_hr_holidays_public(env):
if not openupgrade.table_exists(env.cr, "hr_holidays_public"):
return
model_renames = [

Check warning on line 43 in calendar_public_holiday/hooks.py

View check run for this annotation

Codecov / codecov/patch

calendar_public_holiday/hooks.py#L43

Added line #L43 was not covered by tests
("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)

Check warning on line 48 in calendar_public_holiday/hooks.py

View check run for this annotation

Codecov / codecov/patch

calendar_public_holiday/hooks.py#L46-L48

Added lines #L46 - L48 were not covered by tests


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
126 changes: 126 additions & 0 deletions calendar_public_holiday/models/calendar_public_holiday.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# Copyright 2015 2011,2013 Michael Telahun Makonnen <mmakonnen@gmail.com>
# 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"

year = fields.Integer(
"Calendar Year", required=True, default=fields.Date.today().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(

Check warning on line 65 in calendar_public_holiday/models/calendar_public_holiday.py

View check run for this annotation

Codecov / codecov/patch

calendar_public_holiday/models/calendar_public_holiday.py#L65

Added line #L65 was not covered by tests
[
"|",
("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))

Check warning on line 99 in calendar_public_holiday/models/calendar_public_holiday.py

View check run for this annotation

Codecov / codecov/patch

calendar_public_holiday/models/calendar_public_holiday.py#L99

Added line #L99 was not covered by tests
public_holidays = self.search(holidays_filter)
public_holiday_line = self.env["calendar.public.holiday.line"]
if not public_holidays:
return public_holiday_line

Check warning on line 103 in calendar_public_holiday/models/calendar_public_holiday.py

View check run for this annotation

Codecov / codecov/patch

calendar_public_holiday/models/calendar_public_holiday.py#L103

Added line #L103 was not covered by tests
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
Loading