Skip to content

Commit

Permalink
[ADD] hr_shift: New module
Browse files Browse the repository at this point in the history
TT50623
  • Loading branch information
chienandalu committed Oct 17, 2024
1 parent 4bd2fcc commit ccdbd85
Show file tree
Hide file tree
Showing 22 changed files with 1,792 additions and 0 deletions.
78 changes: 78 additions & 0 deletions hr_shift/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
================
Employees Shifts
================

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

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

Assign shifts to employees.

**Table of contents**

.. contents::
:local:

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

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

* Tecnativa

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

- `Tecnativa <https://tecnativa.com>`__:

- David Vidal

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/shift-planning <https://github.com/OCA/shift-planning/tree/14.0/hr_shift>`_ 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 hr_shift/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
19 changes: 19 additions & 0 deletions hr_shift/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2024 Tecnativa - David Vidal
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
{
"name": "Employees Shifts",
"summary": "Define shifts for employees",
"version": "14.0.1.0.0",
"author": "Tecnativa, Odoo Community Association (OCA)",
"license": "AGPL-3",
"website": "https://github.com/OCA/shift-planning",
"category": "Marketing",
"depends": ["hr_holidays_public", "base_sparse_field"],
"data": [
"security/ir.model.access.csv",
"views/shift_planning_views.xml",
"views/shift_template_views.xml",
"views/res_config_settings_views.xml",
"views/assets.xml",
],
}
6 changes: 6 additions & 0 deletions hr_shift/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from . import res_company
from . import resource_calendar
from . import res_config_settings
from . import shift_planning
from . import shift_template
from . import hr_employee
42 changes: 42 additions & 0 deletions hr_shift/models/hr_employee.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2024 Tecnativa - David Vidal
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo import fields, models


class HrEmployeeBase(models.AbstractModel):
_inherit = "hr.employee.base"

current_shift_id = fields.Many2one(
comodel_name="hr.shift.planning.line", compute="_compute_current_shift_id"
)

def _shift_of_date(self, min_time, max_time):
return (

Check warning on line 14 in hr_shift/models/hr_employee.py

View check run for this annotation

Codecov / codecov/patch

hr_shift/models/hr_employee.py#L14

Added line #L14 was not covered by tests
self.env["hr.shift.planning.line"]
.sudo()
.search(
[
("employee_id", "=", self.id),
("state", "=", "assigned"),
("start_time", ">=", min_time),
("end_time", "<=", max_time),
]
)
)

def _compute_current_shift_id(self):
"""Current shift for a given employee if any"""
today = fields.Date.today()
now = fields.Datetime.now()
min_time = fields.datetime.combine(today, now.min.time())
max_time = fields.datetime.combine(today, now.max.time())

Check warning on line 32 in hr_shift/models/hr_employee.py

View check run for this annotation

Codecov / codecov/patch

hr_shift/models/hr_employee.py#L29-L32

Added lines #L29 - L32 were not covered by tests
for employee in self:
employee.current_shift_id = employee._shift_of_date(min_time, max_time)

Check warning on line 34 in hr_shift/models/hr_employee.py

View check run for this annotation

Codecov / codecov/patch

hr_shift/models/hr_employee.py#L34

Added line #L34 was not covered by tests

def _get_employee_working_now(self):
# Get shift info if available
employees_in_current_shift = self.filtered("current_shift_id")
others = super(

Check warning on line 39 in hr_shift/models/hr_employee.py

View check run for this annotation

Codecov / codecov/patch

hr_shift/models/hr_employee.py#L38-L39

Added lines #L38 - L39 were not covered by tests
HrEmployeeBase, (self - employees_in_current_shift)
)._get_employee_working_now()
return others + employees_in_current_shift.ids

Check warning on line 42 in hr_shift/models/hr_employee.py

View check run for this annotation

Codecov / codecov/patch

hr_shift/models/hr_employee.py#L42

Added line #L42 was not covered by tests
13 changes: 13 additions & 0 deletions hr_shift/models/res_company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2024 Tecnativa - David Vidal
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo import fields, models

from .shift_template import WEEK_DAYS_SELECTION


class ResCompany(models.Model):
_inherit = "res.company"

# Default from monday to friday
shift_start_day = fields.Selection(selection=WEEK_DAYS_SELECTION, default="0")
shift_end_day = fields.Selection(selection=WEEK_DAYS_SELECTION, default="4")
12 changes: 12 additions & 0 deletions hr_shift/models/res_config_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright 2024 Tecnativa - David Vidal
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo import fields, models


class ResConfigSettings(models.TransientModel):
_inherit = "res.config.settings"

shift_start_day = fields.Selection(
related="company_id.shift_start_day", readonly=False
)
shift_end_day = fields.Selection(related="company_id.shift_end_day", readonly=False)
57 changes: 57 additions & 0 deletions hr_shift/models/resource_calendar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright 2024 Tecnativa - David Vidal
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from datetime import datetime

from odoo import api, models
from odoo.tools import groupby


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

@api.model
def _resource_shift_for_datetime_range(self, start_dt, end_dt, resources, tz=None):
min_time = datetime.combine(start_dt, start_dt.min.time())
max_time = datetime.combine(end_dt, end_dt.max.time())
shifts = self.env["hr.shift.planning.line"].search(

Check warning on line 16 in hr_shift/models/resource_calendar.py

View check run for this annotation

Codecov / codecov/patch

hr_shift/models/resource_calendar.py#L14-L16

Added lines #L14 - L16 were not covered by tests
[
("resource_id", "in", resources.ids),
("state", "=", "assigned"),
("start_time", ">=", min_time),
("end_time", "<=", max_time),
]
)
return shifts

Check warning on line 24 in hr_shift/models/resource_calendar.py

View check run for this annotation

Codecov / codecov/patch

hr_shift/models/resource_calendar.py#L24

Added line #L24 was not covered by tests

def _attendance_intervals_batch(
self, start_dt, end_dt, resources=None, domain=None, tz=None
):
# Override calendar intervals when a shift is found and substitute those
# intervals with the ones on the shift
# TODO: deal with TZ!
res = super()._attendance_intervals_batch(

Check warning on line 32 in hr_shift/models/resource_calendar.py

View check run for this annotation

Codecov / codecov/patch

hr_shift/models/resource_calendar.py#L32

Added line #L32 was not covered by tests
start_dt, end_dt, resources, domain, tz
)
if resources:
shift_ids = self._resource_shift_for_datetime_range(

Check warning on line 36 in hr_shift/models/resource_calendar.py

View check run for this annotation

Codecov / codecov/patch

hr_shift/models/resource_calendar.py#L36

Added line #L36 was not covered by tests
start_dt, end_dt, resources, tz=tz
)
for resource, shifts in groupby(shift_ids, lambda x: x.resource_id):
intervals_to_add = []
intervals_to_remove = []
resource_intervals = res[resource.id]._items

Check warning on line 42 in hr_shift/models/resource_calendar.py

View check run for this annotation

Codecov / codecov/patch

hr_shift/models/resource_calendar.py#L40-L42

Added lines #L40 - L42 were not covered by tests
for shift in shifts:
intervals_to_remove += [
(start, end, resource_item)
for start, end, resource_item in resource_intervals
if (
shift.start_time
>= datetime.combine(start, start.min.time())
and shift.end_time >= datetime.combine(end, end.min.time())
)
]
intervals_to_add.append((shift.start_time, shift.end_time, shift))

Check warning on line 53 in hr_shift/models/resource_calendar.py

View check run for this annotation

Codecov / codecov/patch

hr_shift/models/resource_calendar.py#L53

Added line #L53 was not covered by tests
res[resource.id]._items = [
x for x in resource_intervals if x not in intervals_to_remove
] + intervals_to_add
return res

Check warning on line 57 in hr_shift/models/resource_calendar.py

View check run for this annotation

Codecov / codecov/patch

hr_shift/models/resource_calendar.py#L57

Added line #L57 was not covered by tests
Loading

0 comments on commit ccdbd85

Please sign in to comment.