[IMP] pms_api_rest: controller to modify reservation lines (room_id, & dates)

This commit is contained in:
miguelpadin
2021-12-16 09:53:43 +01:00
committed by Darío Lodeiros
parent 23b44d053a
commit 72d7600755
3 changed files with 59 additions and 0 deletions

View File

@@ -14,3 +14,4 @@ from . import pms_checkin_partner_info
from . import pms_partner_info
from . import pms_calendar_swap_info
from . import pms_calendar_changes

View File

@@ -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))

View File

@@ -44,3 +44,53 @@ class PmsRoomService(Component):
)
)
return result_reservations
@restapi.method(
[
(
[
"/<int:reservation_id>",
],
"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"]