Skip to content

Commit

Permalink
[ADD] beesdoo_shift: next_shift_id on cooperative.status
Browse files Browse the repository at this point in the history
  • Loading branch information
robinkeunen committed Feb 18, 2022
1 parent c2fc019 commit e0ac974
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 4 deletions.
61 changes: 59 additions & 2 deletions beesdoo_shift/models/cooperative_status.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import logging
from datetime import timedelta
from datetime import datetime, timedelta

from odoo import _, api, fields, models
from odoo.exceptions import UserError, ValidationError

_logger = logging.getLogger(__name__)

# todo rename mentions to cooperatives


def add_days_delta(date_from, days_delta):
if not date_from:
Expand Down Expand Up @@ -65,7 +67,7 @@ def _get_status(self):
"are based on the current date",
default=fields.Date.today,
)
cooperator_id = fields.Many2one("res.partner")
cooperator_id = fields.Many2one("res.partner") # todo rename to worker_id
active = fields.Boolean(
related="cooperator_id.active", store=True, index=True
)
Expand Down Expand Up @@ -119,6 +121,23 @@ def _get_status(self):
) # TODO unsubscribe when reach -2
future_alert_date = fields.Date(compute="_compute_future_alert_date")
next_countdown_date = fields.Date(compute="_compute_next_countdown_date")
next_shift_id = fields.Many2one(
comodel_name="beesdoo.shift.shift",
string="Next Shift",
compute="_compute_next_shift",
store=True,
help="Next shift the worker is subscribed to.",
)
next_shift_date = fields.Datetime(
related="next_shift_id.start_time",
string="Next Shift Date",
store=True,
)
is_subscribed_to_shift = fields.Boolean(
string="Subscribed before Alert Date",
compute="_compute_next_shift",
store=True,
)

temporary_exempt_reason_id = fields.Many2one(
comodel_name="cooperative.exempt.reason",
Expand Down Expand Up @@ -403,6 +422,44 @@ def _compute_next_countdown_date(self):
for rec in self:
rec.next_countdown_date = False

@api.multi
@api.depends(
"cooperator_id.shift_shift_ids",
"cooperator_id.shift_shift_ids.start_time",
"future_alert_date",
)
def _compute_next_shift(self):
now = fields.Datetime.now()
cooperator_ids = self.mapped("cooperator_id").ids
# avoid searching for shift in loop
next_shifts = self.env["beesdoo.shift.shift"].search(
[("start_time", ">=", now), ("worker_id", "in", cooperator_ids)],
order="worker_id, start_time",
)

next_shift_by_worker = {}
for shift in next_shifts:
# take fist shift for each worker
if shift.worker_id not in next_shift_by_worker:
next_shift_by_worker[shift.worker_id] = shift

for rec in self:
rec.next_shift_id = next_shift_by_worker[rec.cooperator_id]
if rec.next_shift_id:
if rec.future_alert_date:
future_alert_date_time = datetime(
rec.future_alert_date.year,
rec.future_alert_date.month,
rec.future_alert_date.day,
)
rec.is_subscribed_to_shift = (
rec.next_shift_id.start_time < future_alert_date_time
)
else:
rec.is_subscribed_to_shift = True
else:
rec.is_subscribed_to_shift = False

def _can_shop_status(self):
"""
return the list of status that give access
Expand Down
17 changes: 17 additions & 0 deletions beesdoo_shift/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class ResPartner(models.Model):
compute="_compute_can_shop",
store=True,
)
# todo implement as delegated inheritance ?
# resolve as part of migration to OCA
cooperative_status_ids = fields.One2many(
string="Cooperative Statuses",
comodel_name="cooperative.status",
Expand Down Expand Up @@ -66,6 +68,21 @@ class ResPartner(models.Model):
subscribed_shift_ids = fields.Many2many(
comodel_name="beesdoo.shift.template", readonly=True
)
shift_shift_ids = fields.One2many(
comodel_name="beesdoo.shift.shift",
inverse_name="worker_id",
string="Shifts",
help="All the shifts the worker is subscribed to.",
)
next_shift_id = fields.Many2one(
related="cooperative_status_ids.next_shift_id", store=True,
)
is_subscribed_to_shift = fields.Boolean(
related="cooperative_status_ids.is_subscribed_to_shift", store=True,
)
next_shift_date = fields.Datetime(
related="cooperative_status_ids.next_shift_date", store=True,
)

@api.depends("cooperative_status_ids")
def _compute_can_shop(self):
Expand Down
2 changes: 0 additions & 2 deletions beesdoo_shift/models/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

class Task(models.Model):
_name = "beesdoo.shift.shift"

_inherit = ["mail.thread"]

_order = "start_time asc"

##################################
Expand Down
62 changes: 62 additions & 0 deletions beesdoo_shift/tests/test_beesdoo_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def setUp(self):
self.worker_regular_1 = self.env.ref(
"beesdoo_shift.res_partner_worker_1_demo"
)
self.worker_irregular_2 = self.env.ref(
"beesdoo_shift.res_partner_worker_2_demo"
)
self.worker_regular_3 = self.env.ref(
"beesdoo_shift.res_partner_worker_3_demo"
)
Expand Down Expand Up @@ -473,3 +476,62 @@ def test_change_worker_temporary_exemption_2(self):
)
exemption_wiz.exempt()
self.assertEqual(self._count_number_of_shift(self.worker_regular_1), 4)

def test_irregular_worker_subscribed_to_shift_before_alert(self):
self._generate_shifts(days=1, nb=2)
self.assertFalse(
self.worker_irregular_2.cooperative_status_ids.next_shift_id
)

some_empty_shift = self.shift_model.search(
[("start_time", ">=", datetime.now()), ("worker_id", "=", False)],
limit=1,
)

some_empty_shift.worker_id = self.worker_irregular_2
self.assertEqual(
self.worker_irregular_2.cooperative_status_ids.next_shift_id,
some_empty_shift,
)

some_empty_shift.worker_id = False
self.assertFalse(
self.worker_irregular_2.cooperative_status_ids.next_shift_id
)

def test_unsubscribe_worker_from_task_template_computes_next_shift(self):
self._generate_shifts(days=1, nb=2)
now = datetime.now()
# Check that initialisation works well
next_shift = self.shift_model.search(
[
("start_time", ">=", now),
("worker_id", "=", self.worker_regular_1.id),
("task_template_id", "=", self.task_template_1.id),
]
)
self.assertEqual(
self.worker_regular_1.cooperative_status_ids.next_shift_id,
next_shift,
)

# Unsubscribe a worker from the task template
self.task_template_1.worker_ids -= self.worker_regular_1

self.assertFalse(
self.worker_regular_1.cooperative_status_ids.next_shift_id
)

# Subscribe a worker from the task template
self.task_template_1.worker_ids += self.worker_regular_1
next_shift = self.shift_model.search(
[
("start_time", ">=", now),
("worker_id", "=", self.worker_regular_1.id),
("task_template_id", "=", self.task_template_1.id),
]
)
self.assertEqual(
self.worker_regular_1.cooperative_status_ids.next_shift_id,
next_shift,
)
7 changes: 7 additions & 0 deletions beesdoo_shift/views/cooperative_status.xml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@
<field name="can_shop" />
</group>
<group string="Timing information">
<field name="next_shift_id" />
<field name="next_shift_date" />
<field
name="next_countdown_date"
readonly="1"
Expand All @@ -159,6 +161,11 @@
readonly="1"
attrs="{'invisible':[('working_mode', '!=', 'irregular')]}"
/>
<field
name="is_subscribed_to_shift"
readonly="1"
attrs="{'invisible':[('working_mode', '!=', 'irregular')]}"
/>
<field name="time_extension" />
<field name="holiday_start_time" />
<field name="holiday_end_time" />
Expand Down

0 comments on commit e0ac974

Please sign in to comment.