use of org.folio.rest.acq.model.finance.Budget in project mod-orders by folio-org.
the class BudgetRestrictionServiceTest method checkEnoughMoneyInBudgetShouldPassIfTransactionsAmountDifferenceLessThanBudgetRemainingAmount.
@Test
void checkEnoughMoneyInBudgetShouldPassIfTransactionsAmountDifferenceLessThanBudgetRemainingAmount() {
String fiscalYearId = UUID.randomUUID().toString();
String fundId = UUID.randomUUID().toString();
String ledgerId = UUID.randomUUID().toString();
String budgetId = UUID.randomUUID().toString();
Transaction existingTransaction = new Transaction().withTransactionType(Transaction.TransactionType.ENCUMBRANCE).withAmount(50d).withFiscalYearId(fiscalYearId).withFromFundId(fundId).withCurrency("USD");
Transaction newTransaction = new Transaction().withTransactionType(Transaction.TransactionType.ENCUMBRANCE).withAmount(60d).withFiscalYearId(fiscalYearId).withFromFundId(fundId).withCurrency("USD");
Budget budget = new Budget().withId(budgetId).withFiscalYearId(fiscalYearId).withFundId(fundId).withAllocated(59d).withAvailable(9d).withTotalFunding(59d).withUnavailable(50d).withAwaitingPayment(50D).withAllowableEncumbrance(150d);
Fund fund = new Fund().withId(fundId).withLedgerId(ledgerId);
Ledger ledger = new Ledger().withId(ledgerId).withRestrictEncumbrance(true);
List<EncumbranceRelationsHolder> holders = new ArrayList<>();
EncumbranceRelationsHolder holder = new EncumbranceRelationsHolder().withOldEncumbrance(existingTransaction).withNewEncumbrance(newTransaction).withBudget(budget).withLedgerId(fund.getLedgerId()).withRestrictEncumbrances(ledger.getRestrictExpenditures()).withCurrentFiscalYearId(fiscalYearId).withCurrency("USD");
holders.add(holder);
assertDoesNotThrow(() -> restrictionService.checkEncumbranceRestrictions(holders));
}
use of org.folio.rest.acq.model.finance.Budget in project mod-orders by folio-org.
the class BudgetRestrictionService method checkEncumbranceRestrictions.
public void checkEncumbranceRestrictions(List<? extends EncumbranceRelationsHolder> dataHolders) {
Map<Budget, List<EncumbranceRelationsHolder>> budgetHoldersMap = dataHolders.stream().filter(EncumbranceRelationsHolder::getRestrictEncumbrance).collect(groupingBy(EncumbranceRelationsHolder::getBudget));
Map<String, String> fundHoldersMap = dataHolders.stream().filter(EncumbranceRelationsHolder::getRestrictEncumbrance).map(EncumbranceRelationsHolder::getFundDistribution).filter(fd -> fd != null && fd.getFundId() != null && fd.getCode() != null).distinct().collect(Collectors.toMap(FundDistribution::getFundId, FundDistribution::getCode, (fundEntityKey, fundEntityDupKey) -> fundEntityKey));
List<String> failedFundIds = budgetHoldersMap.entrySet().stream().filter(entry -> Objects.nonNull(entry.getKey().getAllowableEncumbrance())).filter(entry -> {
MonetaryAmount newEncumberedAmount = calculateNewEncumberedAmount(entry.getValue());
return isRemainingAmountExceed(entry.getKey(), newEncumberedAmount);
}).map(Map.Entry::getKey).map(Budget::getFundId).collect(toList());
if (!failedFundIds.isEmpty()) {
Parameter parameter = new Parameter().withKey(FUNDS).withValue(failedFundIds.stream().map(fundHoldersMap::get).collect(toList()).toString());
throw new HttpException(422, FUND_CANNOT_BE_PAID.toError().withParameters(Collections.singletonList(parameter)));
}
}
use of org.folio.rest.acq.model.finance.Budget in project mod-orders by folio-org.
the class MockServer method getBudgetsByFundIds.
private JsonObject getBudgetsByFundIds(List<String> budgetByFundIds) {
Supplier<List<Budget>> getFromFile = () -> {
try {
return new JsonObject(getMockData(BUDGETS_PATH)).mapTo(BudgetCollection.class).getBudgets();
} catch (IOException e) {
return Collections.emptyList();
}
};
List<Budget> budgets = getMockEntries(BUDGETS, Budget.class).orElseGet(getFromFile);
if (!budgetByFundIds.isEmpty()) {
budgets.removeIf(item -> !budgetByFundIds.contains(item.getFundId()));
}
Object record = new BudgetCollection().withBudgets(budgets).withTotalRecords(budgets.size());
return JsonObject.mapFrom(record);
}
use of org.folio.rest.acq.model.finance.Budget in project mod-orders by folio-org.
the class EncumbranceRelationsHoldersBuilderTest method testShouldPopulateHoldersWithCorrespondingBudgets.
@Test
void testShouldPopulateHoldersWithCorrespondingBudgets() {
// given
Budget budget1 = new Budget().withId(UUID.randomUUID().toString()).withFundId(holder1.getFundId());
Budget budget2 = new Budget().withId(UUID.randomUUID().toString()).withFundId(holder2.getFundId());
Budget budget3 = new Budget().withId(UUID.randomUUID().toString()).withFundId(holder3.getFundId());
List<EncumbranceRelationsHolder> holders = new ArrayList<>();
holders.add(holder1);
holders.add(holder2);
holders.add(holder3);
when(budgetService.getBudgets(anyCollection(), any())).thenReturn(CompletableFuture.completedFuture(List.of(budget1, budget2, budget3)));
// When
encumbranceRelationsHoldersBuilder.withBudgets(holders, requestContextMock).join();
// Then
assertEquals(budget1, holder1.getBudget());
assertEquals(budget2, holder2.getBudget());
assertEquals(budget3, holder3.getBudget());
}
use of org.folio.rest.acq.model.finance.Budget in project mod-orders by folio-org.
the class OrderReEncumberServiceTest method shouldVerifyFYROAdjustmentAndFundDistributionValuesWhenMixedFunds.
@Test
void shouldVerifyFYROAdjustmentAndFundDistributionValuesWhenMixedFunds() {
String ledgerId = UUID.randomUUID().toString();
String fromFyId = UUID.randomUUID().toString();
String toFyId = UUID.randomUUID().toString();
String orderId = UUID.randomUUID().toString();
String fund1Id = UUID.randomUUID().toString();
String fund2Id = UUID.randomUUID().toString();
String rolloverId = UUID.randomUUID().toString();
Ledger notRestrictedLedger = new Ledger().withId(ledgerId).withRestrictEncumbrance(false);
double exchangeRate = 1.1d;
Fund fund1 = new Fund().withId(fund1Id).withLedgerId(ledgerId);
Fund fund2 = new Fund().withId(fund2Id).withLedgerId(ledgerId);
Budget notRestrictedBudget1 = new Budget().withId(UUID.randomUUID().toString()).withFundId(fund1Id);
EncumbranceRollover encumbranceRollover = new EncumbranceRollover().withBasedOn(EncumbranceRollover.BasedOn.EXPENDED).withOrderType(EncumbranceRollover.OrderType.ONE_TIME).withIncreaseBy(10d);
LedgerFiscalYearRollover rollover = new LedgerFiscalYearRollover().withId(rolloverId).withLedgerId(ledgerId).withFromFiscalYearId(fromFyId).withToFiscalYearId(toFyId).withEncumbrancesRollover(Collections.singletonList(encumbranceRollover));
CompositePurchaseOrder compPO = new CompositePurchaseOrder().withId(orderId).withOrderType(CompositePurchaseOrder.OrderType.ONE_TIME);
Cost cost1 = new Cost().withListUnitPrice(120d).withQuantityPhysical(1).withExchangeRate(exchangeRate).withCurrency("EUR").withPoLineEstimatedPrice(120d);
Transaction fromEncumbrance1 = new Transaction().withEncumbrance(new Encumbrance().withSourcePurchaseOrderId(orderId).withInitialAmountEncumbered(60d).withAmountExpended(40d)).withAmount(20d).withFiscalYearId(fromFyId).withFromFundId(fund1Id).withId(UUID.randomUUID().toString()).withCurrency("USD");
Transaction fromEncumbrance2 = new Transaction().withEncumbrance(new Encumbrance().withSourcePurchaseOrderId(orderId).withInitialAmountEncumbered(60d).withAmountExpended(30d)).withAmount(30d).withFiscalYearId(fromFyId).withFromFundId(fund2Id).withId(UUID.randomUUID().toString()).withCurrency("USD");
Transaction toEncumbrance1 = new Transaction().withEncumbrance(new Encumbrance().withStatus(Encumbrance.Status.UNRELEASED).withAmountAwaitingPayment(0d).withSourcePurchaseOrderId(orderId).withInitialAmountEncumbered(60d).withAmountExpended(0d)).withAmount(40d).withFiscalYearId(fromFyId).withFromFundId(fund1Id).withId(UUID.randomUUID().toString()).withCurrency("USD");
Transaction toEncumbrance2 = new Transaction().withEncumbrance(new Encumbrance().withStatus(Encumbrance.Status.UNRELEASED).withAmountAwaitingPayment(0d).withSourcePurchaseOrderId(orderId).withInitialAmountEncumbered(60d).withAmountExpended(0d)).withAmount(30d).withFiscalYearId(fromFyId).withFromFundId(fund2Id).withId(UUID.randomUUID().toString()).withCurrency("USD");
TransactionCollection toTransactionCollection = new TransactionCollection().withTransactions(Collections.EMPTY_LIST).withTotalRecords(0);
FundDistribution fundDistribution1 = new FundDistribution().withDistributionType(FundDistribution.DistributionType.PERCENTAGE).withValue(50d).withEncumbrance(fromEncumbrance1.getId()).withFundId(fund1Id);
FundDistribution fundDistribution2 = new FundDistribution().withDistributionType(FundDistribution.DistributionType.AMOUNT).withValue(60d).withEncumbrance(fromEncumbrance2.getId()).withFundId(fund2Id);
CompositePoLine line1 = new CompositePoLine().withId(UUID.randomUUID().toString()).withCost(cost1).withFundDistribution(List.of(fundDistribution1, fundDistribution2));
ConversionQuery conversionPoLineToFyQuery = ConversionQueryBuilder.of().setBaseCurrency("EUR").setTermCurrency("USD").set(ExchangeRateProviderResolver.RATE_KEY, exchangeRate).build();
ConversionQuery conversionFyToPoLineQuery = ConversionQueryBuilder.of().setBaseCurrency("USD").setTermCurrency("EUR").set(ExchangeRateProviderResolver.RATE_KEY, exchangeRate).build();
ReEncumbranceHolder holder1 = new ReEncumbranceHolder().withPurchaseOrder(compPO).withRollover(rollover).withPoLine(line1).withFundDistribution(fundDistribution1).withLedgerId(ledgerId).withRestrictEncumbrances(notRestrictedLedger.getRestrictEncumbrance()).withBudget(notRestrictedBudget1).withEncumbranceRollover(encumbranceRollover).withPreviousFyEncumbrance(fromEncumbrance1).withNewEncumbrance(toEncumbrance1).withPoLineToFyConversion(exchangeRateProvider.getCurrencyConversion(conversionPoLineToFyQuery)).withFyToPoLineConversion(exchangeRateProvider.getCurrencyConversion(conversionFyToPoLineQuery));
ReEncumbranceHolder holder2 = new ReEncumbranceHolder().withPurchaseOrder(compPO).withRollover(rollover).withPoLine(line1).withFundDistribution(fundDistribution2).withLedgerId(ledgerId).withRestrictEncumbrances(notRestrictedLedger.getRestrictEncumbrance()).withBudget(notRestrictedBudget1).withEncumbranceRollover(encumbranceRollover).withNewEncumbrance(toEncumbrance2).withPreviousFyEncumbrance(fromEncumbrance2).withPoLineToFyConversion(exchangeRateProvider.getCurrencyConversion(conversionPoLineToFyQuery)).withFyToPoLineConversion(exchangeRateProvider.getCurrencyConversion(conversionFyToPoLineQuery));
List<ReEncumbranceHolder> holders = Arrays.asList(holder1, holder2);
when(purchaseOrderStorageService.getCompositeOrderById(anyString(), eq(requestContext))).thenAnswer(invocation -> completedFuture(new CompositePurchaseOrder().withId(invocation.getArgument(0))));
doReturn(holders).when(spyReEncumbranceHoldersBuilder).buildReEncumbranceHoldersWithOrdersData(any());
doReturn(completedFuture(holders)).when(spyReEncumbranceHoldersBuilder).withFundsData(any(), any());
doReturn(completedFuture(holders)).when(spyReEncumbranceHoldersBuilder).withLedgersData(any(), any());
doReturn(completedFuture(holders)).when(spyReEncumbranceHoldersBuilder).withCurrentFiscalYearData(any(), any());
doReturn(completedFuture(holders)).when(spyReEncumbranceHoldersBuilder).withRollovers(any(), any());
doReturn(holders).when(spyReEncumbranceHoldersBuilder).withEncumbranceRollover(any());
doReturn(completedFuture(holders)).when(spyReEncumbranceHoldersBuilder).withBudgets(any(), any());
doReturn(completedFuture(holders)).when(spyReEncumbranceHoldersBuilder).withConversions(any(), any());
doReturn(completedFuture(holders)).when(spyReEncumbranceHoldersBuilder).withPreviousFyEncumbrances(any(), any());
doReturn(completedFuture(holders)).when(spyReEncumbranceHoldersBuilder).withToEncumbrances(any(), any());
doNothing().when(budgetRestrictionService).checkEncumbranceRestrictions(anyList());
when(rolloverErrorService.getLedgerFyRolloverErrors(anyString(), any())).thenReturn(completedFuture(new LedgerFiscalYearRolloverErrorCollection()));
when(rolloverErrorService.deleteRolloverErrors(anyList(), any())).thenReturn(completedFuture(null));
when(purchaseOrderLineService.saveOrderLines(anyList(), any())).thenReturn(completedFuture(null));
when(rolloverRetrieveService.getRolloversProgress(eq(rolloverId), any())).thenReturn(completedFuture(Collections.singletonList(success)));
when(exchangeRateProviderResolver.resolve(conversionPoLineToFyQuery, requestContext)).thenReturn(exchangeRateProvider);
when(exchangeRateProviderResolver.resolve(conversionFyToPoLineQuery, requestContext)).thenReturn(exchangeRateProvider);
when(transactionService.getTransactions(anyString(), eq(0), eq(Integer.MAX_VALUE), eq(requestContext))).thenReturn(completedFuture(toTransactionCollection));
when(transactionSummaryService.updateOrderTransactionSummary(eq(orderId), anyInt(), eq(requestContext))).thenReturn(completedFuture(null));
when(transactionService.createTransaction(any(), eq(requestContext))).thenReturn(completedFuture(new Transaction()));
// When
CompletableFuture<Void> future = orderReEncumberService.reEncumber(UUID.randomUUID().toString(), requestContext);
future.join();
// Then
assertFalse(future.isCompletedExceptionally());
assertEquals(-35.3d, line1.getCost().getFyroAdjustmentAmount());
assertEquals(50d, line1.getFundDistribution().get(0).getValue());
assertEquals(42.35d, line1.getFundDistribution().get(1).getValue());
}
Aggregations