Commit 2dc2d7ee authored by Romain Courteaud's avatar Romain Courteaud

Modify getPrice in order to be compatible with options.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@4399 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent aa58be9b
...@@ -482,7 +482,8 @@ class Resource(XMLMatrix, CoreResource, Variated): ...@@ -482,7 +482,8 @@ class Resource(XMLMatrix, CoreResource, Variated):
# pass # pass
# Predicate handling # Predicate handling
security.declareProtected(Permissions.AccessContentsInformation, 'asPredicate') security.declareProtected(Permissions.AccessContentsInformation,
'asPredicate')
def asPredicate(self): def asPredicate(self):
""" """
Returns a temporary Predicate based on the Resource properties Returns a temporary Predicate based on the Resource properties
...@@ -494,10 +495,10 @@ class Resource(XMLMatrix, CoreResource, Variated): ...@@ -494,10 +495,10 @@ class Resource(XMLMatrix, CoreResource, Variated):
return p return p
security.declareProtected(Permissions.AccessContentsInformation, security.declareProtected(Permissions.AccessContentsInformation,
'getPrice') '_getPriceParameterDict')
def getPrice(self, context=None, REQUEST=None, **kw): def _getPriceParameterDict(self, context=None, REQUEST=None, **kw):
""" """
Return the unit price of a resource in a specific context. Get all pricing parameters from Predicate.
""" """
# Search all categories context # Search all categories context
new_category_list = [] new_category_list = []
...@@ -509,36 +510,76 @@ class Resource(XMLMatrix, CoreResource, Variated): ...@@ -509,36 +510,76 @@ class Resource(XMLMatrix, CoreResource, Variated):
resource_category = 'resource/' + self.getRelativeUrl() resource_category = 'resource/' + self.getRelativeUrl()
if not resource_category in new_category_list: if not resource_category in new_category_list:
new_category_list += (resource_category, ) new_category_list += (resource_category, )
# Generate the fake context # Generate a mapped value without option, and one for each option
tmp_context = self.asContext(context=context, # Separate option from new_category_list
categories=new_category_list, option_base_category_list = self.getPortalOptionBaseCategoryList()
REQUEST=REQUEST, **kw) option_category_list = []
no_option_category_list = []
for new_category in new_category_list:
is_option = 0
for option_base_category in option_base_category_list:
if new_category.startswith(option_base_category):
is_option = 1
break
if is_option:
option_category_list.append(new_category)
else:
no_option_category_list.append(new_category)
# Generate the predicate mapped value # Generate the predicate mapped value
# to get some price values. # to get some price values.
mapped_value_list = []
domain_tool = getToolByName(self,'portal_domains') domain_tool = getToolByName(self,'portal_domains')
portal_type_list = self.getPortalSupplyTypeList() portal_type_list = self.getPortalSupplyTypeList()
mapped_value = domain_tool.generateMappedValue(
tmp_context, category_list_list = [no_option_category_list] + \
portal_type=portal_type_list, [no_option_category_list+[x] for x in option_category_list]
has_cell_content=0, **kw) for category_list in category_list_list:
# Calculate the unit price # Generate the fake context
unit_base_price = None tmp_context = self.asContext(context=context,
# (base_price + SUM(addtional_price)) * categories=category_list,
# (1 + SUM(surcharge_ratio)) * REQUEST=REQUEST, **kw)
# (1 - MIN(1, MAX(SUM(discount_ratio) , exclusive_discount_ratio )))) mapped_value = domain_tool.generateMappedValue(
tmp_context,
portal_type=portal_type_list,
has_cell_content=0, **kw)
if mapped_value is not None:
mapped_value_list.append(mapped_value)
# Get price parameters # Get price parameters
price_parameter_dict = { price_parameter_dict = {
'base_price': None, 'base_price': None,
'additional_price': None, 'additional_price': [],
'surcharge_ratio': None, 'surcharge_ratio': [],
'discount_ratio': None, 'discount_ratio': [],
'exclusive_discount_ratio': None, 'exclusive_discount_ratio': None,
} }
if mapped_value is not None: for mapped_value in mapped_value_list:
for price_parameter_name in price_parameter_dict.keys(): for price_parameter_name in price_parameter_dict.keys():
price_parameter_dict[price_parameter_name] = \ price_parameter_value = \
mapped_value.getProperty(price_parameter_name) mapped_value.getProperty(price_parameter_name)
if price_parameter_value not in [None, '']:
try:
price_parameter_dict[price_parameter_name].append(
price_parameter_value)
except AttributeError:
if price_parameter_dict[price_parameter_name] is None:
price_parameter_dict[price_parameter_name] = \
price_parameter_value
return price_parameter_dict
security.declareProtected(Permissions.AccessContentsInformation,
'getPrice')
def getPrice(self, context=None, REQUEST=None, **kw):
"""
Return the unit price of a resource in a specific context.
"""
price_parameter_dict = self._getPriceParameterDict(
context=context, REQUEST=REQUEST, **kw)
# Calculate the unit price
unit_base_price = None
# Calculate # Calculate
# (base_price + SUM(addtional_price)) *
# (1 + SUM(surcharge_ratio)) *
# (1 - MIN(1, MAX(SUM(discount_ratio) , exclusive_discount_ratio ))))
base_price = price_parameter_dict['base_price'] base_price = price_parameter_dict['base_price']
if base_price in [None, '']: if base_price in [None, '']:
# XXX Compatibility # XXX Compatibility
...@@ -547,21 +588,21 @@ class Resource(XMLMatrix, CoreResource, Variated): ...@@ -547,21 +588,21 @@ class Resource(XMLMatrix, CoreResource, Variated):
if base_price not in [None, '']: if base_price not in [None, '']:
unit_base_price = base_price unit_base_price = base_price
# Sum additional price # Sum additional price
additional_price = price_parameter_dict['additional_price'] for additional_price in price_parameter_dict['additional_price']:
if additional_price not in [None, '']:
unit_base_price += additional_price unit_base_price += additional_price
# Surcharge ratio # Surcharge ratio
surcharge_ratio = price_parameter_dict['surcharge_ratio'] sum_surcharge_ratio = 1
if surcharge_ratio not in [None, '']: for surcharge_ratio in price_parameter_dict['surcharge_ratio']:
unit_base_price = unit_base_price * \ sum_surcharge_ratio += surcharge_ratio
(1 + surcharge_ratio) unit_base_price = unit_base_price * sum_surcharge_ratio
# Discount # Discount
discount_ratio = price_parameter_dict['discount_ratio'] sum_discount_ratio = 0
for discount_ratio in price_parameter_dict['discount_ratio']:
sum_discount_ratio += discount_ratio
exclusive_discount_ratio = \ exclusive_discount_ratio = \
price_parameter_dict['exclusive_discount_ratio'] price_parameter_dict['exclusive_discount_ratio']
d_ratio = 0 d_ratio = 0
if discount_ratio not in [None, '']: d_ratio = max(d_ratio, sum_discount_ratio)
d_ratio = max(d_ratio, discount_ratio)
if exclusive_discount_ratio not in [None, '']: if exclusive_discount_ratio not in [None, '']:
d_ratio = max(d_ratio, exclusive_discount_ratio) d_ratio = max(d_ratio, exclusive_discount_ratio)
if d_ratio != 0: if d_ratio != 0:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment