diff --git a/hr_attendance_break/README.rst b/hr_attendance_break/README.rst new file mode 100644 index 00000000..acb066ee --- /dev/null +++ b/hr_attendance_break/README.rst @@ -0,0 +1,114 @@ +=========== +Work breaks +=========== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:8e846363bbf23a871d39db16370bbd9be190261df6cb79caa6a982eaba0f7c77 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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--attendance-lightgray.png?logo=github + :target: https://github.com/OCA/hr-attendance/tree/15.0/hr_attendance_break + :alt: OCA/hr-attendance +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/hr-attendance-15-0/hr-attendance-15-0-hr_attendance_break + :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-attendance&target_branch=15.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module lets employees take breaks on the attendance screen: + +.. figure:: https://raw.githubusercontent.com/OCA/hr-attendance/15.0/hr_attendance_break/static/description/hr_attendance_break.png + +This allows them to check in at the beginning of the working day, check out at the end and record breaks in between. + +To be sure employees take enough breaks, there's also flagging for employees who didn't take enough breaks. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +To configure this module, you need to: + +- Go to Attendances/Configuration/Settings, sections Breaks +- Fill in the minimum length of a break to be counted +- If you want to enforce a minimum amount of break taken for a certain amount of hours worked per day, click ``Edit thresholds for mandatory breaks`` +- Review the server action linked there, you can ie disable imposing mandatory breaks, or rewrite the mail template informing about this + +Usage +===== + +To use this module, you need to: + +#. Go to Attendances/Attendances +#. Click Show/Edit breaks + +or + +#. Go to your check in screen +#. After checking in, click the coffee mug for starting a break, and click the gears for ending it + +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 to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Hunki Enterprises BV +* verdigado eG + +Contributors +~~~~~~~~~~~~ + +* Holger Brunn (https://hunki-enterprises.com) + +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-hbrunn| image:: https://github.com/hbrunn.png?size=40px + :target: https://github.com/hbrunn + :alt: hbrunn + +Current `maintainer `__: + +|maintainer-hbrunn| + +This module is part of the `OCA/hr-attendance `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/hr_attendance_break/__init__.py b/hr_attendance_break/__init__.py new file mode 100644 index 00000000..0650744f --- /dev/null +++ b/hr_attendance_break/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/hr_attendance_break/__manifest__.py b/hr_attendance_break/__manifest__.py new file mode 100644 index 00000000..c22b59cc --- /dev/null +++ b/hr_attendance_break/__manifest__.py @@ -0,0 +1,45 @@ +# Copyright 2023 Hunki Enterprises BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0) + +{ + "name": "Work breaks", + "summary": "Allows employees to manage their work breaks", + "version": "15.0.1.0.0", + "development_status": "Beta", + "category": "Human Resources/Attendances", + "website": "https://github.com/OCA/hr-attendance", + "author": "Hunki Enterprises BV, Odoo Community Association (OCA), verdigado eG", + "maintainers": ["hbrunn"], + "license": "AGPL-3", + "depends": [ + "hr_attendance", + "hr_attendance_reason", + ], + "data": [ + "security/hr_attendance_break.xml", + "security/ir.model.access.csv", + "data/hr_attendance_reason.xml", + "data/mail_activity_type.xml", + "data/mail_template.xml", + "data/ir_actions_server.xml", + "data/ir_cron.xml", + "views/hr_attendance.xml", + "views/hr_attendance_break.xml", + "views/hr_attendance_break_threshold.xml", + "views/hr_attendance_reason.xml", + "views/hr_attendance_report.xml", + "views/res_config_settings.xml", + ], + "demo": [ + "demo/res_company.xml", + ], + "assets": { + "web.assets_backend": [ + "hr_attendance_break/static/src/js/hr_attendance_break.js", + "hr_attendance_break/static/src/scss/hr_attendance_break.scss", + ], + "web.assets_qweb": [ + "hr_attendance_break/static/src/xml/hr_attendance_break.xml", + ], + }, +} diff --git a/hr_attendance_break/data/hr_attendance_reason.xml b/hr_attendance_break/data/hr_attendance_reason.xml new file mode 100644 index 00000000..a851987a --- /dev/null +++ b/hr_attendance_break/data/hr_attendance_reason.xml @@ -0,0 +1,11 @@ + + + + + Imposed break + break + + + + diff --git a/hr_attendance_break/data/ir_actions_server.xml b/hr_attendance_break/data/ir_actions_server.xml new file mode 100644 index 00000000..b6d51baf --- /dev/null +++ b/hr_attendance_break/data/ir_actions_server.xml @@ -0,0 +1,39 @@ + + + + + Flag mandatory breaks + next_activity + + + generic + user_id + You didn't record enough break time, please adjust your time record + + + Impose mandatory breaks + code + + env.context.get('hr_attendance_break_attendances', model)._impose_break(env.context.get('hr_attendance_break_threshold', env['hr.attendance.break.threshold']).min_break - env.context.get('hr_attendance_break_hours', 0)) + + + Mail about mandatory breaks + email + + + + + Take action if employee didn't take mandatory break + multi + + + + diff --git a/hr_attendance_break/data/ir_cron.xml b/hr_attendance_break/data/ir_cron.xml new file mode 100644 index 00000000..d436ea1a --- /dev/null +++ b/hr_attendance_break/data/ir_cron.xml @@ -0,0 +1,13 @@ + + + + + Flag mandatory breaks + + days + 1 + -1 + model.search([])._check_mandatory_break_yesterday() + + diff --git a/hr_attendance_break/data/mail_activity_type.xml b/hr_attendance_break/data/mail_activity_type.xml new file mode 100644 index 00000000..2f03ecf6 --- /dev/null +++ b/hr_attendance_break/data/mail_activity_type.xml @@ -0,0 +1,10 @@ + + + + + Take mandatory breaks + fa-coffee + hr.employee + + diff --git a/hr_attendance_break/data/mail_template.xml b/hr_attendance_break/data/mail_template.xml new file mode 100644 index 00000000..f303fe95 --- /dev/null +++ b/hr_attendance_break/data/mail_template.xml @@ -0,0 +1,37 @@ + + + + + Attendance: Mandatory break + + Please review your breaks from {{ format_date(ctx.get('hr_attendance_break_date', object.last_attendance_id.check_in)) }} + {{ object.work_email or object.user_id.email }} + {{ object.parent_id.work_email or object.parent_id.user_id.email }} + +

Dear ,

+

on , you only took hours of break, while you should have been taking hours.

+

Please review the attendances below:

+
    +
  • + +
  • +
+
+
+
diff --git a/hr_attendance_break/demo/res_company.xml b/hr_attendance_break/demo/res_company.xml new file mode 100644 index 00000000..b3c22770 --- /dev/null +++ b/hr_attendance_break/demo/res_company.xml @@ -0,0 +1,12 @@ + + + + + 0.25 + + + diff --git a/hr_attendance_break/models/__init__.py b/hr_attendance_break/models/__init__.py new file mode 100644 index 00000000..2e50ddf8 --- /dev/null +++ b/hr_attendance_break/models/__init__.py @@ -0,0 +1,9 @@ +from . import hr_employee +from . import hr_employee_public +from . import hr_attendance +from . import hr_attendance_break +from . import hr_attendance_break_threshold +from . import hr_attendance_report +from . import hr_attendance_reason +from . import res_config_settings +from . import res_company diff --git a/hr_attendance_break/models/hr_attendance.py b/hr_attendance_break/models/hr_attendance.py new file mode 100644 index 00000000..78ec8e09 --- /dev/null +++ b/hr_attendance_break/models/hr_attendance.py @@ -0,0 +1,201 @@ +# Copyright 2023 Hunki Enterprises BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0) + + +from datetime import datetime, timedelta + +from odoo import _, api, fields, models +from odoo.osv.expression import AND, OR + + +class HrAttendance(models.Model): + _inherit = "hr.attendance" + + break_ids = fields.One2many("hr.attendance.break", "attendance_id", string="Breaks") + break_hours = fields.Float(store=True, compute="_compute_break_hours") + + @api.constrains("check_in", "check_out") + def _check_times_break(self): + self.mapped("break_ids")._check_times() + + @api.depends("break_ids.begin", "break_ids.end") + def _compute_break_hours(self): + """Compute break hours""" + for this in self: + this.break_hours = sum( + this.break_ids.filtered( + lambda x: x.reason_id.bypass_minimum_break + or x.break_hours + >= this.employee_id.company_id.hr_attendance_break_min_break + ).mapped("break_hours") + ) + + @api.depends("break_hours") + def _compute_worked_hours(self): + """Subtract break time from work hours""" + result = super()._compute_worked_hours() + for this in self: + this.worked_hours = this.worked_hours - this.break_hours + return result + + def button_show_breaks(self): + this = self[:1] + return { + "type": "ir.actions.act_window", + "name": _("Edit breaks for %s") % this.display_name, + "views": [(False, "tree")], + "res_model": "hr.attendance.break", + "domain": [("attendance_id", "=", this.id)], + "context": { + "default_attendance_id": this.id, + "default_begin": this.check_in, + "default_end": this.check_in, + }, + } + + def write(self, vals): + """Close open breaks if writing check_out""" + result = super().write(vals) + if vals.get("check_out"): + self.mapped("break_ids").filtered(lambda x: not x.end).write( + {"end": vals["check_out"]} + ) + return result + + def _impose_break_prepare_data(self, begin, end): + """ + Return a dict to create an imposed break for self + """ + self.ensure_one() + return { + "attendance_id": self.id, + "begin": max(self.check_in, begin), + "end": min(self.check_out or datetime.max, end), + "reason_id": self.env.ref("hr_attendance_break.reason_imposed").id, + } + + def _impose_break(self, hours): + """ + Add hours of break time to attendances in self, append to existing + break(s), otherwise add it in the middle of the attendance + """ + break_hours = hours + + def add_break(attendance, begin, end): + return ( + self.env["hr.attendance.break"] + .create(attendance._impose_break_prepare_data(begin, end)) + .break_hours + if begin != end + else 0.0 + ) + + # the below might create overlapping breaks with a weird combination of + # breaks and attendances, but we don't expect that to matter in practice + for this in self: + for _break in this.break_ids: + if not _break.end: + _break.end = _break.begin + timedelta(seconds=1) + break_hours -= add_break( + this, _break.end, _break.end + timedelta(hours=break_hours) + ) + break_start = this.check_in + ( + timedelta( + hours=(this.check_out - this.check_in).total_seconds() / 3600 / 2 + - break_hours / 2 + ) + if this.check_out and break_hours + else timedelta(0) + ) + break_hours -= add_break( + this, break_start, break_start + timedelta(hours=break_hours) + ) + + def _update_overtime(self, employee_attendance_dates=None): + """Adjust overtime with breaks taken""" + if employee_attendance_dates is None: + employee_attendance_dates = self._get_attendances_dates() + attendances = self.search( + OR( + [ + AND( + [ + OR( + [ + [ + ("check_in", ">=", attendance_date[0]), + ( + "check_in", + "<", + attendance_date[0] + timedelta(hours=24), + ), + ] + for attendance_date in attendance_dates + ] + ), + [("employee_id", "=", employee.id)], + ] + ) + for employee, attendance_dates in employee_attendance_dates.items() + ] + ) + ) + # super looks at work_hours, in some cases, at checkin, checkout in others + # we manipulate the cache that work_hours comes back without breaks, so that + # we can subtract the break time unconditionally below + attendances.read(["worked_hours"]) + for attendance in attendances: + attendance._cache["worked_hours"] += attendance.break_hours + + result = super()._update_overtime( + employee_attendance_dates=employee_attendance_dates + ) + + attendances.invalidate_cache(["worked_hours"], attendances.ids) + overtimes = ( + self.env["hr.attendance.overtime"] + .sudo() + .search( + OR( + [ + [ + ( + "date", + "in", + [ + attendance_date[1] + for attendance_date in attendance_dates + ], + ), + ("employee_id", "=", employee.id), + ] + for employee, attendance_dates in employee_attendance_dates.items() + ] + ) + + [("adjustment", "=", False)], + ) + ) + for employee_id, attendance_dates in employee_attendance_dates.items(): + for attendance_datetime, attendance_date in attendance_dates: + overtime = overtimes.filtered( + lambda x: x.employee_id == employee_id and x.date == attendance_date + ) + break_duration = sum( + attendances.filtered( + lambda x: x.employee_id == employee_id + and x.check_in >= attendance_datetime + and x.check_in < attendance_datetime + timedelta(hours=24) + ).mapped("break_hours") + ) + + if overtime: + overtime.duration -= break_duration + else: + self.env["hr.attendance.overtime"].sudo().create( + { + "employee_id": employee_id.id, + "date": attendance_date, + "duration": -break_duration, + } + ) + return result diff --git a/hr_attendance_break/models/hr_attendance_break.py b/hr_attendance_break/models/hr_attendance_break.py new file mode 100644 index 00000000..81cd7d8b --- /dev/null +++ b/hr_attendance_break/models/hr_attendance_break.py @@ -0,0 +1,71 @@ +# Copyright 2023 Hunki Enterprises BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0) + + +from odoo import _, api, exceptions, fields, models + + +class HrAttendanceBreak(models.Model): + _name = "hr.attendance.break" + _rec_name = "attendance_id" + _description = "Work break taken" + _order = "begin desc" + + attendance_id = fields.Many2one("hr.attendance", required=True, ondelete="cascade") + begin = fields.Datetime(required=True) + end = fields.Datetime(required=False) + break_hours = fields.Float(compute="_compute_break_hours") + reason_id = fields.Many2one("hr.attendance.reason") + + @api.constrains("attendance_id", "begin", "end") + def _check_times(self): + for this in self: + valid = True + if this.attendance_id.check_in and this.begin: + valid &= this.attendance_id.check_in <= this.begin + if this.attendance_id.check_out and this.end: + valid &= this.attendance_id.check_out >= this.end + if valid and this.begin and this.end: + valid &= not bool( + self.search_count( + [ + ("begin", "<", this.end), + ("end", ">", this.begin), + ("id", "not in", this.ids), + ("attendance_id", "=", this.attendance_id.id), + ] + ) + ) + valid &= this.begin < this.end + if not valid: + raise exceptions.ValidationError( + _( + "Breaks must be fully contained by the attendance they belong to " + "and can't overlap" + ) + ) + + @api.depends("begin", "end") + def _compute_break_hours(self): + for this in self: + this.break_hours = ( + 0 if not this.end else (this.end - this.begin).total_seconds() / 3600 + ) + + @api.model_create_multi + def create(self, vals_list): + result = super().create(vals_list) + result.mapped("attendance_id")._update_overtime() + return result + + def write(self, vals): + result = super().write(vals) + if {"attendance_id", "begin", "end"} & set(vals): + self.mapped("attendance_id")._update_overtime() + return result + + def unlink(self): + to_update = self.mapped("attendance_id") + result = super().unlink() + to_update._update_overtime() + return result diff --git a/hr_attendance_break/models/hr_attendance_break_threshold.py b/hr_attendance_break/models/hr_attendance_break_threshold.py new file mode 100644 index 00000000..3b19a259 --- /dev/null +++ b/hr_attendance_break/models/hr_attendance_break_threshold.py @@ -0,0 +1,16 @@ +# Copyright 2023 Hunki Enterprises BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0) + + +from odoo import fields, models + + +class HrAttendanceBreakThreshold(models.Model): + _name = "hr.attendance.break.threshold" + _description = "Minimum break times" + _rec_name = "company_id" + _order = "min_hours desc" + + company_id = fields.Many2one("res.company", required=True, ondelete="cascade") + min_hours = fields.Float(required=True) + min_break = fields.Float(required=True) diff --git a/hr_attendance_break/models/hr_attendance_reason.py b/hr_attendance_break/models/hr_attendance_reason.py new file mode 100644 index 00000000..2fbfab37 --- /dev/null +++ b/hr_attendance_break/models/hr_attendance_reason.py @@ -0,0 +1,15 @@ +# Copyright 2023 Hunki Enterprises BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0) + + +from odoo import fields, models + + +class HrAttendanceReason(models.Model): + _inherit = "hr.attendance.reason" + + action_type = fields.Selection(selection_add=[("break", "Break")]) + bypass_minimum_break = fields.Boolean( + help="Check this to have breaks of this type always be counted, independent " + "of minimum break settings" + ) diff --git a/hr_attendance_break/models/hr_attendance_report.py b/hr_attendance_break/models/hr_attendance_report.py new file mode 100644 index 00000000..2f2d091c --- /dev/null +++ b/hr_attendance_break/models/hr_attendance_report.py @@ -0,0 +1,19 @@ +# Copyright 2023 Hunki Enterprises BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0) + + +from odoo import api, fields, models + + +class HrAttendanceReport(models.Model): + _inherit = "hr.attendance.report" + + break_hours = fields.Float(readonly=True) + + @api.model + def _select(self): + return super()._select().rstrip() + ", break_hours" + + @api.model + def _from(self): + return super()._from().replace("worked_hours", "worked_hours, break_hours") diff --git a/hr_attendance_break/models/hr_employee.py b/hr_attendance_break/models/hr_employee.py new file mode 100644 index 00000000..1a1652b0 --- /dev/null +++ b/hr_attendance_break/models/hr_employee.py @@ -0,0 +1,167 @@ +# Copyright 2023 Hunki Enterprises BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0) + + +import datetime + +import pytz + +from odoo import api, fields, models + + +class HrEmployee(models.Model): + _inherit = "hr.employee" + + break_state = fields.Selection( + [("on_break", "On break"), ("no_break", "Not on break")], + compute="_compute_break_state", + ) + break_hours_today = fields.Float( + compute="_compute_hours_today", + ) + + @api.depends("attendance_ids.break_ids.begin", "attendance_ids.break_ids.end") + def _compute_break_state(self): + for this in self: + current_break = self._get_current_break() + this.break_state = ( + "on_break" if current_break and not current_break.end else "no_break" + ) + + @api.depends("attendance_ids.break_ids.begin", "attendance_ids.break_ids.end") + def _compute_presence_state(self): + result = super()._compute_presence_state() + for this in self: + if this.break_state == "on_break" and this.hr_presence_state == "present": + this.hr_presence_state = "absent" + return result + + def _compute_hours_today(self): + result = super()._compute_hours_today() + now = fields.Datetime.now() + for this in self: + day_start, _dummy = self.env["hr.attendance"]._get_day_start_and_day( + this, now + ) + attendances = self.env["hr.attendance"].search( + [ + ("employee_id", "=", this.id), + ("check_in", "<=", now), + "|", + ("check_out", ">=", day_start), + ("check_out", "=", False), + ] + ) + this.break_hours_today = sum(attendances.mapped("break_hours")) + sum( + attendances.mapped("break_ids") + .filtered(lambda x: not x.end) + .mapped(lambda x: x.begin and ((now - x.begin).total_seconds() / 3600)) + ) + this.hours_today -= this.break_hours_today + return result + + def _get_current_break(self): + HrAttendanceBreak = self.env["hr.attendance.break"].sudo() + return HrAttendanceBreak.search( + [ + ("attendance_id", "in", self.mapped("last_attendance_id").ids), + ("begin", "<=", fields.Datetime.now()), + ("end", "=", False), + ], + limit=1, + ) + + def attendance_manual_break(self, next_action, entered_pin=None): + """Create break record or end currently open break""" + now = fields.Datetime.now() + employee = self.sudo() + HrAttendanceBreak = self.env["hr.attendance.break"].sudo() + current_break = self._get_current_break() + + if current_break: + current_break.end = now + else: + current_break = HrAttendanceBreak.create( + { + "attendance_id": employee.last_attendance_id.id, + "begin": now, + } + ) + return employee.break_state + + def _attendance_action_change(self): + """Close possibly open breaks when closing an attendance""" + attendance = super()._attendance_action_change() + if attendance.check_out: + attendance.mapped("break_ids").filtered(lambda x: not x.end).write( + { + "end": attendance.check_out, + } + ) + return attendance + + def _check_mandatory_break_yesterday(self): + """Run _check_mandatory_break for yesterday""" + yesterday = fields.Date.context_today(self) - datetime.timedelta(days=1) + for this in self: + this._check_mandatory_break(yesterday) + + def _check_mandatory_break(self, date): + """Run an action if an employee did not take enough break time on a working day""" + self.ensure_one() + day_start = ( + pytz.timezone(self.tz) + .localize(datetime.datetime.combine(date, datetime.time.min)) + .astimezone(pytz.utc) + .replace(tzinfo=None) + ) + day_end = ( + pytz.timezone(self.tz) + .localize(datetime.datetime.combine(date, datetime.time.max)) + .astimezone(pytz.utc) + .replace(tzinfo=None) + ) + attendances = self.env["hr.attendance"].search( + [ + ("employee_id", "in", self.ids), + ("check_in", ">=", day_start), + ("check_in", "<=", day_end), + ], + order="check_in asc", + ) + # TODO how do we handle attendances that span multiple days? + work_hours = sum(attendances.mapped("worked_hours")) + break_hours = sum(attendances.mapped("break_hours")) + + for last_attendance, attendance in zip(attendances, attendances[1:]): + between_work = ( + attendance.check_in - last_attendance.check_out + ).total_seconds() / 3600 + if between_work >= self.company_id.hr_attendance_break_min_break: + break_hours += between_work + + for threshold in self.company_id.hr_attendance_break_threshold_ids.sorted( + "min_hours", reverse=True + ): + if work_hours >= threshold.min_hours and break_hours < threshold.min_break: + self._check_mandatory_break_action( + date, threshold, attendances, break_hours + ) + break + + def _check_mandatory_break_action(self, date, threshold, attendances, break_hours): + """Run an action when an employee didn't take mandatory breaks""" + self.ensure_one() + return ( + self.env.ref("hr_attendance_break.action_mandatory_break") + .with_context( + active_id=self.id, + active_ids=self.ids, + active_model=self._name, + hr_attendance_break_date=date, + hr_attendance_break_threshold=threshold, + hr_attendance_break_attendances=attendances, + hr_attendance_break_hours=break_hours, + ) + .run() + ) diff --git a/hr_attendance_break/models/hr_employee_public.py b/hr_attendance_break/models/hr_employee_public.py new file mode 100644 index 00000000..e42ace9f --- /dev/null +++ b/hr_attendance_break/models/hr_employee_public.py @@ -0,0 +1,20 @@ +# Copyright 2023 Hunki Enterprises BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0) + + +from odoo import fields, models + + +class HrEmployeePublic(models.Model): + _inherit = "hr.employee.public" + + break_state = fields.Selection( + related="employee_id.break_state", + groups="hr_attendance.group_hr_attendance_kiosk," + "hr_attendance.group_hr_attendance", + ) + break_hours_today = fields.Float( + related="employee_id.break_hours_today", + groups="hr_attendance.group_hr_attendance_kiosk," + "hr_attendance.group_hr_attendance", + ) diff --git a/hr_attendance_break/models/res_company.py b/hr_attendance_break/models/res_company.py new file mode 100644 index 00000000..ab64f5f5 --- /dev/null +++ b/hr_attendance_break/models/res_company.py @@ -0,0 +1,16 @@ +# Copyright 2023 Hunki Enterprises BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0) + + +from odoo import fields, models + + +class ResCompany(models.Model): + _inherit = "res.company" + + hr_attendance_break_min_break = fields.Float("Minimal break length") + hr_attendance_break_threshold_ids = fields.One2many( + "hr.attendance.break.threshold", + "company_id", + string="Minimal break thresholds", + ) diff --git a/hr_attendance_break/models/res_config_settings.py b/hr_attendance_break/models/res_config_settings.py new file mode 100644 index 00000000..ec01957b --- /dev/null +++ b/hr_attendance_break/models/res_config_settings.py @@ -0,0 +1,35 @@ +# Copyright 2023 Hunki Enterprises BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0) + + +from odoo import _, fields, models + + +class ResConfigSettings(models.TransientModel): + _inherit = "res.config.settings" + + hr_attendance_break_min_break = fields.Float( + related="company_id.hr_attendance_break_min_break", readonly=False + ) + + def button_hr_attendance_break_thresholds(self): + return { + "type": "ir.actions.act_window", + "name": _("Edit break thresholds"), + "res_model": "hr.attendance.break.threshold", + "domain": [("company_id", "=", self.company_id.id)], + "views": [(False, "tree")], + "context": { + "default_company_id": self.company_id.id, + }, + } + + def button_hr_attendance_break_action(self): + action = self.env.ref("hr_attendance_break.action_mandatory_break") + return { + "type": "ir.actions.act_window", + "name": action.name, + "res_model": action._name, + "res_id": action.id, + "views": [(False, "form")], + } diff --git a/hr_attendance_break/readme/CONFIGURE.rst b/hr_attendance_break/readme/CONFIGURE.rst new file mode 100644 index 00000000..7a99c644 --- /dev/null +++ b/hr_attendance_break/readme/CONFIGURE.rst @@ -0,0 +1,6 @@ +To configure this module, you need to: + +- Go to Attendances/Configuration/Settings, sections Breaks +- Fill in the minimum length of a break to be counted +- If you want to enforce a minimum amount of break taken for a certain amount of hours worked per day, click ``Edit thresholds for mandatory breaks`` +- Review the server action linked there, you can ie disable imposing mandatory breaks, or rewrite the mail template informing about this diff --git a/hr_attendance_break/readme/CONTRIBUTORS.rst b/hr_attendance_break/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000..33b6eb2c --- /dev/null +++ b/hr_attendance_break/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Holger Brunn (https://hunki-enterprises.com) diff --git a/hr_attendance_break/readme/DESCRIPTION.rst b/hr_attendance_break/readme/DESCRIPTION.rst new file mode 100644 index 00000000..b0b0a893 --- /dev/null +++ b/hr_attendance_break/readme/DESCRIPTION.rst @@ -0,0 +1,7 @@ +This module lets employees take breaks on the attendance screen: + +.. figure:: ../static/description/hr_attendance_break.png + +This allows them to check in at the beginning of the working day, check out at the end and record breaks in between. + +To be sure employees take enough breaks, there's also flagging for employees who didn't take enough breaks. diff --git a/hr_attendance_break/readme/USAGE.rst b/hr_attendance_break/readme/USAGE.rst new file mode 100644 index 00000000..02bd3398 --- /dev/null +++ b/hr_attendance_break/readme/USAGE.rst @@ -0,0 +1,9 @@ +To use this module, you need to: + +#. Go to Attendances/Attendances +#. Click Show/Edit breaks + +or + +#. Go to your check in screen +#. After checking in, click the coffee mug for starting a break, and click the gears for ending it diff --git a/hr_attendance_break/security/hr_attendance_break.xml b/hr_attendance_break/security/hr_attendance_break.xml new file mode 100644 index 00000000..44c51b6e --- /dev/null +++ b/hr_attendance_break/security/hr_attendance_break.xml @@ -0,0 +1,39 @@ + + + + + Restrict to own company + + ['|', ('attendance_id.employee_id.company_id', '=', False), ('attendance_id.employee_id.company_id', 'in', company_ids)] + + + + Restrict to own breaks for users + + [('attendance_id.employee_id.user_id', '=', user.id)] + + + + + Lift restrictions for managers + + [(1, '=', 1)] + + + + + Restrict to own company + + ['|', ('company_id', '=', False), ('company_id', 'in', company_ids)] + + diff --git a/hr_attendance_break/security/ir.model.access.csv b/hr_attendance_break/security/ir.model.access.csv new file mode 100644 index 00000000..bc854a30 --- /dev/null +++ b/hr_attendance_break/security/ir.model.access.csv @@ -0,0 +1,5 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +manage_hr_attendance_break,access_hr_attendance_break,model_hr_attendance_break,hr_attendance.group_hr_attendance_user,1,1,1,1 +access_hr_attendance_break,access_hr_attendance_break,model_hr_attendance_break,base.group_user,1,1,1,0 +manage_hr_attendance_break_threshold,access_hr_attendance_break_threshold,model_hr_attendance_break_threshold,hr_attendance.group_hr_attendance_user,1,1,1,1 +access_hr_attendance_break_threshold,access_hr_attendance_break_threshold,model_hr_attendance_break_threshold,base.group_user,1,0,0,0 diff --git a/hr_attendance_break/static/description/hr_attendance_break.png b/hr_attendance_break/static/description/hr_attendance_break.png new file mode 100644 index 00000000..534fb6ed Binary files /dev/null and b/hr_attendance_break/static/description/hr_attendance_break.png differ diff --git a/hr_attendance_break/static/description/icon.png b/hr_attendance_break/static/description/icon.png new file mode 100644 index 00000000..3a0328b5 Binary files /dev/null and b/hr_attendance_break/static/description/icon.png differ diff --git a/hr_attendance_break/static/description/index.html b/hr_attendance_break/static/description/index.html new file mode 100644 index 00000000..8c51e210 --- /dev/null +++ b/hr_attendance_break/static/description/index.html @@ -0,0 +1,454 @@ + + + + + + +Work breaks + + + +
+

Work breaks

+ + +

Beta License: AGPL-3 OCA/hr-attendance Translate me on Weblate Try me on Runboat

+

This module lets employees take breaks on the attendance screen:

+
+https://raw.githubusercontent.com/OCA/hr-attendance/15.0/hr_attendance_break/static/description/hr_attendance_break.png +
+

This allows them to check in at the beginning of the working day, check out at the end and record breaks in between.

+

To be sure employees take enough breaks, there’s also flagging for employees who didn’t take enough breaks.

+

Table of contents

+ +
+

Configuration

+

To configure this module, you need to:

+
    +
  • Go to Attendances/Configuration/Settings, sections Breaks
  • +
  • Fill in the minimum length of a break to be counted
  • +
  • If you want to enforce a minimum amount of break taken for a certain amount of hours worked per day, click Edit thresholds for mandatory breaks
  • +
  • Review the server action linked there, you can ie disable imposing mandatory breaks, or rewrite the mail template informing about this
  • +
+
+
+

Usage

+

To use this module, you need to:

+
    +
  1. Go to Attendances/Attendances
  2. +
  3. Click Show/Edit breaks
  4. +
+

or

+
    +
  1. Go to your check in screen
  2. +
  3. After checking in, click the coffee mug for starting a break, and click the gears for ending it
  4. +
+
+
+

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 to smash it by providing a detailed and welcomed +feedback.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • Hunki Enterprises BV
  • +
  • verdigado eG
  • +
+
+ +
+

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:

+

hbrunn

+

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

+

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

+
+
+
+ + diff --git a/hr_attendance_break/static/src/js/hr_attendance_break.js b/hr_attendance_break/static/src/js/hr_attendance_break.js new file mode 100644 index 00000000..ba8b4247 --- /dev/null +++ b/hr_attendance_break/static/src/js/hr_attendance_break.js @@ -0,0 +1,55 @@ +/* Copyright 2023 Hunki Enterprises BV + * License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */ + +odoo.define("hr_attendance_break", function (require) { + "use strict"; + + var myAttendances = require("hr_attendance.my_attendances"); + var field_utils = require("web.field_utils"); + + myAttendances.include({ + events: _.extend({}, myAttendances.prototype.events, { + "click .o_hr_attendance_break_icon": _.debounce( + function () { + this._hr_attendance_break(); + }, + 200, + true + ), + }), + + willStart: function () { + var self = this; + var promise = this._rpc({ + model: "hr.employee", + method: "search_read", + args: [ + [["user_id", "=", this.getSession().uid]], + ["break_state", "break_hours_today"], + ], + }).then(function (data) { + self.break_state = data[0].break_state; + self.break_hours_today = field_utils.format.float_time( + data[0].break_hours_today + ); + }); + return Promise.all([this._super.apply(this, arguments), promise]); + }, + + _hr_attendance_break: function () { + var self = this; + return this._rpc({ + model: "hr.employee", + method: "attendance_manual_break", + args: [ + [this.employee.id], + "hr_attendance.hr_attendance_action_my_attendances", + ], + }).then(function () { + return self.do_action( + "hr_attendance.hr_attendance_action_my_attendances" + ); + }); + }, + }); +}); diff --git a/hr_attendance_break/static/src/scss/hr_attendance_break.scss b/hr_attendance_break/static/src/scss/hr_attendance_break.scss new file mode 100644 index 00000000..04cd29e2 --- /dev/null +++ b/hr_attendance_break/static/src/scss/hr_attendance_break.scss @@ -0,0 +1,13 @@ +.o_hr_attendance_kiosk_mode { + .o_hr_attendance_break_icon { + cursor: pointer; + margin: 0.1em 0 0.1em; + padding: 0.15em 0.3em; + border-radius: 0.1em; + box-shadow: inset 0 -3px 0 fade-out(black, 0.7); + + &.btn-secondary:hover { + color: $o-brand-primary; + } + } +} diff --git a/hr_attendance_break/static/src/xml/hr_attendance_break.xml b/hr_attendance_break/static/src/xml/hr_attendance_break.xml new file mode 100644 index 00000000..35cbc9ce --- /dev/null +++ b/hr_attendance_break/static/src/xml/hr_attendance_break.xml @@ -0,0 +1,43 @@ + + + diff --git a/hr_attendance_break/tests/__init__.py b/hr_attendance_break/tests/__init__.py new file mode 100644 index 00000000..fdf3a79b --- /dev/null +++ b/hr_attendance_break/tests/__init__.py @@ -0,0 +1 @@ +from . import test_hr_attendance_break diff --git a/hr_attendance_break/tests/test_hr_attendance_break.py b/hr_attendance_break/tests/test_hr_attendance_break.py new file mode 100644 index 00000000..ef3ee173 --- /dev/null +++ b/hr_attendance_break/tests/test_hr_attendance_break.py @@ -0,0 +1,235 @@ +# Copyright 2023 Hunki Enterprises BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0) + + +import datetime +import time +from unittest.mock import patch + +from odoo import exceptions +from odoo.tests.common import TransactionCase + +from ..models.hr_employee import fields as hr_employee_fields + + +class TestHrAttendance(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.env["hr.attendance"].search([]).unlink() + cls.employee = cls.env.ref("hr.employee_admin") + cls.attendance = cls.env["hr.attendance"].create( + { + "employee_id": cls.employee.id, + "check_in": datetime.datetime(2023, 8, 21, 7, 0, 0), + "check_out": datetime.datetime(2023, 8, 21, 17, 0, 0), + "break_ids": [ + (0, 0, {"begin": datetime.datetime(2023, 8, 21, 7, 30, 0)}), + ], + } + ) + + def test_break_computation(self): + """Test that computing breaks from an attendance works""" + self.assertEqual(self.employee.break_state, "on_break") + self.assertEqual(self.attendance.worked_hours, 10) + self.attendance.break_ids.end = datetime.datetime(2023, 8, 21, 8, 0, 0) + self.assertEqual(self.employee.break_state, "no_break") + self.assertEqual(self.attendance.break_hours, 0.5) + self.assertEqual(self.attendance.worked_hours, 9.5) + with patch.object( + hr_employee_fields.Datetime, + "now", + side_effect=lambda: datetime.datetime(2023, 8, 21, 18, 0, 0), + ): + self.assertEqual(self.employee.break_hours_today, 0.5) + + def test_mandatory_break(self): + """Test that we flag for minimal break taken""" + original_activities = self.employee.activity_ids + # no break, should flag and impose break + self.employee._check_mandatory_break(datetime.datetime(2023, 8, 21)) + flag_activity = self.employee.activity_ids - original_activities + self.assertEqual( + flag_activity.activity_type_id, + self.env.ref("hr_attendance_break.activity_type_mandatory_break"), + ) + original_activities += flag_activity + self.assertEqual(len(self.attendance.break_ids), 2) + imposed_break = self.attendance.break_ids.filtered( + lambda x: x.reason_id == self.env.ref("hr_attendance_break.reason_imposed") + ) + self.assertEqual(imposed_break.break_hours, 0.75) + # break is exactly minimum, no flag + imposed_break.unlink() + self.attendance.break_ids.end = datetime.datetime(2023, 8, 21, 8, 15, 0) + self.employee._check_mandatory_break(datetime.datetime(2023, 8, 21)) + flag_activity = self.employee.activity_ids - original_activities + self.assertFalse(flag_activity) + # break is a little bit less than minimum, flag and impose break + self.attendance.break_ids.end = datetime.datetime(2023, 8, 21, 8, 13, 0) + self.employee._check_mandatory_break(datetime.datetime(2023, 8, 21)) + flag_activity = self.employee.activity_ids - original_activities + self.assertTrue(flag_activity) + original_activities += flag_activity + imposed_break = self.attendance.break_ids.filtered( + lambda x: x.reason_id == self.env.ref("hr_attendance_break.reason_imposed") + ) + self.assertEqual(imposed_break.break_hours, 1 / 30) + self.assertEqual(sum(self.attendance.break_ids.mapped("break_hours")), 0.75) + # break is not actually a break but just two attendances with time in between + # that suffices for this day as mandatory break + self.attendance.break_ids.unlink() + self.attendance.check_out = datetime.datetime(2023, 8, 21, 11, 0, 0) + self.env["hr.attendance"].create( + { + "employee_id": self.employee.id, + "check_in": datetime.datetime(2023, 8, 21, 13, 0, 0), + "check_out": datetime.datetime(2023, 8, 21, 17, 0, 0), + } + ) + with patch.object( + hr_employee_fields.Date, + "context_today", + side_effect=lambda record: datetime.datetime(2023, 8, 22), + ): + self.employee._check_mandatory_break_yesterday() + flag_activity = self.employee.activity_ids - original_activities + self.assertFalse(flag_activity) + + def test_manual_break(self): + """Test break taking works as expected""" + self.employee.invalidate_cache(["hr_presence_state", "break_state"]) + attendance = self.employee._attendance_action_change() + self.assertFalse(attendance.break_ids) + self.assertEqual(self.employee.hr_presence_state, "present") + self.employee.attendance_manual_break(None) + self.assertTrue(attendance.break_ids) + self.assertEqual(self.employee.hr_presence_state, "absent") + # begin is datetime.now(), but below the code will set + # end to datetime.now() which will fail the constraint begin + + + + hr.attendance + + + + +