From 5f30373953e4b593de31bf7293798ab936729daa Mon Sep 17 00:00:00 2001 From: miguelpadin Date: Wed, 19 Jul 2023 13:37:51 +0200 Subject: [PATCH] [IMP] pms-api-rest: param for daily pricelists-services & new plannign pricelist service --- pms_api_rest/datamodels/pms_calendar.py | 6 + pms_api_rest/datamodels/pms_pricelist.py | 2 +- pms_api_rest/services/pms_calendar_service.py | 124 +++++++++++++++++- .../services/pms_pricelist_service.py | 12 +- 4 files changed, 136 insertions(+), 8 deletions(-) diff --git a/pms_api_rest/datamodels/pms_calendar.py b/pms_api_rest/datamodels/pms_calendar.py index d06545703..a136074b0 100644 --- a/pms_api_rest/datamodels/pms_calendar.py +++ b/pms_api_rest/datamodels/pms_calendar.py @@ -99,6 +99,12 @@ class PmsCalendarRenderInfo(Datamodel): dates = fields.List(fields.Dict(required=True, allow_none=False)) +class PmsCalendarPricesRulesRenderInfo(Datamodel): + _name = "pms.calendar.prices.rules.render.info" + roomTypeId = fields.Integer(required=True, allow_none=False) + dates = fields.List(fields.Dict(required=True, allow_none=False)) + + class PmsCalendarAlertsPerDay(Datamodel): _name = "pms.calendar.alerts.per.day" date = fields.String(required=True, allow_none=False) diff --git a/pms_api_rest/datamodels/pms_pricelist.py b/pms_api_rest/datamodels/pms_pricelist.py index cd023db48..07189c658 100644 --- a/pms_api_rest/datamodels/pms_pricelist.py +++ b/pms_api_rest/datamodels/pms_pricelist.py @@ -5,10 +5,10 @@ from odoo.addons.datamodel.core import Datamodel class PmsPricelistSearch(Datamodel): _name = "pms.pricelist.search" - pmsPropertyId = fields.Integer(required=False, allow_none=True) pmsPropertyIds = fields.List(fields.Integer(), required=False) saleChannelId = fields.Integer(required=False, allow_none=True) + daily = fields.Boolean(required=False, allow_none=True) class PmsPricelistInfo(Datamodel): diff --git a/pms_api_rest/services/pms_calendar_service.py b/pms_api_rest/services/pms_calendar_service.py index d4851d650..2697a149a 100644 --- a/pms_api_rest/services/pms_calendar_service.py +++ b/pms_api_rest/services/pms_calendar_service.py @@ -91,7 +91,7 @@ class PmsCalendarService(Component): output_param=Datamodel("pms.calendar.info", is_list=True), auth="jwt_api_pms", ) - def get_old_calendar(self, calendar_search_param): + def get_old_reservations_calendar(self, calendar_search_param): date_from = datetime.strptime(calendar_search_param.dateFrom, "%Y-%m-%d").date() date_to = datetime.strptime(calendar_search_param.dateTo, "%Y-%m-%d").date() count_nights = (date_to - date_from).days + 1 @@ -229,7 +229,7 @@ class PmsCalendarService(Component): output_param=Datamodel("pms.calendar.render.info", is_list=True), auth="jwt_api_pms", ) - def get_calendar_new(self, calendar_search_param): + def get_reservations_calendar(self, calendar_search_param): response = [] date_from = datetime.strptime(calendar_search_param.dateFrom, "%Y-%m-%d").date() date_to = datetime.strptime(calendar_search_param.dateTo, "%Y-%m-%d").date() @@ -375,6 +375,126 @@ class PmsCalendarService(Component): last_reservation_id = False return response + @restapi.method( + [ + ( + [ + "/calendar-prices-rules", + ], + "GET", + ) + ], + input_param=Datamodel("pms.calendar.search.param"), + output_param=Datamodel("pms.calendar.prices.rules.render.info", is_list=True), + auth="jwt_api_pms", + ) + def get_prices_rules_calendar(self, calendar_search_param): + response = [] + date_from = datetime.strptime(calendar_search_param.dateFrom, "%Y-%m-%d").date() + date_to = datetime.strptime(calendar_search_param.dateTo, "%Y-%m-%d").date() + + self.env.cr.execute( + f""" + SELECT dr.room_type_id, + dr.date date, + it.id pricelist_item_id, + av.id availability_plan_rule_id, + COALESCE(av.max_avail, dr.default_max_avail) max_avail, + COALESCE(av.quota, dr.default_quota) quota, + COALESCE(av.closed, FALSE) closed, + COALESCE(av.closed_arrival, FALSE) closed_arrival, + COALESCE(av.closed_Departure, FALSE) closed_departure, + COALESCE(av.min_stay, 0) min_stay, + COALESCE(av.min_stay_arrival, 0) min_stay_arrival, + COALESCE(av.max_stay, 0) max_stay, + COALESCE(av.max_stay_arrival, 0) max_stay_arrival, + COALESCE(it.fixed_price, ( + SELECT ipp.value_float + FROM ir_pms_property ipp, (SELECT id field_id, model_id + FROM ir_model_fields + WHERE name = 'list_price' AND model = 'product.template' + ) imf + WHERE ipp.model_id = imf.model_id + AND ipp.field_id = imf.field_id + AND ipp.record = pp.product_tmpl_id + AND ipp.pms_property_id = %s + ) + ) price + FROM + ( + SELECT dates.date, rt_r.room_type_id, rt_r.product_id, rt_r.default_max_avail, rt_r.default_quota + FROM + ( + SELECT (CURRENT_DATE + date) date + FROM generate_series(date %s- CURRENT_DATE, date %s - CURRENT_DATE) date + ) dates, + ( + SELECT rt.id room_type_id, + rt.product_id, + rt.default_max_avail, + rt.default_quota + FROM pms_room_type rt + WHERE EXISTS ( SELECT 1 + FROM pms_room + WHERE pms_property_id = %s AND room_type_id = rt.id) + ) rt_r + ) dr + INNER JOIN product_product pp ON pp.id = dr.product_id + LEFT OUTER JOIN pms_availability_plan_rule av ON av.date = dr.date + AND av.room_type_id = dr.room_type_id + AND av.pms_property_id = %s + AND av.availability_plan_id = %s + LEFT OUTER JOIN product_pricelist_item it ON it.date_start_consumption = dr.date + AND it.date_end_consumption = dr.date + AND it.product_id = dr.product_id + AND it.active = true + AND it.pricelist_id = %s + ORDER BY dr.room_type_id, dr.date; + """, + ( + calendar_search_param.pmsPropertyId, + date_from, + date_to, + calendar_search_param.pmsPropertyId, + calendar_search_param.pmsPropertyId, + calendar_search_param.availabilityPlanId, + calendar_search_param.pricelistId, + ), + ) + + result = self.env.cr.dictfetchall() + CalendarPricesRulesRenderInfo = self.env.datamodels["pms.calendar.prices.rules.render.info"] + last_room_type_id = False + for index, item in enumerate(result): + date = { + "date": datetime.combine(item['date'], datetime.min.time()).isoformat(), + "pricelistItemId": item['pricelist_item_id'], + "price": item['price'], + + "availabilityPlanRuleId": item['availability_plan_rule_id'], + "maxAvail": item['max_avail'], + "quota": item['quota'], + + "closed": item['closed'], + "closedArrival": item['closed_arrival'], + "closedDeparture": item['closed_departure'], + + "minStay": item['min_stay'], + "minStayArrival": item['min_stay_arrival'], + "maxStay": item['max_stay'], + "maxStayArrival": item['max_stay_arrival'], + } + if last_room_type_id != item['room_type_id']: + last_room_type_id = item['room_type_id'] + response.append( + CalendarPricesRulesRenderInfo( + roomTypeId=item["room_type_id"], + dates=[date], + ) + ) + else: + response[-1].dates.append(date) + return response @restapi.method( [ diff --git a/pms_api_rest/services/pms_pricelist_service.py b/pms_api_rest/services/pms_pricelist_service.py index dd5d43ea6..538cac390 100644 --- a/pms_api_rest/services/pms_pricelist_service.py +++ b/pms_api_rest/services/pms_pricelist_service.py @@ -28,11 +28,13 @@ class PmsPricelistService(Component): auth="jwt_api_pms", ) def get_pricelists(self, pms_search_param, **args): - pricelists = self.env["product.pricelist"].search( - [ - ("is_pms_available", "=", True), - ] - ) + domain = [ + ("is_pms_available", "=", True), + ] + if pms_search_param.daily and pms_search_param.daily is True: + domain.append(("pricelist_type", "=", 'daily')) + + pricelists = self.env["product.pricelist"].search(domain) if pms_search_param.pmsPropertyIds and pms_search_param.pmsPropertyId: raise ValidationError( _(