Commit c80c5658 authored by Julien Muchembled's avatar Julien Muchembled

Fix testComplexTradeModelLineUseCase

git-svn-id: https://svn.erp5.org/repos/public/erp5/sandbox/amount_generator@39069 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent e2075796
...@@ -57,17 +57,8 @@ class BaseAmount(dict): ...@@ -57,17 +57,8 @@ class BaseAmount(dict):
def getContext(self): def getContext(self):
return self._context return self._context
def updateCache(self, base_amount_set, amount_generator_line): def setAmountGeneratorLine(self, amount_generator_line):
cache = self._cache self._amount_generator_line = amount_generator_line
base_amount_set = base_amount_set.difference(cache)
if base_amount_set:
method = amount_generator_line._getTypeBasedMethod(
'getBaseAmountQuantityMethod')
for base_amount in base_amount_set:
if method is None:
cache[base_amount] = amount_generator_line.getBaseAmountQuantity
else:
cache[base_amount] = method(base_amount)
def recurse(self, portal_type=None): def recurse(self, portal_type=None):
for amount in self._context.objectValues(portal_type=portal_type): for amount in self._context.objectValues(portal_type=portal_type):
...@@ -92,7 +83,9 @@ class BaseAmount(dict): ...@@ -92,7 +83,9 @@ class BaseAmount(dict):
return dict.__getitem__(self, key) return dict.__getitem__(self, key)
except KeyError: except KeyError:
value = 0 value = 0
amount_generator_line = self._amount_generator_line
for lazy in self._lazy: for lazy in self._lazy:
lazy._amount_generator_line = amount_generator_line
value += lazy.getQuantity(key) value += lazy.getQuantity(key)
self[key] = value self[key] = value
return value return value
...@@ -109,7 +102,17 @@ class BaseAmount(dict): ...@@ -109,7 +102,17 @@ class BaseAmount(dict):
return dict.__getitem__(self, key) return dict.__getitem__(self, key)
self[key] # initialize entry before we freeze it self[key] # initialize entry before we freeze it
self._frozen.add(key) self._frozen.add(key)
self[key] = value = self._cache[key](self._context, key, **self._method_kw) try:
method = self._cache[key]
except KeyError:
method = self._amount_generator_line._getTypeBasedMethod(
'getBaseAmountQuantityMethod')
if method is not None:
method = method(key)
if method is None:
method = self._amount_generator_line.getBaseAmountQuantity
self._cache[key] = method
self[key] = value = method(self._context, key, **self._method_kw)
return value return value
...@@ -241,11 +244,9 @@ class AmountGeneratorMixin: ...@@ -241,11 +244,9 @@ class AmountGeneratorMixin:
cell.getBaseContributionList()) cell.getBaseContributionList())
property_dict['causality_value_list'].append(cell) property_dict['causality_value_list'].append(cell)
base_amount.setAmountGeneratorLine(self)
for property_dict in cell_aggregate.itervalues(): for property_dict in cell_aggregate.itervalues():
base_application_set = property_dict['base_application_set'] base_application_set = property_dict['base_application_set']
# Cache must be prepared with the right context in case that we iterate
# through different kinds of amount generator lines.
base_amount.updateCache(base_application_set, self)
# property_dict may include # property_dict may include
# resource - VAT service or a Component in MRP # resource - VAT service or a Component in MRP
# (if unset, the amount will only be used for reporting) # (if unset, the amount will only be used for reporting)
...@@ -293,13 +294,10 @@ class AmountGeneratorMixin: ...@@ -293,13 +294,10 @@ class AmountGeneratorMixin:
amount = getRoundingProxy(amount, context=self) amount = getRoundingProxy(amount, context=self)
result.append(amount) result.append(amount)
# Contribute # Contribute
base_contribution_set = property_dict['base_contribution_set'] quantity *= (property_dict.get('price') or 1) / \
if base_contribution_set: (property_dict.get('efficiency') or 1)
quantity *= (property_dict.get('price') or 1) / \ for base_contribution in property_dict['base_contribution_set']:
(property_dict.get('efficiency') or 1) base_amount[base_contribution] += quantity
base_amount.updateCache(base_contribution_set, self)
for base_contribution in base_contribution_set:
base_amount[base_contribution] += quantity
is_mapped_value = isinstance(self, MappedValue) is_mapped_value = isinstance(self, MappedValue)
......
...@@ -60,7 +60,7 @@ class TestBPMMixin(ERP5TypeTestCase): ...@@ -60,7 +60,7 @@ class TestBPMMixin(ERP5TypeTestCase):
def createCategories(self): def createCategories(self):
category_tool = self.portal.portal_categories category_tool = self.portal.portal_categories
self.createCategoriesInCategory(category_tool.base_amount, ['discount', self.createCategoriesInCategory(category_tool.base_amount, ['discount',
'tax', 'total_tax', 'total_discount', 'total', 'fixed_quantity']) 'tax', 'total_tax', 'total_discount', 'total'])
self.createCategoriesInCategory(category_tool.use, self.createCategoriesInCategory(category_tool.use,
self.normal_resource_use_category_list + \ self.normal_resource_use_category_list + \
self.invoicing_resource_use_category_list) self.invoicing_resource_use_category_list)
......
...@@ -56,6 +56,8 @@ class TestTradeModelLineMixin(TestBPMMixin, UserDict): ...@@ -56,6 +56,8 @@ class TestTradeModelLineMixin(TestBPMMixin, UserDict):
"""Provides methods to implementations sharing similar logic to Trade Model Lines""" """Provides methods to implementations sharing similar logic to Trade Model Lines"""
# Constants and variables shared by tests # Constants and variables shared by tests
base_unit_quantity = 0.01 base_unit_quantity = 0.01
node_portal_type = 'Organisation'
order_date = DateTime()
def setBaseAmountQuantityMethod(self, base_amount_id, text): def setBaseAmountQuantityMethod(self, base_amount_id, text):
"""Populate TradeModelLine_getBaseAmountQuantityMethod shared script """Populate TradeModelLine_getBaseAmountQuantityMethod shared script
...@@ -65,10 +67,11 @@ class TestTradeModelLineMixin(TestBPMMixin, UserDict): ...@@ -65,10 +67,11 @@ class TestTradeModelLineMixin(TestBPMMixin, UserDict):
- data produced by previous still behaves as expected - data produced by previous still behaves as expected
""" """
base_amount = self.portal.portal_categories.base_amount base_amount = self.portal.portal_categories.base_amount
try: for name in self.__class__.__name__, self._testMethodName:
base_amount = base_amount[self._testMethodName] try:
except KeyError: base_amount = base_amount[name]
base_amount = base_amount.newContent(self._testMethodName) except KeyError:
base_amount = base_amount.newContent(name)
try: try:
return base_amount[base_amount_id].getRelativeUrl() return base_amount[base_amount_id].getRelativeUrl()
except KeyError: except KeyError:
...@@ -79,7 +82,7 @@ class TestTradeModelLineMixin(TestBPMMixin, UserDict): ...@@ -79,7 +82,7 @@ class TestTradeModelLineMixin(TestBPMMixin, UserDict):
try: try:
old_text = '\n' + skin[script_id].body() old_text = '\n' + skin[script_id].body()
except KeyError: except KeyError:
old_text = "\nreturn context.getBaseAmountQuantity" old_text = ''
else: else:
skin._delObject(script_id) skin._delObject(script_id)
text = test + '\n '.join(text.splitlines()) + old_text text = test + '\n '.join(text.splitlines()) + old_text
...@@ -176,19 +179,29 @@ class TestTradeModelLineMixin(TestBPMMixin, UserDict): ...@@ -176,19 +179,29 @@ class TestTradeModelLineMixin(TestBPMMixin, UserDict):
order.newContent(portal_type=self.order_line_portal_type, **line_kw) order.newContent(portal_type=self.order_line_portal_type, **line_kw)
return order return order
def getAggregatedAmountDict(self, amount_generator, **expected_amount_dict): def getAggregatedAmountDict(self, amount_generator, partial_check=False,
**expected_amount_dict):
amount_list = amount_generator.getAggregatedAmountList() amount_list = amount_generator.getAggregatedAmountList()
amount_dict = {} amount_dict = {}
for amount in amount_list: for amount in amount_list:
reference = amount.getReference() reference = amount.getReference()
expected_amount = expected_amount_dict.pop(reference) try:
for k, v in expected_amount.iteritems(): expected_amount = expected_amount_dict.pop(reference)
if k == 'causality_value_list': except KeyError:
self.assertEqual(v, amount.getValueList('causality')) if not partial_check:
else: raise
self.assertEqual(v, amount.getProperty(k)) else:
amount_dict[reference] = amount for k, v in expected_amount.iteritems():
self.assertEqual({}, expected_amount_dict) if k == 'causality_value_list':
self.assertEqual(v, amount.getValueList('causality'))
else:
self.assertEqual(v, amount.getProperty(k))
amount_dict[reference] = amount
if partial_check:
for value in expected_amount_dict.itervalues():
self.assertEqual(None, value)
else:
self.assertEqual({}, expected_amount_dict)
return amount_dict return amount_dict
def getTradeModelSimulationMovementList(self, delivery_line): def getTradeModelSimulationMovementList(self, delivery_line):
...@@ -220,9 +233,6 @@ class TestTradeModelLine(TestTradeModelLineMixin): ...@@ -220,9 +233,6 @@ class TestTradeModelLine(TestTradeModelLineMixin):
new_discount_ratio = -0.04 # -4% new_discount_ratio = -0.04 # -4%
new_tax_ratio = 0.22 # 22% new_tax_ratio = 0.22 # 22%
node_portal_type = 'Organisation'
order_date = DateTime()
modified_order_line_price_ratio = 2.0 modified_order_line_price_ratio = 2.0
modified_packing_list_line_quantity_ratio = 0.4 modified_packing_list_line_quantity_ratio = 0.4
modified_invoice_line_quantity_ratio = modified_order_line_quantity_ratio \ modified_invoice_line_quantity_ratio = modified_order_line_quantity_ratio \
......
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