[IMP]pms_api_rest: get pricelists improvements (refact + saleChannelId param)

This commit is contained in:
Darío Lodeiros
2022-09-01 17:12:36 +02:00
parent be177fd6ca
commit 68579dbd94
2 changed files with 42 additions and 30 deletions

View File

@@ -3,6 +3,14 @@ from marshmallow import fields
from odoo.addons.datamodel.core import Datamodel
class PmsPricelistSearch(Datamodel):
_name = "pms.pricelist.search"
pmsPropertyId = fields.Integer(required=False, allow_none=True)
pmsPropertyIds = fields.List(fields.Integer(), required=False)
saleChannelId = fields.Integer(required=False, allow_none=True)
class PmsPricelistInfo(Datamodel):
_name = "pms.pricelist.info"
id = fields.Integer(required=False, allow_none=True)
@@ -10,3 +18,4 @@ class PmsPricelistInfo(Datamodel):
cancelationRuleId = fields.Integer(required=False, allow_none=True)
defaultAvailabilityPlanId = fields.Integer(required=False, allow_none=True)
pmsPropertyIds = fields.List(fields.Integer(required=False, allow_none=True))
saleChannelIds = fields.List(fields.Integer(required=False, allow_none=True))

View File

@@ -1,7 +1,7 @@
import re
from datetime import datetime, timedelta
from odoo.exceptions import MissingError
from odoo import _
from odoo.exceptions import MissingError, ValidationError
from odoo.addons.base_rest import restapi
from odoo.addons.base_rest_datamodel.restapi import Datamodel
@@ -23,39 +23,42 @@ class PmsPricelistService(Component):
"GET",
)
],
input_param=Datamodel("pms.search.param", is_list=False),
input_param=Datamodel("pms.pricelist.search", is_list=False),
output_param=Datamodel("pms.pricelist.info", is_list=True),
auth="jwt_api_pms",
)
def get_pricelists(self, pms_search_param, **args):
pricelists_all_properties = self.env["product.pricelist"].search(
[("pms_property_ids", "=", False)]
)
if pms_search_param.pmsPropertyIds:
pricelists = set()
for index, prop in enumerate(pms_search_param.pmsPropertyIds):
pricelists_with_query_property = self.env["product.pricelist"].search(
[("pms_property_ids", "=", prop)]
pricelists = self.env["product.pricelist"].search([])
if pms_search_param.pmsPropertyIds and pms_search_param.pmsPropertyId:
raise ValidationError(
_(
"""
Simultaneous search by list of properties and by specific property:
make sure to use only one of the two search parameters
"""
)
if index == 0:
pricelists = set(pricelists_with_query_property.ids)
else:
pricelists = pricelists.intersection(
set(pricelists_with_query_property.ids)
)
pricelists_total = list(
set(list(pricelists) + pricelists_all_properties.ids)
)
else:
pricelists_total = list(pricelists_all_properties.ids)
domain = [
("id", "in", pricelists_total),
]
if pms_search_param.pmsPropertyIds:
pricelists = pricelists.filtered(
lambda p: not p.pms_property_ids
or all(
item in p.pms_property_ids.ids
for item in pms_search_param.pmsPropertyIds
)
)
if pms_search_param.pmsPropertyId:
pricelists = pricelists.filtered(
lambda p: not p.pms_property_ids
or pms_search_param.pmsPropertyId in p.pms_property_ids.ids
)
if pms_search_param.saleChannelId:
pricelists = pricelists.filtered(
lambda p: not p.pms_sale_channel_ids
or pms_search_param.saleChannelId in p.pms_sale_channel_ids.ids
)
PmsPricelistInfo = self.env.datamodels["pms.pricelist.info"]
result_pricelists = []
for pricelist in self.env["product.pricelist"].search(domain):
for pricelist in pricelists:
result_pricelists.append(
PmsPricelistInfo(
id=pricelist.id,
@@ -66,7 +69,8 @@ class PmsPricelistService(Component):
defaultAvailabilityPlanId=pricelist.availability_plan_id.id
if pricelist.availability_plan_id
else None,
pmsPropertyIds=pricelist.pms_property_ids.mapped("id"),
pmsPropertyIds=pricelist.pms_property_ids.ids,
saleChannelIds=pricelist.pms_sale_channel_ids.ids,
)
)
return result_pricelists
@@ -135,8 +139,7 @@ class PmsPricelistService(Component):
)
pricelist_info.pricelistItemId = item.id
price = re.findall(r"[+-]?\d+\.\d+", item.price)
pricelist_info.price = float(price[0])
pricelist_info.price = item.fixed_price
result.append(pricelist_info)