Search in sources :

Example 1 with MonetaryFunctions

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));
}
Also used : Monetary(javax.money.Monetary) FUND_CANNOT_BE_PAID(org.folio.invoices.utils.ErrorCodes.FUND_CANNOT_BE_PAID) CurrencyUnit(javax.money.CurrencyUnit) Money(org.javamoney.moneta.Money) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) MonetaryFunctions(org.javamoney.moneta.function.MonetaryFunctions) InvoiceWorkflowDataHolder(org.folio.models.InvoiceWorkflowDataHolder) Fund(org.folio.rest.acq.model.finance.Fund) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) BigDecimal(java.math.BigDecimal) HttpException(org.folio.invoices.rest.exceptions.HttpException) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) FUNDS(org.folio.invoices.utils.ResourcePathResolver.FUNDS) Map(java.util.Map) Optional(java.util.Optional) Parameter(org.folio.rest.jaxrs.model.Parameter) Collections(java.util.Collections) MonetaryAmount(javax.money.MonetaryAmount) Budget(org.folio.rest.acq.model.finance.Budget) CurrencyUnit(javax.money.CurrencyUnit) MonetaryAmount(javax.money.MonetaryAmount)

Example 2 with MonetaryFunctions

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));
}
Also used : Monetary(javax.money.Monetary) FUND_CANNOT_BE_PAID(org.folio.rest.core.exceptions.ErrorCodes.FUND_CANNOT_BE_PAID) CurrencyUnit(javax.money.CurrencyUnit) Money(org.javamoney.moneta.Money) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) HttpException(org.folio.rest.core.exceptions.HttpException) MonetaryFunctions(org.javamoney.moneta.function.MonetaryFunctions) EncumbranceRelationsHolder(org.folio.models.EncumbranceRelationsHolder) FundDistribution(org.folio.rest.jaxrs.model.FundDistribution) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) FUNDS(org.folio.orders.utils.ResourcePathResolver.FUNDS) BigDecimal(java.math.BigDecimal) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Map(java.util.Map) Optional(java.util.Optional) Parameter(org.folio.rest.jaxrs.model.Parameter) Collections(java.util.Collections) MonetaryAmount(javax.money.MonetaryAmount) Budget(org.folio.rest.acq.model.finance.Budget) CurrencyUnit(javax.money.CurrencyUnit) MonetaryAmount(javax.money.MonetaryAmount)

Example 3 with MonetaryFunctions

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));
}
Also used : Monetary(javax.money.Monetary) FUND_CANNOT_BE_PAID(org.folio.invoices.utils.ErrorCodes.FUND_CANNOT_BE_PAID) CurrencyUnit(javax.money.CurrencyUnit) Money(org.javamoney.moneta.Money) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) MonetaryFunctions(org.javamoney.moneta.function.MonetaryFunctions) InvoiceWorkflowDataHolder(org.folio.models.InvoiceWorkflowDataHolder) Fund(org.folio.rest.acq.model.finance.Fund) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) BigDecimal(java.math.BigDecimal) HttpException(org.folio.invoices.rest.exceptions.HttpException) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) FUNDS(org.folio.invoices.utils.ResourcePathResolver.FUNDS) Map(java.util.Map) Optional(java.util.Optional) Parameter(org.folio.rest.jaxrs.model.Parameter) Collections(java.util.Collections) MonetaryAmount(javax.money.MonetaryAmount) Budget(org.folio.rest.acq.model.finance.Budget) CurrencyUnit(javax.money.CurrencyUnit) MonetaryAmount(javax.money.MonetaryAmount)

Aggregations

BigDecimal (java.math.BigDecimal)3 Collections (java.util.Collections)3 List (java.util.List)3 Map (java.util.Map)3 Objects (java.util.Objects)3 Optional (java.util.Optional)3 Collectors (java.util.stream.Collectors)3 Collectors.groupingBy (java.util.stream.Collectors.groupingBy)3 Collectors.toList (java.util.stream.Collectors.toList)3 CurrencyUnit (javax.money.CurrencyUnit)3 Monetary (javax.money.Monetary)3 MonetaryAmount (javax.money.MonetaryAmount)3 Budget (org.folio.rest.acq.model.finance.Budget)3 Parameter (org.folio.rest.jaxrs.model.Parameter)3 Money (org.javamoney.moneta.Money)3 MonetaryFunctions (org.javamoney.moneta.function.MonetaryFunctions)3 HttpException (org.folio.invoices.rest.exceptions.HttpException)2 FUND_CANNOT_BE_PAID (org.folio.invoices.utils.ErrorCodes.FUND_CANNOT_BE_PAID)2 FUNDS (org.folio.invoices.utils.ResourcePathResolver.FUNDS)2 InvoiceWorkflowDataHolder (org.folio.models.InvoiceWorkflowDataHolder)2