Search in sources :

Example 1 with CurrencyConversionLine

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

the class CurrencyService method getCurrencyConversionLine.

private CurrencyConversionLine getCurrencyConversionLine(Currency startCurrency, Currency endCurrency, LocalDate localDate) {
    List<CurrencyConversionLine> currencyConversionLineList = appBaseService.getCurrencyConfigurationLineList();
    if (currencyConversionLineList == null) {
        return null;
    }
    log.debug("Currency from: {}, Currency to: {}, localDate: {}", startCurrency, endCurrency, localDate);
    for (CurrencyConversionLine ccl : currencyConversionLineList) {
        String cclStartCode = ccl.getStartCurrency().getCode();
        String cclEndCode = ccl.getEndCurrency().getCode();
        String startCode = startCurrency.getCode();
        String endCode = endCurrency.getCode();
        LocalDate fromDate = ccl.getFromDate();
        LocalDate toDate = ccl.getToDate();
        if (cclStartCode.equals(startCode) && cclEndCode.equals(endCode)) {
            if ((fromDate.isBefore(localDate) || fromDate.equals(localDate)) && (toDate == null || toDate.isAfter(localDate) || toDate.isEqual(localDate))) {
                return ccl;
            }
        }
    }
    return null;
}
Also used : CurrencyConversionLine(com.axelor.apps.base.db.CurrencyConversionLine) LocalDate(java.time.LocalDate)

Example 2 with CurrencyConversionLine

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

the class CurrencyConversionLineController method checkDate.

public void checkDate(ActionRequest request, ActionResponse response) {
    CurrencyConversionLine ccl = request.getContext().asType(CurrencyConversionLine.class);
    LOG.debug("Currency Conversion Line Id : {}", ccl.getId());
    if (ccl.getId() != null && Beans.get(CurrencyConversionLineRepository.class).all().filter("self.startCurrency.id = ?1 and self.endCurrency.id = ?2 and (self.toDate = null OR  self.toDate >= ?3) and self.id != ?4)", ccl.getStartCurrency().getId(), ccl.getEndCurrency().getId(), ccl.getFromDate(), ccl.getId()).count() > 0) {
        response.setFlash(I18n.get(IExceptionMessage.CURRENCY_3));
    // response.setValue("fromDate", "");
    } else if (ccl.getId() == null && Beans.get(CurrencyConversionLineRepository.class).all().filter("self.startCurrency.id = ?1 and self.endCurrency.id = ?2 and (self.toDate = null OR  self.toDate >= ?3))", ccl.getStartCurrency().getId(), ccl.getEndCurrency().getId(), ccl.getFromDate()).count() > 0) {
        response.setFlash(I18n.get(IExceptionMessage.CURRENCY_3));
    // response.setValue("fromDate", "");
    }
    if (ccl.getFromDate() != null && ccl.getToDate() != null && ccl.getFromDate().isAfter(ccl.getToDate())) {
        response.setFlash(I18n.get(IExceptionMessage.CURRENCY_4));
    // response.setValue("fromDate", "");
    }
}
Also used : CurrencyConversionLine(com.axelor.apps.base.db.CurrencyConversionLine) CurrencyConversionLineRepository(com.axelor.apps.base.db.repo.CurrencyConversionLineRepository)

Example 3 with CurrencyConversionLine

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

Example 4 with CurrencyConversionLine

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

the class CurrencyService method getCurrencyConversionRate.

public BigDecimal getCurrencyConversionRate(Currency startCurrency, Currency endCurrency, LocalDate date) throws AxelorException {
    // So we convert the amount
    if (startCurrency != null && endCurrency != null && !startCurrency.equals(endCurrency)) {
        LocalDate dateToConvert = this.getDateToConvert(date);
        boolean isInverse = true;
        BigDecimal exchangeRate = null;
        CurrencyConversionLine currencyConversionLine = this.getCurrencyConversionLine(startCurrency, endCurrency, dateToConvert);
        if (currencyConversionLine != null) {
            exchangeRate = currencyConversionLine.getExchangeRate();
            isInverse = false;
        } else {
            currencyConversionLine = this.getCurrencyConversionLine(endCurrency, startCurrency, dateToConvert);
            if (currencyConversionLine == null) {
                throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.CURRENCY_1), startCurrency.getName(), endCurrency.getName(), dateToConvert);
            }
            exchangeRate = currencyConversionLine.getExchangeRate();
        }
        if (exchangeRate == null || exchangeRate.compareTo(BigDecimal.ZERO) == 0) {
            throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.CURRENCY_2), startCurrency.getName(), endCurrency.getName(), dateToConvert);
        }
        return isInverse ? BigDecimal.ONE.divide(exchangeRate, 10, RoundingMode.HALF_UP) : exchangeRate;
    }
    return BigDecimal.ONE;
}
Also used : AxelorException(com.axelor.exception.AxelorException) CurrencyConversionLine(com.axelor.apps.base.db.CurrencyConversionLine) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal)

Example 5 with CurrencyConversionLine

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

the class CurrencyConversionService method createCurrencyConversionLine.

/**
 * Create new CurrencyConversionLine. (Transaction)
 *
 * @param currencyFrom
 * @param currencyTo
 * @param fromDate
 * @param rate
 * @param appBase
 * @param variations
 */
@Transactional
public void createCurrencyConversionLine(Currency currencyFrom, Currency currencyTo, LocalDate fromDate, BigDecimal rate, AppBase appBase, String variations) {
    LOG.trace("Create new currency conversion line CurrencyFrom: {}, CurrencyTo: {},FromDate: {},ConversionRate: {}, AppBase: {}, Variations: {}", new Object[] { currencyFrom, currencyTo, fromDate, rate, appBase, variations });
    CurrencyConversionLine ccl = new CurrencyConversionLine();
    ccl.setStartCurrency(currencyFrom);
    ccl.setEndCurrency(currencyTo);
    ccl.setFromDate(fromDate);
    ccl.setExchangeRate(rate);
    ccl.setAppBase(appBase);
    ccl.setVariations(variations);
    cclRepo.save(ccl);
}
Also used : CurrencyConversionLine(com.axelor.apps.base.db.CurrencyConversionLine) Transactional(com.google.inject.persist.Transactional)

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