Search in sources :

Example 6 with CurrencyConversionLine

use of com.axelor.apps.base.db.CurrencyConversionLine in project axelor-open-suite by axelor.

the class ECBCurrencyConversionService method updateCurrencyConverion.

@Override
public void updateCurrencyConverion() throws AxelorException {
    AppBase appBase = appBaseService.getAppBase();
    LocalDate today = appBaseService.getTodayDate(Optional.ofNullable(AuthUtils.getUser()).map(User::getActiveCompany).orElse(null));
    Map<Long, Set<Long>> currencyMap = new HashMap<Long, Set<Long>>();
    for (CurrencyConversionLine ccl : appBase.getCurrencyConversionLineList()) {
        if (currencyMap.containsKey(ccl.getEndCurrency().getId())) {
            currencyMap.get(ccl.getEndCurrency().getId()).add(ccl.getStartCurrency().getId());
        } else {
            Set<Long> startCurrencyIds = new HashSet<>();
            startCurrencyIds.add(ccl.getStartCurrency().getId());
            currencyMap.put(ccl.getEndCurrency().getId(), startCurrencyIds);
        }
    }
    for (Long key : currencyMap.keySet()) {
        List<CurrencyConversionLine> cclList = cclRepo.all().filter("startCurrency.id IN (?1) AND endCurrency.id = ?2 AND fromDate <= ?3 AND toDate is null", currencyMap.get(key), key, today).fetch();
        for (CurrencyConversionLine ccl : cclList) {
            LOG.trace("Currency Conversion Line without toDate : {}", ccl);
            BigDecimal currentRate = BigDecimal.ZERO;
            try {
                currentRate = this.convert(ccl.getStartCurrency(), ccl.getEndCurrency());
            } catch (Exception e) {
                throw new AxelorException(e.getCause(), TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, e.getLocalizedMessage());
            }
            if (currentRate.compareTo(new BigDecimal(-1)) == 0) {
                throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.CURRENCY_6));
            }
            ccl = cclRepo.find(ccl.getId());
            BigDecimal previousRate = ccl.getExchangeRate();
            if (currentRate.compareTo(previousRate) != 0) {
                ccl.setToDate(today.minusDays(1).isAfter(ccl.getFromDate()) ? today.minusDays(1) : today);
                this.saveCurrencyConversionLine(ccl);
                String variations = this.getVariations(currentRate, previousRate);
                this.createCurrencyConversionLine(ccl.getStartCurrency(), ccl.getEndCurrency(), today, currentRate, appBase, variations);
            }
        }
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) User(com.axelor.auth.db.User) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) AxelorException(com.axelor.exception.AxelorException) JSONException(wslite.json.JSONException) MalformedURLException(java.net.MalformedURLException) AppBase(com.axelor.apps.base.db.AppBase) CurrencyConversionLine(com.axelor.apps.base.db.CurrencyConversionLine) HashSet(java.util.HashSet)

Example 7 with CurrencyConversionLine

use of com.axelor.apps.base.db.CurrencyConversionLine in project axelor-open-suite by axelor.

the class FixerCurrencyConversionService method updateCurrencyConverion.

@Override
public void updateCurrencyConverion() throws AxelorException {
    AppBase appBase = appBaseService.getAppBase();
    try {
        HTTPResponse response = null;
        LocalDate today = appBaseService.getTodayDate(Optional.ofNullable(AuthUtils.getUser()).map(User::getActiveCompany).orElse(null));
        response = callApiBaseEuru(null, null, today);
        if (response.getStatusCode() != 200) {
            throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.CURRENCY_6));
        }
        List<CurrencyConversionLine> currencyConversionLines = appBase.getCurrencyConversionLineList();
        currencyConversionLines = currencyConversionLines.stream().filter(it -> !it.getFromDate().isAfter(today) && it.getToDate() == null).collect(Collectors.toList());
        for (CurrencyConversionLine ccline : currencyConversionLines) {
            BigDecimal currentRate = BigDecimal.ZERO;
            Float rate = 0.0f;
            if (!ccline.getStartCurrency().getCode().equals(EURO_CURRENCY_CODE) && !ccline.getEndCurrency().getCode().equals(EURO_CURRENCY_CODE)) {
                isRelatedConversion = true;
                rate = this.getRateFromJson(ccline.getStartCurrency(), ccline.getEndCurrency(), response);
            } else if (ccline.getStartCurrency().getCode().equals(EURO_CURRENCY_CODE)) {
                rate = this.getRateFromJson(ccline.getStartCurrency(), ccline.getEndCurrency(), response);
            } else {
                rate = this.getRateFromJson(ccline.getEndCurrency(), ccline.getStartCurrency(), response);
                rate = 1.0f / rate;
            }
            currentRate = BigDecimal.valueOf(rate).setScale(8, RoundingMode.HALF_UP);
            if (currentRate.compareTo(new BigDecimal(-1)) == 0) {
                throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.CURRENCY_6));
            }
            ccline = cclRepo.find(ccline.getId());
            BigDecimal previousRate = ccline.getExchangeRate();
            if (currentRate.compareTo(previousRate) != 0) {
                ccline.setToDate(today.minusDays(1).isAfter(ccline.getFromDate()) ? today.minusDays(1) : today);
                this.saveCurrencyConversionLine(ccline);
                String variations = this.getVariations(currentRate, previousRate);
                this.createCurrencyConversionLine(ccline.getStartCurrency(), ccline.getEndCurrency(), today, currentRate, appBase, variations);
            }
        }
    } catch (Exception e) {
        throw new AxelorException(e.getCause(), TraceBackRepository.CATEGORY_INCONSISTENCY, e.getLocalizedMessage());
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) User(com.axelor.auth.db.User) HTTPResponse(wslite.http.HTTPResponse) CurrencyConversionLine(com.axelor.apps.base.db.CurrencyConversionLine) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) AxelorException(com.axelor.exception.AxelorException) JSONException(wslite.json.JSONException) MalformedURLException(java.net.MalformedURLException) AppBase(com.axelor.apps.base.db.AppBase)

Aggregations

CurrencyConversionLine (com.axelor.apps.base.db.CurrencyConversionLine)7 AxelorException (com.axelor.exception.AxelorException)4 BigDecimal (java.math.BigDecimal)4 LocalDate (java.time.LocalDate)4 AppBase (com.axelor.apps.base.db.AppBase)2 User (com.axelor.auth.db.User)2 MalformedURLException (java.net.MalformedURLException)2 JSONException (wslite.json.JSONException)2 Currency (com.axelor.apps.base.db.Currency)1 CurrencyConversionLineRepository (com.axelor.apps.base.db.repo.CurrencyConversionLineRepository)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 Context (com.axelor.rpc.Context)1 Transactional (com.google.inject.persist.Transactional)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 HTTPResponse (wslite.http.HTTPResponse)1