From 3b0bf392150bc76bc0109d377a9fb19d33e99b92 Mon Sep 17 00:00:00 2001 From: miguelpadin Date: Tue, 6 Jul 2021 20:32:08 +0200 Subject: [PATCH] [FIX] pms: fix check adults w. extra beds, fix day_qty @ pms.service.line, change literal code spain & fix tests --- pms/models/pms_reservation.py | 47 ++++++++++++++++++---------- pms/models/pms_reservation_line.py | 14 --------- pms/models/pms_room.py | 20 ++++++++++++ pms/models/pms_service.py | 38 +++++++++++----------- pms/tests/test_pms_reservation.py | 15 +++++---- pms/views/pms_reservation_views.xml | 1 + pms_l10n_es/tests/test_wizard_ine.py | 26 +++++++++------ pms_l10n_es/wizards/wizard_ine.py | 46 +++++++++++++++------------ 8 files changed, 122 insertions(+), 85 deletions(-) diff --git a/pms/models/pms_reservation.py b/pms/models/pms_reservation.py index 8194c1ed3..10d738505 100644 --- a/pms/models/pms_reservation.py +++ b/pms/models/pms_reservation.py @@ -605,6 +605,29 @@ class PmsReservation(models.Model): readonly=False, ) + check_adults = fields.Boolean( + help="Internal field to force room capacity validations", + compute="_compute_check_adults", + readonly=False, + store=True, + ) + + def _compute_date_order(self): + for record in self: + record.date_order = datetime.datetime.today() + + @api.depends( + "service_ids", + "service_ids.service_line_ids", + "service_ids.service_line_ids.product_id", + "service_ids.service_line_ids.day_qty", + "reservation_line_ids", + "reservation_line_ids.room_id", + ) + def _compute_check_adults(self): + for record in self: + record.check_adults = True + @api.depends( "checkin", "checkout", @@ -1423,23 +1446,6 @@ class PmsReservation(models.Model): # _("The room already is completed (%s)", record.name) # ) - @api.constrains("adults") - def _check_adults(self): - for record in self: - for line in record.reservation_line_ids: - extra_beds = record.service_ids.service_line_ids.filtered( - lambda x: x.date == line.date and x.product_id.is_extra_bed is True - ) - if ( - record.adults + record.children_occupying - ) > line.room_id.get_capacity(len(extra_beds)): - raise ValidationError( - _( - "Persons can't be higher than room capacity (%s)", - record.name, - ) - ) - @api.constrains("state") def _check_onboard_reservation(self): for record in self: @@ -1484,6 +1490,13 @@ class PmsReservation(models.Model): if record.agency_id and not record.agency_id.is_agency: raise ValidationError(_("booking agency with wrong configuration: ")) + @api.constrains("check_adults") + def _check_capacity(self): + for record in self: + self.env["pms.room"]._check_adults( + record, record.service_ids.service_line_ids + ) + # Action methods def open_partner(self): """ Utility method used to add an "View Customer" button in reservation views """ diff --git a/pms/models/pms_reservation_line.py b/pms/models/pms_reservation_line.py index 3d5e2b641..6331997fa 100644 --- a/pms/models/pms_reservation_line.py +++ b/pms/models/pms_reservation_line.py @@ -470,17 +470,3 @@ class PmsReservationLine(models.Model): ) if duplicated: raise ValidationError(_("Duplicated reservation line date")) - - @api.constrains("room_id") - def _check_adults(self): - for record in self.filtered("room_id"): - extra_bed = record.reservation_id.service_ids.filtered( - lambda r: r.product_id.is_extra_bed is True - ) - if ( - record.reservation_id.adults + record.reservation_id.children_occupying - > record.room_id.get_capacity(len(extra_bed)) - ): - raise ValidationError(_("Persons can't be higher than room capacity")) - # if record.reservation_id.adults == 0: - # raise ValidationError(_("Reservation has no adults")) diff --git a/pms/models/pms_room.py b/pms/models/pms_room.py index e83742eac..dcb6869a0 100644 --- a/pms/models/pms_room.py +++ b/pms/models/pms_room.py @@ -118,6 +118,26 @@ class PmsRoom(models.Model): ) ) + @api.model + def _check_adults(self, reservation, service_line_ids=False): + for line in reservation.reservation_line_ids: + num_extra_beds = 0 + if service_line_ids: + extra_beds = service_line_ids.filtered( + lambda x: x.date == line.date and x.product_id.is_extra_bed is True + ) + num_extra_beds = sum(extra_beds.mapped("day_qty")) if extra_beds else 0 + if line.room_id: + if ( + reservation.adults + reservation.children_occupying + ) > line.room_id.get_capacity(num_extra_beds): + raise ValidationError( + _( + "Persons can't be higher than room capacity (%s)", + reservation.name, + ) + ) + # Business methods def get_capacity(self, extra_bed=0): diff --git a/pms/models/pms_service.py b/pms/models/pms_service.py index 858c0cfa7..af621271a 100644 --- a/pms/models/pms_service.py +++ b/pms/models/pms_service.py @@ -340,23 +340,24 @@ class PmsService(models.Model): move_day = 0 if consumed_on == "after": move_day = 1 - service.service_line_ids -= ( - service.service_line_ids.filtered_domain( - [ - "|", - ( - "date", - "<", - reservation.checkin + timedelta(move_day), - ), - ( - "date", - ">=", - reservation.checkout + timedelta(move_day), - ), - ] - ) - ) + for del_service_id in service.service_line_ids.filtered_domain( + [ + "|", + ( + "date", + "<", + reservation.checkin + timedelta(move_day), + ), + ( + "date", + ">=", + reservation.checkout + timedelta(move_day), + ), + ] + ).ids: + lines.append((2, del_service_id)) + # TODO: check intermediate states in check_adults restriction + # when lines are removed service.service_line_ids = lines else: if not service.service_line_ids: @@ -535,10 +536,11 @@ class PmsService(models.Model): # Businness Methods def _service_day_qty(self): self.ensure_one() - qty = self.product_qty if len(self.service_line_ids) == 1 else 0 + qty = self.product_qty if len(self.service_line_ids) == 1 else 1 if not self.reservation_id: return qty # TODO: Pass per_person to service line from product default_per_person + # When the user modifies the quantity avoid overwriting if self.product_id.per_person: qty = self.reservation_id.adults return qty diff --git a/pms/tests/test_pms_reservation.py b/pms/tests/test_pms_reservation.py index 6557b1dfe..296b2112d 100644 --- a/pms/tests/test_pms_reservation.py +++ b/pms/tests/test_pms_reservation.py @@ -36,6 +36,7 @@ class TestPmsReservations(TestPms): "name": "Double 101", "room_type_id": self.room_type_double.id, "capacity": 2, + "extra_beds_allowed": 1, } ) @@ -626,19 +627,20 @@ class TestPmsReservations(TestPms): # ACT & ASSERT with self.assertRaises( ValidationError, - msg="The number of people is lower than the capacity of the room", + msg="The number of people is greater than the capacity of the room", ): - self.env["pms.reservation"].create( + reservation = self.env["pms.reservation"].create( { "adults": 2, "children_occupying": 1, "checkin": datetime.datetime.now(), "checkout": datetime.datetime.now() + datetime.timedelta(days=1), - "room_type_id": self.room_type_double.id, + "preferred_room_id": self.room1.id, "partner_id": self.partner1.id, "pms_property_id": self.pms_property1.id, } ) + reservation.flush() def test_to_assign_priority_reservation(self): """ @@ -1930,16 +1932,17 @@ class TestPmsReservations(TestPms): } ) with self.assertRaises(ValidationError): - self.env["pms.reservation"].create( + reservation = self.env["pms.reservation"].create( { "checkin": fields.date.today(), "checkout": fields.date.today() + datetime.timedelta(days=3), "pms_property_id": self.pms_property1.id, "partner_id": self.host1.id, - "room_type_id": self.room_type_double.id, + "preferred_room_id": self.room1.id, "adults": 4, } ) + reservation.flush() def test_check_format_arrival_hour(self): """ @@ -2616,7 +2619,7 @@ class TestPmsReservations(TestPms): { "checkin": fields.date.today() + datetime.timedelta(days=-3), "checkout": fields.date.today() + datetime.timedelta(days=3), - "room_type_id": self.room_type_double.id, + "preferred_room_id": self.room1.id, "partner_id": self.partner1.id, "pms_property_id": self.pms_property1.id, "pricelist_id": self.pricelist1.id, diff --git a/pms/views/pms_reservation_views.xml b/pms/views/pms_reservation_views.xml index af3fd4b9a..7fbe53825 100644 --- a/pms/views/pms_reservation_views.xml +++ b/pms/views/pms_reservation_views.xml @@ -16,6 +16,7 @@ +