use of org.folio.models.InvoiceWorkflowDataHolder in project mod-invoice by folio-org.
the class PaymentCreditWorkflowService method createTransactions.
private CompletionStage<Void> createTransactions(List<InvoiceWorkflowDataHolder> holders, RequestContext requestContext) {
CompletableFuture<Void> future = completedFuture(null);
for (InvoiceWorkflowDataHolder holder : holders) {
Transaction tr = holder.getNewTransaction();
future = future.thenCompose(v -> baseTransactionService.createTransaction(tr, requestContext).thenAccept(t -> {
}).exceptionally(t -> {
logger.error("Failed to create transaction for invoice with id - {}", tr.getSourceInvoiceId(), t);
List<Parameter> parameters = new ArrayList<>();
parameters.add(new Parameter().withKey("invoiceLineId").withValue(tr.getSourceInvoiceLineId()));
parameters.add(new Parameter().withKey(FUND_ID).withValue((tr.getTransactionType() == Transaction.TransactionType.PAYMENT) ? tr.getFromFundId() : tr.getToFundId()));
throw new HttpException(400, TRANSACTION_CREATION_FAILURE.toError().withParameters(parameters));
}));
}
return future;
}
use of org.folio.models.InvoiceWorkflowDataHolder in project mod-invoice by folio-org.
the class PendingPaymentWorkflowService method handlePendingPaymentsCreation.
public CompletableFuture<Void> handlePendingPaymentsCreation(List<InvoiceWorkflowDataHolder> dataHolders, RequestContext requestContext) {
List<InvoiceWorkflowDataHolder> holders = withNewPendingPayments(dataHolders);
holderValidator.validate(holders);
InvoiceTransactionSummary summary = buildInvoiceTransactionsSummary(holders);
return invoiceTransactionSummaryService.createInvoiceTransactionSummary(summary, requestContext).thenCompose(s -> createPendingPayments(holders, requestContext)).thenCompose(s -> cleanupOldEncumbrances(holders, requestContext));
}
use of org.folio.models.InvoiceWorkflowDataHolder in project mod-invoice by folio-org.
the class PendingPaymentWorkflowService method handlePendingPaymentsUpdate.
public CompletableFuture<Void> handlePendingPaymentsUpdate(List<InvoiceWorkflowDataHolder> dataHolders, RequestContext requestContext) {
List<InvoiceWorkflowDataHolder> holders = withNewPendingPayments(dataHolders);
holderValidator.validate(holders);
InvoiceTransactionSummary invoiceTransactionSummary = buildInvoiceTransactionsSummary(holders);
return invoiceTransactionSummaryService.updateInvoiceTransactionSummary(invoiceTransactionSummary, requestContext).thenCompose(aVoid -> updateTransactions(holders, requestContext));
}
use of org.folio.models.InvoiceWorkflowDataHolder in project mod-invoice by folio-org.
the class PendingPaymentWorkflowService method createPendingPayments.
private CompletableFuture<Void> createPendingPayments(List<InvoiceWorkflowDataHolder> holders, RequestContext requestContext) {
CompletableFuture<Void> future = completedFuture(null);
for (InvoiceWorkflowDataHolder holder : holders) {
Transaction pendingPayment = holder.getNewTransaction();
future = future.thenCompose(v -> baseTransactionService.createTransaction(pendingPayment, requestContext).thenAccept(t -> {
}).exceptionally(t -> {
logger.error("Failed to create pending payment with id {}", pendingPayment.getId(), t);
throw new HttpException(400, PENDING_PAYMENT_ERROR.toError());
}));
}
return future;
}
use of org.folio.models.InvoiceWorkflowDataHolder in project mod-invoice by folio-org.
the class PendingPaymentWorkflowServiceTest method updatePendingPayments.
@Test
void updatePendingPayments() {
String fiscalYearId = UUID.randomUUID().toString();
String fundId = UUID.randomUUID().toString();
String invoiceId = UUID.randomUUID().toString();
String invoiceLineId = UUID.randomUUID().toString();
double exchangeRate = 1.3;
FiscalYear fiscalYear = new FiscalYear().withId(fiscalYearId).withCurrency("USD");
Transaction existingInvoiceLineTransaction = new Transaction().withId(UUID.randomUUID().toString()).withCurrency("USD").withFromFundId(fundId).withFiscalYearId(fiscalYearId).withSourceInvoiceId(invoiceId).withSourceInvoiceLineId(invoiceLineId).withAmount(50d);
Transaction existingInvoiceTransaction = new Transaction().withId(UUID.randomUUID().toString()).withCurrency("USD").withFromFundId(fundId).withFiscalYearId(fiscalYearId).withSourceInvoiceId(invoiceId).withAmount(10d);
FundDistribution invoiceFundDistribution = new FundDistribution().withDistributionType(FundDistribution.DistributionType.AMOUNT).withFundId(fundId).withValue(30.5);
Adjustment adjustment = new Adjustment().withFundDistributions(Collections.singletonList(invoiceFundDistribution)).withProrate(Adjustment.Prorate.NOT_PRORATED).withValue(30.5).withType(Adjustment.Type.AMOUNT);
Invoice invoice = new Invoice().withAdjustments(Collections.singletonList(adjustment)).withId(invoiceId).withSubTotal(50d).withExchangeRate(exchangeRate).withCurrency("EUR");
InvoiceLine invoiceLine = new InvoiceLine().withSubTotal(60d).withTotal(60d).withId(invoiceLineId);
FundDistribution invoiceLineFundDistribution = new FundDistribution().withDistributionType(FundDistribution.DistributionType.AMOUNT).withFundId(fundId).withValue(60d);
invoiceLine.getFundDistributions().add(invoiceLineFundDistribution);
ConversionQuery conversionQuery = ConversionQueryBuilder.of().setTermCurrency(DEFAULT_SYSTEM_CURRENCY).set(RATE_KEY, exchangeRate).build();
ExchangeRateProvider exchangeRateProvider = new ExchangeRateProviderResolver().resolve(conversionQuery, new RequestContext(Vertx.currentContext(), Collections.emptyMap()));
CurrencyConversion conversion = exchangeRateProvider.getCurrencyConversion(conversionQuery);
List<InvoiceWorkflowDataHolder> holders = new ArrayList<>();
InvoiceWorkflowDataHolder holder1 = new InvoiceWorkflowDataHolder().withInvoice(invoice).withInvoiceLine(invoiceLine).withFundDistribution(invoiceLineFundDistribution).withFiscalYear(fiscalYear).withExistingTransaction(existingInvoiceLineTransaction).withConversion(conversion);
InvoiceWorkflowDataHolder holder2 = new InvoiceWorkflowDataHolder().withInvoice(invoice).withAdjustment(adjustment).withFundDistribution(invoiceFundDistribution).withFiscalYear(fiscalYear).withExistingTransaction(existingInvoiceTransaction).withConversion(conversion);
holders.add(holder1);
holders.add(holder2);
doNothing().when(fundAvailabilityValidator).validate(anyList());
when(invoiceTransactionSummaryService.updateInvoiceTransactionSummary(any(), any())).thenReturn(CompletableFuture.completedFuture(null));
when(baseTransactionService.updateTransaction(any(), any())).thenReturn(CompletableFuture.completedFuture(null));
when(requestContext.getContext()).thenReturn(Vertx.vertx().getOrCreateContext());
pendingPaymentWorkflowService.handlePendingPaymentsUpdate(holders, requestContext);
ArgumentCaptor<List<InvoiceWorkflowDataHolder>> argumentCaptor = ArgumentCaptor.forClass(List.class);
verify(fundAvailabilityValidator).validate(argumentCaptor.capture());
assertThat(argumentCaptor.getValue(), hasSize(2));
List<InvoiceWorkflowDataHolder> holdersWithNewTransactions = argumentCaptor.getValue();
Transaction newInvoiceTransaction = holdersWithNewTransactions.stream().map(InvoiceWorkflowDataHolder::getNewTransaction).filter(transaction -> Objects.isNull(transaction.getSourceInvoiceLineId())).findFirst().get();
Transaction newInvoiceLineTransaction = holdersWithNewTransactions.stream().map(InvoiceWorkflowDataHolder::getNewTransaction).filter(transaction -> Objects.nonNull(transaction.getSourceInvoiceLineId())).findFirst().get();
double expectedInvoiceLineTransactionAmount = BigDecimal.valueOf(60).multiply(BigDecimal.valueOf(exchangeRate)).doubleValue();
assertEquals(expectedInvoiceLineTransactionAmount, newInvoiceLineTransaction.getAmount());
assertEquals(fundId, newInvoiceLineTransaction.getFromFundId());
assertEquals(fiscalYearId, newInvoiceLineTransaction.getFiscalYearId());
assertEquals(invoiceId, newInvoiceLineTransaction.getSourceInvoiceId());
assertEquals(invoiceLineId, newInvoiceLineTransaction.getSourceInvoiceLineId());
assertEquals(Transaction.TransactionType.PENDING_PAYMENT, newInvoiceLineTransaction.getTransactionType());
assertEquals(Transaction.Source.INVOICE, newInvoiceLineTransaction.getSource());
double expectedInvoiceTransactionAmount = BigDecimal.valueOf(30.5).multiply(BigDecimal.valueOf(exchangeRate)).doubleValue();
assertEquals(expectedInvoiceTransactionAmount, newInvoiceTransaction.getAmount());
assertEquals(fundId, newInvoiceTransaction.getFromFundId());
assertEquals(fiscalYearId, newInvoiceTransaction.getFiscalYearId());
assertEquals(invoiceId, newInvoiceTransaction.getSourceInvoiceId());
assertNull(newInvoiceTransaction.getSourceInvoiceLineId());
assertEquals(Transaction.TransactionType.PENDING_PAYMENT, newInvoiceTransaction.getTransactionType());
assertEquals(Transaction.Source.INVOICE, newInvoiceTransaction.getSource());
InvoiceTransactionSummary expectedSummary = new InvoiceTransactionSummary().withId(invoiceId).withNumPaymentsCredits(2).withNumPendingPayments(2);
verify(invoiceTransactionSummaryService).updateInvoiceTransactionSummary(eq(expectedSummary), eq(requestContext));
ArgumentCaptor<Transaction> transactionArgumentCaptor = ArgumentCaptor.forClass(Transaction.class);
verify(baseTransactionService, times(2)).updateTransaction(transactionArgumentCaptor.capture(), eq(requestContext));
Transaction updateArgumentInvoiceTransaction = transactionArgumentCaptor.getAllValues().stream().filter(transaction -> Objects.isNull(transaction.getSourceInvoiceLineId())).findFirst().get();
assertEquals(existingInvoiceTransaction.getId(), updateArgumentInvoiceTransaction.getId());
assertEquals(expectedInvoiceTransactionAmount, updateArgumentInvoiceTransaction.getAmount());
Transaction updateArgumentInvoiceLineTransaction = transactionArgumentCaptor.getAllValues().stream().filter(transaction -> Objects.nonNull(transaction.getSourceInvoiceLineId())).findFirst().get();
assertEquals(existingInvoiceLineTransaction.getId(), updateArgumentInvoiceLineTransaction.getId());
assertEquals(expectedInvoiceLineTransactionAmount, updateArgumentInvoiceLineTransaction.getAmount());
}
Aggregations