diff --git a/hr_holidays_summary_email/README.rst b/hr_holidays_summary_email/README.rst new file mode 100644 index 00000000..6a9da1da --- /dev/null +++ b/hr_holidays_summary_email/README.rst @@ -0,0 +1,97 @@ +====================== +Holidays Summary Email +====================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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/14.0/hr_holidays_summary_email + :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-14-0/hr-holidays-14-0-hr_holidays_summary_email + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/290/14.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module allows sending a daily/weekly email to employees with a summary +of the leaves on that period of other employees in the same company. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +For each employee, three options can be selected for the Leave Summary Email +configuration: + +- No: No email with the leaves summary is sent to the employee. +- Daily: A daily email with the leaves summary is sent to the employee. +- Weekly: A weekly email with the leaves summary is sent to the employee. + +For the weekly email, the setting `Leave Weekly Summary Day of Week` allows +setting the day of the week in which the email is sent. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* ForgeFlow + +Contributors +~~~~~~~~~~~~ + +* ForgeFlow + + * Jordi Masvidal + +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-JordiMForgeFlow| image:: https://github.com/JordiMForgeFlow.png?size=40px + :target: https://github.com/JordiMForgeFlow + :alt: JordiMForgeFlow + +Current `maintainer `__: + +|maintainer-JordiMForgeFlow| + +This module is part of the `OCA/hr-holidays `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/hr_holidays_summary_email/__init__.py b/hr_holidays_summary_email/__init__.py new file mode 100644 index 00000000..0650744f --- /dev/null +++ b/hr_holidays_summary_email/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/hr_holidays_summary_email/__manifest__.py b/hr_holidays_summary_email/__manifest__.py new file mode 100644 index 00000000..676dcd2f --- /dev/null +++ b/hr_holidays_summary_email/__manifest__.py @@ -0,0 +1,24 @@ +# Copyright 2023 ForgeFlow S.L. +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + "name": "Holidays Summary Email", + "summary": """ + Notify employees with daily or weekly leaves summaries of their company. + """, + "version": "14.0.1.0.0", + "category": "Human Resources", + "website": "https://github.com/OCA/hr-holidays", + "author": "ForgeFlow, Odoo Community Association (OCA)", + "license": "AGPL-3", + "installable": True, + "depends": ["hr_holidays", "hr_holidays_settings"], + "data": [ + "data/ir_cron.xml", + "data/mail_template_data.xml", + "views/hr_employee_views.xml", + "views/res_users_views.xml", + "views/res_config_settings.xml", + ], + "maintainers": ["JordiMForgeFlow"], +} diff --git a/hr_holidays_summary_email/data/ir_cron.xml b/hr_holidays_summary_email/data/ir_cron.xml new file mode 100644 index 00000000..8b17eb4e --- /dev/null +++ b/hr_holidays_summary_email/data/ir_cron.xml @@ -0,0 +1,15 @@ + + + + Leaves Summary Email + + + 1 + days + -1 + + + code + model._cron_send_hr_leave_summary_emails() + + diff --git a/hr_holidays_summary_email/data/mail_template_data.xml b/hr_holidays_summary_email/data/mail_template_data.xml new file mode 100644 index 00000000..72840431 --- /dev/null +++ b/hr_holidays_summary_email/data/mail_template_data.xml @@ -0,0 +1,109 @@ + + + + + Leaves Summary: Daily + + Daily Leaves Summary + ${object.work_email | safe} + ${object.company_id.partner_id.email_formatted | safe} + +
+

Dear ${object.name},

+
+

This is today's leave summary:

+
+ + + + + + + + % set timeoffs = ctx.get('time_offs', False) + % for data in timeoffs + % set d_from = data.format_hr_leave_summary_date() + % set d_to = data.format_hr_leave_summary_date(False) + + + + + + + % endfor +
EmployeeTime OffFromTo
${data.employee_id.name}${data.name or ''}${d_from}${d_to}
+
+
+ ${object.lang} + +
+ + Leaves Summary: Weekly + + Weekly Leaves Summary + ${object.work_email | safe} + ${object.company_id.partner_id.email_formatted | safe} + +
+

Dear ${object.name},

+
+

This is the leaves summary for this week:

+
+ + + + + + + + % set timeoffs = ctx.get('time_offs', False) + % for data in timeoffs + % set d_from = data.format_hr_leave_summary_date() + % set d_to = data.format_hr_leave_summary_date(False) + + + + + + + % endfor +
EmployeeTime OffFromTo
${data.employee_id.name}${data.name or ''}${d_from}${d_to}
+
+
+ ${object.lang} + +
+
+
diff --git a/hr_holidays_summary_email/models/__init__.py b/hr_holidays_summary_email/models/__init__.py new file mode 100644 index 00000000..74ec9fc5 --- /dev/null +++ b/hr_holidays_summary_email/models/__init__.py @@ -0,0 +1,5 @@ +from . import hr_leave +from . import hr_employee +from . import res_company +from . import res_config_settings +from . import res_users diff --git a/hr_holidays_summary_email/models/hr_employee.py b/hr_holidays_summary_email/models/hr_employee.py new file mode 100644 index 00000000..58a23a75 --- /dev/null +++ b/hr_holidays_summary_email/models/hr_employee.py @@ -0,0 +1,18 @@ +# Copyright 2023 ForgeFlow S.L. +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo import fields, models + + +class HrEmployeeBase(models.AbstractModel): + _inherit = "hr.employee.base" + + hr_leave_summary_type = fields.Selection( + selection=[("no", "No"), ("daily", "Daily"), ("weekly", "Weekly")], + string="Leave Summary Email", + required=True, + default="no", + ) + last_hr_leave_summary_sent = fields.Date( + string="Last Leave Summary Email Sent", + readonly=True, + ) diff --git a/hr_holidays_summary_email/models/hr_leave.py b/hr_holidays_summary_email/models/hr_leave.py new file mode 100644 index 00000000..74bfa7b6 --- /dev/null +++ b/hr_holidays_summary_email/models/hr_leave.py @@ -0,0 +1,137 @@ +# Copyright 2023 ForgeFlow S.L. +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from dateutil.relativedelta import relativedelta + +from odoo import api, fields, models +from odoo.tools import DEFAULT_SERVER_DATE_FORMAT + + +class HrLeave(models.Model): + _inherit = "hr.leave" + + @api.model + def _get_hr_leave_summary_mail_template(self, summary_type): + return self.env.ref( + "hr_holidays_summary_email.hr_holidays_summary_mail_template_%s" + % summary_type, + raise_if_not_found=False, + ) + + @api.model + def _get_hr_leave_summary_employee_domain(self, summary_type, company_id): + return [ + ("hr_leave_summary_type", "=", summary_type), + ("last_hr_leave_summary_sent", "!=", fields.Date.today()), + ("company_id", "=", company_id), + ] + + @api.model + def _get_hr_leave_summary_base_domain(self, company_id): + return [ + ("state", "=", "validate"), + ("employee_id.company_id", "=", company_id), + ] + + @api.model + def _get_hr_leave_summary_daily_domain(self, company_id): + res = self._get_hr_leave_summary_base_domain(company_id) + res += [ + ("date_from", "<=", fields.Date.today()), + ("date_to", ">=", fields.Date.today()), + ] + return res + + @api.model + def _get_hr_leave_summary_weekly_domain(self, company_id): + res = self._get_hr_leave_summary_base_domain(company_id) + date_from = fields.Date.today() + date_to = date_from + relativedelta(days=6) + res += [ + ("date_from", "<=", date_to), + ("date_to", ">=", date_from), + ] + return res + + @api.model + def _cron_send_hr_leave_summary_emails_daily(self, employees_to_notify, company): + domain = self._get_hr_leave_summary_daily_domain(company.id) + today_time_offs = self.env["hr.leave"].sudo().search(domain) + template = self._get_hr_leave_summary_mail_template("daily") + if not template: + return + for employee in employees_to_notify: + template.with_context(time_offs=today_time_offs).send_mail( + employee.id, force_send=False + ) + employees_to_notify.write({"last_hr_leave_summary_sent": fields.Date.today()}) + + @api.model + def _cron_send_hr_leave_summary_emails_weekly(self, employees_to_notify, company): + if str(fields.Date.today().weekday()) != company.hr_holidays_summary_weekly_dow: + return + domain = self._get_hr_leave_summary_weekly_domain(company.id) + today_time_offs = self.env["hr.leave"].sudo().search(domain) + template = self._get_hr_leave_summary_mail_template("weekly") + if not template: + return + for employee in employees_to_notify: + template.with_context(time_offs=today_time_offs).send_mail( + employee.id, force_send=False + ) + employees_to_notify.write({"last_hr_leave_summary_sent": fields.Date.today()}) + + @api.model + def _cron_send_hr_leave_summary_emails(self): + summary_types = self.env["hr.employee"].fields_get(["hr_leave_summary_type"])[ + "hr_leave_summary_type" + ]["selection"] + for company in self.env["res.company"].search([]): + for stype_tuple in summary_types: + stype = stype_tuple[0] + employee_domain = self._get_hr_leave_summary_employee_domain( + stype, company.id + ) + employees_to_notify = self.env["hr.employee"].search(employee_domain) + if employees_to_notify and hasattr( + self, "_cron_send_hr_leave_summary_emails_%s" % stype + ): + getattr(self, "_cron_send_hr_leave_summary_emails_%s" % stype)( + employees_to_notify, company + ) + + def format_hr_leave_summary_date(self, date_from=True): + self.ensure_one() + if date_from: + res = self.date_from.strftime(DEFAULT_SERVER_DATE_FORMAT) + else: + res = self.date_to.strftime(DEFAULT_SERVER_DATE_FORMAT) + if self.request_unit_half: + res += ( + " %s" + % dict( + self.env["hr.leave"].fields_get(["request_date_from_period"])[ + "request_date_from_period" + ]["selection"] + )[self.request_date_from_period] + ) + elif self.request_unit_hours: + if date_from: + res += ( + " %s" + % dict( + self.env["hr.leave"].fields_get(["request_hour_from"])[ + "request_hour_from" + ]["selection"] + )[self.request_hour_from] + ) + else: + res += ( + " %s" + % dict( + self.env["hr.leave"].fields_get(["request_hour_to"])[ + "request_hour_to" + ]["selection"] + )[self.request_hour_to] + ) + return res diff --git a/hr_holidays_summary_email/models/res_company.py b/hr_holidays_summary_email/models/res_company.py new file mode 100644 index 00000000..03b36d56 --- /dev/null +++ b/hr_holidays_summary_email/models/res_company.py @@ -0,0 +1,23 @@ +# Copyright 2023 ForgeFlow S.L. +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class ResCompany(models.Model): + _inherit = "res.company" + + hr_holidays_summary_weekly_dow = fields.Selection( + selection=[ + ("0", "Monday"), + ("1", "Tuesday"), + ("2", "Wednesday"), + ("3", "Thursday"), + ("4", "Friday"), + ("5", "Saturday"), + ("6", "Sunday"), + ], + string="Leave Weekly Summary Day of Week", + default="0", + required=True, + ) diff --git a/hr_holidays_summary_email/models/res_config_settings.py b/hr_holidays_summary_email/models/res_config_settings.py new file mode 100644 index 00000000..9d914966 --- /dev/null +++ b/hr_holidays_summary_email/models/res_config_settings.py @@ -0,0 +1,14 @@ +# Copyright 2023 ForgeFlow S.L. +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class ResConfigSettings(models.TransientModel): + _inherit = "res.config.settings" + + hr_holidays_summary_weekly_dow = fields.Selection( + related="company_id.hr_holidays_summary_weekly_dow", + readonly=False, + required=True, + ) diff --git a/hr_holidays_summary_email/models/res_users.py b/hr_holidays_summary_email/models/res_users.py new file mode 100644 index 00000000..f44c4998 --- /dev/null +++ b/hr_holidays_summary_email/models/res_users.py @@ -0,0 +1,11 @@ +# Copyright 2023 ForgeFlow S.L. +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo import fields, models + + +class ResUser(models.Model): + _inherit = "res.users" + + hr_leave_summary_type = fields.Selection( + related="employee_id.hr_leave_summary_type", readonly=False, related_sudo=False + ) diff --git a/hr_holidays_summary_email/readme/CONTRIBUTORS.rst b/hr_holidays_summary_email/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000..bbe976d7 --- /dev/null +++ b/hr_holidays_summary_email/readme/CONTRIBUTORS.rst @@ -0,0 +1,3 @@ +* ForgeFlow + + * Jordi Masvidal diff --git a/hr_holidays_summary_email/readme/DESCRIPTION.rst b/hr_holidays_summary_email/readme/DESCRIPTION.rst new file mode 100644 index 00000000..7230f841 --- /dev/null +++ b/hr_holidays_summary_email/readme/DESCRIPTION.rst @@ -0,0 +1,2 @@ +This module allows sending a daily/weekly email to employees with a summary +of the leaves on that period of other employees in the same company. diff --git a/hr_holidays_summary_email/readme/USAGE.rst b/hr_holidays_summary_email/readme/USAGE.rst new file mode 100644 index 00000000..f2dea967 --- /dev/null +++ b/hr_holidays_summary_email/readme/USAGE.rst @@ -0,0 +1,9 @@ +For each employee, three options can be selected for the Leave Summary Email +configuration: + +- No: No email with the leaves summary is sent to the employee. +- Daily: A daily email with the leaves summary is sent to the employee. +- Weekly: A weekly email with the leaves summary is sent to the employee. + +For the weekly email, the setting `Leave Weekly Summary Day of Week` allows +setting the day of the week in which the email is sent. diff --git a/hr_holidays_summary_email/static/description/icon.png b/hr_holidays_summary_email/static/description/icon.png new file mode 100644 index 00000000..3a0328b5 Binary files /dev/null and b/hr_holidays_summary_email/static/description/icon.png differ diff --git a/hr_holidays_summary_email/static/description/index.html b/hr_holidays_summary_email/static/description/index.html new file mode 100644 index 00000000..e900281b --- /dev/null +++ b/hr_holidays_summary_email/static/description/index.html @@ -0,0 +1,442 @@ + + + + + + +Holidays natural period + + + +
+

Holidays natural period

+ + +

Beta License: AGPL-3 OCA/hr-holidays Translate me on Weblate Try me on Runbot

+

This module was written to define natural day option in request unit on holidays type.

+

Table of contents

+ +
+

Usage

+

For using natural period on leaves:

+
    +
  1. Go to Leaves > Dashboard.
  2. +
  3. Select dragging on the calendar the days you want to be on leave, or go +to the form view for selecting start and end dates.
  4. +
  5. Select the proper “Leave Type” that has “Natural day” selected in “Request unit”.
  6. +
  7. If no leave type is yet specified, then default configuration is to exclude +public holidays.
  8. +
  9. The number of days will be computed without employee calendar used.
  10. +
+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +feedback.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • Tecnativa
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

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.

+

Current maintainer:

+

victoralmau

+

This module is part of the OCA/hr-holidays project on GitHub.

+

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

+
+
+
+ + diff --git a/hr_holidays_summary_email/tests/__init__.py b/hr_holidays_summary_email/tests/__init__.py new file mode 100644 index 00000000..dfe66941 --- /dev/null +++ b/hr_holidays_summary_email/tests/__init__.py @@ -0,0 +1 @@ +from . import test_hr_leave_summary_email diff --git a/hr_holidays_summary_email/tests/test_hr_leave_summary_email.py b/hr_holidays_summary_email/tests/test_hr_leave_summary_email.py new file mode 100644 index 00000000..68546be8 --- /dev/null +++ b/hr_holidays_summary_email/tests/test_hr_leave_summary_email.py @@ -0,0 +1,101 @@ +# Copyright 2023 ForgeFlow S.L. +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from datetime import date, datetime + +from dateutil.relativedelta import relativedelta + +from odoo.tests import common + + +class TestHrLeaveSummaryEmail(common.TransactionCase): + def setUp(self): + super().setUp() + self.HrLeave = self.env["hr.leave"] + leave_type = self.env.ref("hr_holidays.hr_holiday_status_dv") + # Add allocation days for the test + leave_type.write({"allocation_type": "no"}) + calendar = self.env.ref("resource.resource_calendar_std") + partner = self.env["res.partner"].create( + { + "name": "Test employee", + "type": "private", + "country_id": self.env.ref("base.es").id, + } + ) + self.employee = self.env["hr.employee"].create( + { + "name": "Test employee", + "address_home_id": partner.id, + "resource_calendar_id": calendar.id, + } + ) + # Today leave + self.leave_1 = ( + self.env["hr.leave"] + .with_context(leave_skip_state_check=True) + .create( + { + "name": "Test 1", + "employee_id": self.employee.id, + "holiday_status_id": leave_type.id, + "date_from": datetime.today(), + "date_to": (datetime.today() + relativedelta(days=1)), + "number_of_days": 1, + "state": "validate", + } + ) + ) + + # Week leave + self.leave_2 = ( + self.env["hr.leave"] + .with_context(leave_skip_state_check=True) + .create( + { + "name": "Test 2", + "employee_id": self.employee.id, + "holiday_status_id": leave_type.id, + "date_from": (datetime.today() + relativedelta(days=3)), + "date_to": (datetime.today() + relativedelta(days=5)), + "number_of_days": 2, + "state": "validate", + } + ) + ) + + def test_hr_leave_summary_no(self): + """ + No email sent with the default setting in the employee + """ + self.HrLeave._cron_send_hr_leave_summary_emails() + self.assertFalse(self.employee.last_hr_leave_summary_sent) + + def test_hr_leave_summary_daily(self): + """ + Today's active leave is part of the daily email, but the other not. + """ + domain = self.HrLeave._get_hr_leave_summary_daily_domain(self.env.company.id) + today_time_offs = self.HrLeave.search(domain) + self.assertIn(self.leave_1, today_time_offs) + self.assertNotIn(self.leave_2, today_time_offs) + self.employee.write({"hr_leave_summary_type": "daily"}) + self.HrLeave._cron_send_hr_leave_summary_emails() + self.assertEqual(self.employee.last_hr_leave_summary_sent, date.today()) + + def test_hr_leave_summary_weekly(self): + """ + Today's active leave is part of the daily email, as well as the other + in the same week. + """ + domain = self.HrLeave._get_hr_leave_summary_weekly_domain(self.env.company.id) + today_time_offs = self.HrLeave.search(domain) + self.assertIn(self.leave_1, today_time_offs) + self.assertIn(self.leave_2, today_time_offs) + self.employee.write( + {"hr_leave_summary_type": "weekly", "last_hr_leave_summary_sent": False} + ) + self.env.company.write( + {"hr_holidays_summary_weekly_dow": str(date.today().weekday())} + ) + self.HrLeave._cron_send_hr_leave_summary_emails() + self.assertEqual(self.employee.last_hr_leave_summary_sent, date.today()) diff --git a/hr_holidays_summary_email/views/hr_employee_views.xml b/hr_holidays_summary_email/views/hr_employee_views.xml new file mode 100644 index 00000000..67f6f2fb --- /dev/null +++ b/hr_holidays_summary_email/views/hr_employee_views.xml @@ -0,0 +1,27 @@ + + + + + hr.employee.view.form - hr_holidays_summary_email + hr.employee + + + + + + + + + hr.employee.public.form - hr_holidays_summary_email + hr.employee.public + + + + + + + + diff --git a/hr_holidays_summary_email/views/res_config_settings.xml b/hr_holidays_summary_email/views/res_config_settings.xml new file mode 100644 index 00000000..e008486f --- /dev/null +++ b/hr_holidays_summary_email/views/res_config_settings.xml @@ -0,0 +1,43 @@ + + + + + res.config.settings.form - hr_holidays_summary_email + res.config.settings + + + +
+
+
+ Weekly Leaves Summary +
+ Day of week when the weekly leaves summary is sent. +
+
+
+
+
+
+
+
+
+
diff --git a/hr_holidays_summary_email/views/res_users_views.xml b/hr_holidays_summary_email/views/res_users_views.xml new file mode 100644 index 00000000..979037a7 --- /dev/null +++ b/hr_holidays_summary_email/views/res_users_views.xml @@ -0,0 +1,18 @@ + + + + res.users.preferences.form.inherit - hr_holidays_summary_email + res.users + + + + + + + + diff --git a/setup/hr_holidays_summary_email/odoo/addons/hr_holidays_summary_email b/setup/hr_holidays_summary_email/odoo/addons/hr_holidays_summary_email new file mode 120000 index 00000000..b9afd1aa --- /dev/null +++ b/setup/hr_holidays_summary_email/odoo/addons/hr_holidays_summary_email @@ -0,0 +1 @@ +../../../../hr_holidays_summary_email \ No newline at end of file diff --git a/setup/hr_holidays_summary_email/setup.py b/setup/hr_holidays_summary_email/setup.py new file mode 100644 index 00000000..28c57bb6 --- /dev/null +++ b/setup/hr_holidays_summary_email/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)