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
from Products.ERP5.mixin.amount_generator import AmountGeneratorMixin
from Products.ERP5.mixin.composition import CompositionMixin
from Products.ERP5.Document.Amount import Amount
from Products.ERP5Type.Cache import transactional_cached
from zLOG import LOG, WARNING
......@@ -501,28 +502,30 @@ class Movement(XMLObject, Amount, CompositionMixin, AmountGeneratorMixin):
return self._getAssetPrice(section = self.getDestinationSectionValue())
def _getAssetPrice(self,section):
from Products.ERP5Type.Document import newTempAccountingTransactionLine
price = self.getPrice()
source_currency = self.getPriceCurrencyValue()
section_source_currency = section.getPriceCurrency(base=True)
if source_currency and section_source_currency:
temp_transaction = newTempAccountingTransactionLine(
self.getPortalObject(),
"accounting_line",
source_section=section.getRelativeUrl(),
resource=source_currency.getRelativeUrl(),
start_date=self.getStartDate(),
)
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
if section is None or not price:
return price
source_currency_value = self.getPriceCurrencyValue()
if source_currency_value:
section_currency = section.getPriceCurrency()
if section_currency and source_currency_value.getRelativeUrl() != section_currency:
exchange_rate = self._getExchangeRate(
source_currency_value, section_currency, self.getStartDate())
if exchange_rate:
return exchange_rate * 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
security.declareProtected( Permissions.AccessContentsInformation,
'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