From 2da58ae94285b71326ff6cf14ea807026818a061 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Lodeiros?= Date: Mon, 27 Mar 2023 19:50:02 +0200 Subject: [PATCH] [RFC]pms: change contrain avail by compute restriction --- pms/models/pms_reservation_line.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/pms/models/pms_reservation_line.py b/pms/models/pms_reservation_line.py index e49009bf0..3791db72f 100644 --- a/pms/models/pms_reservation_line.py +++ b/pms/models/pms_reservation_line.py @@ -173,6 +173,7 @@ class PmsReservationLine(models.Model): # negative discounts (= surcharge) are included in the display price return max(base_price, final_price) + # flake8: noqa=C901 @api.depends("reservation_id.room_type_id", "reservation_id.preferred_room_id") def _compute_room_id(self): for line in self.filtered("reservation_id.room_type_id").sorted( @@ -235,6 +236,8 @@ class PmsReservationLine(models.Model): if self.env.context.get("force_overbooking"): line.overbooking = True line.room_id = reservation.preferred_room_id + elif not line.occupies_availability: + line.room_id = reservation.preferred_room_id else: raise ValidationError( _("%s: No room available in %s <-> %s.") @@ -417,13 +420,25 @@ class PmsReservationLine(models.Model): ] ) if avail: + room_ids = record.room_id.room_type_id.room_ids.filtered( + lambda r: r.pms_property_id == record.pms_property_id + ).ids + if ( + record.occupies_availability + and record.room_id.id + in avail.get_rooms_not_avail( + checkin=record.date, + checkout=record.date + datetime.timedelta(1), + room_ids=room_ids, + pms_property_id=record.pms_property_id.id, + current_lines=record.ids, + ) + ): + raise ValidationError( + _("There is no availability for the room type %s on %s") + % (record.room_id.room_type_id.name, record.date) + ) record.avail_id = avail.id - if record.occupies_availability: - if avail.real_avail < 0: - raise ValidationError( - _("There is no availability for the room type %s on %s") - % (record.room_id.room_type_id.name, record.date) - ) else: record.avail_id = self.env["pms.availability"].create( {