use of org.folio.services.exchange.ExchangeRateProviderResolver in project mod-invoice by folio-org.
the class BatchVoucherGenerateServiceTest method positiveGenerateBatchVoucherTest.
@Test
public void positiveGenerateBatchVoucherTest() throws IOException, ExecutionException, InterruptedException {
RestClient restClient = new RestClient();
VoucherRetrieveService voucherRetrieveService = new VoucherRetrieveService(restClient);
ConfigurationService configurationService = new ConfigurationService(new RestClient());
VoucherCommandService voucherCommandService = new VoucherCommandService(restClient, new VoucherNumberService(new RestClient()), voucherRetrieveService, new VoucherValidator(), configurationService, new ExchangeRateProviderResolver());
VendorRetrieveService vendorRetrieveService = new VendorRetrieveService(restClient);
AddressConverter addressConverter = AddressConverter.getInstance();
VoucherService voucherService = new VoucherService(voucherRetrieveService, voucherCommandService, vendorRetrieveService, addressConverter);
InvoiceLinesRetrieveService invoiceLinesRetrieveService = new InvoiceLinesRetrieveService(new InvoiceLineService(restClient));
InvoiceLineService invoiceLineService = new InvoiceLineService(new RestClient());
OrderLineService orderLineService = new OrderLineService(restClient);
InvoiceService invoiceService = new BaseInvoiceService(new RestClient(), invoiceLineService, new OrderService(new RestClient(), invoiceLineService, orderLineService));
InvoiceRetrieveService invoiceRetrieveService = new InvoiceRetrieveService(invoiceService);
BatchVoucherGenerateService service = new BatchVoucherGenerateService(okapiHeaders, context, "en", vendorRetrieveService, invoiceRetrieveService, invoiceLinesRetrieveService, voucherService, addressConverter);
BatchVoucherExport batchVoucherExport = new JsonObject(getMockData(BATCH_VOUCHER_EXPORT_SAMPLE_PATH)).mapTo(BatchVoucherExport.class);
CompletableFuture<BatchVoucher> future = service.generateBatchVoucher(batchVoucherExport, new RequestContext(context, okapiHeaders));
BatchVoucher batchVoucher = future.get();
Assertions.assertNotNull(batchVoucher);
}
use of org.folio.services.exchange.ExchangeRateProviderResolver in project mod-invoice by folio-org.
the class BatchVoucherGenerateServiceTest method negativeGetBatchVoucherIfVouchersIsAbsentTest.
@Test
public void negativeGetBatchVoucherIfVouchersIsAbsentTest() {
Assertions.assertThrows(CompletionException.class, () -> {
RestClient restClient = new RestClient();
VoucherRetrieveService voucherRetrieveService = new VoucherRetrieveService(restClient);
ConfigurationService configurationService = new ConfigurationService(restClient);
VoucherCommandService voucherCommandService = new VoucherCommandService(restClient, new VoucherNumberService(restClient), voucherRetrieveService, new VoucherValidator(), configurationService, new ExchangeRateProviderResolver());
VendorRetrieveService vendorRetrieveService = new VendorRetrieveService(restClient);
AddressConverter addressConverter = AddressConverter.getInstance();
VoucherService voucherService = new VoucherService(voucherRetrieveService, voucherCommandService, vendorRetrieveService, addressConverter);
InvoiceLinesRetrieveService invoiceLinesRetrieveService = new InvoiceLinesRetrieveService(new InvoiceLineService(restClient));
InvoiceLineService invoiceLineService = new InvoiceLineService(new RestClient());
OrderLineService orderLineService = new OrderLineService(restClient);
InvoiceService invoiceService = new BaseInvoiceService(new RestClient(), invoiceLineService, new OrderService(new RestClient(), invoiceLineService, orderLineService));
InvoiceRetrieveService invoiceRetrieveService = new InvoiceRetrieveService(invoiceService);
BatchVoucherGenerateService service = new BatchVoucherGenerateService(okapiHeaders, context, "en", vendorRetrieveService, invoiceRetrieveService, invoiceLinesRetrieveService, voucherService, addressConverter);
BatchVoucherExport batchVoucherExport = new BatchVoucherExport();
CompletableFuture<BatchVoucher> future = service.generateBatchVoucher(batchVoucherExport, new RequestContext(context, okapiHeaders));
future.join();
});
}
use of org.folio.services.exchange.ExchangeRateProviderResolver in project mod-invoice by folio-org.
the class InvoicesApiTest method testTransitionToApprovedWithFundDistrEncumbranceId.
@Test
void testTransitionToApprovedWithFundDistrEncumbranceId() {
logger.info("=== Test transition invoice to Approved with links to encumbrances ===");
InvoiceLine invoiceLine = getMockAsJson(INVOICE_LINE_WITH_APPROVED_INVOICE_SAMPLE_PATH).mapTo(InvoiceLine.class);
Invoice reqData = getMockAsJson(REVIEWED_INVOICE_WITH_EXISTING_VOUCHER_SAMPLE_PATH).mapTo(Invoice.class);
reqData.setStatus(Invoice.Status.APPROVED);
reqData.setCurrency("GBP");
reqData.setExchangeRate(1.5d);
String id = reqData.getId();
invoiceLine.setId(UUID.randomUUID().toString());
invoiceLine.setInvoiceId(id);
invoiceLine.setAdjustmentsTotal(0d);
invoiceLine.setAdjustments(emptyList());
invoiceLine.setSubTotal(10d);
FundDistribution fd1 = new FundDistribution().withDistributionType(FundDistribution.DistributionType.PERCENTAGE).withFundId(EXISTING_FUND_ID).withValue(50d).withEncumbrance(UUID.randomUUID().toString());
FundDistribution fd2 = new FundDistribution().withDistributionType(AMOUNT).withFundId(EXISTING_FUND_ID).withValue(3d).withEncumbrance(UUID.randomUUID().toString());
FundDistribution fd3 = new FundDistribution().withDistributionType(AMOUNT).withFundId(EXISTING_FUND_ID).withValue(2d).withEncumbrance(UUID.randomUUID().toString());
List<FundDistribution> fundDistributions = Arrays.asList(fd1, fd2, fd3);
invoiceLine.setFundDistributions(fundDistributions);
addMockEntry(INVOICE_LINES, JsonObject.mapFrom(invoiceLine));
String jsonBody = JsonObject.mapFrom(reqData).encode();
Headers headers = prepareHeaders(X_OKAPI_TENANT, X_OKAPI_TOKEN, X_OKAPI_USER_ID);
verifyPut(String.format(INVOICE_ID_PATH, id), jsonBody, headers, "", 204);
List<JsonObject> invoiceSummariesCreated = serverRqRs.get(INVOICE_TRANSACTION_SUMMARIES, HttpMethod.POST);
List<JsonObject> pendingPaymesCreated = serverRqRs.get(FINANCE_PENDING_PAYMENTS, HttpMethod.POST);
assertThat(invoiceSummariesCreated, hasSize(1));
InvoiceTransactionSummary transactionSummary = invoiceSummariesCreated.get(0).mapTo(InvoiceTransactionSummary.class);
assertThat(transactionSummary.getNumPendingPayments(), is(invoiceLine.getFundDistributions().size()));
assertThat(transactionSummary.getNumPaymentsCredits(), is(invoiceLine.getFundDistributions().size()));
assertThat(pendingPaymesCreated, hasSize(invoiceLine.getFundDistributions().size()));
Map<String, Transaction> pendingPaymentMap = pendingPaymesCreated.stream().map(entries -> entries.mapTo(Transaction.class)).collect(toMap((transaction) -> transaction.getAwaitingPayment().getEncumbranceId(), Function.identity()));
ConversionQuery conversionQuery = ConversionQueryBuilder.of().setTermCurrency(DEFAULT_SYSTEM_CURRENCY).set(RATE_KEY, 1.5).build();
ExchangeRateProvider exchangeRateProvider = new ExchangeRateProviderResolver().resolve(conversionQuery, new RequestContext(Vertx.currentContext(), Collections.emptyMap()));
// currency from MockServer
CurrencyConversion conversion = exchangeRateProvider.getCurrencyConversion(conversionQuery);
fundDistributions.forEach(fundDistribution -> {
MonetaryAmount amount = getFundDistributionAmount(fundDistribution, 10d, reqData.getCurrency()).with(conversion);
assertThat(convertToDoubleWithRounding(amount), is(pendingPaymentMap.get(fundDistribution.getEncumbrance()).getAmount()));
});
}
use of org.folio.services.exchange.ExchangeRateProviderResolver 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