|
|
|
@ -20,6 +20,7 @@ import lombok.RequiredArgsConstructor;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
|
|
|
import java.math.RoundingMode;
|
|
|
|
import java.time.LocalDate;
|
|
|
|
import java.time.LocalDate;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
@ -160,41 +161,40 @@ public class TransactionService {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public EvaluatedCurrencyReponse getEvaluatedCurrency(String porOrgacode, String baseCurrencyCode, String targetCurrencyCode, double sgtGntramtfc) {
|
|
|
|
public EvaluatedCurrencyReponse getEvaluatedCurrency(String porOrgacode, String baseCurrencyCode, String targetCurrencyCode, double sgtGntramtfc) {
|
|
|
|
List<Map> exchangeRateList = (List<Map>) ucoAccountService.fetchExchangeRate(porOrgacode);
|
|
|
|
double pkrAmt = convertToPKR(baseCurrencyCode,sgtGntramtfc,porOrgacode);
|
|
|
|
Map<String, Double> exchangeRates = new HashMap<>();
|
|
|
|
double convertFromPkr = convertFromPKR(targetCurrencyCode,pkrAmt,porOrgacode);
|
|
|
|
for (Map rateEntry : exchangeRateList) {
|
|
|
|
List<ExchangeRateModel> exchangeRateModelList = fetchExchangeRate(porOrgacode);
|
|
|
|
String currencyCode = (String) rateEntry.get("pcrCurrcode");
|
|
|
|
|
|
|
|
Double rate = (Double) rateEntry.get("perEratrateact");
|
|
|
|
Optional<Double> rate = exchangeRateModelList.stream()
|
|
|
|
exchangeRates.put(currencyCode, rate);
|
|
|
|
.filter(x -> x.isPcrCurrbase() && x.getPcrCurrcode().equals(targetCurrencyCode))
|
|
|
|
|
|
|
|
.map(x -> {
|
|
|
|
|
|
|
|
BigDecimal bd = BigDecimal.valueOf(1 / x.getPerEratrateact());
|
|
|
|
|
|
|
|
bd = bd.setScale(2, RoundingMode.HALF_UP);
|
|
|
|
|
|
|
|
return bd.doubleValue();
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.findFirst();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!rate.isPresent()) {
|
|
|
|
|
|
|
|
rate = exchangeRateModelList.stream()
|
|
|
|
|
|
|
|
.filter(x -> !x.isPcrCurrbase() && x.getPcrCurrcode().equals(targetCurrencyCode))
|
|
|
|
|
|
|
|
.map(x -> {
|
|
|
|
|
|
|
|
BigDecimal bd = BigDecimal.valueOf(x.getPerEratrateact());
|
|
|
|
|
|
|
|
bd = bd.setScale(2, RoundingMode.HALF_UP);
|
|
|
|
|
|
|
|
return bd.doubleValue();
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.findFirst();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double convertedAmount = convertCurrency(baseCurrencyCode, targetCurrencyCode, sgtGntramtfc, exchangeRates);
|
|
|
|
double targetPerEratrateact = rate.orElse(0.0);
|
|
|
|
|
|
|
|
|
|
|
|
return EvaluatedCurrencyReponse.builder()
|
|
|
|
return EvaluatedCurrencyReponse.builder()
|
|
|
|
.pcrCurrcode(targetCurrencyCode)
|
|
|
|
.targetPerEratrateact(targetPerEratrateact)
|
|
|
|
.sgtGntramtfc(convertedAmount)
|
|
|
|
|
|
|
|
.serviceCharges(0.0)
|
|
|
|
.serviceCharges(0.0)
|
|
|
|
.targetPerEratrateact(targetCurrencyCode.equals("default") ? (1 / exchangeRates.get(baseCurrencyCode)) : exchangeRates.get(targetCurrencyCode))
|
|
|
|
.pcrCurrcode(targetCurrencyCode)
|
|
|
|
|
|
|
|
.sgtGntramtfc(BigDecimal.valueOf(convertFromPkr).setScale(2, RoundingMode.HALF_UP))
|
|
|
|
.build();
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public double convertCurrency(String baseCurrencyCode, String targetCurrencyCode, double amount, Map<String, Double> exchangeRates) {
|
|
|
|
|
|
|
|
if (baseCurrencyCode.equals(targetCurrencyCode)) {
|
|
|
|
|
|
|
|
return amount;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (baseCurrencyCode.equals("default")) {
|
|
|
|
|
|
|
|
return exchangeRates.get(targetCurrencyCode) * amount;
|
|
|
|
|
|
|
|
} else if (targetCurrencyCode.equals("default")) {
|
|
|
|
|
|
|
|
return exchangeRates.get(baseCurrencyCode) * amount;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!exchangeRates.containsKey(baseCurrencyCode) || !exchangeRates.containsKey(targetCurrencyCode)) {
|
|
|
|
|
|
|
|
throw new IllegalArgumentException("Unsupported currency code");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
double amountInPKR = amount * exchangeRates.get(baseCurrencyCode);
|
|
|
|
|
|
|
|
double amountInTargetCurrency = amountInPKR / exchangeRates.get(targetCurrencyCode);
|
|
|
|
|
|
|
|
return amountInTargetCurrency;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public double convertToPKR(String fromcrCurrcode, double amount, String porOrgacode) {
|
|
|
|
public double convertToPKR(String fromcrCurrcode, double amount, String porOrgacode) {
|
|
|
|
|
|
|
|
|
|
|
|
|