Search in sources :

Example 16 with EncumbranceRelationsHolder

use of org.folio.models.EncumbranceRelationsHolder in project mod-orders by folio-org.

the class OpenToPendingEncumbranceStrategyTest method testShouldSetEncumbrancesToPending.

@Test
void testShouldSetEncumbrancesToPending() {
    // Given
    CompositePurchaseOrder order = getMockAsJson(ORDER_PATH).mapTo(CompositePurchaseOrder.class);
    Transaction encumbrance = getMockAsJson(ENCUMBRANCE_PATH).getJsonArray("transactions").getJsonObject(0).mapTo(Transaction.class);
    doReturn(completedFuture(Collections.singletonList(encumbrance))).when(encumbranceService).getOrderUnreleasedEncumbrances(any(), any());
    doReturn(completedFuture(null)).when(transactionSummariesService).updateOrderTransactionSummary(eq(order.getId()), anyInt(), any());
    doReturn(completedFuture(null)).when(encumbranceService).updateEncumbrances(any(), any());
    List<EncumbranceRelationsHolder> encumbranceRelationsHolders = new ArrayList<>();
    encumbranceRelationsHolders.add(new EncumbranceRelationsHolder().withOldEncumbrance(encumbrance).withCurrentFiscalYearId(UUID.randomUUID().toString()));
    doReturn(new ArrayList<EncumbranceRelationsHolder>()).when(encumbranceRelationsHoldersBuilder).buildBaseHolders(any());
    doReturn(completedFuture(new ArrayList<EncumbranceRelationsHolder>())).when(encumbranceRelationsHoldersBuilder).withBudgets(any(), any());
    doReturn(completedFuture(new ArrayList<EncumbranceRelationsHolder>())).when(encumbranceRelationsHoldersBuilder).withLedgersData(any(), any());
    doReturn(completedFuture(new ArrayList<EncumbranceRelationsHolder>())).when(encumbranceRelationsHoldersBuilder).withFiscalYearData(any(), any());
    doReturn(completedFuture(new ArrayList<EncumbranceRelationsHolder>())).when(encumbranceRelationsHoldersBuilder).withConversion(any(), any());
    doReturn(completedFuture(encumbranceRelationsHolders)).when(encumbranceRelationsHoldersBuilder).withExistingTransactions(any(), any(), any());
    doReturn(completedFuture(encumbranceRelationsHolders)).when(encumbranceRelationsHoldersBuilder).prepareEncumbranceRelationsHolder(any(), any(), any());
    Map<String, List<CompositePoLine>> mapFiscalYearsWithCompPOLines = new HashMap<>();
    String fiscalYearId = UUID.randomUUID().toString();
    mapFiscalYearsWithCompPOLines.put(fiscalYearId, Arrays.asList(new CompositePoLine().withId(UUID.randomUUID().toString())));
    CompositePurchaseOrder orderFromStorage = JsonObject.mapFrom(order).mapTo(CompositePurchaseOrder.class);
    orderFromStorage.setWorkflowStatus(CompositePurchaseOrder.WorkflowStatus.OPEN);
    doReturn(completedFuture(mapFiscalYearsWithCompPOLines)).when(encumbranceRelationsHoldersBuilder).retrieveMapFiscalYearsWithCompPOLines(eq(order), eq(orderFromStorage), eq(requestContext));
    String compositePoLineId = UUID.randomUUID().toString();
    List<CompositePoLine> compositePoLines = Arrays.asList(new CompositePoLine().withId(compositePoLineId));
    List<String> poLineIds = Arrays.asList(compositePoLineId);
    List<Transaction> allTransactions = Arrays.asList(encumbrance);
    doReturn(completedFuture(Arrays.asList(encumbrance))).when(transactionService).getTransactionsByPoLinesIds(eq(poLineIds), eq(fiscalYearId), eq(requestContext));
    doReturn(completedFuture(Arrays.asList(encumbrance))).when(encumbranceService).getCurrentPoLinesEncumbrances(eq(compositePoLines), eq(fiscalYearId), eq(requestContext));
    doReturn(completedFuture(allTransactions)).when(encumbranceService).getEncumbrancesByPoLinesFromCurrentFy(eq(mapFiscalYearsWithCompPOLines), eq(requestContext));
    // When
    openToPendingEncumbranceStrategy.processEncumbrances(order, orderFromStorage, requestContext).join();
    // Then
    assertEquals(0d, encumbrance.getAmount(), 0.0);
    assertEquals(0d, encumbrance.getEncumbrance().getInitialAmountEncumbered(), 0.0);
    assertEquals(Encumbrance.Status.PENDING, encumbrance.getEncumbrance().getStatus());
    assertEquals(Encumbrance.OrderStatus.PENDING, encumbrance.getEncumbrance().getOrderStatus());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CompositePoLine(org.folio.rest.jaxrs.model.CompositePoLine) CompositePurchaseOrder(org.folio.rest.jaxrs.model.CompositePurchaseOrder) Transaction(org.folio.rest.acq.model.finance.Transaction) EncumbranceRelationsHolder(org.folio.models.EncumbranceRelationsHolder) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 17 with EncumbranceRelationsHolder

use of org.folio.models.EncumbranceRelationsHolder in project mod-orders by folio-org.

the class FundsDistributionServiceTest method shouldSubtractExtraPennyFromEncumbrancesRelatedToAmountFundDistributions.

@Test
void shouldSubtractExtraPennyFromEncumbrancesRelatedToAmountFundDistributions() {
    Transaction transaction1 = new Transaction().withEncumbrance(new Encumbrance());
    Transaction transaction2 = new Transaction().withEncumbrance(new Encumbrance());
    Transaction transaction3 = new Transaction().withEncumbrance(new Encumbrance());
    Transaction transaction4 = new Transaction().withEncumbrance(new Encumbrance());
    FundDistribution fundDistribution1 = new FundDistribution().withDistributionType(FundDistribution.DistributionType.AMOUNT).withValue(0.40);
    FundDistribution fundDistribution2 = new FundDistribution().withDistributionType(FundDistribution.DistributionType.PERCENTAGE).withValue(30d);
    FundDistribution fundDistribution3 = new FundDistribution().withDistributionType(FundDistribution.DistributionType.PERCENTAGE).withValue(30d);
    FundDistribution fundDistribution4 = new FundDistribution().withDistributionType(FundDistribution.DistributionType.AMOUNT).withValue(0.39);
    CompositePoLine poLine = new CompositePoLine().withFundDistribution(List.of(fundDistribution1, fundDistribution2, fundDistribution3, fundDistribution4)).withCost(new Cost().withListUnitPrice(1.99).withQuantityPhysical(1).withCurrency("USD"));
    CurrencyConversion conversion = Mockito.mock(ManualCurrencyConversion.class);
    when(conversion.apply(any(MonetaryAmount.class))).thenAnswer(invocation -> invocation.getArgument(0));
    EncumbranceRelationsHolder holder1 = new EncumbranceRelationsHolder().withPoLine(poLine).withNewEncumbrance(transaction1).withFundDistribution(fundDistribution1).withCurrency("USD").withPoLineToFyConversion(conversion);
    EncumbranceRelationsHolder holder2 = new EncumbranceRelationsHolder().withPoLine(poLine).withNewEncumbrance(transaction2).withFundDistribution(fundDistribution2).withCurrency("USD").withPoLineToFyConversion(conversion);
    EncumbranceRelationsHolder holder3 = new EncumbranceRelationsHolder().withPoLine(poLine).withNewEncumbrance(transaction3).withFundDistribution(fundDistribution3).withCurrency("USD").withPoLineToFyConversion(conversion);
    EncumbranceRelationsHolder holder4 = new EncumbranceRelationsHolder().withPoLine(poLine).withNewEncumbrance(transaction4).withFundDistribution(fundDistribution4).withCurrency("USD").withPoLineToFyConversion(conversion);
    List<EncumbranceRelationsHolder> holders = List.of(holder1, holder2, holder3, holder4);
    List<EncumbranceRelationsHolder> resultHolders = distributionService.distributeFunds(holders);
    assertEquals(0.4, resultHolders.get(0).getNewEncumbrance().getAmount());
    assertEquals(0.6, resultHolders.get(1).getNewEncumbrance().getAmount());
    assertEquals(0.6, resultHolders.get(2).getNewEncumbrance().getAmount());
    assertEquals(0.39, resultHolders.get(3).getNewEncumbrance().getAmount());
}
Also used : FundDistribution(org.folio.rest.jaxrs.model.FundDistribution) Transaction(org.folio.rest.acq.model.finance.Transaction) MonetaryAmount(javax.money.MonetaryAmount) Encumbrance(org.folio.rest.acq.model.finance.Encumbrance) EncumbranceRelationsHolder(org.folio.models.EncumbranceRelationsHolder) CompositePoLine(org.folio.rest.jaxrs.model.CompositePoLine) CurrencyConversion(javax.money.convert.CurrencyConversion) ManualCurrencyConversion(org.folio.service.exchange.ManualCurrencyConversion) Cost(org.folio.rest.jaxrs.model.Cost) Test(org.junit.jupiter.api.Test)

Example 18 with EncumbranceRelationsHolder

use of org.folio.models.EncumbranceRelationsHolder in project mod-orders by folio-org.

the class FundsDistributionServiceTest method shouldDistributeFunds.

@Test
void shouldDistributeFunds() {
    Transaction transaction1 = new Transaction().withEncumbrance(new Encumbrance());
    Transaction transaction2 = new Transaction().withEncumbrance(new Encumbrance());
    Transaction transaction3 = new Transaction().withEncumbrance(new Encumbrance());
    Transaction transaction4 = new Transaction().withEncumbrance(new Encumbrance());
    FundDistribution fundDistribution1 = new FundDistribution().withDistributionType(FundDistribution.DistributionType.PERCENTAGE).withValue(30d);
    FundDistribution fundDistribution2 = new FundDistribution().withDistributionType(FundDistribution.DistributionType.PERCENTAGE).withValue(30d);
    FundDistribution fundDistribution3 = new FundDistribution().withDistributionType(FundDistribution.DistributionType.PERCENTAGE).withValue(30d);
    FundDistribution fundDistribution4 = new FundDistribution().withDistributionType(FundDistribution.DistributionType.PERCENTAGE).withValue(10d);
    CompositePoLine poLine = new CompositePoLine().withFundDistribution(List.of(fundDistribution1, fundDistribution2, fundDistribution3, fundDistribution4)).withCost(new Cost().withListUnitPrice(1.94).withQuantityPhysical(1).withCurrency("USD"));
    CurrencyConversion conversion = Mockito.mock(ManualCurrencyConversion.class);
    when(conversion.apply(any(MonetaryAmount.class))).thenAnswer(invocation -> invocation.getArgument(0));
    EncumbranceRelationsHolder holder1 = new EncumbranceRelationsHolder().withPoLine(poLine).withNewEncumbrance(transaction1).withFundDistribution(fundDistribution1).withCurrency("USD").withPoLineToFyConversion(conversion);
    EncumbranceRelationsHolder holder2 = new EncumbranceRelationsHolder().withPoLine(poLine).withNewEncumbrance(transaction2).withFundDistribution(fundDistribution2).withCurrency("USD").withPoLineToFyConversion(conversion);
    EncumbranceRelationsHolder holder3 = new EncumbranceRelationsHolder().withPoLine(poLine).withNewEncumbrance(transaction3).withFundDistribution(fundDistribution3).withCurrency("USD").withPoLineToFyConversion(conversion);
    EncumbranceRelationsHolder holder4 = new EncumbranceRelationsHolder().withPoLine(poLine).withNewEncumbrance(transaction4).withFundDistribution(fundDistribution4).withCurrency("USD").withPoLineToFyConversion(conversion);
    List<EncumbranceRelationsHolder> holders = List.of(holder1, holder2, holder3, holder4);
    List<EncumbranceRelationsHolder> resultHolders = distributionService.distributeFunds(holders);
    assertEquals(0.58, resultHolders.get(0).getNewEncumbrance().getAmount());
    assertEquals(0.58, resultHolders.get(1).getNewEncumbrance().getAmount());
    assertEquals(0.58, resultHolders.get(2).getNewEncumbrance().getAmount());
    assertEquals(0.2, resultHolders.get(3).getNewEncumbrance().getAmount());
}
Also used : FundDistribution(org.folio.rest.jaxrs.model.FundDistribution) Transaction(org.folio.rest.acq.model.finance.Transaction) MonetaryAmount(javax.money.MonetaryAmount) Encumbrance(org.folio.rest.acq.model.finance.Encumbrance) EncumbranceRelationsHolder(org.folio.models.EncumbranceRelationsHolder) CompositePoLine(org.folio.rest.jaxrs.model.CompositePoLine) CurrencyConversion(javax.money.convert.CurrencyConversion) ManualCurrencyConversion(org.folio.service.exchange.ManualCurrencyConversion) Cost(org.folio.rest.jaxrs.model.Cost) Test(org.junit.jupiter.api.Test)

Example 19 with EncumbranceRelationsHolder

use of org.folio.models.EncumbranceRelationsHolder in project mod-orders by folio-org.

the class FundsDistributionServiceTest method shouldDistributeFundsAfterConversion.

@Test
void shouldDistributeFundsAfterConversion() {
    double exchangeRate = 1.99;
    Transaction transaction1 = new Transaction().withEncumbrance(new Encumbrance());
    Transaction transaction2 = new Transaction().withEncumbrance(new Encumbrance());
    Transaction transaction3 = new Transaction().withEncumbrance(new Encumbrance());
    FundDistribution fundDistribution1 = new FundDistribution().withDistributionType(FundDistribution.DistributionType.PERCENTAGE).withValue(30d);
    FundDistribution fundDistribution2 = new FundDistribution().withDistributionType(FundDistribution.DistributionType.PERCENTAGE).withValue(30d);
    FundDistribution fundDistribution3 = new FundDistribution().withDistributionType(FundDistribution.DistributionType.PERCENTAGE).withValue(40d);
    CompositePoLine poLine = new CompositePoLine().withFundDistribution(List.of(fundDistribution1, fundDistribution2, fundDistribution3)).withCost(new Cost().withListUnitPrice(1d).withQuantityPhysical(1).withCurrency("USD"));
    CurrencyConversion conversion = Mockito.mock(ManualCurrencyConversion.class);
    when(conversion.apply(any(MonetaryAmount.class))).thenAnswer(invocation -> {
        MonetaryAmount amount = invocation.getArgument(0);
        return amount.multiply(exchangeRate);
    });
    EncumbranceRelationsHolder holder1 = new EncumbranceRelationsHolder().withPoLine(poLine).withNewEncumbrance(transaction1).withFundDistribution(fundDistribution1).withCurrency("USD").withPoLineToFyConversion(conversion);
    EncumbranceRelationsHolder holder2 = new EncumbranceRelationsHolder().withPoLine(poLine).withNewEncumbrance(transaction2).withFundDistribution(fundDistribution2).withCurrency("USD").withPoLineToFyConversion(conversion);
    EncumbranceRelationsHolder holder3 = new EncumbranceRelationsHolder().withPoLine(poLine).withNewEncumbrance(transaction3).withFundDistribution(fundDistribution3).withCurrency("USD").withPoLineToFyConversion(conversion);
    List<EncumbranceRelationsHolder> holders = List.of(holder1, holder2, holder3);
    List<EncumbranceRelationsHolder> resultHolders = distributionService.distributeFunds(holders);
    assertEquals(0.59, resultHolders.get(0).getNewEncumbrance().getAmount());
    assertEquals(0.6, resultHolders.get(1).getNewEncumbrance().getAmount());
    assertEquals(0.8, resultHolders.get(2).getNewEncumbrance().getAmount());
}
Also used : FundDistribution(org.folio.rest.jaxrs.model.FundDistribution) Transaction(org.folio.rest.acq.model.finance.Transaction) MonetaryAmount(javax.money.MonetaryAmount) Encumbrance(org.folio.rest.acq.model.finance.Encumbrance) EncumbranceRelationsHolder(org.folio.models.EncumbranceRelationsHolder) CompositePoLine(org.folio.rest.jaxrs.model.CompositePoLine) CurrencyConversion(javax.money.convert.CurrencyConversion) ManualCurrencyConversion(org.folio.service.exchange.ManualCurrencyConversion) Cost(org.folio.rest.jaxrs.model.Cost) Test(org.junit.jupiter.api.Test)

Example 20 with EncumbranceRelationsHolder

use of org.folio.models.EncumbranceRelationsHolder in project mod-orders by folio-org.

the class BudgetRestrictionServiceTest method checkEnoughMoneyInBudgetShouldThrowFundCannotBePaidIfTransactionsAmountDifferenceGreaterThanBudgetRemainingAmount.

@Test
void checkEnoughMoneyInBudgetShouldThrowFundCannotBePaidIfTransactionsAmountDifferenceGreaterThanBudgetRemainingAmount() {
    String fiscalYearId = UUID.randomUUID().toString();
    String fundId = UUID.randomUUID().toString();
    String budgetId = UUID.randomUUID().toString();
    String fundCode = "TEST-FUND";
    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).withTotalFunding(59d).withAvailable(9d).withUnavailable(50d).withAwaitingPayment(50D).withAllowableEncumbrance(100d);
    List<EncumbranceRelationsHolder> holders = new ArrayList<>();
    FundDistribution fundDistribution = new FundDistribution().withFundId(fundId).withCode(fundCode);
    EncumbranceRelationsHolder holder = new EncumbranceRelationsHolder().withOldEncumbrance(existingTransaction).withNewEncumbrance(newTransaction).withBudget(budget).withRestrictEncumbrances(true).withCurrentFiscalYearId(fiscalYearId).withCurrency("USD").withFundDistribution(fundDistribution);
    holders.add(holder);
    HttpException httpException = assertThrows(HttpException.class, () -> restrictionService.checkEncumbranceRestrictions(holders));
    assertEquals(422, httpException.getCode());
    Error error = httpException.getError();
    assertEquals(FUND_CANNOT_BE_PAID.getCode(), error.getCode());
    assertEquals(Collections.singletonList(fundCode).toString(), error.getParameters().get(0).getValue());
}
Also used : FundDistribution(org.folio.rest.jaxrs.model.FundDistribution) Transaction(org.folio.rest.acq.model.finance.Transaction) EncumbranceRelationsHolder(org.folio.models.EncumbranceRelationsHolder) ArrayList(java.util.ArrayList) Error(org.folio.rest.jaxrs.model.Error) Budget(org.folio.rest.acq.model.finance.Budget) HttpException(org.folio.rest.core.exceptions.HttpException) Test(org.junit.jupiter.api.Test)

Aggregations

EncumbranceRelationsHolder (org.folio.models.EncumbranceRelationsHolder)24 Transaction (org.folio.rest.acq.model.finance.Transaction)18 Test (org.junit.jupiter.api.Test)15 ArrayList (java.util.ArrayList)13 FundDistribution (org.folio.rest.jaxrs.model.FundDistribution)12 Budget (org.folio.rest.acq.model.finance.Budget)11 Encumbrance (org.folio.rest.acq.model.finance.Encumbrance)10 CompositePoLine (org.folio.rest.jaxrs.model.CompositePoLine)9 Objects (java.util.Objects)8 List (java.util.List)7 MonetaryAmount (javax.money.MonetaryAmount)7 Map (java.util.Map)6 CurrencyConversion (javax.money.convert.CurrencyConversion)6 Cost (org.folio.rest.jaxrs.model.Cost)6 Optional (java.util.Optional)5 Collectors (java.util.stream.Collectors)5 Collectors.toList (java.util.stream.Collectors.toList)5 CompositePurchaseOrder (org.folio.rest.jaxrs.model.CompositePurchaseOrder)5 Collectors.groupingBy (java.util.stream.Collectors.groupingBy)4 Fund (org.folio.rest.acq.model.finance.Fund)4