diff --git a/pms_api_rest/datamodels/__init__.py b/pms_api_rest/datamodels/__init__.py index 9be95bc02..901486689 100644 --- a/pms_api_rest/datamodels/__init__.py +++ b/pms_api_rest/datamodels/__init__.py @@ -22,3 +22,4 @@ from . import pms_pricelist_item from . import pms_availability_plan from . import pms_availability_plan_rule from . import pms_search_param +from . import pms_ubication diff --git a/pms_api_rest/datamodels/pms_ubication.py b/pms_api_rest/datamodels/pms_ubication.py new file mode 100644 index 000000000..ad7ae7059 --- /dev/null +++ b/pms_api_rest/datamodels/pms_ubication.py @@ -0,0 +1,17 @@ +from marshmallow import fields + +from odoo.addons.datamodel.core import Datamodel + + +class PmsUbicationSearchParam(Datamodel): + _name = "pms.ubication.search.param" + id = fields.Integer(required=False, allow_none=True) + name = fields.String(required=False, allow_none=True) + pms_property_ids = fields.List(fields.Integer(), required=False) + + +class PmsUbicationInfo(Datamodel): + _name = "pms.ubication.info" + id = fields.Integer(required=False, allow_none=True) + name = fields.String(required=False, allow_none=True) + pms_property_ids = fields.List(fields.Integer(), required=False) diff --git a/pms_api_rest/services/__init__.py b/pms_api_rest/services/__init__.py index fdbc2d46a..f5b706756 100644 --- a/pms_api_rest/services/__init__.py +++ b/pms_api_rest/services/__init__.py @@ -10,3 +10,4 @@ from . import pms_login_service from . import pms_pricelist_service from . import pms_availability_plan_service from . import pms_room_type_class_service +from . import pms_ubication_service diff --git a/pms_api_rest/services/pms_ubication_service.py b/pms_api_rest/services/pms_ubication_service.py new file mode 100644 index 000000000..3f23e0958 --- /dev/null +++ b/pms_api_rest/services/pms_ubication_service.py @@ -0,0 +1,63 @@ +from odoo.addons.base_rest import restapi +from odoo.addons.base_rest_datamodel.restapi import Datamodel +from odoo.addons.component.core import Component + + +class PmsUbicationService(Component): + _inherit = "base.rest.service" + _name = "pms.ubication.service" + _usage = "ubication" + _collection = "pms.services" + + @restapi.method( + [ + ( + [ + "/", + ], + "GET", + ) + ], + input_param=Datamodel("pms.ubication.search.param"), + output_param=Datamodel("pms.ubication.info", is_list=True), + auth="jwt_api_pms", + ) + def get_ubications(self, ubication_search_param): + ubication_all_properties = self.env["pms.ubication"].search( + [("pms_property_ids", "=", False)] + ) + if ubication_search_param.pms_property_ids: + ubication = set() + for index, prop in enumerate(ubication_search_param.pms_property_ids): + ubication_with_query_property = self.env[ + "pms.ubication" + ].search([("pms_property_ids", "=", prop)]) + if index == 0: + ubication = set(ubication_with_query_property.ids) + else: + ubication = ubication.intersection( + set(ubication_with_query_property.ids) + ) + ubication_total = list( + set(list(ubication) + ubication_all_properties.ids) + ) + else: + ubication_total = list(ubication_all_properties.ids) + domain = [ + ("id", "in", ubication_total), + ] + + result_ubications = [] + PmsUbicationInfo = self.env.datamodels["pms.ubication.info"] + for room in self.env["pms.ubication"].search( + domain, + ): + + result_ubications.append( + PmsUbicationInfo( + id=room.id, + name=room.name, + pms_property_ids=room.pms_property_ids.mapped("id"), + ) + ) + return result_ubications