From 1b9d99da974ee7aea106ac5f6d745935964abe7b Mon Sep 17 00:00:00 2001 From: Sara Lago Date: Mon, 7 Feb 2022 16:44:38 +0100 Subject: [PATCH] [IMP] pms_api_rest: add patch reseration --- pms_api_rest/datamodels/pms_calendar.py | 7 +- .../services/pms_reservation_service.py | 114 +++++++++++------- 2 files changed, 74 insertions(+), 47 deletions(-) diff --git a/pms_api_rest/datamodels/pms_calendar.py b/pms_api_rest/datamodels/pms_calendar.py index 05d4cb7b4..062a9e0ba 100644 --- a/pms_api_rest/datamodels/pms_calendar.py +++ b/pms_api_rest/datamodels/pms_calendar.py @@ -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): diff --git a/pms_api_rest/services/pms_reservation_service.py b/pms_api_rest/services/pms_reservation_service.py index c583ba8ac..4e7c11757 100644 --- a/pms_api_rest/services/pms_reservation_service.py +++ b/pms_api_rest/services/pms_reservation_service.py @@ -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( [