use of org.folio.models.InvoiceWorkflowDataHolder in project mod-invoice by folio-org.
the class BudgetExpenseClassTest method shouldThrowExceptionWithInactiveExpenseClassCodeWhenCheckExpenseClassesWithInactiveExpenseClass.
@Test
void shouldThrowExceptionWithInactiveExpenseClassCodeWhenCheckExpenseClassesWithInactiveExpenseClass() {
String inactiveExpenseClassId = UUID.randomUUID().toString();
String activeExpenseClassId = UUID.randomUUID().toString();
FundDistribution fundDistributionWithInactiveExpenseClass = new FundDistribution().withFundId(UUID.randomUUID().toString()).withExpenseClassId(inactiveExpenseClassId);
FundDistribution fundDistributionWithActiveExpenseClass = new FundDistribution().withFundId(UUID.randomUUID().toString()).withExpenseClassId(activeExpenseClassId);
FundDistribution fundDistributionWithoutExpenseClass = new FundDistribution().withFundId(UUID.randomUUID().toString());
InvoiceLine invoiceLine = new InvoiceLine().withFundDistributions(Arrays.asList(fundDistributionWithInactiveExpenseClass, fundDistributionWithoutExpenseClass));
List<InvoiceLine> invoiceLines = Collections.singletonList(invoiceLine);
Adjustment adjustment = new Adjustment().withFundDistributions(Collections.singletonList(fundDistributionWithActiveExpenseClass));
Invoice invoice = new Invoice().withAdjustments(Collections.singletonList(adjustment));
BudgetExpenseClass active = new BudgetExpenseClass().withBudgetId(UUID.randomUUID().toString()).withExpenseClassId(activeExpenseClassId).withStatus(BudgetExpenseClass.Status.ACTIVE).withId(UUID.randomUUID().toString());
BudgetExpenseClass inactive = new BudgetExpenseClass().withBudgetId(UUID.randomUUID().toString()).withExpenseClassId(inactiveExpenseClassId).withStatus(BudgetExpenseClass.Status.INACTIVE).withId(UUID.randomUUID().toString());
Fund inactiveExpenseClassFund = new Fund().withCode("inactive fund");
ExpenseClass inactiveExpenseClass = new ExpenseClass().withName("inactive class");
List<InvoiceWorkflowDataHolder> holders = new ArrayList<>();
InvoiceWorkflowDataHolder holder1 = new InvoiceWorkflowDataHolder().withInvoice(invoice).withInvoiceLine(invoiceLine).withFundDistribution(fundDistributionWithInactiveExpenseClass).withExpenseClass(inactiveExpenseClass).withBudget(new Budget().withFundId(UUID.randomUUID().toString())).withFund(inactiveExpenseClassFund);
InvoiceWorkflowDataHolder holder2 = new InvoiceWorkflowDataHolder().withInvoice(invoice).withInvoiceLine(invoiceLine).withBudget(new Budget().withFundId(UUID.randomUUID().toString())).withFundDistribution(fundDistributionWithoutExpenseClass);
InvoiceWorkflowDataHolder holder3 = new InvoiceWorkflowDataHolder().withInvoice(invoice).withAdjustment(adjustment).withFundDistribution(fundDistributionWithActiveExpenseClass).withBudget(new Budget().withFundId(UUID.randomUUID().toString())).withExpenseClass(new ExpenseClass().withId(activeExpenseClassId));
holders.add(holder1);
holders.add(holder2);
holders.add(holder3);
when(restClient.get(any(RequestEntry.class), any(), any())).thenAnswer(invocation -> {
RequestEntry requestEntry = invocation.getArgument(0);
BudgetExpenseClass bec = requestEntry.buildEndpoint().contains(activeExpenseClassId) ? active : inactive;
return CompletableFuture.completedFuture(new BudgetExpenseClassCollection().withBudgetExpenseClasses(Collections.singletonList(bec)).withTotalRecords(1));
});
when(requestContext.getContext()).thenReturn(Vertx.vertx().getOrCreateContext());
CompletableFuture<List<InvoiceWorkflowDataHolder>> future = budgetExpenseClassService.checkExpenseClasses(holders, requestContext);
ExecutionException executionException = assertThrows(ExecutionException.class, future::get);
assertThat(executionException.getCause(), instanceOf(HttpException.class));
HttpException exception = (HttpException) executionException.getCause();
assertEquals(400, exception.getCode());
Errors errors = exception.getErrors();
Error error = errors.getErrors().get(0);
assertEquals(INACTIVE_EXPENSE_CLASS.getCode(), error.getCode());
String expenseClassNameFromError = error.getParameters().stream().filter(parameter -> parameter.getKey().equals(EXPENSE_CLASS_NAME)).findFirst().get().getValue();
assertEquals(inactiveExpenseClass.getName(), expenseClassNameFromError);
String fundCodeFromError = error.getParameters().stream().filter(parameter -> parameter.getKey().equals(FUND_CODE)).findFirst().get().getValue();
assertEquals(inactiveExpenseClassFund.getCode(), fundCodeFromError);
}
use of org.folio.models.InvoiceWorkflowDataHolder in project mod-invoice by folio-org.
the class FundAvailabilityHolderValidator method calculateTotalExpendedAmount.
private MonetaryAmount calculateTotalExpendedAmount(List<InvoiceWorkflowDataHolder> dataHolders) {
CurrencyUnit currency = Monetary.getCurrency(dataHolders.get(0).getFyCurrency());
return dataHolders.stream().map(holder -> {
MonetaryAmount newTransactionAmount = Money.of(holder.getNewTransaction().getAmount(), holder.getFyCurrency());
MonetaryAmount existingTransactionAmount = Optional.ofNullable(holder.getExistingTransaction()).map(transaction -> Money.of(transaction.getAmount(), transaction.getCurrency())).orElseGet(() -> Money.zero(Monetary.getCurrency(holder.getFyCurrency())));
return newTransactionAmount.subtract(existingTransactionAmount);
}).reduce(MonetaryFunctions::sum).orElseGet(() -> Money.zero(currency));
}
use of org.folio.models.InvoiceWorkflowDataHolder in project mod-invoice by folio-org.
the class FundAvailabilityHolderValidator method validate.
@Override
public void validate(List<InvoiceWorkflowDataHolder> dataHolders) {
Map<Budget, List<InvoiceWorkflowDataHolder>> budgetHoldersMap = dataHolders.stream().filter(InvoiceWorkflowDataHolder::isRestrictExpenditures).collect(groupingBy(InvoiceWorkflowDataHolder::getBudget));
Map<String, String> fundHoldersMap = dataHolders.stream().filter(InvoiceWorkflowDataHolder::isRestrictExpenditures).map(InvoiceWorkflowDataHolder::getFund).filter(Objects::nonNull).collect(Collectors.toMap(Fund::getId, Fund::getCode, (fundEntityKey, fundEntityDupKey) -> fundEntityKey));
List<String> failedBudgetIds = budgetHoldersMap.entrySet().stream().filter(entry -> Objects.nonNull(entry.getKey().getAllowableExpenditure())).filter(entry -> {
MonetaryAmount newExpendedAmount = calculateNewExpendedAmount(entry.getValue());
MonetaryAmount totalExpendedAmount = calculateTotalExpendedAmount(entry.getValue());
return isRemainingAmountExceed(entry.getKey(), newExpendedAmount, totalExpendedAmount);
}).map(Map.Entry::getKey).map(Budget::getFundId).collect(toList());
if (!failedBudgetIds.isEmpty()) {
Parameter parameter = new Parameter().withKey(FUNDS).withValue(failedBudgetIds.stream().map(fundHoldersMap::get).collect(toList()).toString());
throw new HttpException(422, FUND_CANNOT_BE_PAID.toError().withParameters(Collections.singletonList(parameter)));
}
}
use of org.folio.models.InvoiceWorkflowDataHolder in project mod-invoice by folio-org.
the class FundAvailabilityHolderValidatorTest 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.PENDING_PAYMENT).withAmount(50d).withFiscalYearId(fiscalYearId).withFromFundId(fundId).withCurrency("USD");
Transaction newTransaction = new Transaction().withTransactionType(Transaction.TransactionType.PENDING_PAYMENT).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).withAllowableExpenditure(150d);
Fund fund = new Fund().withId(fundId).withLedgerId(ledgerId).withCode("FC").withFundStatus(Fund.FundStatus.ACTIVE);
Ledger ledger = new Ledger().withId(ledgerId).withRestrictExpenditures(true);
List<InvoiceWorkflowDataHolder> holders = new ArrayList<>();
InvoiceWorkflowDataHolder holder = new InvoiceWorkflowDataHolder().withExistingTransaction(existingTransaction).withNewTransaction(newTransaction).withBudget(budget).withFund(fund).withRestrictExpenditures(ledger.getRestrictExpenditures()).withFiscalYear(new FiscalYear().withId(fiscalYearId).withCurrency("USD"));
holders.add(holder);
assertDoesNotThrow(() -> fundAvailabilityValidator.validate(holders));
}
use of org.folio.models.InvoiceWorkflowDataHolder in project mod-invoice by folio-org.
the class FundAvailabilityHolderValidatorTest method shouldPassValidationWhenBudgetRestrictedAndAmountGreaterThanAvailableAndRequiredAmountEncumbered.
@Test
void shouldPassValidationWhenBudgetRestrictedAndAmountGreaterThanAvailableAndRequiredAmountEncumbered() {
String fiscalYearId = UUID.randomUUID().toString();
String fundId = UUID.randomUUID().toString();
String budgetId = UUID.randomUUID().toString();
String ledgerId = UUID.randomUUID().toString();
FiscalYear fiscalYear = new FiscalYear().withCurrency("USD").withId(fiscalYearId);
Fund fund = new Fund().withId(fundId).withName("TestFund").withLedgerId(ledgerId).withCode("FC").withFundStatus(Fund.FundStatus.ACTIVE);
Budget budget = new Budget().withId(budgetId).withFiscalYearId(fiscalYearId).withFundId(fundId).withAllocated(260d).withTotalFunding(260d).withAvailable(20d).withUnavailable(240d).withEncumbered(200d).withAwaitingPayment(40d).withAllowableExpenditure(100d);
List<InvoiceWorkflowDataHolder> holders = new ArrayList<>();
Transaction adjustmentPendingPayment = new Transaction().withAmount(20d).withCurrency("USD");
Transaction encumbrance = new Transaction().withId(UUID.randomUUID().toString()).withAmount(200d).withCurrency("USD");
Transaction linePendingPayment = new Transaction().withAmount(200d).withAwaitingPayment(new AwaitingPayment().withEncumbranceId(encumbrance.getId()).withReleaseEncumbrance(false)).withCurrency("USD");
InvoiceWorkflowDataHolder holder1 = new InvoiceWorkflowDataHolder().withFund(fund).withBudget(budget).withRestrictExpenditures(true).withFiscalYear(fiscalYear).withNewTransaction(linePendingPayment).withEncumbrance(encumbrance);
InvoiceWorkflowDataHolder holder2 = new InvoiceWorkflowDataHolder().withFund(fund).withBudget(budget).withRestrictExpenditures(true).withFiscalYear(fiscalYear).withNewTransaction(adjustmentPendingPayment);
holders.add(holder1);
holders.add(holder2);
assertDoesNotThrow(() -> fundAvailabilityValidator.validate(holders));
}
Aggregations