[ADD]pms_api_rest: Add Price API Service boardServiceId Option

This commit is contained in:
Darío Lodeiros
2022-07-14 18:59:04 +02:00
parent 465dd898c4
commit 2617a82901
2 changed files with 84 additions and 22 deletions

View File

@@ -10,6 +10,7 @@ class PmsPriceSearchParam(Datamodel):
pmsPropertyId = fields.Integer(required=True, allow_none=True)
pricelistId = fields.Integer(required=True, allow_none=True)
roomTypeId = fields.Integer(required=False, allow_none=True)
boardServiceId = 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)

View File

@@ -27,14 +27,17 @@ class PmsAgencyService(Component):
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)])
product = room_type = board_service = False
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
if prices_search_param.productId:
product = self.env["product.product"].search([("id", "=", prices_search_param.productId)])
if prices_search_param.boardServiceId:
board_service = self.env["pms.board.service.room.type"].search([
("id", "=", prices_search_param.boardServiceId)]
)
if sum([var is not False for var in [product, room_type, board_service]]) != 1:
raise MissingError(_("It is necessary to indicate one and only one product, board service or room type"))
PmsPriceInfo = self.env.datamodels["pms.price.info"]
result_prices = []
@@ -45,42 +48,100 @@ class PmsAgencyService(Component):
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
),
if board_service:
result_prices.append(
PmsPriceInfo(
date=datetime.combine(
price_date, datetime.min.time()
).isoformat(),
price=round(
self._get_board_service_price(
board_service=board_service,
pms_property_id=prices_search_param.pmsPropertyId,
pricelist_id=prices_search_param.pricelistId,
partner_id=prices_search_param.partnerId,
product_qty=prices_search_param.productQty,
date_consumption=price_date
), 2
),
)
)
else:
result_prices.append(
PmsPriceInfo(
date=datetime.combine(
price_date, datetime.min.time()
).isoformat(),
price=round(
self._get_product_price(
product=product if product else room_type.product_id,
pms_property_id=prices_search_param.pmsPropertyId,
pricelist_id=prices_search_param.pricelistId,
partner_id=prices_search_param.partnerId,
product_qty=prices_search_param.productQty,
date_consumption=price_date
), 2
),
)
)
)
return result_prices
def _get_product_price(self, product, price_search_param, date_consumption=False):
def _get_product_price(
self,
product,
pms_property_id,
pricelist_id=False,
partner_id=False,
product_qty=False,
date_consumption=False,
board_service_id=False,
):
pms_property = self.env["pms.property"].browse(
price_search_param.pmsPropertyId
pms_property_id
)
product_context = dict(
self.env.context,
date=datetime.today().date(),
pricelist=price_search_param.pricelistId or False,
pricelist=pricelist_id,
uom=product.uom_id.id,
fiscal_position=False,
property=price_search_param.pmsPropertyId,
property=pms_property_id,
)
if date_consumption:
product_context["consumption_date"] = date_consumption
if board_service_id:
product_context["board_service"] = board_service_id
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,
pricelist_id=pricelist_id,
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_qty=product_qty or 1,
partner_id=partner_id,
),
product.taxes_id,
product.taxes_id, # Not exist service line, we repeat product taxes
pms_property.company_id,
)
def _get_board_service_price(
self,
board_service,
pms_property_id,
pricelist_id=False,
partner_id=False,
product_qty=False,
date_consumption=False,
):
price = 0
for product in board_service.board_service_line_ids.mapped("product_id"):
price += self._get_product_price(
product=product,
pms_property_id=pms_property_id,
pricelist_id=pricelist_id,
partner_id=partner_id,
product_qty=product_qty,
date_consumption=date_consumption,
)
return price