[IMP] Allow change preferred_room_id on reservation

This commit is contained in:
Darío Lodeiros
2020-11-16 18:19:31 +01:00
parent 447de5f8f3
commit f4edee18a6
2 changed files with 28 additions and 24 deletions

View File

@@ -547,13 +547,14 @@ class PmsReservation(models.Model):
@api.depends("reservation_line_ids", "reservation_line_ids.room_id") @api.depends("reservation_line_ids", "reservation_line_ids.room_id")
def _compute_splitted(self): def _compute_splitted(self):
for reservation in self: for reservation in self:
if len(reservation.reservation_line_ids.mapped("room_id")) > 1: room_ids = reservation.reservation_line_ids.mapped("room_id.id")
if len(room_ids) > 1:
reservation.splitted = True reservation.splitted = True
reservation.preferred_room_id = False
else: else:
reservation.splitted = False reservation.splitted = False
reservation.preferred_room_id = reservation.reservation_line_ids[ if room_ids:
0 reservation.preferred_room_id = room_ids[0]
].room_id
@api.depends("state", "qty_to_invoice", "qty_invoiced") @api.depends("state", "qty_to_invoice", "qty_invoiced")
def _compute_invoice_status(self): def _compute_invoice_status(self):

View File

@@ -91,20 +91,25 @@ class PmsReservationLine(models.Model):
] ]
# Compute and Search methods # Compute and Search methods
@api.depends("reservation_id.room_type_id") @api.depends("reservation_id.room_type_id", "reservation_id.preferred_room_id")
def _compute_room_id(self): def _compute_room_id(self):
for line in self.sorted(key=lambda r: (r.reservation_id, r.date)): for line in self.filtered("reservation_id.room_type_id").sorted(
key=lambda r: (r.reservation_id, r.date)
# if the reservation has a room type and no room id ):
if line.reservation_id.room_type_id and not line.room_id: reservation = line.reservation_id
if reservation.preferred_room_id or not line.room_id:
# If reservation has a preferred_room_id We can allow
# select room_id regardless room_type_id selected on reservation
free_room_select = True if reservation.preferred_room_id else False
# we get the rooms available for the entire stay # we get the rooms available for the entire stay
rooms_available = self.env[ rooms_available = self.env[
"pms.room.type.availability" "pms.room.type.availability"
].rooms_available( ].rooms_available(
checkin=line.reservation_id.checkin, checkin=reservation.checkin,
checkout=line.reservation_id.checkout, checkout=reservation.checkout,
room_type_id=line.reservation_id.room_type_id.id, room_type_id=reservation.room_type_id.id
if not free_room_select
else False,
current_lines=line._origin.reservation_id.reservation_line_ids.ids, current_lines=line._origin.reservation_id.reservation_line_ids.ids,
) )
@@ -112,17 +117,17 @@ class PmsReservationLine(models.Model):
if rooms_available: if rooms_available:
# if the reservation has a preferred room # if the reservation has a preferred room
if line.reservation_id.preferred_room_id: if reservation.preferred_room_id:
# if the preferred room is available # if the preferred room is available
if line.reservation_id.preferred_room_id in rooms_available: if reservation.preferred_room_id in rooms_available:
line.room_id = line.reservation_id.preferred_room_id line.room_id = reservation.preferred_room_id
# if the preferred room is NOT available # if the preferred room is NOT available
else: else:
raise ValidationError( raise ValidationError(
_("%s: No room available.") _("%s: No room available.")
% (line.reservation_id.preferred_room_id.name) % (reservation.preferred_room_id.name)
) )
# otherwise we assign the first of those # otherwise we assign the first of those
@@ -137,19 +142,17 @@ class PmsReservationLine(models.Model):
# we go through the rooms of the type # we go through the rooms of the type
for room in self.env["pms.room"].search( for room in self.env["pms.room"].search(
[("room_type_id", "=", line.reservation_id.room_type_id.id)] [("room_type_id", "=", reservation.room_type_id.id)]
): ):
# we iterate the dates from the date of the line to the checkout # we iterate the dates from the date of the line to the checkout
for date_iterator in [ for date_iterator in [
line.date + datetime.timedelta(days=x) line.date + datetime.timedelta(days=x)
for x in range( for x in range(0, (reservation.checkout - line.date).days)
0, (line.reservation_id.checkout - line.date).days
)
]: ]:
# if the room is already assigned for # if the room is already assigned for
# a date we go to the next room # a date we go to the next room
ids = line.reservation_id.reservation_line_ids.ids ids = reservation.reservation_line_ids.ids
if ( if (
self.env["pms.reservation.line"].search_count( self.env["pms.reservation.line"].search_count(
[ [
@@ -173,7 +176,7 @@ class PmsReservationLine(models.Model):
if len(rooms_ranking) == 0: if len(rooms_ranking) == 0:
raise ValidationError( raise ValidationError(
_("%s: No room type available") _("%s: No room type available")
% (line.reservation_id.room_type_id.name) % (reservation.room_type_id.name)
) )
else: else:
# we get the best score in the ranking # we get the best score in the ranking
@@ -194,7 +197,7 @@ class PmsReservationLine(models.Model):
line_past_night = self.env["pms.reservation.line"].search( line_past_night = self.env["pms.reservation.line"].search(
[ [
("date", "=", date_last_night), ("date", "=", date_last_night),
("reservation_id", "=", line.reservation_id.id), ("reservation_id", "=", reservation.id),
] ]
) )
# if there is the night before and if the room # if there is the night before and if the room