mirror of
https://github.com/OCA/pms.git
synced 2025-01-29 00:17:45 +02:00
[IMP] pms: obtaining all room types of a property (#37)
Get all room types of a property excluding the room types with the same codes and without properties or company This is a generalization of the current behavior allowing to search also without specifying a room type code
This commit is contained in:
@@ -78,41 +78,49 @@ class PmsRoomType(models.Model):
|
|||||||
record.total_rooms_count = len(record.room_ids)
|
record.total_rooms_count = len(record.room_ids)
|
||||||
|
|
||||||
@api.model
|
@api.model
|
||||||
def get_unique_by_code_property(self, code_type, pms_property_id):
|
def get_unique_by_property_code(self, pms_property_id, code_type=None):
|
||||||
"""
|
"""
|
||||||
:param code_type: room type code
|
|
||||||
:param pms_property_id: property ID
|
:param pms_property_id: property ID
|
||||||
:return: one or 0 room types ValidationError if more than one found
|
:param code_type: room type code (optional)
|
||||||
|
:return: - recordset of
|
||||||
|
- all the pms.room.type of the pms_property_id
|
||||||
|
if code_type not defined
|
||||||
|
- one or 0 pms.room.type if code_type defined
|
||||||
|
- ValidationError if more than one code_type found by
|
||||||
|
the same pms_property_id
|
||||||
"""
|
"""
|
||||||
|
domain = []
|
||||||
|
if code_type:
|
||||||
|
domain += ["&", ("code_type", "=", code_type)]
|
||||||
company_id = self.env["pms.property"].browse(pms_property_id).company_id.id
|
company_id = self.env["pms.property"].browse(pms_property_id).company_id.id
|
||||||
records = self.search(
|
domain += [
|
||||||
[
|
"|",
|
||||||
"&",
|
("pms_property_ids", "in", pms_property_id),
|
||||||
("code_type", "=", code_type),
|
"|",
|
||||||
"|",
|
"&",
|
||||||
("pms_property_ids", "in", pms_property_id),
|
("pms_property_ids", "=", False),
|
||||||
"|",
|
("company_id", "=", company_id),
|
||||||
"&",
|
"&",
|
||||||
("pms_property_ids", "=", False),
|
("pms_property_ids", "=", False),
|
||||||
("company_id", "=", company_id),
|
("company_id", "=", False),
|
||||||
"&",
|
]
|
||||||
("pms_property_ids", "=", False),
|
records = self.search(domain)
|
||||||
("company_id", "=", False),
|
res, res_priority = {}, {}
|
||||||
]
|
|
||||||
)
|
|
||||||
res, res_priority = self, -1
|
|
||||||
for rec in records:
|
for rec in records:
|
||||||
|
res_priority.setdefault(rec.code_type, -1)
|
||||||
priority = (rec.pms_property_ids and 2) or (rec.company_id and 1 or 0)
|
priority = (rec.pms_property_ids and 2) or (rec.company_id and 1 or 0)
|
||||||
if priority > res_priority:
|
if priority > res_priority[rec.code_type]:
|
||||||
res, res_priority = rec, priority
|
res.setdefault(rec.code_type, rec.id)
|
||||||
elif priority == res_priority:
|
res[rec.code_type], res_priority[rec.code_type] = rec.id, priority
|
||||||
|
elif priority == res_priority[rec.code_type]:
|
||||||
raise ValidationError(
|
raise ValidationError(
|
||||||
_(
|
_(
|
||||||
"Integrity error: There's multiple room types "
|
"Integrity error: There's multiple room types with code %s"
|
||||||
"with the same code and properties"
|
"with the same code and properties"
|
||||||
)
|
)
|
||||||
|
% rec.code_type
|
||||||
)
|
)
|
||||||
return res
|
return self.browse(list(res.values()))
|
||||||
|
|
||||||
@api.constrains("pms_property_ids", "company_id")
|
@api.constrains("pms_property_ids", "company_id")
|
||||||
def _check_property_company_integrity(self):
|
def _check_property_company_integrity(self):
|
||||||
@@ -144,7 +152,7 @@ class PmsRoomType(models.Model):
|
|||||||
else:
|
else:
|
||||||
for pms_property in rec.pms_property_ids:
|
for pms_property in rec.pms_property_ids:
|
||||||
if (
|
if (
|
||||||
rec.get_unique_by_code_property(rec.code_type, pms_property.id)
|
rec.get_unique_by_property_code(pms_property.id, rec.code_type)
|
||||||
!= rec
|
!= rec
|
||||||
):
|
):
|
||||||
raise ValidationError(msg)
|
raise ValidationError(msg)
|
||||||
|
|||||||
@@ -286,8 +286,8 @@ class TestRoomTypeCodePropertyUniqueness(TestRoomType):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# ACT
|
# ACT
|
||||||
room_type = self.env["pms.room.type"].get_unique_by_code_property(
|
room_type = self.env["pms.room.type"].get_unique_by_property_code(
|
||||||
"c1", self.p1.id
|
self.p1.id, "c1"
|
||||||
)
|
)
|
||||||
|
|
||||||
# ASSERT
|
# ASSERT
|
||||||
@@ -316,8 +316,8 @@ class TestRoomTypeCodePropertyUniqueness(TestRoomType):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# ACT
|
# ACT
|
||||||
room_type = self.env["pms.room.type"].get_unique_by_code_property(
|
room_type = self.env["pms.room.type"].get_unique_by_property_code(
|
||||||
"c1", self.p1.id
|
self.p1.id, "c1"
|
||||||
)
|
)
|
||||||
|
|
||||||
# ASSERT
|
# ASSERT
|
||||||
@@ -346,8 +346,8 @@ class TestRoomTypeCodePropertyUniqueness(TestRoomType):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# ACT
|
# ACT
|
||||||
room_type = self.env["pms.room.type"].get_unique_by_code_property(
|
room_type = self.env["pms.room.type"].get_unique_by_property_code(
|
||||||
"c1", self.p3.id
|
self.p3.id, "c1"
|
||||||
)
|
)
|
||||||
|
|
||||||
# ASSERT
|
# ASSERT
|
||||||
@@ -374,8 +374,8 @@ class TestRoomTypeCodePropertyUniqueness(TestRoomType):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# ACT
|
# ACT
|
||||||
room_type = self.env["pms.room.type"].get_unique_by_code_property(
|
room_type = self.env["pms.room.type"].get_unique_by_property_code(
|
||||||
"c1", self.p1.id
|
self.p1.id, "c1"
|
||||||
)
|
)
|
||||||
|
|
||||||
# ASSERT
|
# ASSERT
|
||||||
@@ -403,8 +403,8 @@ class TestRoomTypeCodePropertyUniqueness(TestRoomType):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# ACT
|
# ACT
|
||||||
room_type = self.env["pms.room.type"].get_unique_by_code_property(
|
room_type = self.env["pms.room.type"].get_unique_by_property_code(
|
||||||
"c1", self.p3.id
|
self.p3.id, "c1"
|
||||||
)
|
)
|
||||||
|
|
||||||
# ASSERT
|
# ASSERT
|
||||||
@@ -446,8 +446,8 @@ class TestRoomTypeCodePropertyUniqueness(TestRoomType):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# ACT
|
# ACT
|
||||||
room_type = self.env["pms.room.type"].get_unique_by_code_property(
|
room_type = self.env["pms.room.type"].get_unique_by_property_code(
|
||||||
"c1", self.p1.id
|
self.p1.id, "c1"
|
||||||
)
|
)
|
||||||
|
|
||||||
# ASSERT
|
# ASSERT
|
||||||
@@ -488,8 +488,8 @@ class TestRoomTypeCodePropertyUniqueness(TestRoomType):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# ACT
|
# ACT
|
||||||
room_type = self.env["pms.room.type"].get_unique_by_code_property(
|
room_type = self.env["pms.room.type"].get_unique_by_property_code(
|
||||||
"c1", self.p2.id
|
self.p2.id, "c1"
|
||||||
)
|
)
|
||||||
|
|
||||||
# ASSERT
|
# ASSERT
|
||||||
@@ -530,8 +530,8 @@ class TestRoomTypeCodePropertyUniqueness(TestRoomType):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# ACT
|
# ACT
|
||||||
room_type = self.env["pms.room.type"].get_unique_by_code_property(
|
room_type = self.env["pms.room.type"].get_unique_by_property_code(
|
||||||
"c1", self.p3.id
|
self.p3.id, "c1"
|
||||||
)
|
)
|
||||||
|
|
||||||
# ASSERT
|
# ASSERT
|
||||||
@@ -573,8 +573,8 @@ class TestRoomTypeCodePropertyUniqueness(TestRoomType):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# ACT
|
# ACT
|
||||||
room_type = self.env["pms.room.type"].get_unique_by_code_property(
|
room_type = self.env["pms.room.type"].get_unique_by_property_code(
|
||||||
"c1", self.p3.id
|
self.p3.id, "c1"
|
||||||
)
|
)
|
||||||
|
|
||||||
# ASSERT
|
# ASSERT
|
||||||
@@ -615,8 +615,8 @@ class TestRoomTypeCodePropertyUniqueness(TestRoomType):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# ACT
|
# ACT
|
||||||
room_type = self.env["pms.room.type"].get_unique_by_code_property(
|
room_type = self.env["pms.room.type"].get_unique_by_property_code(
|
||||||
"c1", self.p3.id
|
self.p3.id, "c1"
|
||||||
)
|
)
|
||||||
|
|
||||||
# ASSERT
|
# ASSERT
|
||||||
@@ -670,8 +670,8 @@ class TestRoomTypeCodePropertyUniqueness(TestRoomType):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# ACT
|
# ACT
|
||||||
room_type = self.env["pms.room.type"].get_unique_by_code_property(
|
room_type = self.env["pms.room.type"].get_unique_by_property_code(
|
||||||
"c1", self.p3.id
|
self.p3.id, "c1"
|
||||||
)
|
)
|
||||||
|
|
||||||
# ASSERT
|
# ASSERT
|
||||||
|
|||||||
Reference in New Issue
Block a user