Skip to content

Commit

Permalink
[IMP] pms_api_rest: add patch reseration
Browse files Browse the repository at this point in the history
  • Loading branch information
saralb9 committed Feb 7, 2022
1 parent 8291095 commit a273d47
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 46 deletions.
7 changes: 5 additions & 2 deletions pms_api_rest/datamodels/pms_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
from odoo.addons.datamodel.core import Datamodel


class PmsCalendarChanges(Datamodel):
_name = "pms.calendar.changes"
class PmsReservationUpdates(Datamodel):
_name = "pms.reservation.updates"
reservationLinesChanges = fields.List(fields.Dict(required=False, allow_none=True))
preferredRoomId = fields.Integer(required=False, allow_none=True)
boardServiceId = fields.Integer(required=False, allow_none=True)
pricelistId = fields.Integer(required=False, allow_none=True)


class PmsCalendarSwapInfo(Datamodel):
Expand Down
112 changes: 68 additions & 44 deletions pms_api_rest/services/pms_reservation_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,60 +116,84 @@ def get_reservation(self, reservation_id, pms_search_param):
"PATCH",
)
],
input_param=Datamodel("pms.calendar.changes", is_list=False),
input_param=Datamodel("pms.reservation.updates", is_list=False),
auth="jwt_api_pms",
)
def update_reservation(self, reservation_id, reservation_lines_changes):
if reservation_lines_changes.reservationLinesChanges:

# get date of first reservation id to change
first_reservation_line_id_to_change = (
reservation_lines_changes.reservationLinesChanges[0]["reservationLineId"]
)
first_reservation_line_to_change = self.env["pms.reservation.line"].browse(
first_reservation_line_id_to_change
)
date_first_reservation_line_to_change = datetime.strptime(
reservation_lines_changes.reservationLinesChanges[0]["date"], "%Y-%m-%d"
)

# iterate changes
for change_iterator in sorted(
reservation_lines_changes.reservationLinesChanges,
# adjust order to start changing from last/first reservation line
# to avoid reservation line date constraint
reverse=first_reservation_line_to_change.date
< date_first_reservation_line_to_change.date(),
key=lambda x: datetime.strptime(x["date"], "%Y-%m-%d"),
):
# recordset of each line
line_to_change = self.env["pms.reservation.line"].search(
[
("reservation_id", "=", reservation_id),
("id", "=", change_iterator["reservationLineId"]),
# get date of first reservation id to change
first_reservation_line_id_to_change = (
reservation_lines_changes.reservationLinesChanges[0][
"reservationLineId"
]
)
# modifying date, room_id, ...
if "date" in change_iterator:
line_to_change.date = change_iterator["date"]
if (
"roomId" in change_iterator
and line_to_change.room_id.id != change_iterator["roomId"]
first_reservation_line_to_change = self.env["pms.reservation.line"].browse(
first_reservation_line_id_to_change
)
date_first_reservation_line_to_change = datetime.strptime(
reservation_lines_changes.reservationLinesChanges[0]["date"], "%Y-%m-%d"
)

# iterate changes
for change_iterator in sorted(
reservation_lines_changes.reservationLinesChanges,
# adjust order to start changing from last/first reservation line
# to avoid reservation line date constraint
reverse=first_reservation_line_to_change.date
< date_first_reservation_line_to_change.date(),
key=lambda x: datetime.strptime(x["date"], "%Y-%m-%d"),
):
line_to_change.room_id = change_iterator["roomId"]
# recordset of each line
line_to_change = self.env["pms.reservation.line"].search(
[
("reservation_id", "=", reservation_id),
("id", "=", change_iterator["reservationLineId"]),
]
)
# modifying date, room_id, ...
if "date" in change_iterator:
line_to_change.date = change_iterator["date"]
if (
"roomId" in change_iterator
and line_to_change.room_id.id != change_iterator["roomId"]
):
line_to_change.room_id = change_iterator["roomId"]

max_value = max(
first_reservation_line_to_change.reservation_id.reservation_line_ids.mapped(
"date"
max_value = max(
first_reservation_line_to_change.reservation_id.reservation_line_ids.mapped(
"date"
)
) + timedelta(days=1)
min_value = min(
first_reservation_line_to_change.reservation_id.reservation_line_ids.mapped(
"date"
)
)
) + timedelta(days=1)
min_value = min(
first_reservation_line_to_change.reservation_id.reservation_line_ids.mapped(
"date"
reservation = self.env["pms.reservation"].browse(reservation_id)
reservation.checkin = min_value
reservation.checkout = max_value

else:
reservation_to_update = (
self.env["pms.reservation"].sudo().search([("id", "=", reservation_id)])
)
)
reservation = self.env["pms.reservation"].browse(reservation_id)
reservation.checkin = min_value
reservation.checkout = max_value
reservation_vals = {}

if reservation_lines_changes.preferredRoomId:
reservation_vals.update(
{"preferred_room_id": reservation_lines_changes.preferredRoomId}
)
if reservation_lines_changes.boardServiceId:
reservation_vals.update(
{"board_service_room_id": reservation_lines_changes.boardServiceId}
)
if reservation_lines_changes.pricelistId:
reservation_vals.update(
{"pricelist_id": reservation_lines_changes.pricelistId}
)

reservation_to_update.write(reservation_vals)

@restapi.method(
[
Expand Down

0 comments on commit a273d47

Please sign in to comment.