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