Skip to content

Commit

Permalink
[WIP] pms-housekeeping: wip method to create automated tasksÇ
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelpadin authored and braisab committed Feb 21, 2024
1 parent 662a287 commit 1b10dd5
Show file tree
Hide file tree
Showing 8 changed files with 763 additions and 3 deletions.
17 changes: 17 additions & 0 deletions pms_housekeeping/models/hr_employee.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,26 @@ class HrEmployee(models.Model):
help="Rooms pre assigned to this employee",
)

allowed_pre_assigned_room_ids = fields.Many2many(
comodel_name="pms.room",
string="Allowed Pre Assigned Rooms",
help="Rooms allowed to be pre assigned to this employee",
compute="_compute_allowed_pre_assigned_room_ids",
)

job_name = fields.Char(string="Job Name", compute="_compute_job_name")

@api.depends("job_id")
def _compute_job_name(self):
for record in self:
record.job_name = record.job_id.name

@api.depends("property_ids")
def _compute_allowed_pre_assigned_room_ids(self):
for record in self:
domain = []
if record.property_ids:
domain.append(("pms_property_id", "in", record.property_ids.ids))
record.allowed_pre_assigned_room_ids = (
self.env["pms.room"].search(domain).ids
)
145 changes: 145 additions & 0 deletions pms_housekeeping/models/pms_housekeeping_task.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import timedelta

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

Expand Down Expand Up @@ -103,6 +105,12 @@ def _check_task_date(self):
_("Task Date must be greater than or equal to today")
)

@api.constrains("parent_id")
def _check_parent_id(self):
for rec in self:
if rec.parent_id.parent_id:
raise ValidationError(_("Parent task cannot have a parent task"))

def action_cancel(self):
for rec in self:
rec.state = "cancel"
Expand Down Expand Up @@ -235,3 +243,140 @@ def create(self, vals):
)

return super(PmsHouseKeepingTask, self).create(vals)

@api.model
def generate_tasks(self, pms_property_id):
for room in self.env["pms.room"].search(
[("pms_property_id", "=", pms_property_id.id)]
):
for task_type in self.env["pms.housekeeping.task.type"].search(
[
"|",
("pms_property_ids", "in", [pms_property_id.id]),
("pms_property_ids", "=", False),
],
order="priority asc",
):
if task_type.is_checkout:
reservations_with_checkout_today = self.env[
"pms.reservation"
].search(
[
("checkout", "=", fields.Date.today()),
]
)
reservation_line_with_checkout_today = self.env[
"pms.reservation.line"
].search(
[
(
"reservation_id",
"in",
reservations_with_checkout_today.ids,
),
("room_id", "=", room.id),
]
)
if reservation_line_with_checkout_today:
self.create_housekeeping_tasks(room, task_type)
break

if task_type.is_overnight:
reservation_line_today = self.env["pms.reservation.line"].search(
[
("room_id", "=", room.id),
("date", "=", fields.Date.today() + timedelta(days=-1)),
("occupies_availability", "=", True),
]
)
if reservation_line_today and len(reservation_line_today) == 1:
reservation_checkin = (
self.env["pms.reservation"]
.browse(reservation_line_today.reservation_id.id)
.checkin
)

days_between_checkin_and_today = (
fields.Date.today()
) - reservation_checkin
if (
days_between_checkin_and_today.days
% task_type.days_after_clean_overnight
== 0
):
self.create_housekeeping_tasks(room, task_type)
break
if task_type.is_checkin:
reservations_with_checkin_today = self.env[
"pms.reservation"
].search(
[
("checkin", "=", fields.Date.today()),
]
)
reservation_line_with_checkout_today = self.env[
"pms.reservation.line"
].search(
[
(
"reservation_id",
"in",
reservations_with_checkin_today.ids,
),
("room_id", "=", room.id),
]
)
if reservation_line_with_checkout_today:
self.create_housekeeping_tasks(room, task_type)
break
if task_type.is_empty:
previous_reservations = self.env["pms.reservation"].search(
[
("checkout", "<", fields.Date.today()),
("pms_property_id", "=", pms_property_id.id),
]
)
checkouts = (
self.env["pms.reservation.line"]
.search(
[
("reservation_id", "in", previous_reservations.ids),
("room_id", "=", room.id),
],
)
.mapped("date")
)

if checkouts:
last_checkout = max(checkouts)
days_between_last_checkout_and_today = (fields.Date.today()) - (
last_checkout + timedelta(days=1)
)
if (
days_between_last_checkout_and_today.days
% task_type.days_after_clean_empty
== 0
):
self.create_housekeeping_tasks(room, task_type)
break

def create_housekeeping_tasks(self, room, task_type):
task = self.env["pms.housekeeping.task"].create(
{
"name": task_type.name + " " + room.name,
"room_id": room.id,
"task_type_id": task_type.id,
"task_date": fields.Date.today(),
}
)

for task_type_child in task_type.child_ids:
self.env["pms.housekeeping.task"].create(
{
"name": task_type_child.name + " " + room.name,
"task_type_id": task_type_child.id,
"room_id": room.id,
"task_date": fields.Date.today(),
"parent_id": task.id,
}
)
63 changes: 61 additions & 2 deletions pms_housekeeping/models/pms_housekeeping_task_type.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from odoo import fields, models
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError


class PmsHouseKeepingTaskType(models.Model):
Expand All @@ -24,12 +25,70 @@ class PmsHouseKeepingTaskType(models.Model):
column1="task_type_id",
column2="employee_id",
string="Housekeepers",
domain="[('job_id.name', '=', 'Housekeeper')]",
)
parent_id = fields.Many2one(
string="Parent Task Type",
help="Indicates that this task type is a child of another task type",
comodel_name="pms.housekeeping.task.type",
domain="[('id', '!=', id)]",
)
child_ids = fields.One2many(
string="Child Task Types",
comodel_name="pms.housekeeping.task.type",
inverse_name="parent_id",
)
is_inspection = fields.Boolean(string="Inspection")

pms_property_ids = fields.Many2many(
comodel_name="pms.property",
relation="pms_housekeeping_task_type_pms_property_rel",
column1="task_type_id",
column2="property_id",
string="Properties",
)

allowed_housekeeper_ids = fields.Many2many(
comodel_name="hr.employee",
relation="pms_housekeeping_task_type_allowed_hr_employee_rel",
column1="task_type_id",
column2="employee_id",
string="Allowed Employees",
compute="_compute_allowed_housekeeper_ids",
)

@api.constrains("is_overnight", "days_after_clean_overnight")
def _check_days_after_clean_overnight(self):
for record in self:
if record.is_overnight and record.days_after_clean_overnight <= 0:
raise ValidationError(
_("Days After Clean Overnight should be greater than 0")
)

@api.constrains("is_empty", "days_after_clean_empty")
def _check_days_after_clean_empty(self):
for record in self:
if record.is_empty and record.days_after_clean_empty <= 0:
raise ValidationError(
_("Days After Clean Empty should be greater than 0")
)

@api.constrains("parent_id")
def _check_parent_id(self):
for rec in self:
if rec.parent_id.parent_id:
raise ValidationError(
_("Parent task type cannot have a parent task type")
)

@api.depends("pms_property_ids")
def _compute_allowed_housekeeper_ids(self):
for record in self:
domain = []
if record.pms_property_ids:
domain = [
"|",
("property_ids", "in", record.pms_property_ids.ids),
("property_ids", "in", False),
]
domain.append(("job_id.name", "=", "Housekeeper"))
record.allowed_housekeeper_ids = self.env["hr.employee"].search(domain).ids
1 change: 1 addition & 0 deletions pms_housekeeping/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_pms_housekeeping_task
64 changes: 64 additions & 0 deletions pms_housekeeping/tests/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from odoo.tests import common


class TestPms(common.SavepointCase):
def setUp(self):
super().setUp()
# delete all previous pms.housekeeping.task.type records (only for the test purpose)
self.env["pms.housekeeping.task.type"].search([]).unlink()
# create a sale channel
self.sale_channel1 = self.env["pms.sale.channel"].create(
{
"name": "Door",
"channel_type": "direct",
}
)
self.availability_plan1 = self.env["pms.availability.plan"].create(
{
"name": "Availability Plan 1",
}
)
self.pricelist1 = self.env["product.pricelist"].create(
{
"name": "Pricelist 1",
"availability_plan_id": self.availability_plan1.id,
"is_pms_available": True,
}
)
self.company1 = self.env["res.company"].create(
{
"name": "Company 1",
}
)
self.pms_property1 = self.env["pms.property"].create(
{
"name": "Property 1",
"company_id": self.company1.id,
"default_pricelist_id": self.pricelist1.id,
}
)
self.room_type_class1 = self.env["pms.room.type.class"].create(
{
"name": "Room Type Class 1",
"default_code": "RTC1",
}
)
self.room_type1 = self.env["pms.room.type"].create(
{
"name": "Room type 1",
"default_code": "c1",
"pms_property_ids": [(6, 0, [self.pms_property1.id])],
"company_id": self.company1.id,
"class_id": self.room_type_class1.id,
}
)

self.room1 = self.env["pms.room"].create(
{
"name": "Room 101",
"pms_property_id": self.pms_property1.id,
"room_type_id": self.room_type1.id,
}
)
# create partner
self.partner1 = self.env["res.partner"].create({"name": "Ana"})
Loading

0 comments on commit 1b10dd5

Please sign in to comment.