Commit 214ff439 authored by Xiaowu Zhang's avatar Xiaowu Zhang

Movement: improve code

only calculate exchange rate if necessary
use transaction cache
parent 94b5fe63
...@@ -42,6 +42,7 @@ from Products.ERP5Type.UnrestrictedMethod import unrestricted_apply ...@@ -42,6 +42,7 @@ from Products.ERP5Type.UnrestrictedMethod import unrestricted_apply
from Products.ERP5.mixin.amount_generator import AmountGeneratorMixin from Products.ERP5.mixin.amount_generator import AmountGeneratorMixin
from Products.ERP5.mixin.composition import CompositionMixin from Products.ERP5.mixin.composition import CompositionMixin
from Products.ERP5.Document.Amount import Amount from Products.ERP5.Document.Amount import Amount
from Products.ERP5Type.Cache import transactional_cached
from zLOG import LOG, WARNING from zLOG import LOG, WARNING
...@@ -501,28 +502,30 @@ class Movement(XMLObject, Amount, CompositionMixin, AmountGeneratorMixin): ...@@ -501,28 +502,30 @@ class Movement(XMLObject, Amount, CompositionMixin, AmountGeneratorMixin):
return self._getAssetPrice(section = self.getDestinationSectionValue()) return self._getAssetPrice(section = self.getDestinationSectionValue())
def _getAssetPrice(self,section): def _getAssetPrice(self,section):
from Products.ERP5Type.Document import newTempAccountingTransactionLine
price = self.getPrice() price = self.getPrice()
source_currency = self.getPriceCurrencyValue() if section is None or not price:
section_source_currency = section.getPriceCurrency(base=True) return price
if source_currency and section_source_currency: source_currency_value = self.getPriceCurrencyValue()
temp_transaction = newTempAccountingTransactionLine( if source_currency_value:
self.getPortalObject(), section_currency = section.getPriceCurrency()
"accounting_line", if section_currency and source_currency_value.getRelativeUrl() != section_currency:
source_section=section.getRelativeUrl(), exchange_rate = self._getExchangeRate(
resource=source_currency.getRelativeUrl(), source_currency_value, section_currency, self.getStartDate())
start_date=self.getStartDate(), if exchange_rate:
) return exchange_rate * price
exchange_rate = source_currency.getPrice(
context=temp_transaction.asContext(
categories=[temp_transaction.getResource(base=True),
section_source_currency],
)
)
if exchange_rate and price:
return exchange_rate * price
return price return price
@transactional_cached(lambda self, *args: args)
def _getExchangeRate(self,source_currency_value,section_currency,start_date):
from Products.ERP5Type.Document import newTempAccountingTransactionLine
return source_currency_value.getPrice(context=newTempAccountingTransactionLine(
self.getPortalObject(),
"accounting_line",
resource=source_currency_value.getRelativeUrl(),
start_date=start_date,
price_currency=section_currency
))
# Causality computation # Causality computation
security.declareProtected( Permissions.AccessContentsInformation, security.declareProtected( Permissions.AccessContentsInformation,
'isConvergent') 'isConvergent')
......
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