Skip to content

Commit

Permalink
[ADD] hr_attendance_sheet_compensatory - Refactor of hr_attendance_va…
Browse files Browse the repository at this point in the history
…lidation to make it works with hr_attendance_sheet
  • Loading branch information
FrancoMaxime committed Aug 30, 2023
1 parent 097b5d4 commit 7ab791e
Show file tree
Hide file tree
Showing 41 changed files with 2,577 additions and 458 deletions.
2 changes: 2 additions & 0 deletions hr_attendance_sheet/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
"data": [
"security/ir.model.access.csv",
"security/security_groups.xml",
"security/hr_attendance_sheet_config_acl.xml",
"data/cron.xml",
"data/mail_data.xml",
"report/hr_attendance_sheet_report.xml",
"views/hr_attendance_sheet.xml",
"views/hr_attendance_sheet_config.xml",
"views/hr_attendance_view.xml",
"views/hr_department.xml",
"views/hr_employee.xml",
Expand Down
2 changes: 2 additions & 0 deletions hr_attendance_sheet/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Copyright 2020 Pavlov Media
# Copyright 2023 ACSONE SA/NV
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from . import hr_attendance
from . import hr_attendance_sheet
from . import hr_attendance_sheet_config
from . import hr_department
from . import hr_employee
from . import res_company
Expand Down
61 changes: 43 additions & 18 deletions hr_attendance_sheet/models/hr_attendance.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ class HrAttendance(models.Model):
auto_lunch = fields.Boolean(
string="Auto Lunch Applied",
help="If Auto Lunch is enabled and applied on this attendance.",
compute="_compute_duration",
)
company_id = fields.Many2one(
"res.company", string="Company", related="attendance_sheet_id.company_id"
)
auto_lunch_enabled = fields.Boolean(
string="Auto Lunch Enabled", related="company_id.auto_lunch"
string="Auto Lunch Enabled", related="attendance_sheet_config_id.auto_lunch"
)
override_auto_lunch = fields.Boolean(
string="Override Auto Lunch",
Expand All @@ -49,6 +50,31 @@ class HrAttendance(models.Model):
is set on the department.""",
related="department_id.attendance_admin",
)
attendance_sheet_config_id = fields.Many2one(
comodel_name="hr.attendance.sheet.config",
compute="_compute_attendance_sheet_config_id",
)

@api.depends(
"check_in",
"check_out",
"employee_id.company_id",
)
def _compute_attendance_sheet_config_id(self):
for attendance in self:
if attendance.attendance_sheet_id:
config = attendance.attendance_sheet_id.attendance_sheet_config_id
else:
config = self.env["hr.attendance.sheet.config"].search(
[
("start_date", "<=", attendance.check_in),
"|",
("end_date", ">=", attendance.check_out),
("end_date", "=", False),
("company_id", "=", attendance.employee_id.company_id.id),
]
)
attendance.attendance_sheet_config_id = config

# Get Methods
def _get_attendance_employee_tz(self, date=None):
Expand Down Expand Up @@ -88,16 +114,22 @@ def _compute_attendance_sheet_id(self):
domain = [("employee_id", "=", attendance.employee_id.id)]
if check_in:
domain += [
("date_start", "<=", check_in),
("date_end", ">=", check_in),
("start_date", "<=", check_in),
("end_date", ">=", check_in),
]
attendance_sheet_ids = sheet_obj.search(domain, limit=1)
if attendance_sheet_ids.state not in ("locked", "done"):
attendance.attendance_sheet_id = attendance_sheet_ids or False

@api.depends(
"check_in",
"check_out",
"employee_id.company_id",
)
def _compute_duration(self):
for rec in self:
rec.duration = 0.0
rec.auto_lunch = False
if rec.check_in and rec.check_out:
delta = rec.check_out - rec.check_in
duration = delta.total_seconds() / 3600
Expand All @@ -121,38 +153,31 @@ def _compute_duration(self):
# If auto lunch is enabled for the company and time between
# other attendances < lunch period, then adjust the duration
# calculation for the first attendance.
config = rec.attendance_sheet_config_id
if (
rec.company_id.auto_lunch
and total_duration > rec.company_id.auto_lunch_duration != 0.0
config.auto_lunch
and total_duration > config.auto_lunch_duration != 0.0
and not rec.override_auto_lunch
):
first_attendance = self.get_first_attendances(today_attendances)
if first_attendance and first_attendance.id == rec.id:
if len(today_attendances) > 1:
rec.write({"auto_lunch": False})
time_between_attendances = (

Check warning on line 165 in hr_attendance_sheet/models/hr_attendance.py

View check run for this annotation

Codecov / codecov/patch

hr_attendance_sheet/models/hr_attendance.py#L165

Added line #L165 was not covered by tests
self.compute_time_between_attendances(today_attendances)
)
total_time_between_attendances = sum(

Check warning on line 168 in hr_attendance_sheet/models/hr_attendance.py

View check run for this annotation

Codecov / codecov/patch

hr_attendance_sheet/models/hr_attendance.py#L168

Added line #L168 was not covered by tests
time_between_attendances
)
if (
total_time_between_attendances
< rec.company_id.auto_lunch_hours
):
if total_time_between_attendances < config.auto_lunch_hours:
rec.duration = self.compute_rest_of_autolunch(

Check warning on line 172 in hr_attendance_sheet/models/hr_attendance.py

View check run for this annotation

Codecov / codecov/patch

hr_attendance_sheet/models/hr_attendance.py#L172

Added line #L172 was not covered by tests
duration,
time_between_attendances,
rec.company_id.auto_lunch_hours,
config.auto_lunch_hours,
)
rec.write({"auto_lunch": True})
rec.auto_lunch = True

Check warning on line 177 in hr_attendance_sheet/models/hr_attendance.py

View check run for this annotation

Codecov / codecov/patch

hr_attendance_sheet/models/hr_attendance.py#L177

Added line #L177 was not covered by tests
else:
rec.duration = duration - rec.company_id.auto_lunch_hours
rec.write({"auto_lunch": True})
else:
rec.write({"auto_lunch": False})
elif rec.company_id.auto_lunch and rec.auto_lunch:
rec.write({"auto_lunch": False})
rec.duration = duration - config.auto_lunch_hours
rec.auto_lunch = True

def compute_time_between_attendances(self, attendances):
previous_attendance = attendances[0]
Expand Down
Loading

0 comments on commit 7ab791e

Please sign in to comment.