[WIP] pms-housekeeping: wip method to create automated tasksÇ

This commit is contained in:
miguelpadin
2024-02-20 17:46:48 +00:00
committed by Darío Lodeiros
parent ade0673c81
commit 30f0748cb8
13 changed files with 901 additions and 6 deletions

View File

@@ -1,7 +1,8 @@
# Copyright 2020 Jose Luis Algara (Alda Hotels <https://www.aldahotels.es>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import api, fields, models
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
class HrEmployee(models.Model):
@@ -13,9 +14,47 @@ 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.constrains("pre_assigned_room_ids")
def _check_pre_assigned_room_ids(self):
for record in self:
if record.pre_assigned_room_ids:
for room in record.pre_assigned_room_ids:
if room not in record.allowed_pre_assigned_room_ids:
raise ValidationError(
_("The room should belong to the employee's property.")
)
@api.constrains("pre_assigned_room_ids")
def _check_job_id(self):
for record in self:
if (
record.job_id
and record.job_id
!= self.env.ref("pms_housekeeping.housekeeping_job_id")
and record.pre_assigned_room_ids
):
raise ValidationError(_("The job position should be Housekeeper."))
@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
)

View File

@@ -1,3 +1,5 @@
from datetime import timedelta
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
@@ -103,6 +105,12 @@ class PmsHouseKeepingTask(models.Model):
_("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"
@@ -235,3 +243,144 @@ class PmsHouseKeepingTask(models.Model):
)
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,
}
)
def generate_task_properties(self):
for pms_property in self.env["pms.property"].search([]):
self.generate_tasks(pms_property)

View File

@@ -1,4 +1,5 @@
from odoo import fields, models
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
class PmsHouseKeepingTaskType(models.Model):
@@ -24,7 +25,6 @@ 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",
@@ -32,4 +32,63 @@ class PmsHouseKeepingTaskType(models.Model):
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