From 9705e657c78ddb70e53d41bfccf544fa719dda18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Lodeiros?= Date: Sat, 23 Jul 2022 16:12:03 +0200 Subject: [PATCH] [ADD]pms: avoid sale channel constrain if not channel set --- pms/models/pms_reservation.py | 66 ++++++++++++--------- pms/models/pms_reservation_line.py | 1 + pms/tests/test_pms_reservation.py | 1 + pms/tests/test_pms_service.py | 94 ++++++++++++++++++------------ pms/views/pms_folio_views.xml | 4 -- 5 files changed, 96 insertions(+), 70 deletions(-) diff --git a/pms/models/pms_reservation.py b/pms/models/pms_reservation.py index 8f857dee4..3b5080f80 100644 --- a/pms/models/pms_reservation.py +++ b/pms/models/pms_reservation.py @@ -1892,7 +1892,7 @@ class PmsReservation(models.Model): @api.constrains("sale_channel_ids") def _check_lines_with_sale_channel_id(self): - for record in self: + for record in self.filtered("sale_channel_origin_id"): if record.reservation_line_ids: if record.sale_channel_origin_id not in record.sale_channel_ids: raise ValidationError( @@ -2041,36 +2041,15 @@ class PmsReservation(models.Model): and "sale_channel_origin_id" in vals and ("partner_name" in vals or "partner_id" in vals or "agency_id" in vals) ): - folio_vals = { - "pms_property_id": vals["pms_property_id"], - } - if vals.get("sale_channel_origin_id"): - folio_vals["sale_channel_origin_id"] = vals.get( - "sale_channel_origin_id" - ) - if vals.get("partner_id"): - folio_vals["partner_id"] = vals.get("partner_id") - elif vals.get("agency_id"): - folio_vals["agency_id"] = vals.get("agency_id") - elif vals.get("partner_name"): - folio_vals["partner_name"] = vals.get("partner_name") - folio_vals["mobile"] = vals.get("mobile") - folio_vals["email"] = vals.get("email") - elif vals.get("reservation_type") != "out": - raise ValidationError(_("Partner contact name is required")) + folio_vals = self._get_folio_vals(vals) + + self._check_clousure_reason( + reservation_type=vals.get("reservation_type"), + clousure_reason=vals.get("clousure_reason_id"), + ) + # Create the folio in case of need # (To allow to create reservations direct) - if vals.get("reservation_type"): - folio_vals["reservation_type"] = vals.get("reservation_type") - if vals.get("reservation_type") == "out" and not vals.get( - "closure_reason_id" - ): - raise ValidationError( - _( - "A closure reason is mandatory when reservation" - " type is 'out of service'" - ) - ) folio = self.env["pms.folio"].create(folio_vals) vals.update( { @@ -2133,6 +2112,35 @@ class PmsReservation(models.Model): self._check_capacity() return res + def _get_folio_vals(self, reservation_vals): + folio_vals = { + "pms_property_id": reservation_vals["pms_property_id"], + } + if reservation_vals.get("sale_channel_origin_id"): + folio_vals["sale_channel_origin_id"] = reservation_vals.get( + "sale_channel_origin_id" + ) + if reservation_vals.get("partner_id"): + folio_vals["partner_id"] = reservation_vals.get("partner_id") + elif reservation_vals.get("agency_id"): + folio_vals["agency_id"] = reservation_vals.get("agency_id") + elif reservation_vals.get("partner_name"): + folio_vals["partner_name"] = reservation_vals.get("partner_name") + folio_vals["mobile"] = reservation_vals.get("mobile") + folio_vals["email"] = reservation_vals.get("email") + elif reservation_vals.get("reservation_type") != "out": + raise ValidationError(_("Partner contact name is required")) + return folio_vals + + def _check_clousure_reason(self, reservation_type, clousure_reason): + if reservation_type == "out" and not clousure_reason: + raise ValidationError( + _( + "A closure reason is mandatory when reservation" + " type is 'out of service'" + ) + ) + def _check_services(self, vals): # If we create a reservation with board service and other service at the same time, # compute_service_ids dont run (compute with readonly to False), diff --git a/pms/models/pms_reservation_line.py b/pms/models/pms_reservation_line.py index 55d37ffef..7f201da5c 100644 --- a/pms/models/pms_reservation_line.py +++ b/pms/models/pms_reservation_line.py @@ -119,6 +119,7 @@ class PmsReservationLine(models.Model): comodel_name="pms.sale.channel", check_pms_properties=True, ) + _sql_constraints = [ ( "rule_availability", diff --git a/pms/tests/test_pms_reservation.py b/pms/tests/test_pms_reservation.py index 85e7ad1d9..b70160637 100644 --- a/pms/tests/test_pms_reservation.py +++ b/pms/tests/test_pms_reservation.py @@ -3871,6 +3871,7 @@ class TestPmsReservations(TestPms): "The out of service reservation should be created properly with " "a closure reason.", ) + # tests for several sale channels in reservation @freeze_time("2000-11-10") def test_reservation_sale_channel_origin_in_reservation_lines(self): diff --git a/pms/tests/test_pms_service.py b/pms/tests/test_pms_service.py index 37cb5489d..5adfeedae 100644 --- a/pms/tests/test_pms_service.py +++ b/pms/tests/test_pms_service.py @@ -3,10 +3,11 @@ import datetime from freezegun import freeze_time from odoo import fields + from .common import TestPms -class TestPmsService(TestPms): +class TestPmsService(TestPms): def setUp(self): super().setUp() # create room type @@ -112,7 +113,7 @@ class TestPmsService(TestPms): self.assertEqual( self.reservation.sale_channel_origin_id, self.reservation.service_ids.sale_channel_origin_id, - "sale_channel_origin of board_Service must be the same as its reservation" + "sale_channel_origin of board_Service must be the same as its reservation", ) @freeze_time("2002-01-03") @@ -169,7 +170,7 @@ class TestPmsService(TestPms): self.assertEqual( self.reservation.service_ids.sale_channel_origin_id, self.reservation.service_ids.service_line_ids.mapped("sale_channel_id"), - "sale_channel_origin of board_Service must be the same as its service_lines" + "sale_channel_origin of board_Service must be the same as its service_lines", ) @freeze_time("2002-01-05") @@ -237,7 +238,7 @@ class TestPmsService(TestPms): self.reservation.service_ids.sale_channel_origin_id, self.reservation.service_ids.service_line_ids.mapped("sale_channel_id"), "sale_channel_origin of board_Service must corresponds to the" - "sale_channel_id of its new service_line" + "sale_channel_id of its new service_line", ) @freeze_time("2002-01-07") @@ -298,23 +299,26 @@ class TestPmsService(TestPms): self.env["pms.service.line"].create( { "service_id": self.reservation.service_ids.id, - "sale_channel_id": self.sale_channel_phone.id + "sale_channel_id": self.sale_channel_phone.id, } ) sale_channel_ids = [ self.reservation.folio_id.sale_channel_ids.ids, self.reservation.sale_channel_ids.ids, - self.reservation.service_ids.sale_channel_ids.ids + self.reservation.service_ids.sale_channel_ids.ids, ] - expected_sale_channel_ids = [self.sale_channel_door.id, self.sale_channel_phone.id] + expected_sale_channel_ids = [ + self.sale_channel_door.id, + self.sale_channel_phone.id, + ] # ASSERT for sale_channel in sale_channel_ids: with self.subTest(k=sale_channel): self.assertItemsEqual( sale_channel, expected_sale_channel_ids, - "sale_channel_ids must contain sale_channel_id of all board_service_lines" + "sale_channel_ids must contain sale_channel_id of all board_service_lines", ) @freeze_time("2002-01-09") @@ -372,7 +376,8 @@ class TestPmsService(TestPms): self.assertEqual( self.reservation.service_ids.sale_channel_ids, self.reservation.service_ids.service_line_ids.mapped("sale_channel_id"), - "sale_channel_ids of board_Service must correspond to the sale_channel_id set of its service_lines" + """sale_channel_ids of board_Service must correspond + to the sale_channel_id set of its service_lines""", ) @freeze_time("2002-01-11") @@ -438,7 +443,8 @@ class TestPmsService(TestPms): self.assertNotEqual( self.reservation.sale_channel_origin_id, self.reservation.service_ids.sale_channel_origin_id, - "sale_channel_origin_id mustn't match with sale_channel_origin_id of its reservation" + """sale_channel_origin_id mustn't match + with sale_channel_origin_id of its reservation""", ) @freeze_time("2002-01-13") @@ -503,7 +509,7 @@ class TestPmsService(TestPms): self.env["pms.service.line"].create( { "service_id": self.reservation.service_ids.id, - "sale_channel_id": self.sale_channel_phone.id + "sale_channel_id": self.sale_channel_phone.id, } ) # ACT @@ -513,7 +519,8 @@ class TestPmsService(TestPms): self.assertIn( self.sale_channel_mail, self.reservation.service_ids.service_line_ids.mapped("sale_channel_id"), - "new sale_channel_origin_id of board service must be changed in lines with same sale_channel" + """new sale_channel_origin_id of board service + must be changed in lines with same sale_channel""", ) @freeze_time("2002-01-15") @@ -578,7 +585,7 @@ class TestPmsService(TestPms): self.env["pms.service.line"].create( { "service_id": self.reservation.service_ids.id, - "sale_channel_id": self.sale_channel_phone.id + "sale_channel_id": self.sale_channel_phone.id, } ) # ACT @@ -588,7 +595,8 @@ class TestPmsService(TestPms): self.assertIn( self.sale_channel_phone, self.reservation.service_ids.service_line_ids.mapped("sale_channel_id"), - "new sale_channel_origin_id of board service mustn't changed in lines with different sale_channel" + """new sale_channel_origin_id of board service + mustn't changed in lines with different sale_channel""", ) @freeze_time("2002-01-17") @@ -601,7 +609,8 @@ class TestPmsService(TestPms): | ---> board_service.sale_channel_origin = Door - Change origin of board services to Phone and check sale_channel_ids of reservation and folio: + Change origin of board services to Phone and + check sale_channel_ids of reservation and folio: Reservation --> sale_channel_origin = Door sale_channel_ids = {Door, Phone} | ---> board_service.sale_channel_origin = Phone @@ -655,14 +664,17 @@ class TestPmsService(TestPms): self.reservation.sale_channel_ids.ids, ] - expected_sale_channel_ids = [self.sale_channel_door.id, self.sale_channel_phone.id] + expected_sale_channel_ids = [ + self.sale_channel_door.id, + self.sale_channel_phone.id, + ] # ASSERT for sale_channel in sale_channel_ids: with self.subTest(k=sale_channel): self.assertItemsEqual( sale_channel, expected_sale_channel_ids, - "sale_channel_ids must contain sale_channel_origin_id of all board_service" + "sale_channel_ids must contain sale_channel_origin_id of all board_service", ) @freeze_time("2002-01-19") @@ -729,7 +741,7 @@ class TestPmsService(TestPms): self.assertIn( self.reservation.sale_channel_origin_id, self.reservation.service_ids.sale_channel_origin_id, - "sale_channel_origin_id of service must be the same that its reservation " + "sale_channel_origin_id of service must be the same that its reservation ", ) @freeze_time("2002-02-01") @@ -776,7 +788,7 @@ class TestPmsService(TestPms): self.assertEqual( self.reservation.sale_channel_origin_id, self.service1.sale_channel_origin_id, - "sale_channel_origin of service must be the same as its reservation" + "sale_channel_origin of service must be the same as its reservation", ) @freeze_time("2002-02-03") @@ -844,7 +856,9 @@ class TestPmsService(TestPms): self.reservation.sale_channel_ids, ] - expected_sale_channel_ids = self.reservation.service_ids.mapped("sale_channel_origin_id") + expected_sale_channel_ids = self.reservation.service_ids.mapped( + "sale_channel_origin_id" + ) # ASSERT for sale_channel in sale_channel_ids: @@ -852,7 +866,7 @@ class TestPmsService(TestPms): self.assertItemsEqual( sale_channel, expected_sale_channel_ids, - "sale_channel_ids must contain sale_channel_id of all board_service_lines" + "sale_channel_ids must contain sale_channel_id of all board_service_lines", ) @freeze_time("2002-02-16") @@ -908,7 +922,8 @@ class TestPmsService(TestPms): self.assertNotEqual( self.reservation.sale_channel_origin_id, self.reservation.service_ids.sale_channel_origin_id, - "sale_channel_origin_id mustn't match with sale_channel_origin_id of its reservation" + """sale_channel_origin_id mustn't match + with sale_channel_origin_id of its reservation""", ) @freeze_time("2002-02-23") @@ -970,7 +985,10 @@ class TestPmsService(TestPms): self.reservation.sale_channel_ids.ids, ] - expected_sale_channel_ids = [self.sale_channel_door.id, self.sale_channel_mail.id] + expected_sale_channel_ids = [ + self.sale_channel_door.id, + self.sale_channel_mail.id, + ] # ASSERT for sale_channel in sale_channel_ids: @@ -978,7 +996,7 @@ class TestPmsService(TestPms): self.assertItemsEqual( sale_channel, expected_sale_channel_ids, - "sale_channel_ids must contain sale_channel_origin_id of all services" + "sale_channel_ids must contain sale_channel_origin_id of all services", ) @freeze_time("2002-02-25") @@ -1045,7 +1063,7 @@ class TestPmsService(TestPms): self.assertIn( self.reservation.sale_channel_origin_id, self.reservation.service_ids.mapped("sale_channel_origin_id"), - "sale_channel_origin_id of that service should be changed" + "sale_channel_origin_id of that service should be changed", ) @freeze_time("2002-03-29") @@ -1112,7 +1130,7 @@ class TestPmsService(TestPms): self.assertIn( self.sale_channel_phone, self.reservation.service_ids.mapped("sale_channel_origin_id"), - "sale_channel_origin_id of that service shouldn't be changed" + "sale_channel_origin_id of that service shouldn't be changed", ) @freeze_time("2002-03-01") @@ -1147,14 +1165,14 @@ class TestPmsService(TestPms): { "product_id": self.product1.id, "is_board_service": False, - "folio_id":self.folio1.id, + "folio_id": self.folio1.id, } ) # ASSERT self.assertEqual( self.folio1.sale_channel_origin_id, self.service1.sale_channel_origin_id, - "Service that is just created must have its folio sale_channel_origin" + "Service that is just created must have its folio sale_channel_origin", ) @freeze_time("2002-03-03") @@ -1203,7 +1221,7 @@ class TestPmsService(TestPms): self.assertEqual( self.folio1.sale_channel_origin_id, self.service1.sale_channel_origin_id, - "Service must have equal sale_channel_origin than folio" + "Service must have equal sale_channel_origin than folio", ) @freeze_time("2002-03-05") @@ -1252,7 +1270,7 @@ class TestPmsService(TestPms): self.assertEqual( self.folio1.sale_channel_origin_id, self.service1.sale_channel_origin_id, - "Service must have equal sale_channel_origin than folio" + "Service must have equal sale_channel_origin than folio", ) @freeze_time("2002-03-07") @@ -1299,17 +1317,19 @@ class TestPmsService(TestPms): "product_id": self.product1.id, "is_board_service": False, "folio_id": self.folio1.id, - "sale_channel_origin_id": self.sale_channel_mail.id + "sale_channel_origin_id": self.sale_channel_mail.id, } ) - expected_sale_channels = self.folio1.service_ids.mapped("sale_channel_origin_id") + expected_sale_channels = self.folio1.service_ids.mapped( + "sale_channel_origin_id" + ) # ASSERT self.assertEqual( self.folio1.sale_channel_ids, expected_sale_channels, - "sale_channel_ids must be the set of sale_channel_origin of its services" + "sale_channel_ids must be the set of sale_channel_origin of its services", ) @freeze_time("2002-03-10") @@ -1361,7 +1381,7 @@ class TestPmsService(TestPms): "product_id": self.product1.id, "is_board_service": False, "folio_id": self.folio1.id, - "sale_channel_origin_id": self.sale_channel_mail.id + "sale_channel_origin_id": self.sale_channel_mail.id, } ) # ACT @@ -1371,7 +1391,7 @@ class TestPmsService(TestPms): self.assertIn( self.sale_channel_phone, self.folio1.service_ids.mapped("sale_channel_origin_id"), - "sale_channel_origin_id of that service must be changed" + "sale_channel_origin_id of that service must be changed", ) @freeze_time("2002-03-13") @@ -1424,7 +1444,7 @@ class TestPmsService(TestPms): "product_id": self.product1.id, "is_board_service": False, "folio_id": self.folio1.id, - "sale_channel_origin_id": self.sale_channel_mail.id + "sale_channel_origin_id": self.sale_channel_mail.id, } ) # ACT @@ -1434,5 +1454,5 @@ class TestPmsService(TestPms): self.assertIn( self.sale_channel_mail, self.folio1.service_ids.mapped("sale_channel_origin_id"), - "sale_channel_origin_id of that service mustn't be changed" + "sale_channel_origin_id of that service mustn't be changed", ) diff --git a/pms/views/pms_folio_views.xml b/pms/views/pms_folio_views.xml index 72a5bd734..61b914f69 100644 --- a/pms/views/pms_folio_views.xml +++ b/pms/views/pms_folio_views.xml @@ -358,10 +358,6 @@ widget="many2many_tags" options="{'no_create': True,'no_open': True}" /> -