use of com.axelor.apps.base.db.repo.CurrencyRepository in project axelor-open-suite by axelor.
the class CurrencyConversionLineController method convert.
@SuppressWarnings("unchecked")
public void convert(ActionRequest request, ActionResponse response) throws MalformedURLException, JSONException, AxelorException {
Context context = request.getContext();
Currency fromCurrency = null;
Currency toCurrency = null;
CurrencyRepository currencyRepository = Beans.get(CurrencyRepository.class);
if (context.get("startCurrency") instanceof Currency) {
fromCurrency = (Currency) context.get("startCurrency");
toCurrency = (Currency) context.get("endCurrency");
} else {
Map<String, Object> startCurrency = (Map<String, Object>) context.get("startCurrency");
Map<String, Object> endCurrency = (Map<String, Object>) context.get("endCurrency");
fromCurrency = currencyRepository.find(Long.parseLong(startCurrency.get("id").toString()));
toCurrency = currencyRepository.find(Long.parseLong(endCurrency.get("id").toString()));
}
CurrencyConversionLine prevLine = null;
if (fromCurrency != null && toCurrency != null) {
if (context.get("id") != null)
prevLine = Beans.get(CurrencyConversionLineRepository.class).all().filter("startCurrency.id = ?1 AND endCurrency.id = ?2 AND id != ?3", fromCurrency.getId(), toCurrency.getId(), context.get("id")).order("-fromDate").fetchOne();
else
prevLine = Beans.get(CurrencyConversionLineRepository.class).all().filter("startCurrency.id = ?1 AND endCurrency.id = ?2", fromCurrency.getId(), toCurrency.getId()).order("-fromDate").fetchOne();
LOG.debug("Previous currency conversion line: {}", prevLine);
fromCurrency = currencyRepository.find(fromCurrency.getId());
toCurrency = currencyRepository.find(toCurrency.getId());
try {
CurrencyConversionService currencyConversionService = Beans.get(CurrencyConversionFactory.class).getCurrencyConversionService();
BigDecimal rate = currencyConversionService.convert(fromCurrency, toCurrency);
if (rate.compareTo(new BigDecimal(-1)) == 0)
response.setFlash(I18n.get(IExceptionMessage.CURRENCY_6));
else {
response.setValue("variations", "0");
if (context.get("_model").equals("com.axelor.apps.base.db.Wizard"))
response.setValue("newExchangeRate", rate);
else
response.setValue("exchangeRate", rate);
response.setValue("fromDate", Beans.get(AppBaseService.class).getTodayDateTime());
if (prevLine != null)
response.setValue("variations", currencyConversionService.getVariations(rate, prevLine.getExchangeRate()));
}
} catch (AxelorException axelorException) {
response.setFlash(axelorException.getMessage());
response.setCanClose(true);
}
}
}
Aggregations