Search in sources :

Example 1 with CurrencyRepository

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);
        }
    }
}
Also used : Context(com.axelor.rpc.Context) AxelorException(com.axelor.exception.AxelorException) Currency(com.axelor.apps.base.db.Currency) CurrencyRepository(com.axelor.apps.base.db.repo.CurrencyRepository) CurrencyConversionLine(com.axelor.apps.base.db.CurrencyConversionLine) CurrencyConversionFactory(com.axelor.apps.base.service.currency.CurrencyConversionFactory) CurrencyConversionService(com.axelor.apps.base.service.currency.CurrencyConversionService) Map(java.util.Map) BigDecimal(java.math.BigDecimal)

Aggregations

Currency (com.axelor.apps.base.db.Currency)1 CurrencyConversionLine (com.axelor.apps.base.db.CurrencyConversionLine)1 CurrencyRepository (com.axelor.apps.base.db.repo.CurrencyRepository)1 CurrencyConversionFactory (com.axelor.apps.base.service.currency.CurrencyConversionFactory)1 CurrencyConversionService (com.axelor.apps.base.service.currency.CurrencyConversionService)1 AxelorException (com.axelor.exception.AxelorException)1 Context (com.axelor.rpc.Context)1 BigDecimal (java.math.BigDecimal)1 Map (java.util.Map)1