Skip to content

Commit

Permalink
[MODFIN-394]. Budget summary for Net Transfers do not include Rollove…
Browse files Browse the repository at this point in the history
…r transfer amounts. (#265)

* [MODFIN-394]. Budget summary for Net Transfers do not include Rollover transfer amounts.
  • Loading branch information
SerhiiNosko authored Dec 12, 2024
1 parent 44bebfc commit 4cb4e6a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 deletions.
3 changes: 3 additions & 0 deletions src/main/java/org/folio/rest/util/BudgetUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<TransactionType> TRANSFER_TRANSACTION_TYPES = List.of(TransactionType.TRANSFER, TransactionType.ROLLOVER_TRANSFER);

private BudgetUtils() {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {

Expand Down Expand Up @@ -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<Transaction> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -284,10 +285,9 @@ private Future<GroupFiscalYearTransactionsHolder> updateHolderWithAllocations(Re
private Future<GroupFiscalYearTransactionsHolder> updateHolderWithTransfers(RequestContext requestContext, GroupFiscalYearTransactionsHolder holder) {
List<String> groupFundIds = holder.getGroupFundIds();
String fiscalYearId = holder.getGroupFiscalYearSummary().getFiscalYearId();
List<TransactionType> 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()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -99,9 +100,8 @@ private Future<LedgerFiscalYearTransactionsHolder> updateHolderWithTransfers(Led
RequestContext requestContext) {
List<String> ledgerFundIds = holder.getLedgerFundIds();
String fiscalYearId = holder.getFiscalYearId();
List<TransactionType> 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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -81,7 +83,7 @@ void recalculateBudgetComplexPositiveTest(VertxTestContext vertxTestContext) {
Transaction payment = buildTransaction(budgetFundId, null, 60d, Transaction.TransactionType.PAYMENT);

List<Transaction> 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)));
Expand All @@ -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
Expand Down

0 comments on commit 4cb4e6a

Please sign in to comment.