diff --git a/pms_api_rest/datamodels/__init__.py b/pms_api_rest/datamodels/__init__.py index 35f81da72..b0bec9988 100644 --- a/pms_api_rest/datamodels/__init__.py +++ b/pms_api_rest/datamodels/__init__.py @@ -37,3 +37,5 @@ from . import pms_amenity from . import pms_board_service from . import pms_board_service_line + +from . import pms_product diff --git a/pms_api_rest/datamodels/pms_product.py b/pms_api_rest/datamodels/pms_product.py new file mode 100644 index 000000000..7ceabe03d --- /dev/null +++ b/pms_api_rest/datamodels/pms_product.py @@ -0,0 +1,23 @@ +from marshmallow import fields + +from odoo.addons.datamodel.core import Datamodel + + +class PmsProductSearchParam(Datamodel): + _name = "pms.product.search.param" + ids = fields.List(fields.Integer(required=False, allow_none=True)) + 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): + _name = "pms.product.info" + id = fields.Integer(required=False, allow_none=True) + 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 a4ed64412..f9817f835 100644 --- a/pms_api_rest/services/__init__.py +++ b/pms_api_rest/services/__init__.py @@ -23,3 +23,5 @@ from . import pms_amenity_type_service from . import pms_board_service_line_service from . import pms_board_service_service + +from . import pms_product_service diff --git a/pms_api_rest/services/pms_board_service_service.py b/pms_api_rest/services/pms_board_service_service.py index 4462077c2..06312dda9 100644 --- a/pms_api_rest/services/pms_board_service_service.py +++ b/pms_api_rest/services/pms_board_service_service.py @@ -89,3 +89,47 @@ class PmsBoardServiceService(Component): ) else: raise MissingError(_("Amenity Type not found")) + + @restapi.method( + [ + ( + [ + "//lines", + ], + "GET", + ) + ], + input_param=Datamodel("pms.search.param"), + output_param=Datamodel("pms.board.service.line.info", is_list=True), + auth="jwt_api_pms", + ) + def get_board_service_lines(self, board_service_id, pms_search_param): + domain = list() + domain.append(("pms_board_service_room_type_id", "=", board_service_id)) + if pms_search_param.pms_property_id: + domain.extend( + [ + "|", + ( + "pms_property_ids", + "in", + pms_search_param.pms_property_id, + ), + ("pms_property_ids", "=", False), + ] + ) + result_board_service_lines = [] + PmsBoardServiceInfo = self.env.datamodels["pms.board.service.line.info"] + for line in self.env["pms.board.service.room.type.line"].search( + domain, + ): + result_board_service_lines.append( + PmsBoardServiceInfo( + id=line.id, + name=line.pms_board_service_room_type_id.display_name, + boardServiceId=line.pms_board_service_room_type_id.id, + productId=line.product_id.id, + amount=line.amount, + ) + ) + return result_board_service_lines diff --git a/pms_api_rest/services/pms_product_service.py b/pms_api_rest/services/pms_product_service.py new file mode 100644 index 000000000..d4ee08489 --- /dev/null +++ b/pms_api_rest/services/pms_product_service.py @@ -0,0 +1,117 @@ +from datetime import datetime + +from odoo import _ +from odoo.exceptions import MissingError + +from odoo.addons.base_rest import restapi +from odoo.addons.base_rest_datamodel.restapi import Datamodel +from odoo.addons.component.core import Component + + +class PmsProductService(Component): + _inherit = "base.rest.service" + _name = "pms.product.service" + _usage = "products" + _collection = "pms.services" + + @restapi.method( + [ + ( + [ + "/", + ], + "GET", + ) + ], + input_param=Datamodel("pms.product.search.param"), + output_param=Datamodel("pms.product.info", is_list=True), + auth="jwt_api_pms", + ) + def get_products(self, product_search_param): + domain = [("sale_ok", "=", True)] + if product_search_param.name: + domain.append(("name", "like", product_search_param.name)) + if product_search_param.ids: + domain.append(("id", "in", product_search_param.ids)) + if product_search_param.pmsPropertyId: + domain.extend( + [ + "|", + ( + "pms_property_ids", + "in", + product_search_param.pmsPropertyId, + ), + ("pms_property_ids", "=", False), + ] + ) + result_products = [] + PmsProductInfo = self.env.datamodels["pms.product.info"] + for product in self.env["product.product"].search( + domain, + ): + result_products.append( + PmsProductInfo( + id=product.id, + name=product.name, + price=self._get_product_price(product, product_search_param), + perDay=product.per_day, + perPerson=product.per_person, + ) + ) + return result_products + + @restapi.method( + [ + ( + [ + "/", + ], + "GET", + ) + ], + input_param=Datamodel("pms.product.search.param"), + output_param=Datamodel("pms.product.info", is_list=False), + auth="jwt_api_pms", + ) + def get_product(self, product_id, product_search_param): + product = self.env["product.product"].browse(product_id) + if product and product.sale_ok: + PmsProductInfo = self.env.datamodels["pms.product.info"] + return PmsProductInfo( + id=product.id, + name=product.name, + price=self._get_product_price(product, product_search_param), + 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, + )