From 61f479897a9079aed187d37e5d236300a88fe4f4 Mon Sep 17 00:00:00 2001 From: Serhii Nosko Date: Thu, 12 Dec 2024 11:54:13 +0200 Subject: [PATCH] [MODFIN-394]. Budget summary for Net Transfers do not include Rollover transfer amounts. (#265) * [MODFIN-394]. Budget summary for Net Transfers do not include Rollover transfer amounts. (cherry picked from commit 4cb4e6ac15937376735b0ea104ea706ab5831f55) --- .../java/org/folio/rest/util/BudgetUtils.java | 3 +++ .../budget/RecalculatedBudgetBuilder.java | 17 +++++++++++------ .../group/GroupFiscalYearTotalsService.java | 6 +++--- .../services/ledger/LedgerTotalsService.java | 6 +++--- .../budget/RecalculateBudgetServiceTest.java | 6 ++++-- 5 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/folio/rest/util/BudgetUtils.java b/src/main/java/org/folio/rest/util/BudgetUtils.java index 4694b867..447eb656 100644 --- a/src/main/java/org/folio/rest/util/BudgetUtils.java +++ b/src/main/java/org/folio/rest/util/BudgetUtils.java @@ -7,11 +7,14 @@ import org.folio.rest.jaxrs.model.BudgetExpenseClass; import org.folio.rest.jaxrs.model.SharedBudget; import org.folio.rest.jaxrs.model.StatusExpenseClass; +import org.folio.rest.jaxrs.model.Transaction.TransactionType; import io.vertx.core.json.JsonObject; public final class BudgetUtils { + public static final List TRANSFER_TRANSACTION_TYPES = List.of(TransactionType.TRANSFER, TransactionType.ROLLOVER_TRANSFER); + private BudgetUtils() { } diff --git a/src/main/java/org/folio/services/budget/RecalculatedBudgetBuilder.java b/src/main/java/org/folio/services/budget/RecalculatedBudgetBuilder.java index 73991036..64bdb453 100644 --- a/src/main/java/org/folio/services/budget/RecalculatedBudgetBuilder.java +++ b/src/main/java/org/folio/services/budget/RecalculatedBudgetBuilder.java @@ -12,11 +12,12 @@ import javax.money.CurrencyUnit; import javax.money.Monetary; import javax.money.MonetaryAmount; -import java.util.Map; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; import java.util.List; +import java.util.Map; import java.util.Objects; -import java.util.Comparator; -import java.util.Collections; import java.util.function.Function; import java.util.stream.Stream; @@ -26,7 +27,7 @@ import static org.folio.rest.jaxrs.model.Transaction.TransactionType.ENCUMBRANCE; import static org.folio.rest.jaxrs.model.Transaction.TransactionType.PAYMENT; import static org.folio.rest.jaxrs.model.Transaction.TransactionType.PENDING_PAYMENT; -import static org.folio.rest.jaxrs.model.Transaction.TransactionType.TRANSFER; +import static org.folio.rest.util.BudgetUtils.TRANSFER_TRANSACTION_TYPES; public class RecalculatedBudgetBuilder { @@ -107,14 +108,18 @@ public RecalculatedBudgetBuilder withAllocationFrom(String fundId) { } /** - * Sets the net transfers amount based on the sum of 'Transfer' transactions. + * Sets the net transfers amount based on the sum of 'Transfer' and 'Rollover transfer' transactions. * Transfers originating from the budget are subtracted, while others are summed. * * @param fundId Fund ID * @return This RecalculatedBudgetBuilder instance for method chaining */ public RecalculatedBudgetBuilder withNetTransfers(String fundId) { - netTransfers = getTransactionByType(TRANSFER).stream() + List transferTransactions = new ArrayList<>(); + for (Transaction.TransactionType trType: TRANSFER_TRANSACTION_TYPES) { + transferTransactions.addAll(getTransactionByType(trType)); + } + netTransfers = transferTransactions.stream() .map(transaction -> { MonetaryAmount amount = Money.of(transaction.getAmount(), currency); return Objects.equals(transaction.getFromFundId(), fundId) ? amount.negate() : amount; diff --git a/src/main/java/org/folio/services/group/GroupFiscalYearTotalsService.java b/src/main/java/org/folio/services/group/GroupFiscalYearTotalsService.java index 1391ece9..84d6baf6 100644 --- a/src/main/java/org/folio/services/group/GroupFiscalYearTotalsService.java +++ b/src/main/java/org/folio/services/group/GroupFiscalYearTotalsService.java @@ -4,6 +4,7 @@ import static java.util.stream.Collectors.mapping; import static java.util.stream.Collectors.reducing; import static java.util.stream.Collectors.toList; +import static org.folio.rest.util.BudgetUtils.TRANSFER_TRANSACTION_TYPES; import static org.folio.rest.util.HelperUtils.collectResultsOnSuccess; import static org.folio.rest.util.HelperUtils.removeInitialAllocationByFunds; import static org.folio.rest.util.ResourcePathResolver.BUDGETS_STORAGE; @@ -284,10 +285,9 @@ private Future updateHolderWithAllocations(Re private Future updateHolderWithTransfers(RequestContext requestContext, GroupFiscalYearTransactionsHolder holder) { List groupFundIds = holder.getGroupFundIds(); String fiscalYearId = holder.getGroupFiscalYearSummary().getFiscalYearId(); - List trTypes = List.of(TransactionType.TRANSFER, TransactionType.ROLLOVER_TRANSFER); - var fromTransfers = transactionService.getTransactionsFromFunds(groupFundIds, fiscalYearId, trTypes, requestContext); - var toTransfers = transactionService.getTransactionsToFunds(groupFundIds, fiscalYearId, trTypes, requestContext); + var fromTransfers = transactionService.getTransactionsFromFunds(groupFundIds, fiscalYearId, TRANSFER_TRANSACTION_TYPES, requestContext); + var toTransfers = transactionService.getTransactionsToFunds(groupFundIds, fiscalYearId, TRANSFER_TRANSACTION_TYPES, requestContext); return GenericCompositeFuture.join(List.of(fromTransfers, toTransfers)) .map(cf -> holder.withToTransfers(toTransfers.result()).withFromTransfers(fromTransfers.result())); } diff --git a/src/main/java/org/folio/services/ledger/LedgerTotalsService.java b/src/main/java/org/folio/services/ledger/LedgerTotalsService.java index 4a52aedb..181834d7 100644 --- a/src/main/java/org/folio/services/ledger/LedgerTotalsService.java +++ b/src/main/java/org/folio/services/ledger/LedgerTotalsService.java @@ -1,5 +1,6 @@ package org.folio.services.ledger; +import static org.folio.rest.util.BudgetUtils.TRANSFER_TRANSACTION_TYPES; import static org.folio.rest.util.HelperUtils.collectResultsOnSuccess; import static org.folio.rest.util.HelperUtils.removeInitialAllocationByFunds; @@ -99,9 +100,8 @@ private Future updateHolderWithTransfers(Led RequestContext requestContext) { List ledgerFundIds = holder.getLedgerFundIds(); String fiscalYearId = holder.getFiscalYearId(); - List trTypes = List.of(TransactionType.TRANSFER, TransactionType.ROLLOVER_TRANSFER); - var fromTransfer = transactionService.getTransactionsFromFunds(ledgerFundIds, fiscalYearId, trTypes, requestContext); - var toTransfer = transactionService.getTransactionsToFunds(ledgerFundIds, fiscalYearId, trTypes, requestContext); + var fromTransfer = transactionService.getTransactionsFromFunds(ledgerFundIds, fiscalYearId, TRANSFER_TRANSACTION_TYPES, requestContext); + var toTransfer = transactionService.getTransactionsToFunds(ledgerFundIds, fiscalYearId, TRANSFER_TRANSACTION_TYPES, requestContext); return GenericCompositeFuture.join(List.of(fromTransfer, toTransfer)) .map(f -> holder.withToTransfers(toTransfer.result()).withFromTransfers(fromTransfer.result())); diff --git a/src/test/java/org/folio/services/budget/RecalculateBudgetServiceTest.java b/src/test/java/org/folio/services/budget/RecalculateBudgetServiceTest.java index 0b7360f5..b8972538 100644 --- a/src/test/java/org/folio/services/budget/RecalculateBudgetServiceTest.java +++ b/src/test/java/org/folio/services/budget/RecalculateBudgetServiceTest.java @@ -72,6 +72,8 @@ void recalculateBudgetComplexPositiveTest(VertxTestContext vertxTestContext) { Transaction transferTo = buildTransaction(anotherFundId, budgetFundId, 150d, Transaction.TransactionType.TRANSFER); Transaction transferFrom = buildTransaction(budgetFundId, anotherFundId, 100d, Transaction.TransactionType.TRANSFER); + Transaction rolloverTransfer = buildTransaction(null, budgetFundId, 4d, Transaction.TransactionType.ROLLOVER_TRANSFER); + Transaction encumbrance = buildTransaction(budgetFundId, null, 120d, Transaction.TransactionType.ENCUMBRANCE); Transaction pendingPayment1 = buildTransaction(budgetFundId, null, -20d, Transaction.TransactionType.PENDING_PAYMENT); @@ -81,7 +83,7 @@ void recalculateBudgetComplexPositiveTest(VertxTestContext vertxTestContext) { Transaction payment = buildTransaction(budgetFundId, null, 60d, Transaction.TransactionType.PAYMENT); List transactions = Arrays.asList(initialAllocation, - increaseAllocation, decreaseAllocation, transferTo, transferFrom, + increaseAllocation, decreaseAllocation, transferTo, transferFrom, rolloverTransfer, encumbrance, pendingPayment1, pendingPayment2, credit, payment); when(budgetServiceMock.getBudgetById(anyString(), any())).thenReturn(succeededFuture(BudgetUtils.convertToSharedBudget(budget))); @@ -100,7 +102,7 @@ void recalculateBudgetComplexPositiveTest(VertxTestContext vertxTestContext) { assertEquals(1500d, capturedBudget.getInitialAllocation()); // initialAllocation assertEquals(100d, capturedBudget.getAllocationTo()); // increaseAllocation assertEquals(200d, capturedBudget.getAllocationFrom()); // decreaseAllocation - assertEquals(50d, capturedBudget.getNetTransfers()); // transferTo - transferFrom + assertEquals(54d, capturedBudget.getNetTransfers()); // transferTo - transferFrom assertEquals(120d, capturedBudget.getEncumbered()); // encumbrance assertEquals(30d, capturedBudget.getAwaitingPayment()); // pendingPayment1 + pendingPayment2 assertEquals(60d, capturedBudget.getExpenditures()); // payment