From d630a75012584998976f9e48519301c881a2e55f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Lodeiros?= Date: Mon, 4 Nov 2024 17:03:38 +0100 Subject: [PATCH] [IMP]pms: Add independent address to room --- pms/models/pms_room.py | 119 +++++++++++++++++++++++++++++++++-- pms/tests/test_pms_room.py | 89 ++++++++++++++++++++++++++ pms/views/pms_room_views.xml | 66 ++++++++++++++++++- 3 files changed, 267 insertions(+), 7 deletions(-) diff --git a/pms/models/pms_room.py b/pms/models/pms_room.py index 2823f00547..0a4dc103b4 100644 --- a/pms/models/pms_room.py +++ b/pms/models/pms_room.py @@ -110,6 +110,89 @@ class PmsRoom(models.Model): help="Four character name, if not set, autocompletes with the first two letters of " "the room name and two incremental numbers", ) + address_is_independent = fields.Boolean( + string="Address is Independent", + help="Indicates that the address of the room is independent of the property", + ) + address_id = fields.Many2one( + string="Address", + help="Address of the room", + comodel_name="res.partner", + index=True, + compute="_compute_address_id", + store=True, + ondelete="restrict", + ) + street = fields.Char( + related="address_id.street", + readonly=False, + ) + street2 = fields.Char( + related="address_id.street2", + readonly=False, + ) + zip = fields.Char( + related="address_id.zip", + readonly=False, + ) + city = fields.Char( + related="address_id.city", + readonly=False, + ) + state_id = fields.Many2one( + "res.country.state", + related="address_id.state_id", + string="State", + ondelete="restrict", + domain="[('country_id', '=?', country_id)]", + readonly=False, + ) + country_id = fields.Many2one( + "res.country", + related="address_id.country_id", + string="Country", + ondelete="restrict", + readonly=False, + ) + partner_latitude = fields.Float( + string="Geo Latitude", + related="address_id.partner_latitude", + readonly=False, + digits=(16, 5), + ) + partner_longitude = fields.Float( + string="Geo Longitude", + related="address_id.partner_longitude", + readonly=False, + digits=(16, 5), + ) + email = fields.Char( + related="address_id.email", + readonly=False, + ) + email_formatted = fields.Char( + "Formatted Email", + compute="_compute_email_formatted", + help='Format email address "Name "', + ) + phone = fields.Char( + related="address_id.phone", + readonly=False, + ) + mobile = fields.Char( + related="address_id.mobile", + readonly=False, + ) + website = fields.Char( + related="address_id.website", + readonly=False, + ) + image_1920 = fields.Image( + related="address_id.image_1920", + readonly=False, + max_width=1920, + max_height=1920, + ) _sql_constraints = [ ( @@ -129,10 +212,38 @@ class PmsRoom(models.Model): @api.depends("child_ids") def _compute_is_shared_room(self): for record in self: - if record.child_ids: - record.is_shared_room = True - elif not record.is_shared_room: - record.is_shared_room = False + record.is_shared_room = bool(record.child_ids) + + @api.depends("address_is_independent") + def _compute_address_id(self): + for record in self: + if not record.address_is_independent: + if record.address_id and record.address_id.active: + record.address_id.active = False + if record.address_is_independent: + if record.address_id and not record.address_id.active: + record.address_id.active = True + elif not record.address_id: + record.address_id = ( + self.env["res.partner"] + .with_context(avoid_document_restriction=True) + .create( + { + "name": record.name, + "type": "other", + "is_company": False, + "active": True, + "parent_id": record.pms_property_id.id, + } + ) + ) + + @api.depends("name", "email") + def _compute_email_formatted(self): + for room in self.filtered("address_id"): + room.email_formatted = ( + room.address_id.email_formatted() if room.address_id else False + ) def name_get(self): result = [] diff --git a/pms/tests/test_pms_room.py b/pms/tests/test_pms_room.py index 4c006cb87b..2854239797 100644 --- a/pms/tests/test_pms_room.py +++ b/pms/tests/test_pms_room.py @@ -341,3 +341,92 @@ def test_short_name_gt_4_constraint(self): msg="The short_name of the room should not be able to be write.", ): self.room1.write({"short_name": "SIN-201"}) + + def test_create_independent_address(self): + """ + Check that an independent address is created and associated correctly + when address_is_independent is set to True. + """ + self.room1 = self.env["pms.room"].create( + { + "name": "Room 101", + "pms_property_id": self.pms_property1.id, + "room_type_id": self.room_type1.id, + "address_is_independent": True, + } + ) + self.assertTrue( + self.room1.address_id, + "The address should be created and associated with the room.", + ) + self.assertTrue(self.room1.address_id.active, "The address should be active.") + + def test_deactivate_independent_address(self): + """ + Check that the independent address is archived when a + ddress_is_independent is set to False. + """ + self.room1 = self.env["pms.room"].create( + { + "name": "Room 101", + "pms_property_id": self.pms_property1.id, + "room_type_id": self.room_type1.id, + "address_is_independent": True, + } + ) + self.room1.address_is_independent = False + self.assertFalse( + self.room1.address_id.active, "The address should be archived." + ) + + def test_reactivate_independent_address(self): + """ + Check that the archived independent address is reactivated when + address_is_independent is set to True again. + """ + self.room1 = self.env["pms.room"].create( + { + "name": "Room 101", + "pms_property_id": self.pms_property1.id, + "room_type_id": self.room_type1.id, + "address_is_independent": True, + } + ) + initial_address_id = self.room1.address_id.id + self.room1.address_is_independent = False + self.assertFalse( + self.room1.address_id.active, "The address should be archived." + ) + self.room1.address_is_independent = True + self.assertTrue( + self.room1.address_id.active, + "The address should be reactivated, not created again.", + ) + self.assertEqual( + self.room1.address_id.id, + initial_address_id, + "The reactivated address should be the same as the initial address.", + ) + self.assertEqual( + self.room1.address_id.name, + "Room 101", + "The reactivated address should have the same name.", + ) + + def test_prevent_deletion_of_associated_address(self): + """ + Check that an associated address cannot be deleted. + """ + self.room1 = self.env["pms.room"].create( + { + "name": "Room 101", + "pms_property_id": self.pms_property1.id, + "room_type_id": self.room_type1.id, + "address_is_independent": True, + } + ) + with self.assertRaises( + IntegrityError, + msg="The address associated with a room should not be deletable.", + ): + self.room1.address_id.unlink() diff --git a/pms/views/pms_room_views.xml b/pms/views/pms_room_views.xml index 33d2a448b1..963038fec2 100644 --- a/pms/views/pms_room_views.xml +++ b/pms/views/pms_room_views.xml @@ -27,6 +27,9 @@ + + + @@ -36,6 +39,8 @@ force_save="1" /> + + @@ -72,6 +77,64 @@ + + + + + + + + + - - -