Search in sources :

Example 11 with Budget

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));
}
Also used : Transaction(org.folio.rest.acq.model.finance.Transaction) Fund(org.folio.rest.acq.model.finance.Fund) Ledger(org.folio.rest.acq.model.finance.Ledger) EncumbranceRelationsHolder(org.folio.models.EncumbranceRelationsHolder) ArrayList(java.util.ArrayList) Budget(org.folio.rest.acq.model.finance.Budget) Test(org.junit.jupiter.api.Test)

Example 12 with Budget

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)));
    }
}
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) MonetaryAmount(javax.money.MonetaryAmount) EncumbranceRelationsHolder(org.folio.models.EncumbranceRelationsHolder) Parameter(org.folio.rest.jaxrs.model.Parameter) Budget(org.folio.rest.acq.model.finance.Budget) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) HttpException(org.folio.rest.core.exceptions.HttpException)

Example 13 with Budget

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);
}
Also used : BudgetCollection(org.folio.rest.acq.model.finance.BudgetCollection) JsonObject(io.vertx.core.json.JsonObject) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Budget(org.folio.rest.acq.model.finance.Budget) JsonObject(io.vertx.core.json.JsonObject) IOException(java.io.IOException)

Example 14 with Budget

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());
}
Also used : EncumbranceRelationsHolder(org.folio.models.EncumbranceRelationsHolder) ArrayList(java.util.ArrayList) Budget(org.folio.rest.acq.model.finance.Budget) Test(org.junit.jupiter.api.Test)

Example 15 with Budget

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());
}
Also used : EncumbranceRollover(org.folio.rest.jaxrs.model.EncumbranceRollover) TransactionCollection(org.folio.rest.acq.model.finance.TransactionCollection) LedgerFiscalYearRolloverErrorCollection(org.folio.rest.acq.model.finance.LedgerFiscalYearRolloverErrorCollection) Fund(org.folio.rest.acq.model.finance.Fund) Ledger(org.folio.rest.acq.model.finance.Ledger) ReEncumbranceHolder(org.folio.models.ReEncumbranceHolder) CompositePoLine(org.folio.rest.jaxrs.model.CompositePoLine) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) LedgerFiscalYearRollover(org.folio.rest.jaxrs.model.LedgerFiscalYearRollover) CompositePurchaseOrder(org.folio.rest.jaxrs.model.CompositePurchaseOrder) Cost(org.folio.rest.jaxrs.model.Cost) FundDistribution(org.folio.rest.jaxrs.model.FundDistribution) ConversionQuery(javax.money.convert.ConversionQuery) Transaction(org.folio.rest.acq.model.finance.Transaction) Encumbrance(org.folio.rest.acq.model.finance.Encumbrance) Budget(org.folio.rest.acq.model.finance.Budget) Test(org.junit.jupiter.api.Test)

Aggregations

Budget (org.folio.rest.acq.model.finance.Budget)26 Test (org.junit.jupiter.api.Test)19 ArrayList (java.util.ArrayList)17 Fund (org.folio.rest.acq.model.finance.Fund)14 Transaction (org.folio.rest.acq.model.finance.Transaction)12 FundDistribution (org.folio.rest.jaxrs.model.FundDistribution)10 InvoiceWorkflowDataHolder (org.folio.models.InvoiceWorkflowDataHolder)9 EncumbranceRelationsHolder (org.folio.models.EncumbranceRelationsHolder)8 FiscalYear (org.folio.rest.acq.model.finance.FiscalYear)7 Error (org.folio.rest.jaxrs.model.Error)7 List (java.util.List)6 HttpException (org.folio.invoices.rest.exceptions.HttpException)6 JsonObject (io.vertx.core.json.JsonObject)4 Collections (java.util.Collections)4 Collectors.toList (java.util.stream.Collectors.toList)4 BudgetCollection (org.folio.rest.acq.model.finance.BudgetCollection)4 Ledger (org.folio.rest.acq.model.finance.Ledger)4 Invoice (org.folio.rest.jaxrs.model.Invoice)4 InvoiceLine (org.folio.rest.jaxrs.model.InvoiceLine)4 HttpException (org.folio.rest.core.exceptions.HttpException)3