diff --git a/pms/demo/pms_master_data.xml b/pms/demo/pms_master_data.xml index e93b033fb..26f0c8002 100644 --- a/pms/demo/pms_master_data.xml +++ b/pms/demo/pms_master_data.xml @@ -331,6 +331,7 @@ BreakFast + BB Half Board + HB FullBoard + FB 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) diff --git a/pms/tests/__init__.py b/pms/tests/__init__.py index f0adffd16..dbe4b7344 100644 --- a/pms/tests/__init__.py +++ b/pms/tests/__init__.py @@ -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 diff --git a/pms/tests/test_pms_board_service.py b/pms/tests/test_pms_board_service.py new file mode 100644 index 000000000..f02c7b5ff --- /dev/null +++ b/pms/tests/test_pms_board_service.py @@ -0,0 +1,423 @@ +# Copyright 2021 Eric Antones +# 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" + ) diff --git a/pms/tests/test_pms_board_service_line.py b/pms/tests/test_pms_board_service_line.py index e4678fd4e..4721021fd 100644 --- a/pms/tests/test_pms_board_service_line.py +++ b/pms/tests/test_pms_board_service_line.py @@ -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): diff --git a/pms/tests/test_pms_board_service_room_type_line.py b/pms/tests/test_pms_board_service_room_type_line.py index 65c8426ca..1c9a6eb3c 100644 --- a/pms/tests/test_pms_board_service_room_type_line.py +++ b/pms/tests/test_pms_board_service_room_type_line.py @@ -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( diff --git a/pms/tests/test_pms_reservation.py b/pms/tests/test_pms_reservation.py index 156a21b51..adc32949b 100644 --- a/pms/tests/test_pms_reservation.py +++ b/pms/tests/test_pms_reservation.py @@ -146,6 +146,7 @@ class TestPmsReservations(common.SavepointCase): self.board_service = self.env["pms.board.service"].create( { "name": "Board Service Test", + "code_board": "CB", } ) diff --git a/pms/views/pms_board_service_views.xml b/pms/views/pms_board_service_views.xml index 414ee25ca..2b1ca9855 100644 --- a/pms/views/pms_board_service_views.xml +++ b/pms/views/pms_board_service_views.xml @@ -7,6 +7,7 @@
+ +