Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[13.0][IMP] hr_employee_calendar_planning: Set calendar_ids default value to cover all use cases #1263

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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