Search in sources :

Example 16 with AppBase

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

the class ImportCityServiceImpl method getDownloadUrl.

protected String getDownloadUrl(GEONAMES_FILE geonamesFile) throws AxelorException {
    AppBase appBase = appBaseService.getAppBase();
    String downloadUrl = null;
    switch(geonamesFile) {
        case DUMP:
            downloadUrl = appBase.getGeoNamesDumpUrl();
            break;
        case ZIP:
            downloadUrl = appBase.getGeoNamesZipUrl();
            break;
        default:
            printWriter.append(I18n.get(IExceptionMessage.INVALID_GEONAMES_IMPORT_FILE) + "\n");
    }
    if (StringUtils.isEmpty(downloadUrl)) {
        printWriter.append(String.format(I18n.get(IExceptionMessage.GEONAMES_URL_NOT_SPECIFIED), downloadUrl) + "\n");
    }
    return downloadUrl;
}
Also used : AppBase(com.axelor.apps.base.db.AppBase)

Example 17 with AppBase

use of com.axelor.apps.base.db.AppBase 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 18 with AppBase

use of com.axelor.apps.base.db.AppBase 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

AppBase (com.axelor.apps.base.db.AppBase)18 AxelorException (com.axelor.exception.AxelorException)8 BigDecimal (java.math.BigDecimal)5 AppBaseService (com.axelor.apps.base.service.app.AppBaseService)4 User (com.axelor.auth.db.User)3 JSONException (wslite.json.JSONException)3 Company (com.axelor.apps.base.db.Company)2 CurrencyConversionLine (com.axelor.apps.base.db.CurrencyConversionLine)2 Lead (com.axelor.apps.crm.db.Lead)2 ReportSettings (com.axelor.apps.report.engine.ReportSettings)2 Transactional (com.google.inject.persist.Transactional)2 MalformedURLException (java.net.MalformedURLException)2 LocalDate (java.time.LocalDate)2 AppLeave (com.axelor.apps.base.db.AppLeave)1 EventsPlanning (com.axelor.apps.base.db.EventsPlanning)1 CompanyRepository (com.axelor.apps.base.db.repo.CompanyRepository)1 MapService (com.axelor.apps.base.service.MapService)1 UserService (com.axelor.apps.base.service.user.UserService)1 Employee (com.axelor.apps.hr.db.Employee)1 HRConfig (com.axelor.apps.hr.db.HRConfig)1