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);
}
}
}
}
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());
}
}
Aggregations