Commit 351b887a authored by Xiaowu Zhang's avatar Xiaowu Zhang

Movement: improve code

only calculate exchange rate if necessary
use transaction cache
make getExchangeRate as local function
parent 2bf2f367
...@@ -42,9 +42,23 @@ from Products.ERP5Type.UnrestrictedMethod import unrestricted_apply ...@@ -42,9 +42,23 @@ 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
@transactional_cached()
def getExchangeRate(source_currency_value, section_currency, start_date):
source_currency = source_currency_value.getRelativeUrl()
if source_currency != section_currency:
from Products.ERP5Type.Document import newTempAccountingTransactionLine
return source_currency_value.getPrice(context=newTempAccountingTransactionLine(
source_currency_value.getPortalObject(),
"accounting_line",
resource=source_currency,
start_date=start_date,
price_currency=section_currency
))
class Movement(XMLObject, Amount, CompositionMixin, AmountGeneratorMixin): class Movement(XMLObject, Amount, CompositionMixin, AmountGeneratorMixin):
""" """
The Movement class allows to implement ERP5 universal accounting model. The Movement class allows to implement ERP5 universal accounting model.
...@@ -501,25 +515,16 @@ class Movement(XMLObject, Amount, CompositionMixin, AmountGeneratorMixin): ...@@ -501,25 +515,16 @@ 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:
source_section=section.getRelativeUrl(), exchange_rate = getExchangeRate(
resource=source_currency.getRelativeUrl(), source_currency_value, section_currency, self.getStartDate())
start_date=self.getStartDate(), if exchange_rate:
)
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 exchange_rate * price
return price return price
......
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