Skip to content

Commit

Permalink
[IMP] Add folio services
Browse files Browse the repository at this point in the history
  • Loading branch information
saralb9 committed Aug 31, 2021
1 parent 7ad0417 commit a91bc67
Show file tree
Hide file tree
Showing 9 changed files with 322 additions and 27 deletions.
22 changes: 12 additions & 10 deletions pms_api_rest/controllers/pms_rest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from odoo.addons.base_rest.controllers import main
from ..lib_jwt.jwt_http import jwt_http
from ..lib_jwt.validator import validator


class BaseRestDemoPublicApiController(main.RestController):
Expand All @@ -7,13 +9,13 @@ class BaseRestDemoPublicApiController(main.RestController):
_default_auth = "public"

# RestController OVERRIDE method
# def _process_method(self, service_name, method_name, *args, params=None):
#
# http_method, body, headers, token = jwt_http.parse_request()
# result = validator.verify_token(token)
# if not result["status"]:
# return jwt_http.errcode(code=result["code"], message=result["message"])
# else:
# return super(BaseRestDemoPublicApiController, self)._process_method(
# service_name, method_name, *args, params=params
# )
def _process_method(self, service_name, method_name, *args, params=None):

http_method, body, headers, token = jwt_http.parse_request()
result = validator.verify_token(token)
if not result["status"]:
return jwt_http.errcode(code=result["code"], message=result["message"])
else:
return super(BaseRestDemoPublicApiController, self)._process_method(
service_name, method_name, *args, params=params
)
2 changes: 2 additions & 0 deletions pms_api_rest/datamodels/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
from . import pms_reservation_short_info
from . import pms_reservation_search_param
from . import pms_folio_short_info
from . import pms_folio_search_param
10 changes: 10 additions & 0 deletions pms_api_rest/datamodels/pms_folio_search_param.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from marshmallow import fields

from odoo.addons.datamodel.core import Datamodel


class PmsFolioSearchParam(Datamodel):
_name = "pms.folio.search.param"

id = fields.Integer(required=False, allow_none=False)
name = fields.String(required=False, allow_none=False)
34 changes: 34 additions & 0 deletions pms_api_rest/datamodels/pms_folio_short_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from marshmallow import fields, Schema
from typing import List
from odoo.addons.datamodel.core import Datamodel
from .pms_reservation_short_info import PmsReservationShortInfo


class PmsReservationSchema(Schema):

id = fields.Integer(required=True, allow_none=False)
checkin = fields.String(required=True, allow_none=True)
checkout = fields.String(required=True, allow_none=True)
preferredRoomId = fields.String(required=False, allow_none=True)
roomTypeId = fields.String(required=False, allow_none=True)
priceTotal = fields.Float(required=False, allow_none=True)
adults = fields.Integer(required=False, allow_none=True)
pricelist = fields.String(required=False, allow_none=True)



class PmsFolioShortInfo(Datamodel):
_name = "pms.folio.short.info"

id = fields.Integer(required=False, allow_none=True)
name = fields.String(required=False, allow_none=True)
partnerName = fields.String(required=False, allow_none=True)
partnerPhone = fields.String(required=False, allow_none=True)
partnerEmail = fields.String(required=False, allow_none=True)
channelType = fields.String(required=False, allow_none=True)
agency = fields.String(required=False, allow_none=True)
# paymentState = fields.String(required=False, allow_none=True)
state = fields.String(required=False, allow_none=True)
pendingAmount = fields.Float(required=False, allow_none=True)
reservations = fields.List(fields.Nested(PmsReservationSchema))

26 changes: 14 additions & 12 deletions pms_api_rest/datamodels/pms_reservation_short_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ class PmsReservationShortInfo(Datamodel):
_name = "pms.reservation.short.info"

id = fields.Integer(required=True, allow_none=False)
partner = fields.String(required=True, allow_none=False)
checkin = fields.String(required=True, allow_none=False)
checkout = fields.String(required=True, allow_none=False)
preferredRoomId = fields.String(required=True, allow_none=False)
roomTypeId = fields.String(required=True, allow_none=False)
name = fields.String(required=True, allow_none=False)
partner = fields.String(required=False, allow_none=True)
checkin = fields.String(required=True, allow_none=True)
checkout = fields.String(required=True, allow_none=True)
preferredRoomId = fields.String(required=False, allow_none=True)
roomTypeId = fields.String(required=False, allow_none=True)
name = fields.String(required=False, allow_none=True)
partnerRequests = fields.String(required=False, allow_none=True)
state = fields.String(required=True, allow_none=False)
priceTotal = fields.Float(required=True, allow_none=True)
adults = fields.Integer(required=True, allow_none=False)
state = fields.String(required=False, allow_none=True)
priceTotal = fields.Float(required=False, allow_none=True)
adults = fields.Integer(required=False, allow_none=True)
channelTypeId = fields.String(required=False, allow_none=True)
agencyId = fields.String(required=False, allow_none=True)
boardServiceId = fields.String(required=False, allow_none=True)
checkinsRatio = fields.Float(required=True, allow_none=False)
outstanding = fields.Float(required=True, allow_none=False)
pwaActionButtons = fields.Dict(required=True, allow_none=False)
checkinsRatio = fields.Float(required=False, allow_none=True)
outstanding = fields.Float(required=False, allow_none=True)
pricelist = fields.String(required=False, allow_none=True)
folioId = fields.Integer(required=False, allow_none=True)
pwaActionButtons = fields.Dict(required=False, allow_none=True)
2 changes: 1 addition & 1 deletion pms_api_rest/lib_jwt/jwt_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def do_login(self, login, password):

# login success, generate token
user = request.env.user.read(return_fields)[0]
exp = datetime.datetime.utcnow() + datetime.timedelta(minutes=3)
exp = datetime.datetime.utcnow() + datetime.timedelta(minutes=30000)
token = validator.create_token(user, exp)

return self.response(
Expand Down
1 change: 1 addition & 0 deletions pms_api_rest/services/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import reservation_services
from . import folio_services
179 changes: 179 additions & 0 deletions pms_api_rest/services/folio_services.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
import json

from odoo.addons.base_rest import restapi
from odoo.addons.base_rest_datamodel.restapi import Datamodel
from odoo.addons.component.core import Component

from ..datamodels.pms_folio_short_info import PmsReservationSchema


class PmsFolioService(Component):
_inherit = "base.rest.service"
_name = "pms.folio.service"
_usage = "folios"
_collection = "pms.reservation.service"

@restapi.method(
[
(
[
"/",
],
"GET",
)
],
input_param=Datamodel("pms.folio.search.param"),
output_param=Datamodel("pms.folio.short.info", is_list=True),
auth="public",
)
def search(self, folio_search_param):
domain = []
if folio_search_param.name:
domain.append(("name", "like", folio_search_param.name))
if folio_search_param.id:
domain.append(("id", "=", folio_search_param.id))
res = []
PmsFolioShortInfo = self.env.datamodels["pms.folio.short.info"]
PmsReservationShortInfo = self.env.datamodels["pms.reservation.short.info"]
for folio in (
self.env["pms.folio"]
.sudo()
.search(
domain,
)
):
reservations = []
for reservation in folio.reservation_ids:
reservation_schema = PmsReservationSchema()
room = reservation.preferred_room_id.name if reservation.preferred_room_id else ""
room_type = reservation.room_type_id.name if reservation.room_type_id else ""
data = {
"id": reservation.id,
"checkin": str(reservation.checkin),
"checkout": str(reservation.checkout),
"preferredRoomId": room,
"roomTypeId": room_type,
"priceTotal": reservation.price_total,
"adults": reservation.adults,
"pricelist": reservation.pricelist_id.name,
}
reservations.append(reservation_schema.load(data))
'''{
"id": reservation.id,
"checkin": str(reservation.checkin),
"checkout": str(reservation.checkout),
"preferredRoomId": reservation.preferred_room_id.name
if reservation.preferred_room_id
else "",
"roomTypeId": reservation.room_type_id.name
if reservation.room_type_id
else "",
"priceTotal": reservation.price_total,
"adults": reservation.adults,
"pricelist": reservation.pricelist_id.name,
}'''






'''
reservations.append(
PmsReservationSchema(
"id":reservation.id,
checkin=str(reservation.checkin),
checkout=str(reservation.checkout),
preferredRoomId=reservation.preferred_room_id.name
if reservation.preferred_room_id
else "",
roomTypeId=reservation.room_type_id.name
if reservation.room_type_id
else "",
priceTotal=reservation.price_total,
adults=reservation.adults,
pricelist=reservation.pricelist_id.name,
)
)
'''

res.append(
PmsFolioShortInfo(
id=folio.id,
name=folio.name,
partnerName=folio.partner_name if folio.partner_name else "",
partnerPhone=folio.mobile if folio.mobile else "",
partnerEmail=folio.email if folio.email else "",
channelType=folio.channel_type_id if folio.channel_type_id else "",
agency=folio.agency_id if folio.agency_id else "",
# paymentState=dict(folio.fields_get(["payment_state"])["payment_state"]["selection"])[
# folio.payment_state
# ],
state=dict(folio.fields_get(["state"])["state"]["selection"])[
folio.state
],
pendingAmount=folio.pending_amount,
reservations=reservations,
)
)
return res

@restapi.method(
[
(
[
"/<int:id>/reservations",
],
"GET",
)
],
output_param=Datamodel("pms.reservation.short.info", is_list=True),
auth="public",
)
def get_reservations(self, folio_id):
folio = (
self.env["pms.folio"].sudo().search([("id", "=", folio_id)])
)
res = []
if not folio.reservation_ids:
pass
else:
PmsReservationShortInfo = self.env.datamodels["pms.reservation.short.info"]

for reservation in folio.reservation_ids:
res.append(
PmsReservationShortInfo(
id=reservation.id,
partner=reservation.partner_id.name,
checkin=str(reservation.checkin),
checkout=str(reservation.checkout),
preferredRoomId=reservation.preferred_room_id.name
if reservation.preferred_room_id
else "",
roomTypeId=reservation.room_type_id.name
if reservation.room_type_id
else "",
name=reservation.name,
partnerRequests=reservation.partner_requests
if reservation.partner_requests
else "",
state=dict(reservation.fields_get(["state"])["state"]["selection"])[
reservation.state
],
priceTotal=reservation.price_total,
adults=reservation.adults,
channelTypeId=reservation.channel_type_id
if reservation.channel_type_id
else "",
agencyId=reservation.agency_id if reservation.agency_id else "",
boardServiceId=reservation.board_service_room_id.pms_board_service_id.name
if reservation.board_service_room_id
else "",
checkinsRatio=reservation.checkins_ratio,
outstanding=reservation.folio_id.pending_amount,
pwaActionButtons=json.loads(reservation.pwa_action_buttons)
if reservation.pwa_action_buttons
else {},
)
)
return res
Loading

0 comments on commit a91bc67

Please sign in to comment.