Skip to content

Commit

Permalink
[IMP] pms_api_rest: controller to modify reservation lines (room_id, …
Browse files Browse the repository at this point in the history
…& dates)
  • Loading branch information
miguelpadin committed Dec 16, 2021
1 parent df0ba19 commit f9ff8e2
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions pms_api_rest/datamodels/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
from . import pms_partner_info

from . import pms_calendar_swap_info
from . import pms_calendar_changes
8 changes: 8 additions & 0 deletions pms_api_rest/datamodels/pms_calendar_changes.py
Original file line number Diff line number Diff line change
@@ -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))
50 changes: 50 additions & 0 deletions pms_api_rest/services/reservation_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,53 @@ def get_reservations(self):
)
)
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"]

0 comments on commit f9ff8e2

Please sign in to comment.