Skip to content

Commit

Permalink
fixup! fixup! [17.0][ADD] project_timesheet_holidays_contract
Browse files Browse the repository at this point in the history
  • Loading branch information
yankinmax committed Sep 9, 2024
1 parent 4c78005 commit f2bfc8d
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions project_timesheet_holidays_contract/models/hr_employee.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@
class HrEmployee(models.Model):
_inherit = "hr.employee"

def _get_datetime_date(self, date):
return date if not isinstance(date, datetime) else date.date()

def _get_calendar(self, date_from=None):
res = super()._get_calendar(date_from)
if not date_from:
return res
date_from = self._get_datetime_date(date_from)
date_to = self.env.context.get("date_to", fields.Date.today())
date_to = self._get_datetime_date(date_to)
if not date_from: # no `date_from` => call `super()`
return super()._get_calendar(date_from)

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

View check run for this annotation

Codecov / codecov/patch

project_timesheet_holidays_contract/models/hr_employee.py#L14

Added line #L14 was not covered by tests

date_from = (
date_from if not isinstance(date_from, datetime) else date_from.date()
)
date_to = self.env.context.get("date_to", None)
if date_to is None:
date_to = fields.Date.today()

Check warning on line 21 in project_timesheet_holidays_contract/models/hr_employee.py

View check run for this annotation

Codecov / codecov/patch

project_timesheet_holidays_contract/models/hr_employee.py#L21

Added line #L21 was not covered by tests
date_to = date_to if not isinstance(date_to, datetime) else date_to.date()

contracts = (
self.sudo()
.with_context(active_test=True)
Expand All @@ -33,15 +35,23 @@ def _get_calendar(self, date_from=None):
]
)
)
if contracts:
# Filter out cancelled contracts
contracts = contracts.filtered(lambda c: c.state != "cancel")
if len(contracts) == 1:
return contracts[0].resource_calendar_id
# Multiple non-cancelled contracts for this date period
if contracts:
raise ValidationError(_("This period overlaps multiple contracts!"))
# If no contracts, call super() as a fallback

# If only one contract, return it regardless of its state
if len(contracts) == 1:
return contracts[0].resource_calendar_id

# Filter out cancelled contracts if there are multiple contracts
valid_contracts = contracts.filtered(lambda c: c.state != "cancel")

# If exactly one non-cancelled contract, return it
if len(valid_contracts) == 1:
return valid_contracts[0].resource_calendar_id

Check warning on line 48 in project_timesheet_holidays_contract/models/hr_employee.py

View check run for this annotation

Codecov / codecov/patch

project_timesheet_holidays_contract/models/hr_employee.py#L48

Added line #L48 was not covered by tests

# If there are multiple non-cancelled contracts, raise an error
if len(valid_contracts) > 1:
raise ValidationError(_("This period overlaps multiple contracts!"))

Check warning on line 52 in project_timesheet_holidays_contract/models/hr_employee.py

View check run for this annotation

Codecov / codecov/patch

project_timesheet_holidays_contract/models/hr_employee.py#L52

Added line #L52 was not covered by tests

# If no valid contracts remain, call `super()`
return super()._get_calendar(date_from)

Check warning on line 55 in project_timesheet_holidays_contract/models/hr_employee.py

View check run for this annotation

Codecov / codecov/patch

project_timesheet_holidays_contract/models/hr_employee.py#L55

Added line #L55 was not covered by tests

def _get_work_days_data_batch(
Expand Down

0 comments on commit f2bfc8d

Please sign in to comment.