[IMP] pms: added unique code by property on Board Service (#80)

This commit is contained in:
Eric Antones
2021-04-19 09:17:50 +02:00
committed by GitHub
parent c19d6a76f6
commit 38a71c3b25
8 changed files with 502 additions and 1 deletions

View File

@@ -331,6 +331,7 @@
<!-- pms.board.service -->
<record id="pms_board_service_0" model="pms.board.service">
<field name="name">BreakFast</field>
<field name="code_board">BB</field>
<field
name="board_service_line_ids"
eval="[(5, 0), (0, 0, {
@@ -340,6 +341,7 @@
</record>
<record id="pms_board_service_1" model="pms.board.service">
<field name="name">Half Board</field>
<field name="code_board">HB</field>
<field
name="board_service_line_ids"
eval="[(5, 0), (0, 0, {
@@ -352,6 +354,7 @@
</record>
<record id="pms_board_service_2" model="pms.board.service">
<field name="name">FullBoard</field>
<field name="code_board">FB</field>
<field
name="board_service_line_ids"
eval="[(5, 0), (0, 0, {

View File

@@ -1,6 +1,7 @@
# Copyright 2017 Dario Lodeiros
# 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 PmsBoardService(models.Model):
@@ -15,6 +16,11 @@ class PmsBoardService(models.Model):
size=64,
translate=True,
)
code_board = fields.Char(
string="Board Service Code",
help="Unique Board Service identification code per property",
required=True,
)
board_service_line_ids = fields.One2many(
string="Board Service Lines",
help="Services included in this Board Service",
@@ -59,3 +65,66 @@ class PmsBoardService(models.Model):
for service in record.board_service_line_ids:
total += service.amount
record.update({"amount": total})
@api.model
def get_unique_by_property_code(self, pms_property_id, code_board=None):
"""
:param pms_property_id: property ID
:param code_board: board service code (optional)
:return: - recordset of
- all the pms.board.service of the pms_property_id
if code_board not defined
- one or 0 pms.board.service if code_board defined
- ValidationError if more than one code_board found by
the same pms_property_id
"""
# TODO: similiar code as room.type -> unify
domain = []
if code_board:
domain += ["&", ("code_board", "=", code_board)]
domain += [
"|",
("pms_property_ids", "in", pms_property_id),
("pms_property_ids", "=", False),
]
records = self.search(domain)
res, res_priority = {}, {}
for rec in records:
res_priority.setdefault(rec.code_board, -1)
priority = rec.pms_property_ids and 1 or 0
if priority > res_priority[rec.code_board]:
res.setdefault(rec.code_board, rec.id)
res[rec.code_board], res_priority[rec.code_board] = rec.id, priority
elif priority == res_priority[rec.code_board]:
raise ValidationError(
_(
"Integrity error: There's multiple board services "
"with the same code %s and properties"
)
% rec.code_board
)
return self.browse(list(res.values()))
@api.constrains("code_board", "pms_property_ids")
def _check_code_property_uniqueness(self):
# TODO: similiar code as room.type -> unify
msg = _(
"Already exists another Board Service with the same code and properties"
)
for rec in self:
if not rec.pms_property_ids:
if self.search(
[
("id", "!=", rec.id),
("code_board", "=", rec.code_board),
("pms_property_ids", "=", False),
]
):
raise ValidationError(msg)
else:
for pms_property in rec.pms_property_ids:
other = rec.get_unique_by_property_code(
pms_property.id, rec.code_board
)
if other and other != rec:
raise ValidationError(msg)

View File

@@ -28,6 +28,7 @@ from . import test_pms_folio
from . import test_pms_availability_plan_rules
from . import test_pms_room_type
from . import test_pms_room_type_class
from . import test_pms_board_service
from . import test_pms_wizard_massive_changes
from . import test_pms_wizard_folio
from . import test_pms_res_users

View File

@@ -0,0 +1,423 @@
# Copyright 2021 Eric Antones <eantones@nuobit.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo.exceptions import ValidationError
from .common import TestPms
class TestBoardService(TestPms):
def setUp(self):
super().setUp()
self.company2 = self.env["res.company"].create(
{
"name": "Company 2",
}
)
self.pms_property3 = self.env["pms.property"].create(
{
"name": "Property 3",
"company_id": self.company2.id,
"default_pricelist_id": self.pricelist1.id,
}
)
# external integrity
def test_external_case_01(self):
"""
PRE: - board service bs1 exists
- board_service1 has code c1
- board_service1 has pms_property1
- pms_property1 has company company1
ACT: - create a new board_service2
- board_service2 has code c1
- board_service2 has pms_property1
- pms_property1 has company company1
POST: - Integrity error: the room type already exists
- board_service2 not created
"""
# ARRANGE
# board_service1
self.env["pms.board.service"].create(
{
"name": "Board service bs1",
"code_board": "c1",
"pms_property_ids": [(6, 0, [self.pms_property1.id])],
}
)
# ACT & ASSERT
with self.assertRaises(
ValidationError, msg="The board service has been created and it shouldn't"
):
# board_service2
self.env["pms.board.service"].create(
{
"name": "Board service bs2",
"code_board": "c1",
"pms_property_ids": [(6, 0, [self.pms_property1.id])],
}
)
def test_external_case_02(self):
"""
PRE: - board service bs1 exists
- board_service1 has code c1
- board_service1 has property pms_property1
- pms_property1 has company company1
ACT: - create a new board_service2
- board_service2 has code c1
- board_service2 has property pms_property1, pms_property2,
pms_property3
- pms_property1, pms_property2 has company company1
- pms_property3 has company company2
POST: - Integrity error: the board service already exists
- board_service2 not created
"""
# ARRANGE
self.pms_property2 = self.env["pms.property"].create(
{
"name": "Property 2",
"company_id": self.company1.id,
"default_pricelist_id": self.pricelist1.id,
}
)
# board_service1
self.env["pms.board.service"].create(
{
"name": "Board service 1",
"code_board": "c1",
"pms_property_ids": [(6, 0, [self.pms_property1.id])],
}
)
# ACT & ASSERT
with self.assertRaises(
ValidationError, msg="The board service has been created and it shouldn't"
):
# board_service2
self.env["pms.board.service"].create(
{
"name": "Board service bs2",
"code_board": "c1",
"pms_property_ids": [
(
6,
0,
[
self.pms_property1.id,
self.pms_property2.id,
self.pms_property3.id,
],
)
],
}
)
def test_single_case_01(self):
"""
PRE: - board service bs1 exists
- board_service1 has code c1
- board_service1 has 2 properties pms_property1 and pms_property2
- pms_property_1 and pms_property2 have the same company company1
ACT: - search board service with code c1 and pms_property1
- pms_property1 has company company1
POST: - only board_service1 board service found
"""
# ARRANGE
board_service1 = self.env["pms.board.service"].create(
{
"name": "Board service 1",
"code_board": "c1",
"pms_property_ids": [
(6, 0, [self.pms_property1.id, self.pms_property3.id])
],
}
)
# ACT
board_services = self.env["pms.board.service"].get_unique_by_property_code(
self.pms_property1.id, "c1"
)
# ASSERT
self.assertEqual(
board_services.id,
board_service1.id,
"Expected board service not found",
)
def test_single_case_02(self):
"""
PRE: - board service bs1 exists
- board_service1 has code c1
- board_service1 has 2 properties pms_property1 and pms_property3
- pms_property1 and pms_property2 have different companies
- pms_property1 have company company1 and pms_property3 have company2
ACT: - search board service with code c1 and property pms_property1
- pms_property1 has company company1
POST: - only board_service1 room type found
"""
# ARRANGE
bs1 = self.env["pms.board.service"].create(
{
"name": "Board service 1",
"code_board": "c1",
"pms_property_ids": [
(6, 0, [self.pms_property1.id, self.pms_property3.id])
],
}
)
# ACT
board_services = self.env["pms.board.service"].get_unique_by_property_code(
self.pms_property1.id, "c1"
)
# ASSERT
self.assertEqual(board_services.id, bs1.id, "Expected board service not found")
def test_single_case_03(self):
"""
PRE: - board_service1 exists
- board_service1 has code c1
- board_service1 with 2 properties pms_property1 and pms_property2
- pms_property1 and pms_property2 have same company company1
ACT: - search board service with code c1 and property pms_property3
- pms_property3 have company company2
POST: - no room type found
"""
# ARRANGE
self.pms_property2 = self.env["pms.property"].create(
{
"name": "Property 2",
"company_id": self.company1.id,
"default_pricelist_id": self.pricelist1.id,
}
)
# board_service1
self.env["pms.board.service"].create(
{
"name": "Board service 1",
"code_board": "c1",
"pms_property_ids": [
(6, 0, [self.pms_property1.id, self.pms_property2.id])
],
}
)
# ACT
board_services = self.env["pms.board.service"].get_unique_by_property_code(
self.pms_property3.id, "c1"
)
# ASSERT
self.assertFalse(
board_services, "Board service found but it should not have found any"
)
def test_single_case_04(self):
"""
PRE: - board_service1 exists
- board_service1 has code c1
- board_service1 properties are null
ACT: - search board service with code c1 and property pms_property1
- pms_property1 have company company1
POST: - only board_service1 board service found
"""
# ARRANGE
board_service1 = self.env["pms.board.service"].create(
{
"name": "Board service 1",
"code_board": "c1",
"pms_property_ids": False,
}
)
# ACT
board_services = self.env["pms.board.service"].get_unique_by_property_code(
self.pms_property1.id, "c1"
)
# ASSERT
self.assertEqual(
board_services.id,
board_service1.id,
"Expected board service not found",
)
# tests with more than one board service
def test_multiple_case_01(self):
"""
PRE: - board_service1 exists
- board_service1 has code c1
- board_service1 has 2 properties pms_property1 and pms_property2
- pms_property1 and pms_property2 have the same company company1
- board service board_service2 exists
- board_service2 has code c1
- board_service2 has no properties
ACT: - search board service with code c1 and property pms_property1
- pms_property1 have company company1
POST: - only board_service1 board service found
"""
# ARRANGE
board_service1 = self.env["pms.board.service"].create(
{
"name": "Board service 1",
"code_board": "c1",
"pms_property_ids": [
(6, 0, [self.pms_property1.id, self.pms_property3.id])
],
}
)
# board_service2
self.env["pms.board.service"].create(
{
"name": "Board service bs2",
"code_board": "c1",
"pms_property_ids": False,
}
)
# ACT
board_services = self.env["pms.board.service"].get_unique_by_property_code(
self.pms_property1.id, "c1"
)
# ASSERT
self.assertEqual(
board_services.id,
board_service1.id,
"Expected board service not found",
)
def test_multiple_case_02(self):
"""
PRE: - board_service1 exists
- board_service1 has code c1
- board_service1 has property pms_property1
- pms_property1 have the company company1
- board service board_service2 exists
- board_service2 has code c1
- board_service2 has no properties
ACT: - search board service with code c1 and pms_property2
- pms_property2 have company company1
POST: - only board_service1 board service found
"""
# ARRANGE
self.pms_property2 = self.env["pms.property"].create(
{
"name": "Property 2",
"company_id": self.company1.id,
"default_pricelist_id": self.pricelist1.id,
}
)
# board_service1
self.env["pms.board.service"].create(
{
"name": "Board service 1",
"code_board": "c1",
"pms_property_ids": [(6, 0, [self.pms_property1.id])],
}
)
board_service2 = self.env["pms.board.service"].create(
{
"name": "Board service bs2",
"code_board": "c1",
"pms_property_ids": False,
}
)
# ACT
board_services = self.env["pms.board.service"].get_unique_by_property_code(
self.pms_property2.id, "c1"
)
# ASSERT
self.assertEqual(
board_services.id,
board_service2.id,
"Expected board service not found",
)
def test_multiple_case_03(self):
"""
PRE: - board_service1 exists
- board_service1 has code c1
- board_service1 has property pms_property1
- pms_property1 have the company company1
- board service board_service2 exists
- board_service2 has code c1
- board_service2 has no properties
ACT: - search board service with code c1 and property pms_property3
- pms_property3 have company company2
POST: - only board_service2 board service found
"""
# ARRANGE
# board_service1
self.env["pms.board.service"].create(
{
"name": "Board service bs1",
"code_board": "c1",
"pms_property_ids": [(6, 0, [self.pms_property1.id])],
}
)
board_service2 = self.env["pms.board.service"].create(
{
"name": "Board service bs2",
"code_board": "c1",
"pms_property_ids": False,
}
)
# ACT
board_services = self.env["pms.board.service"].get_unique_by_property_code(
self.pms_property3.id, "c1"
)
# ASSERT
self.assertEqual(
board_services.id,
board_service2.id,
"Expected board service not found",
)
def test_multiple_case_04(self):
"""
PRE: - board_service1 exists
- board_service1 has code c1
- board_service1 has property pms_property1
- pms_property1 have the company company1
- room type board_service2 exists
- board_service2 has code c1
- board_service2 has no properties
ACT: - search board service with code c1 and property pms_property3
- pms_property3 have company company2
POST: - r2 board service found
"""
# ARRANGE
# board_service1
self.env["pms.board.service"].create(
{
"name": "Board service 1",
"code_board": "c1",
"pms_property_ids": [(6, 0, [self.pms_property1.id])],
}
)
board_service2 = self.env["pms.board.service"].create(
{
"name": "Board service bs2",
"code_board": "c1",
"pms_property_ids": False,
}
)
# ACT
board_services = self.env["pms.board.service"].get_unique_by_property_code(
self.pms_property3.id, "c1"
)
# ASSERT
self.assertEqual(
board_services.id, board_service2.id, "Expected room type not found"
)

View File

@@ -60,6 +60,7 @@ class TestPmsBoardService(common.SavepointCase):
self.board_service = self.env["pms.board.service"].create(
{
"name": "Board Service",
"code_board": "CB",
}
)
with self.assertRaises(ValidationError):

View File

@@ -56,6 +56,7 @@ class TestPmsBoardServiceRoomTypeLine(common.SavepointCase):
self.board_service = self.env["pms.board.service"].create(
{
"name": "Board Service",
"code_board": "CB",
}
)
self.room_type_class = self.env["pms.room.type.class"].create(

View File

@@ -146,6 +146,7 @@ class TestPmsReservations(common.SavepointCase):
self.board_service = self.env["pms.board.service"].create(
{
"name": "Board Service Test",
"code_board": "CB",
}
)

View File

@@ -7,6 +7,7 @@
<form string="Board Service Line">
<group>
<field name="name" />
<field name="code_board" />
<field
name="pms_property_ids"
widget="many2many_tags"
@@ -30,6 +31,7 @@
<field name="arch" type="xml">
<tree string="Board Services">
<field name="name" />
<field name="code_board" />
<field name="amount" />
<field name="show_detail_report" />
</tree>