From 574c1a67f4dcb5cc48f2855885d53850b05936ec Mon Sep 17 00:00:00 2001 From: laurentbarontini Date: Fri, 5 Jun 2026 07:46:48 +0200 Subject: [PATCH] Mtm --- modules/purchase_trade/tests/test_module.py | 26 +++++++++++++++++++ modules/purchase_trade/valuation.py | 28 +++++++++++++++------ 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/modules/purchase_trade/tests/test_module.py b/modules/purchase_trade/tests/test_module.py index d3dceb8..aa43a4e 100644 --- a/modules/purchase_trade/tests/test_module.py +++ b/modules/purchase_trade/tests/test_module.py @@ -134,6 +134,32 @@ class PurchaseTradeTestCase(ModuleTestCase): strategy.get_mtm(line, Decimal('3')), Decimal('225.00')) + @with_transaction() + def test_signed_strategy_mtm_follows_valuation_direction(self): + 'strategy MTM follows purchase/sale valuation sign' + Valuation = Pool().get('valuation.valuation') + strategy = Mock(get_mtm=Mock(return_value=Decimal('349167.53'))) + line = Mock() + + self.assertEqual( + Valuation._signed_strategy_mtm( + {'type': 'pur. priced', 'amount': Decimal('-190686.15'), + 'quantity': Decimal('4237.47')}, + strategy, line), + Decimal('-349167.53')) + self.assertEqual( + Valuation._signed_strategy_mtm( + {'type': 'sale priced', 'amount': Decimal('349167.53'), + 'quantity': Decimal('4237.47')}, + strategy, line), + Decimal('349167.53')) + self.assertEqual( + Valuation._signed_strategy_mtm( + {'type': 'pur. priced', 'amount': Decimal('0'), + 'quantity': Decimal('0')}, + strategy, line), + Decimal('-349167.53')) + @with_transaction() def test_purchase_line_charter_conditions_inherit_header_when_empty(self): 'purchase line uses header charter conditions when it has no line terms' diff --git a/modules/purchase_trade/valuation.py b/modules/purchase_trade/valuation.py index abd0e7b..f770d44 100644 --- a/modules/purchase_trade/valuation.py +++ b/modules/purchase_trade/valuation.py @@ -375,6 +375,18 @@ class ValuationBase(ModelSQL): def _supports_strategy_mtm(values): return values and values.get('type') in {'pur. priced', 'sale priced'} + @classmethod + def _signed_strategy_mtm(cls, values, strategy, line): + mtm = strategy.get_mtm(line, values['quantity']) + if mtm is None: + return mtm + amount = values.get('amount') + if amount: + return abs(mtm) if amount > 0 else -abs(mtm) + if values.get('type') in {'pur. priced', 'pur. efp'}: + return -abs(mtm) + return abs(mtm) + @staticmethod def _get_basis_component_total(record): getter = getattr(record, '_get_basis_component_price', None) @@ -526,7 +538,7 @@ class ValuationBase(ModelSQL): if line.mtm and cls._supports_strategy_mtm(values): for strat in line.mtm: values['mtm_price'] = cls._get_strategy_mtm_price(strat, line) - values['mtm'] = strat.get_mtm(line, values['quantity']) + values['mtm'] = cls._signed_strategy_mtm(values, strat, line) values['strategy'] = strat if values: @@ -544,7 +556,7 @@ class ValuationBase(ModelSQL): if line.mtm and cls._supports_strategy_mtm(values): for strat in line.mtm: values['mtm_price'] = cls._get_strategy_mtm_price(strat, line) - values['mtm'] = strat.get_mtm(line,values['quantity']) + values['mtm'] = cls._signed_strategy_mtm(values, strat, line) values['strategy'] = strat if values: @@ -567,7 +579,7 @@ class ValuationBase(ModelSQL): if line.mtm and cls._supports_strategy_mtm(values): for strat in line.mtm: values['mtm_price'] = cls._get_strategy_mtm_price(strat, line) - values['mtm'] = strat.get_mtm(line,values['quantity']) + values['mtm'] = cls._signed_strategy_mtm(values, strat, line) values['strategy'] = strat if values: @@ -602,7 +614,7 @@ class ValuationBase(ModelSQL): if sl_line.mtm and cls._supports_strategy_mtm(values): for strat in line.mtm: values['mtm_price'] = cls._get_strategy_mtm_price(strat, sl_line) - values['mtm'] = strat.get_mtm(sl_line,values['quantity']) + values['mtm'] = cls._signed_strategy_mtm(values, strat, sl_line) values['strategy'] = strat if values: @@ -624,7 +636,7 @@ class ValuationBase(ModelSQL): if sl_line.mtm and cls._supports_strategy_mtm(values): for strat in sl_line.mtm: values['mtm_price'] = cls._get_strategy_mtm_price(strat, sl_line) - values['mtm'] = strat.get_mtm(sl_line,values['quantity']) + values['mtm'] = cls._signed_strategy_mtm(values, strat, sl_line) values['strategy'] = strat if values: @@ -750,7 +762,7 @@ class ValuationBase(ModelSQL): if sale_line.mtm and cls._supports_strategy_mtm(values): for strat in sale_line.mtm: values['mtm_price'] = cls._get_strategy_mtm_price(strat, sale_line) - values['mtm'] = strat.get_mtm(sale_line, values['quantity']) + values['mtm'] = cls._signed_strategy_mtm(values, strat, sale_line) values['strategy'] = strat if values: @@ -767,7 +779,7 @@ class ValuationBase(ModelSQL): if sale_line.mtm and cls._supports_strategy_mtm(values): for strat in sale_line.mtm: values['mtm_price'] = cls._get_strategy_mtm_price(strat, sale_line) - values['mtm'] = strat.get_mtm(sale_line, values['quantity']) + values['mtm'] = cls._signed_strategy_mtm(values, strat, sale_line) values['strategy'] = strat if values: @@ -790,7 +802,7 @@ class ValuationBase(ModelSQL): if sale_line.mtm and cls._supports_strategy_mtm(values): for strat in sale_line.mtm: values['mtm_price'] = cls._get_strategy_mtm_price(strat, sale_line) - values['mtm'] = strat.get_mtm(sale_line, values['quantity']) + values['mtm'] = cls._signed_strategy_mtm(values, strat, sale_line) values['strategy'] = strat if values: