diff --git a/pms_api_rest/datamodels/__init__.py b/pms_api_rest/datamodels/__init__.py index 10c6a60c9..bf04d4160 100644 --- a/pms_api_rest/datamodels/__init__.py +++ b/pms_api_rest/datamodels/__init__.py @@ -20,6 +20,7 @@ from . import pms_user from . import pms_pricelist from . import pms_pricelist_item +from . import pms_price from . import pms_availability_plan from . import pms_availability_plan_rule diff --git a/pms_api_rest/datamodels/pms_price.py b/pms_api_rest/datamodels/pms_price.py new file mode 100644 index 000000000..08aa8a5c1 --- /dev/null +++ b/pms_api_rest/datamodels/pms_price.py @@ -0,0 +1,21 @@ +from marshmallow import fields + +from odoo.addons.datamodel.core import Datamodel + + +class PmsPriceSearchParam(Datamodel): + _name = "pms.price.search.param" + dateFrom = fields.String(required=True, allow_none=True) + dateTo = fields.String(required=True, allow_none=True) + pmsPropertyId = fields.Integer(required=True, allow_none=True) + pricelistId = fields.Integer(required=True, allow_none=True) + roomTypeId = fields.Integer(required=False, allow_none=True) + productId = fields.Integer(required=False, allow_none=True) + productQty = fields.Integer(required=False, allow_none=True) + partnerId = fields.Integer(required=False, allow_none=True) + + +class PmsPriceInfo(Datamodel): + _name = "pms.price.info" + date = fields.String(required=True, allow_none=False) + price = fields.Float(required=True, allow_none=False) diff --git a/pms_api_rest/datamodels/pms_product.py b/pms_api_rest/datamodels/pms_product.py index 60bd6a202..a40470009 100644 --- a/pms_api_rest/datamodels/pms_product.py +++ b/pms_api_rest/datamodels/pms_product.py @@ -7,10 +7,6 @@ class PmsProductSearchParam(Datamodel): _name = "pms.product.search.param" name = fields.String(required=False, allow_none=True) pmsPropertyId = fields.Integer(required=True, allow_none=False) - pricelistId = fields.Integer(required=True, allow_none=False) - partnerId = fields.Integer(required=False, allow_none=True) - dateConsumption = fields.String(required=False, allow_none=True) - productQty = fields.Integer(required=False, allow_none=True) class PmProductInfo(Datamodel): @@ -19,4 +15,3 @@ class PmProductInfo(Datamodel): name = fields.String(required=False, allow_none=True) perDay = fields.Boolean(required=False, allow_none=True) perPerson = fields.Boolean(required=False, allow_none=True) - price = fields.Float(required=False, allow_none=True) diff --git a/pms_api_rest/services/__init__.py b/pms_api_rest/services/__init__.py index 0ccf5d27d..f5ff7c26e 100644 --- a/pms_api_rest/services/__init__.py +++ b/pms_api_rest/services/__init__.py @@ -9,6 +9,7 @@ from . import pms_reservation_line_service from . import pms_property_service from . import pms_login_service from . import pms_pricelist_service +from . import pms_price_service from . import pms_availability_plan_service from . import pms_id_category_service from . import res_country_service diff --git a/pms_api_rest/services/pms_price_service.py b/pms_api_rest/services/pms_price_service.py new file mode 100644 index 000000000..717d813bb --- /dev/null +++ b/pms_api_rest/services/pms_price_service.py @@ -0,0 +1,86 @@ +from odoo import _, fields +from odoo.exceptions import MissingError +from datetime import datetime, timedelta + +from odoo.addons.base_rest import restapi +from odoo.addons.base_rest_datamodel.restapi import Datamodel +from odoo.addons.component.core import Component + + +class PmsAgencyService(Component): + _inherit = "base.rest.service" + _name = "pms.price.service" + _usage = "prices" + _collection = "pms.services" + + @restapi.method( + [ + ( + [ + "/", + ], + "GET", + ) + ], + input_param=Datamodel("pms.price.search.param"), + output_param=Datamodel("pms.price.info", is_list=True), + auth="jwt_api_pms", + ) + def get_prices(self, prices_search_param): + product = room_type = False + if prices_search_param.productId: + product = self.env["product.product"].search([("id", "=", prices_search_param.productId)]) + if prices_search_param.roomTypeId: + room_type = self.env["pms.room.type"].search([("id", "=", prices_search_param.roomTypeId)]) + if (product and room_type) or (not product and not room_type): + raise MissingError(_("It is necessary to indicate one and only one product or room type")) + product = product if product else room_type.product_id + + PmsPriceInfo = self.env.datamodels["pms.price.info"] + result_prices = [] + date_from = fields.Date.from_string(prices_search_param.dateFrom) + date_to = fields.Date.from_string(prices_search_param.dateTo) + dates = [ + date_from + timedelta(days=x) + for x in range(0, (date_to - date_from).days + 1) + ] + for price_date in dates: + result_prices.append( + PmsPriceInfo( + date=datetime.combine( + price_date, datetime.min.time() + ).isoformat(), + price=round( + self._get_product_price(product, prices_search_param, price_date), 2 + ), + ) + ) + return result_prices + + def _get_product_price(self, product, price_search_param, date_consumption=False): + pms_property = self.env["pms.property"].browse( + price_search_param.pmsPropertyId + ) + product_context = dict( + self.env.context, + date=datetime.today().date(), + pricelist=price_search_param.pricelistId or False, + uom=product.uom_id.id, + fiscal_position=False, + property=price_search_param.pmsPropertyId, + ) + if date_consumption: + product_context["consumption_date"] = date_consumption + product = product.with_context(product_context) + return self.env["account.tax"]._fix_tax_included_price_company( + self.env["product.product"]._pms_get_display_price( + pricelist_id=price_search_param.pricelistId, + product=product, + company_id=pms_property.company_id.id, + product_qty=price_search_param.productQty or 1, + partner_id=price_search_param.partnerId or False, + ), + product.taxes_id, + product.taxes_id, # Not exist service line, we repeat product taxes + pms_property.company_id, + ) diff --git a/pms_api_rest/services/pms_product_service.py b/pms_api_rest/services/pms_product_service.py index f2a761083..42259cfa6 100644 --- a/pms_api_rest/services/pms_product_service.py +++ b/pms_api_rest/services/pms_product_service.py @@ -52,9 +52,6 @@ class PmsProductService(Component): PmsProductInfo( id=product.id, name=product.name, - price=round( - self._get_product_price(product, product_search_param), 2 - ), perDay=product.per_day, perPerson=product.per_person, ) @@ -81,37 +78,9 @@ class PmsProductService(Component): return PmsProductInfo( id=product.id, name=product.name, - price=round(self._get_product_price(product, product_search_param), 2), perDay=product.per_day, perPerson=product.per_person, ) else: raise MissingError(_("Product not found")) - def _get_product_price(self, product, product_search_param): - pms_property = self.env["pms.property"].browse( - product_search_param.pmsPropertyId - ) - product_context = dict( - self.env.context, - date=datetime.today().date(), - pricelist=product_search_param.pricelistId or False, - uom=product.uom_id.id, - fiscal_position=False, - property=product_search_param.pmsPropertyId, - ) - if product_search_param.dateConsumption: - product_context["consumption_date"] = product_search_param.dateConsumption - product = product.with_context(product_context) - return self.env["account.tax"]._fix_tax_included_price_company( - self.env["product.product"]._pms_get_display_price( - pricelist_id=product_search_param.pricelistId, - product=product, - company_id=pms_property.company_id.id, - product_qty=product_search_param.productQty or 1, - partner_id=product_search_param.partnerId, - ), - product.taxes_id, - product.taxes_id, # Not exist service line, we repeat product taxes - pms_property.company_id, - )