Skip to content

Commit

Permalink
Merge PR #1266 into 15.0
Browse files Browse the repository at this point in the history
Signed-off-by pedrobaeza
  • Loading branch information
OCA-git-bot committed Jul 5, 2023
2 parents 6a306f1 + 33b0bdb commit 4529db4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
35 changes: 13 additions & 22 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 @@ -151,28 +161,10 @@ def copy(self, default=None):
new.filtered("calendar_ids").regenerate_calendar()
return new

def _sync_user(self, user, employee_has_image=False):
res = super()._sync_user(user=user, employee_has_image=employee_has_image)
# 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"]
Expand All @@ -196,16 +188,15 @@ class HrEmployeeCalendar(models.Model):
string="End Date",
)
employee_id = fields.Many2one(
comodel_name="hr.employee",
string="Employee",
required=True,
comodel_name="hr.employee", string="Employee", required=True, ondelete="cascade"
)
company_id = fields.Many2one(related="employee_id.company_id")
calendar_id = fields.Many2one(
comodel_name="resource.calendar",
string="Working Time",
required=True,
check_company=True,
ondelete="restrict",
)

_sql_constraints = [
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 @@ -98,6 +100,8 @@ def setUpClass(cls):
(6, 0, [cls.global_leave1.id, cls.global_leave2.id])
]
cls.calendar2.global_leave_ids = [(6, 0, [cls.global_leave3.id])]
# 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 4529db4

Please sign in to comment.