mirror of
https://github.com/OCA/pms.git
synced 2025-01-29 00:17:45 +02:00
[IMP]pms_api_rest: improvement calendar load performance
This commit is contained in:
@@ -25,64 +25,121 @@ class PmsCalendarService(Component):
|
|||||||
auth="jwt_api_pms",
|
auth="jwt_api_pms",
|
||||||
)
|
)
|
||||||
def get_calendar(self, calendar_search_param):
|
def get_calendar(self, calendar_search_param):
|
||||||
domain = list()
|
date_from = datetime.strptime(calendar_search_param.dateFrom, "%Y-%m-%d").date()
|
||||||
domain.append(("date", ">=", calendar_search_param.dateFrom))
|
date_to = datetime.strptime(calendar_search_param.dateTo, "%Y-%m-%d").date()
|
||||||
domain.append(("date", "<=", calendar_search_param.dateTo))
|
count_nights = (date_to - date_from).days + 1
|
||||||
domain.append(("pms_property_id", "=", calendar_search_param.pmsPropertyId))
|
target_dates = [date_from + timedelta(days=x) for x in range(count_nights)]
|
||||||
domain.append(("state", "!=", "cancel"))
|
pms_property_id = calendar_search_param.pmsPropertyId
|
||||||
result_lines = []
|
self.env.cr.execute(
|
||||||
|
"""
|
||||||
|
SELECT night.id as id, night.state, DATE(night.date), night.room_id,
|
||||||
|
pms_room_type.default_code, reservation.to_assign, reservation.splitted,
|
||||||
|
reservation.partner_id, reservation.partner_name, folio.id, reservation.id,
|
||||||
|
reservation.name, reservation.reservation_type, reservation.checkin,
|
||||||
|
reservation.checkout, reservation.price_total, folio.pending_amount,
|
||||||
|
reservation.adults
|
||||||
|
FROM pms_reservation_line night
|
||||||
|
LEFT JOIN pms_reservation reservation
|
||||||
|
ON reservation.id = night.reservation_id
|
||||||
|
LEFT JOIN pms_room_type
|
||||||
|
ON pms_room_type.id = reservation.room_type_id
|
||||||
|
LEFT JOIN pms_folio folio
|
||||||
|
ON folio.id = reservation.folio_id
|
||||||
|
WHERE (night.pms_property_id = %s)
|
||||||
|
AND (night.date in %s)
|
||||||
|
AND (night.state != 'cancel')
|
||||||
|
""",
|
||||||
|
(
|
||||||
|
pms_property_id,
|
||||||
|
tuple(target_dates),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
SQL_FIELDS = [
|
||||||
|
"id",
|
||||||
|
"state",
|
||||||
|
"date",
|
||||||
|
"room_id",
|
||||||
|
"room_type_name",
|
||||||
|
"to_assign",
|
||||||
|
"splitted",
|
||||||
|
"partner_id",
|
||||||
|
"partner_name",
|
||||||
|
"folio_id",
|
||||||
|
"reservation_id",
|
||||||
|
"reservation_name",
|
||||||
|
"reservation_type",
|
||||||
|
"checkin",
|
||||||
|
"checkout",
|
||||||
|
"price_total",
|
||||||
|
"folio_pending_amount",
|
||||||
|
"adults",
|
||||||
|
]
|
||||||
|
result_sql = self.env.cr.fetchall()
|
||||||
|
lines = []
|
||||||
|
for res in result_sql:
|
||||||
|
lines.append({field: res[SQL_FIELDS.index(field)] for field in SQL_FIELDS})
|
||||||
|
|
||||||
PmsCalendarInfo = self.env.datamodels["pms.calendar.info"]
|
PmsCalendarInfo = self.env.datamodels["pms.calendar.info"]
|
||||||
for line in self.env["pms.reservation.line"].search(
|
result_lines = []
|
||||||
domain,
|
for line in lines:
|
||||||
):
|
|
||||||
next_line_splitted = False
|
next_line_splitted = False
|
||||||
next_line = self.env["pms.reservation.line"].search(
|
|
||||||
[
|
|
||||||
("reservation_id", "=", line.reservation_id.id),
|
|
||||||
("date", "=", line.date + timedelta(days=1)),
|
|
||||||
]
|
|
||||||
)
|
|
||||||
if next_line:
|
|
||||||
next_line_splitted = next_line.room_id != line.room_id
|
|
||||||
|
|
||||||
previous_line_splitted = False
|
previous_line_splitted = False
|
||||||
previous_line = self.env["pms.reservation.line"].search(
|
is_first_night = line["checkin"] == line["date"]
|
||||||
[
|
is_last_night = line["checkout"] + timedelta(days=-1) == line["date"]
|
||||||
("reservation_id", "=", line.reservation_id.id),
|
if line.get("splitted"):
|
||||||
("date", "=", line.date + timedelta(days=-1)),
|
next_line = next(
|
||||||
]
|
(
|
||||||
)
|
item
|
||||||
if previous_line:
|
for item in lines
|
||||||
previous_line_splitted = previous_line.room_id != line.room_id
|
if item["reservation_id"] == line["reservation_id"]
|
||||||
|
and item["date"] == line["date"] + timedelta(days=1)
|
||||||
|
),
|
||||||
|
False,
|
||||||
|
)
|
||||||
|
if next_line:
|
||||||
|
next_line_splitted = next_line["room_id"] != line["room_id"]
|
||||||
|
|
||||||
|
previous_line = next(
|
||||||
|
(
|
||||||
|
item
|
||||||
|
for item in lines
|
||||||
|
if item["reservation_id"] == line["reservation_id"]
|
||||||
|
and item["date"] == line["date"] + timedelta(days=-1)
|
||||||
|
),
|
||||||
|
False,
|
||||||
|
)
|
||||||
|
if previous_line:
|
||||||
|
previous_line_splitted = previous_line["room_id"] != line["room_id"]
|
||||||
result_lines.append(
|
result_lines.append(
|
||||||
PmsCalendarInfo(
|
PmsCalendarInfo(
|
||||||
id=line.id,
|
id=line["id"],
|
||||||
state=line.reservation_id.state,
|
state=line["state"],
|
||||||
date=datetime.combine(line.date, datetime.min.time()).isoformat(),
|
date=datetime.combine(
|
||||||
roomId=line.room_id.id,
|
line["date"], datetime.min.time()
|
||||||
roomTypeName=str(line.reservation_id.room_type_id.default_code),
|
).isoformat(),
|
||||||
toAssign=line.reservation_id.to_assign,
|
roomId=line["room_id"],
|
||||||
splitted=line.reservation_id.splitted,
|
roomTypeName=str(line["room_type_name"]),
|
||||||
partnerId=line.reservation_id.partner_id.id or None,
|
toAssign=line["to_assign"],
|
||||||
partnerName=line.reservation_id.partner_name or None,
|
splitted=line["splitted"],
|
||||||
folioId=line.reservation_id.folio_id,
|
partnerId=line["partner_id"] or None,
|
||||||
reservationId=line.reservation_id,
|
partnerName=line["partner_name"] or None,
|
||||||
reservationName=line.reservation_id.name,
|
folioId=line["folio_id"],
|
||||||
reservationType=line.reservation_id.reservation_type,
|
reservationId=line["reservation_id"],
|
||||||
isFirstNight=line.reservation_id.checkin == line.date,
|
reservationName=line["reservation_name"],
|
||||||
isLastNight=line.reservation_id.checkout + timedelta(days=-1)
|
reservationType=line["reservation_type"],
|
||||||
== line.date,
|
isFirstNight=is_first_night,
|
||||||
totalPrice=round(line.reservation_id.price_total, 2),
|
isLastNight=is_last_night,
|
||||||
pendingPayment=round(line.reservation_id.folio_pending_amount, 2),
|
totalPrice=round(line["price_total"], 2),
|
||||||
numNotifications=line.reservation_id.message_needaction_counter,
|
pendingPayment=round(line["folio_pending_amount"], 2),
|
||||||
adults=line.reservation_id.adults,
|
# TODO: line.reservation_id.message_needaction_counter is computed field,
|
||||||
|
numNotifications=0,
|
||||||
|
adults=line["adults"],
|
||||||
nextLineSplitted=next_line_splitted,
|
nextLineSplitted=next_line_splitted,
|
||||||
previousLineSplitted=previous_line_splitted,
|
previousLineSplitted=previous_line_splitted,
|
||||||
hasNextLine=bool(next_line),
|
hasNextLine=not is_last_night, # REVIEW: redundant with isLastNight?
|
||||||
closureReason=line.reservation_id.closure_reason_id.name
|
closureReason=line[
|
||||||
if line.reservation_id.closure_reason_id
|
"partner_name"
|
||||||
else "",
|
], # REVIEW: is necesary closure_reason_id?
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return result_lines
|
return result_lines
|
||||||
@@ -144,42 +201,75 @@ class PmsCalendarService(Component):
|
|||||||
auth="jwt_api_pms",
|
auth="jwt_api_pms",
|
||||||
)
|
)
|
||||||
def get_daily_invoincing(self, pms_calendar_search_param):
|
def get_daily_invoincing(self, pms_calendar_search_param):
|
||||||
reservation_lines = self.env["pms.reservation.line"].search(
|
|
||||||
[
|
|
||||||
("date", ">=", pms_calendar_search_param.dateFrom),
|
|
||||||
("date", "<=", pms_calendar_search_param.dateTo),
|
|
||||||
("pms_property_id", "=", pms_calendar_search_param.pmsPropertyId),
|
|
||||||
]
|
|
||||||
)
|
|
||||||
service_lines = self.env["pms.service.line"].search(
|
|
||||||
[
|
|
||||||
("date", ">=", pms_calendar_search_param.dateFrom),
|
|
||||||
("date", "<=", pms_calendar_search_param.dateTo),
|
|
||||||
("pms_property_id", "=", pms_calendar_search_param.pmsPropertyId),
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
date_from = datetime.strptime(
|
date_from = datetime.strptime(
|
||||||
pms_calendar_search_param.dateFrom, "%Y-%m-%d"
|
pms_calendar_search_param.dateFrom, "%Y-%m-%d"
|
||||||
).date()
|
).date()
|
||||||
date_to = datetime.strptime(pms_calendar_search_param.dateTo, "%Y-%m-%d").date()
|
date_to = datetime.strptime(pms_calendar_search_param.dateTo, "%Y-%m-%d").date()
|
||||||
|
count_nights = (date_to - date_from).days + 1
|
||||||
|
target_dates = [date_from + timedelta(days=x) for x in range(count_nights)]
|
||||||
|
pms_property_id = pms_calendar_search_param.pmsPropertyId
|
||||||
|
|
||||||
|
self.env.cr.execute(
|
||||||
|
"""
|
||||||
|
SELECT night.date, SUM(night.price_day_total) AS production
|
||||||
|
FROM pms_reservation_line night
|
||||||
|
WHERE (night.pms_property_id = %s)
|
||||||
|
AND (night.date in %s)
|
||||||
|
GROUP BY night.date
|
||||||
|
""",
|
||||||
|
(
|
||||||
|
pms_property_id,
|
||||||
|
tuple(target_dates),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
production_per_nights_date = self.env.cr.fetchall()
|
||||||
|
|
||||||
|
self.env.cr.execute(
|
||||||
|
"""
|
||||||
|
SELECT service.date, SUM(service.price_day_total) AS production
|
||||||
|
FROM pms_service_line service
|
||||||
|
WHERE (service.pms_property_id = %s)
|
||||||
|
AND (service.date in %s)
|
||||||
|
GROUP BY service.date
|
||||||
|
""",
|
||||||
|
(
|
||||||
|
pms_property_id,
|
||||||
|
tuple(target_dates),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
production_per_services_date = self.env.cr.fetchall()
|
||||||
|
|
||||||
|
production_per_nights_dict = [
|
||||||
|
{"date": item[0], "total": item[1]} for item in production_per_nights_date
|
||||||
|
]
|
||||||
|
production_per_services_dict = [
|
||||||
|
{"date": item[0], "total": item[1]} for item in production_per_services_date
|
||||||
|
]
|
||||||
|
|
||||||
result = []
|
result = []
|
||||||
PmsCalendarDailyInvoicing = self.env.datamodels["pms.calendar.daily.invoicing"]
|
PmsCalendarDailyInvoicing = self.env.datamodels["pms.calendar.daily.invoicing"]
|
||||||
for day in (
|
for day in target_dates:
|
||||||
date_from + timedelta(d) for d in range((date_to - date_from).days + 1)
|
night_production = next(
|
||||||
):
|
(
|
||||||
reservation_lines_by_day = reservation_lines.filtered(
|
item["total"]
|
||||||
lambda d: d.date == day
|
for item in production_per_nights_dict
|
||||||
|
if item["date"] == day
|
||||||
|
),
|
||||||
|
False,
|
||||||
|
)
|
||||||
|
service_production = next(
|
||||||
|
(
|
||||||
|
item["total"]
|
||||||
|
for item in production_per_services_dict
|
||||||
|
if item["date"] == day
|
||||||
|
),
|
||||||
|
False,
|
||||||
)
|
)
|
||||||
service_lines_by_day = service_lines.filtered(lambda d: d.date == day)
|
|
||||||
result.append(
|
result.append(
|
||||||
PmsCalendarDailyInvoicing(
|
PmsCalendarDailyInvoicing(
|
||||||
date=datetime.combine(day, datetime.min.time()).isoformat(),
|
date=datetime.combine(day, datetime.min.time()).isoformat(),
|
||||||
invoicingTotal=round(
|
invoicingTotal=round(
|
||||||
sum(reservation_lines_by_day.mapped("price"))
|
(night_production or 0) + (service_production or 0), 2
|
||||||
+ sum(service_lines_by_day.mapped("price_day_total")),
|
|
||||||
2,
|
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user