[ADD]pms_api_rest: product services

This commit is contained in:
Darío Lodeiros
2022-06-22 17:08:54 +02:00
parent 6b6330beaa
commit 7a8db9757b
5 changed files with 188 additions and 0 deletions

View File

@@ -37,3 +37,5 @@ from . import pms_amenity
from . import pms_board_service
from . import pms_board_service_line
from . import pms_product

View File

@@ -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)

View File

@@ -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

View File

@@ -89,3 +89,47 @@ class PmsBoardServiceService(Component):
)
else:
raise MissingError(_("Amenity Type not found"))
@restapi.method(
[
(
[
"/<int:board_service_id>/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

View File

@@ -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(
[
(
[
"/<int:product_id>",
],
"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,
)