Skip to content

Commit

Permalink
[IMP] hr_employee_calendar_planning: Set calendar_ids default value t…
Browse files Browse the repository at this point in the history
…o cover all use cases

TT44093
  • Loading branch information
victoralmau committed Jul 3, 2023
1 parent 7261e0a commit 372b3eb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
29 changes: 12 additions & 17 deletions hr_employee_calendar_planning/models/hr_employee.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ class HrEmployee(models.Model):
copy=True,
)

@api.model
def default_get(self, fields):
"""Set calendar_ids default value to cover all use cases."""
vals = super().default_get(fields)
if "calendar_ids" in fields and not vals.get("calendar_ids"):
vals["calendar_ids"] = [
(0, 0, {"calendar_id": self.env.company.resource_calendar_id.id}),
]
return vals

def _regenerate_calendar(self):
self.ensure_one()
vals_list = []
Expand Down Expand Up @@ -122,32 +132,17 @@ def copy(self, default=None):
new.filtered("calendar_ids").regenerate_calendar()
return new

def _sync_user(self, user):
res = super()._sync_user(user=user)
# set calendar_ids from Create employee button from user
if not self.calendar_ids:
res.update(
{
"calendar_ids": [
(
0,
0,
{"calendar_id": user.company_id.resource_calendar_id.id},
),
]
}
)
return res

@api.model_create_multi
def create(self, vals_list):
res = super().create(vals_list)
# Avoid creating an employee without calendars
if (
not self.env.context.get("skip_employee_calendars_required")
and not config["test_enable"]
and res.filtered(lambda x: not x.calendar_ids)
):
raise UserError(_("You can not create employees without any calendars."))
# Regenerate calendar
res.filtered("calendar_ids").regenerate_calendar()
return res

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def setUpClass(cls):
mail_create_nosubscribe=True,
mail_notrack=True,
no_reset_password=True,
tracking_disable=True,
test_hr_employee_calendar_planning=True,
)
)
resource_calendar = cls.env["resource.calendar"]
Expand Down Expand Up @@ -73,6 +75,8 @@ def setUpClass(cls):
"date_to": "2019-06-10",
}
)
# By default a calendar_ids is set, we remove it to better clarify the tests.
cls.employee.write({"calendar_ids": [(2, cls.employee.calendar_ids.id)]})

def test_calendar_planning(self):
self.employee.calendar_ids = [
Expand Down

0 comments on commit 372b3eb

Please sign in to comment.