From f9ff8e2e04403849160f07f27d358be45f0d8a69 Mon Sep 17 00:00:00 2001 From: miguelpadin Date: Thu, 16 Dec 2021 09:53:43 +0100 Subject: [PATCH] [IMP] pms_api_rest: controller to modify reservation lines (room_id, & dates) --- pms_api_rest/datamodels/__init__.py | 1 + .../datamodels/pms_calendar_changes.py | 8 +++ pms_api_rest/services/reservation_services.py | 50 +++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 pms_api_rest/datamodels/pms_calendar_changes.py diff --git a/pms_api_rest/datamodels/__init__.py b/pms_api_rest/datamodels/__init__.py index 155aaec3e23..69d305d63c2 100644 --- a/pms_api_rest/datamodels/__init__.py +++ b/pms_api_rest/datamodels/__init__.py @@ -14,3 +14,4 @@ from . import pms_partner_info from . import pms_calendar_swap_info +from . import pms_calendar_changes diff --git a/pms_api_rest/datamodels/pms_calendar_changes.py b/pms_api_rest/datamodels/pms_calendar_changes.py new file mode 100644 index 00000000000..77e3e70747f --- /dev/null +++ b/pms_api_rest/datamodels/pms_calendar_changes.py @@ -0,0 +1,8 @@ +from marshmallow import fields + +from odoo.addons.datamodel.core import Datamodel + + +class PmsCalendarChanges(Datamodel): + _name = "pms.calendar.changes" + reservationLinesChanges = fields.List(fields.Dict(required=False, allow_none=True)) diff --git a/pms_api_rest/services/reservation_services.py b/pms_api_rest/services/reservation_services.py index 1e5c5cdbe54..3bd505ec547 100644 --- a/pms_api_rest/services/reservation_services.py +++ b/pms_api_rest/services/reservation_services.py @@ -48,3 +48,53 @@ def get_reservations(self): ) ) return result_reservations + + @restapi.method( + [ + ( + [ + "/", + ], + "PATCH", + ) + ], + input_param=Datamodel("pms.calendar.changes", is_list=False), + auth="public", + ) + def move_reservation_line(self, reservation_id, reservation_lines_changes): + + # 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"]), + ] + ) + # 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"]