Skip to content

Commit

Permalink
[ADD] hr_attendance_missing_days: Generation of attendance for workin…
Browse files Browse the repository at this point in the history
…g days without one
  • Loading branch information
fkantelberg committed Aug 10, 2023
1 parent 96b8dbc commit ff9677d
Show file tree
Hide file tree
Showing 17 changed files with 768 additions and 0 deletions.
79 changes: 79 additions & 0 deletions hr_attendance_missing_days/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
======================================
Attendance generation for missing days
======================================

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! 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_attendance-lightgray.png?logo=github
:target: https://github.com/OCA/hr_attendance/tree/15.0/hr_attendance_missing_days
: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_missing_days
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/webui/builds.html?repo=OCA/hr_attendance&target_branch=15.0
:alt: Try me on Runboat

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

This modules generates attendances for employee with 0 minutes for working days with missing
attendances. The configured reason is set to make it easier for filtering. This can be used
to generate pseudo attendances for working days otherwise Odoo wouldn't reduce the overtime
of the employee.

#. Go to *Attendances > Configuration > Missing Days*
#. Select a reason to set for the created attendances

**Table of contents**

.. contents::
:local:

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

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

* initOS GmbH

Contributors
~~~~~~~~~~~~

* initOS GmbH (initOS.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.

This module is part of the `OCA/hr_attendance <https://github.com/OCA/hr_attendance/tree/15.0/hr_attendance_missing_days>`_ project on GitHub.

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

from . import models
21 changes: 21 additions & 0 deletions hr_attendance_missing_days/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# © 2023 initOS GmbH
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
"name": "Attendance generation for missing days",
"version": "15.0.1.0.0",
"category": "Hidden",
"author": "initOS GmbH, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/hr-attendance",
"license": "AGPL-3",
"summary": "This modules generates attendances for working days without attendance",
"depends": [
"hr_attendance_reason",
],
"data": [
"data/hr_attendance_reason.xml",
"data/ir_cron.xml",
"views/res_config_settings_views.xml",
],
"installable": True,
}
7 changes: 7 additions & 0 deletions hr_attendance_missing_days/data/hr_attendance_reason.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo noupdate="1">
<record id="attendance_reason_missing_days" model="hr.attendance.reason">
<field name="name">System generated attendances for missing days</field>
<field name="code">S-GMD</field>
</record>
</odoo>
20 changes: 20 additions & 0 deletions hr_attendance_missing_days/data/ir_cron.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo noupdate="1">
<record id="missing_attendance_cron" model="ir.cron">
<field name="name">Missing Attendance</field>
<field name="interval_number">1</field>
<field name="interval_type">days</field>
<field name="numbercall">-1</field>
<field
name="nextcall"
eval="(DateTime.now() + timedelta(minutes=60)).strftime('%Y-%m-%d 04:00:00')"
/>
<field name="doall" eval="True" />
<field name="model_id" ref="hr.model_hr_employee" />
<field name="active" eval="False" />
<field name="state">code</field>
<field name="code">
model.create_missing_attendances(datetime.datetime.now() - datetime.timedelta(days=31))
</field>
</record>
</odoo>
4 changes: 4 additions & 0 deletions hr_attendance_missing_days/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# © 2023 initOS GmbH
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import hr_employee, res_company, res_config_settings
72 changes: 72 additions & 0 deletions hr_attendance_missing_days/models/hr_employee.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# © 2023 initOS GmbH
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import logging
from datetime import datetime, time

import pytz

from odoo import models

_logger = logging.getLogger(__name__)


def ensure_tz(dt, tz=None):
if not dt.tzinfo:
dt = pytz.utc.localize(dt)
return dt.astimezone(tz) if tz else dt


class Employee(models.Model):
_inherit = "hr.employee"

def create_missing_attendances(self, date_from=None, date_to=None):
for emp in self.search([]):
emp._create_missing_attendances(date_from, date_to)

Check warning on line 25 in hr_attendance_missing_days/models/hr_employee.py

View check run for this annotation

Codecov / codecov/patch

hr_attendance_missing_days/models/hr_employee.py#L25

Added line #L25 was not covered by tests

def _create_missing_attendances(self, date_from=None, date_to=None):
self.ensure_one()

reason = self.env.company.sudo().attendance_missing_days_reason
if not reason:
return

if not date_from:
date_from = datetime.combine(
self.env.company.sudo().overtime_start_date, time.min
)

if not date_to:
date_to = datetime.now()

date_from, date_to = map(ensure_tz, (date_from, date_to))

intervals = self.resource_calendar_id._work_intervals_batch(date_from, date_to)
work_dates = {}
for start, _stop, _attendance in sorted(intervals[False]):
start_date = start.date()
if start_date not in work_dates:
work_dates[start_date] = ensure_tz(start, pytz.utc).replace(tzinfo=None)

domain = [
("check_in", ">=", date_from.replace(tzinfo=None)),
("check_in", "<=", date_to.replace(tzinfo=None)),
]
tz = pytz.timezone(self.tz)
attendances = {
ensure_tz(attendance.check_in, tz).date()
for attendance in self.attendance_ids.filtered_domain(domain)
}

vals = []
for missing in set(work_dates) - attendances:
vals.append(
{
"employee_id": self.id,
"check_in": work_dates[missing],
"check_out": work_dates[missing],
"attendance_reason_ids": [(4, reason.id)],
}
)

self.env["hr.attendance"].create(vals)
16 changes: 16 additions & 0 deletions hr_attendance_missing_days/models/res_company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# © 2023 initOS GmbH
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models


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

attendance_missing_days_reason = fields.Many2one(
"hr.attendance.reason",
default=lambda self: self.env.ref(
"hr_attendance_missing_days.attendance_reason_missing_days",
raise_if_not_found=False,
),
)
13 changes: 13 additions & 0 deletions hr_attendance_missing_days/models/res_config_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# © 2023 initOS GmbH
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models


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

attendance_missing_days_reason = fields.Many2one(
related="company_id.attendance_missing_days_reason",
readonly=False,
)
1 change: 1 addition & 0 deletions hr_attendance_missing_days/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* initOS GmbH (initOS.com)
7 changes: 7 additions & 0 deletions hr_attendance_missing_days/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
This modules generates attendances for employee with 0 minutes for working days with missing
attendances. The configured reason is set to make it easier for filtering. This can be used
to generate pseudo attendances for working days otherwise Odoo wouldn't reduce the overtime
of the employee.

#. Go to *Attendances > Configuration > Missing Days*
#. Select a reason to set for the created attendances
Loading

0 comments on commit ff9677d

Please sign in to comment.