use of org.javamoney.moneta.function.MonetaryFunctions in project mod-invoice by folio-org.
the class FundAvailabilityHolderValidator method calculateNewExpendedAmount.
private MonetaryAmount calculateNewExpendedAmount(List<InvoiceWorkflowDataHolder> dataHolders) {
CurrencyUnit currency = Monetary.getCurrency(dataHolders.get(0).getFyCurrency());
return dataHolders.stream().map(holder -> {
MonetaryAmount newTransactionAmount = Money.of(holder.getNewTransaction().getAmount(), holder.getFyCurrency());
MonetaryAmount existingTransactionAmount = Optional.ofNullable(holder.getExistingTransaction()).map(transaction -> Money.of(transaction.getAmount(), transaction.getCurrency())).orElseGet(() -> Money.zero(Monetary.getCurrency(holder.getFyCurrency())));
MonetaryAmount encumbranceAmount = Optional.ofNullable(holder.getEncumbrance()).map(transaction -> Money.of(transaction.getAmount(), transaction.getCurrency())).orElseGet(() -> Money.zero(Monetary.getCurrency(holder.getFyCurrency())));
MonetaryAmount transactionAmountDif = newTransactionAmount.subtract(existingTransactionAmount);
MonetaryAmount newExpendedAmount = transactionAmountDif;
if (transactionAmountDif.isPositive()) {
newExpendedAmount = MonetaryFunctions.max().apply(transactionAmountDif.subtract(encumbranceAmount), Money.zero(currency));
MonetaryAmount encumbranceReminder = MonetaryFunctions.max().apply(encumbranceAmount.subtract(newExpendedAmount).add(existingTransactionAmount), Money.zero(currency));
Optional.ofNullable(holder.getEncumbrance()).ifPresent(transaction -> transaction.setAmount(encumbranceReminder.getNumber().doubleValue()));
}
return newExpendedAmount;
}).reduce(MonetaryFunctions::sum).orElseGet(() -> Money.zero(currency));
}
use of org.javamoney.moneta.function.MonetaryFunctions in project mod-orders by folio-org.
the class BudgetRestrictionService method calculateNewEncumberedAmount.
private MonetaryAmount calculateNewEncumberedAmount(List<EncumbranceRelationsHolder> encumbranceRelationsHolders) {
CurrencyUnit currency = Monetary.getCurrency(encumbranceRelationsHolders.get(0).getCurrency());
return encumbranceRelationsHolders.stream().map(holder -> {
MonetaryAmount newTransactionAmount = Money.of(holder.getNewEncumbrance().getAmount(), holder.getCurrency());
MonetaryAmount existingTransactionAmount = Optional.ofNullable(holder.getOldEncumbrance()).map(transaction -> Money.of(transaction.getAmount(), transaction.getCurrency())).orElseGet(() -> Money.zero(Monetary.getCurrency(holder.getCurrency())));
return newTransactionAmount.subtract(existingTransactionAmount);
}).reduce(MonetaryFunctions::sum).orElseGet(() -> Money.zero(currency));
}
use of org.javamoney.moneta.function.MonetaryFunctions in project mod-invoice by folio-org.
the class FundAvailabilityHolderValidator method calculateTotalExpendedAmount.
private MonetaryAmount calculateTotalExpendedAmount(List<InvoiceWorkflowDataHolder> dataHolders) {
CurrencyUnit currency = Monetary.getCurrency(dataHolders.get(0).getFyCurrency());
return dataHolders.stream().map(holder -> {
MonetaryAmount newTransactionAmount = Money.of(holder.getNewTransaction().getAmount(), holder.getFyCurrency());
MonetaryAmount existingTransactionAmount = Optional.ofNullable(holder.getExistingTransaction()).map(transaction -> Money.of(transaction.getAmount(), transaction.getCurrency())).orElseGet(() -> Money.zero(Monetary.getCurrency(holder.getFyCurrency())));
return newTransactionAmount.subtract(existingTransactionAmount);
}).reduce(MonetaryFunctions::sum).orElseGet(() -> Money.zero(currency));
}
Aggregations