[IMP] pms_api_rest: add patch reseration

This commit is contained in:
Sara Lago
2022-02-07 16:44:38 +01:00
committed by Darío Lodeiros
parent a986b095c9
commit 1b9d99da97
2 changed files with 74 additions and 47 deletions

View File

@@ -3,9 +3,12 @@ from marshmallow import fields
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):

View File

@@ -116,60 +116,84 @@ class PmsReservationService(Component):
"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"]
):
line_to_change.room_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"
)
max_value = max(
first_reservation_line_to_change.reservation_id.reservation_line_ids.mapped(
"date"
# 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"]),
]
)
# 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"
)
) + 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(
[