Browse Source

[FIX] beesdoo_shift: dynamic selection field

Add lambda so there is no more need to redefine field states
Add parameter today to _get_irregular_worker_domain

Move specific constrains to beesdoo_worker_status
12.0-macavrac-prod
Thibault Francois 4 years ago
parent
commit
bd66a9808e
  1. 13
      beesdoo_shift/models/cooperative_status.py
  2. 40
      beesdoo_shift/models/task.py
  3. 2
      beesdoo_shift/views/cooperative_status.xml
  4. 3
      beesdoo_worker_status/models/cooperative_status.py
  5. 40
      beesdoo_worker_status/models/task.py

13
beesdoo_shift/models/cooperative_status.py

@ -91,7 +91,7 @@ class CooperativeStatus(models.Model):
comodel_name="cooperative.exempt.reason", string="Exempt Reason"
)
status = fields.Selection(
selection=_get_status,
selection=lambda x: x._get_status(),
compute="_compute_status",
string="Cooperative Status",
store=True,
@ -106,11 +106,10 @@ class CooperativeStatus(models.Model):
)
# Specific to irregular
irregular_start_date = fields.Date() # TODO migration script
irregular_start_date = fields.Date()
irregular_absence_date = fields.Date()
irregular_absence_counter = (
fields.Integer()
) # TODO unsubscribe when reach -2
irregular_absence_counter = fields.Integer()
future_alert_date = fields.Date(compute="_compute_future_alert_date")
next_countdown_date = fields.Date(compute="_compute_next_countdown_date")
@ -273,7 +272,7 @@ class CooperativeStatus(models.Model):
if not journal:
journal = self.env["beesdoo.shift.journal"].create({"date": today})
domain = self._get_irregular_worker_domain(today=today)
domain = self._get_irregular_worker_domain(today)
irregular = self.search(domain)
for status in irregular:
delta = (today - status.irregular_start_date).days
@ -362,7 +361,7 @@ class CooperativeStatus(models.Model):
# Irregular Cron implementation #
###############################################
def _get_irregular_worker_domain(self):
def _get_irregular_worker_domain(self, today):
"""
return the domain the give the list
of valid irregular worker that should

40
beesdoo_shift/models/task.py

@ -59,7 +59,7 @@ class Task(models.Model):
)
end_time = fields.Datetime(track_visibility="always", required=True)
state = fields.Selection(
selection=_get_selection_status,
selection=lambda x: x._get_selection_status(),
default="open",
required=True,
track_visibility="onchange",
@ -96,20 +96,6 @@ class Task(models.Model):
for rec in self:
rec.color = self._get_color_mapping(rec.state)
def _compensation_validation(self, task):
"""
Raise a validation error if the fields is_regular and
is_compensation are not properly set.
"""
if task.is_regular == task.is_compensation or not (
task.is_regular or task.is_compensation
):
raise ValidationError(
_(
"You must choose between Regular Shift or "
"Compensation Shift."
)
)
@api.constrains("state")
def _lock_future_task(self):
@ -122,30 +108,6 @@ class Task(models.Model):
)
)
@api.constrains("is_regular", "is_compensation")
def _check_compensation(self):
for task in self:
if task.working_mode == "regular":
self._compensation_validation(task)
@api.constrains("worker_id")
def _check_worker_id(self):
"""
When worker_id changes we need to check whether is_regular
and is_compensation are set correctly.
When worker_id is set to a worker that doesn't need field
is_regular and is_compensation, these two fields are set to
False.
"""
for task in self:
if task.working_mode == "regular":
self._compensation_validation(task)
else:
task.write({"is_regular": False, "is_compensation": False})
if task.worker_id:
if task.worker_id == task.replaced_id:
raise UserError(_("A worker cannot replace himself."))
def message_auto_subscribe(self, updated_fields, values=None):
self._add_follower(values)
return super(Task, self).message_auto_subscribe(

2
beesdoo_shift/views/cooperative_status.xml

@ -161,7 +161,7 @@
<field name="holiday_end_time"/>
<field name="alert_start_time"/>
<field name="extension_start_time"/>
<field name="status" widget="statusbar"/>
<field name="status" />
</tree>
</field>
</record>

3
beesdoo_worker_status/models/cooperative_status.py

@ -320,8 +320,7 @@ class CooperativeStatus(models.Model):
# Irregular Cron implementation #
###############################################
def _get_irregular_worker_domain(self, **kwargs):
today = kwargs.get("today") or self.today
def _get_irregular_worker_domain(self, today):
return [
"&",
"&",

40
beesdoo_worker_status/models/task.py

@ -4,6 +4,46 @@ from odoo import fields, models
class Task(models.Model):
_inherit = "beesdoo.shift.shift"
@api.constrains("worker_id")
def _check_worker_id(self):
"""
When worker_id changes we need to check whether is_regular
and is_compensation are set correctly.
When worker_id is set to a worker that doesn't need field
is_regular and is_compensation, these two fields are set to
False.
"""
for task in self:
if task.working_mode == "regular":
self._compensation_validation(task)
else:
task.write({"is_regular": False, "is_compensation": False})
if task.worker_id:
if task.worker_id == task.replaced_id:
raise UserError(_("A worker cannot replace himself."))
def _compensation_validation(self, task):
"""
Raise a validation error if the fields is_regular and
is_compensation are not properly set.
"""
if task.is_regular == task.is_compensation or not (
task.is_regular or task.is_compensation
):
raise ValidationError(
_(
"You must choose between Regular Shift or "
"Compensation Shift."
)
)
@api.constrains("is_regular", "is_compensation")
def _check_compensation(self):
for task in self:
if task.working_mode == "regular":
self._compensation_validation(task)
#################################
# State Definition #
#################################

Loading…
Cancel
Save