Skip to content

Commit

Permalink
Merge PR #1263 into 13.0
Browse files Browse the repository at this point in the history
Signed-off-by pedrobaeza
  • Loading branch information
OCA-git-bot committed Jul 4, 2023
2 parents 6a0b62b + 045dde5 commit dbdd084
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
32 changes: 14 additions & 18 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 All @@ -160,14 +155,15 @@ class HrEmployeeCalendar(models.Model):
date_start = fields.Date(string="Start Date",)
date_end = fields.Date(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 @@ -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 dbdd084

Please sign in to comment.