Search in sources :

Example 1 with ExchangeRateProviderResolver

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);
}
Also used : VendorRetrieveService(org.folio.services.VendorRetrieveService) RestClient(org.folio.rest.core.RestClient) AddressConverter(org.folio.converters.AddressConverter) InvoiceService(org.folio.services.invoice.InvoiceService) BaseInvoiceService(org.folio.services.invoice.BaseInvoiceService) BaseInvoiceService(org.folio.services.invoice.BaseInvoiceService) JsonObject(io.vertx.core.json.JsonObject) VoucherService(org.folio.rest.impl.VoucherService) ExchangeRateProviderResolver(org.folio.services.exchange.ExchangeRateProviderResolver) InvoiceLinesRetrieveService(org.folio.services.InvoiceLinesRetrieveService) OrderLineService(org.folio.services.order.OrderLineService) VoucherValidator(org.folio.services.validator.VoucherValidator) BatchVoucher(org.folio.rest.jaxrs.model.BatchVoucher) InvoiceLineService(org.folio.services.invoice.InvoiceLineService) ConfigurationService(org.folio.services.configuration.ConfigurationService) RequestContext(org.folio.rest.core.models.RequestContext) OrderService(org.folio.services.order.OrderService) InvoiceRetrieveService(org.folio.services.InvoiceRetrieveService) BatchVoucherExport(org.folio.rest.jaxrs.model.BatchVoucherExport) Test(org.junit.jupiter.api.Test)

Example 2 with ExchangeRateProviderResolver

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();
    });
}
Also used : VendorRetrieveService(org.folio.services.VendorRetrieveService) RestClient(org.folio.rest.core.RestClient) AddressConverter(org.folio.converters.AddressConverter) InvoiceService(org.folio.services.invoice.InvoiceService) BaseInvoiceService(org.folio.services.invoice.BaseInvoiceService) BaseInvoiceService(org.folio.services.invoice.BaseInvoiceService) VoucherService(org.folio.rest.impl.VoucherService) ExchangeRateProviderResolver(org.folio.services.exchange.ExchangeRateProviderResolver) InvoiceLinesRetrieveService(org.folio.services.InvoiceLinesRetrieveService) OrderLineService(org.folio.services.order.OrderLineService) VoucherValidator(org.folio.services.validator.VoucherValidator) BatchVoucher(org.folio.rest.jaxrs.model.BatchVoucher) InvoiceLineService(org.folio.services.invoice.InvoiceLineService) ConfigurationService(org.folio.services.configuration.ConfigurationService) RequestContext(org.folio.rest.core.models.RequestContext) OrderService(org.folio.services.order.OrderService) InvoiceRetrieveService(org.folio.services.InvoiceRetrieveService) BatchVoucherExport(org.folio.rest.jaxrs.model.BatchVoucherExport) Test(org.junit.jupiter.api.Test)

Example 3 with ExchangeRateProviderResolver

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()));
    });
}
Also used : ORDER_LINES(org.folio.invoices.utils.ResourcePathResolver.ORDER_LINES) Arrays(java.util.Arrays) HelperUtils.getNoAcqUnitCQL(org.folio.invoices.utils.HelperUtils.getNoAcqUnitCQL) VOUCHERS_STORAGE(org.folio.invoices.utils.ResourcePathResolver.VOUCHERS_STORAGE) PROTECTED_AND_MODIFIED_FIELDS(org.folio.rest.impl.InvoicesImpl.PROTECTED_AND_MODIFIED_FIELDS) Disabled(org.junit.jupiter.api.Disabled) StringUtils(org.apache.commons.lang3.StringUtils) INVOICE_LINE_WITH_APPROVED_INVOICE_SAMPLE_PATH(org.folio.rest.impl.InvoiceLinesApiTest.INVOICE_LINE_WITH_APPROVED_INVOICE_SAMPLE_PATH) MockServer.getInvoiceLineSearches(org.folio.rest.impl.MockServer.getInvoiceLineSearches) BigDecimal(java.math.BigDecimal) Voucher(org.folio.rest.jaxrs.model.Voucher) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Map(java.util.Map) InvoiceTransactionSummary(org.folio.rest.acq.model.finance.InvoiceTransactionSummary) Is.is(org.hamcrest.core.Is.is) JsonObject(io.vertx.core.json.JsonObject) VOUCHERS_LIST_PATH(org.folio.rest.impl.VouchersApiTest.VOUCHERS_LIST_PATH) Errors(org.folio.rest.jaxrs.model.Errors) Budget(org.folio.rest.acq.model.finance.Budget) Awaitility.await(org.awaitility.Awaitility.await) Status(org.folio.rest.jaxrs.model.Invoice.Status) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Matchers.allOf(org.hamcrest.Matchers.allOf) CompositePoLine(org.folio.rest.acq.model.orders.CompositePoLine) ACQUISITIONS_UNIT_IDS(org.folio.rest.impl.ProtectionHelper.ACQUISITIONS_UNIT_IDS) FOLIO_INVOICE_NUMBER(org.folio.invoices.utils.ResourcePathResolver.FOLIO_INVOICE_NUMBER) Ledger(org.folio.rest.acq.model.finance.Ledger) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Fund(org.folio.rest.acq.model.finance.Fund) FUND_DISTRIBUTIONS_NOT_PRESENT(org.folio.invoices.utils.ErrorCodes.FUND_DISTRIBUTIONS_NOT_PRESENT) VOUCHER_UPDATE_FAILURE(org.folio.invoices.utils.ErrorCodes.VOUCHER_UPDATE_FAILURE) FINANCE_TRANSACTIONS(org.folio.invoices.utils.ResourcePathResolver.FINANCE_TRANSACTIONS) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) ADJUSTMENT_FUND_DISTRIBUTIONS_SUMMARY_MISMATCH(org.folio.invoices.utils.ErrorCodes.ADJUSTMENT_FUND_DISTRIBUTIONS_SUMMARY_MISMATCH) HelperUtils.calculateVoucherAmount(org.folio.invoices.utils.HelperUtils.calculateVoucherAmount) MockServer.addMockEntry(org.folio.rest.impl.MockServer.addMockEntry) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Adjustment(org.folio.rest.jaxrs.model.Adjustment) PREFIX_CONFIG_WITH_NON_EXISTING_VALUE_X_OKAPI_TENANT(org.folio.rest.impl.MockServer.PREFIX_CONFIG_WITH_NON_EXISTING_VALUE_X_OKAPI_TENANT) TEXT_PLAIN(javax.ws.rs.core.MediaType.TEXT_PLAIN) Matchers.containsString(org.hamcrest.Matchers.containsString) Matchers.endsWith(org.hamcrest.Matchers.endsWith) FISCAL_YEAR_ID(org.folio.rest.impl.MockServer.FISCAL_YEAR_ID) Invoice(org.folio.rest.jaxrs.model.Invoice) BUDGETS(org.folio.invoices.utils.ResourcePathResolver.BUDGETS) HelperUtils.convertToDoubleWithRounding(org.folio.invoices.utils.HelperUtils.convertToDoubleWithRounding) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) BUDGET_NOT_FOUND(org.folio.invoices.utils.ErrorCodes.BUDGET_NOT_FOUND) Transaction(org.folio.rest.acq.model.finance.Transaction) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) Every(org.hamcrest.core.Every) Headers(io.restassured.http.Headers) HelperUtils.getAdjustmentFundDistributionAmount(org.folio.invoices.utils.HelperUtils.getAdjustmentFundDistributionAmount) MAX_IDS_FOR_GET_RQ(org.folio.rest.RestConstants.MAX_IDS_FOR_GET_RQ) MockServer.getQueryParams(org.folio.rest.impl.MockServer.getQueryParams) Matchers.hasProperty(org.hamcrest.Matchers.hasProperty) InvoiceLineCollection(org.folio.rest.jaxrs.model.InvoiceLineCollection) HelperUtils.calculateInvoiceLineTotals(org.folio.invoices.utils.HelperUtils.calculateInvoiceLineTotals) MockServer.getInvoiceRetrievals(org.folio.rest.impl.MockServer.getInvoiceRetrievals) FINANCE_CREDITS(org.folio.invoices.utils.ResourcePathResolver.FINANCE_CREDITS) InvoiceProtectedFields(org.folio.invoices.utils.InvoiceProtectedFields) Matchers.hasSize(org.hamcrest.Matchers.hasSize) FieldUtils(org.apache.commons.lang3.reflect.FieldUtils) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Monetary(javax.money.Monetary) FUND_CANNOT_BE_PAID(org.folio.invoices.utils.ErrorCodes.FUND_CANNOT_BE_PAID) NO_INVOICE_LINES_ERROR_MSG(org.folio.services.validator.InvoiceValidator.NO_INVOICE_LINES_ERROR_MSG) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) VoucherLineCollection(org.folio.rest.acq.model.VoucherLineCollection) HasProperty(org.hamcrest.beans.HasProperty) CURRENT_FISCAL_YEAR(org.folio.rest.impl.MockServer.CURRENT_FISCAL_YEAR) AMOUNT(org.folio.rest.jaxrs.model.FundDistribution.DistributionType.AMOUNT) VOUCHER_NUMBER_PREFIX_NOT_ALPHA(org.folio.invoices.utils.ErrorCodes.VOUCHER_NUMBER_PREFIX_NOT_ALPHA) MockServer.getInvoiceSearches(org.folio.rest.impl.MockServer.getInvoiceSearches) Vertx(io.vertx.core.Vertx) IOException(java.io.IOException) ACCOUNTING_CODE_NOT_PRESENT(org.folio.invoices.utils.ErrorCodes.ACCOUNTING_CODE_NOT_PRESENT) MockServer.serverRqRs(org.folio.rest.impl.MockServer.serverRqRs) VoucherLine(org.folio.rest.jaxrs.model.VoucherLine) FUNDS_NOT_FOUND(org.folio.invoices.utils.ErrorCodes.FUNDS_NOT_FOUND) LINE_FUND_DISTRIBUTIONS_SUMMARY_MISMATCH(org.folio.invoices.utils.ErrorCodes.LINE_FUND_DISTRIBUTIONS_SUMMARY_MISMATCH) GENERIC_ERROR_CODE(org.folio.invoices.utils.ErrorCodes.GENERIC_ERROR_CODE) INVALID_PREFIX_CONFIG_X_OKAPI_TENANT(org.folio.rest.impl.MockServer.INVALID_PREFIX_CONFIG_X_OKAPI_TENANT) FINANCE_PAYMENTS(org.folio.invoices.utils.ResourcePathResolver.FINANCE_PAYMENTS) ConversionQueryBuilder(javax.money.convert.ConversionQueryBuilder) INVOICES(org.folio.invoices.utils.ResourcePathResolver.INVOICES) VOUCHER_NOT_FOUND(org.folio.invoices.utils.ErrorCodes.VOUCHER_NOT_FOUND) TEST_PREFIX(org.folio.rest.impl.MockServer.TEST_PREFIX) PO_LINE_UPDATE_FAILURE(org.folio.invoices.utils.ErrorCodes.PO_LINE_UPDATE_FAILURE) Date(java.util.Date) CurrencyUnit(javax.money.CurrencyUnit) IsEqual.equalTo(org.hamcrest.core.IsEqual.equalTo) HttpStatus(org.apache.http.HttpStatus) INVOICE_TRANSACTION_SUMMARIES(org.folio.invoices.utils.ResourcePathResolver.INVOICE_TRANSACTION_SUMMARIES) ExchangeRateProviderResolver(org.folio.services.exchange.ExchangeRateProviderResolver) ERROR_CONFIG_X_OKAPI_TENANT(org.folio.rest.impl.MockServer.ERROR_CONFIG_X_OKAPI_TENANT) Collectors.toMap(java.util.stream.Collectors.toMap) Matchers.everyItem(org.hamcrest.Matchers.everyItem) LEDGERS(org.folio.invoices.utils.ResourcePathResolver.LEDGERS) INVOICE_NUMBER_ERROR_X_OKAPI_TENANT(org.folio.rest.impl.MockServer.INVOICE_NUMBER_ERROR_X_OKAPI_TENANT) APPLICATION_JSON(javax.ws.rs.core.MediaType.APPLICATION_JSON) IsNot.not(org.hamcrest.core.IsNot.not) RoundingMode(java.math.RoundingMode) NON_EXIST_CONFIG_X_OKAPI_TENANT(org.folio.rest.impl.MockServer.NON_EXIST_CONFIG_X_OKAPI_TENANT) InvoiceCollection(org.folio.rest.jaxrs.model.InvoiceCollection) InvoiceLine(org.folio.rest.jaxrs.model.InvoiceLine) Collections.emptyList(java.util.Collections.emptyList) INVOICE_LINES(org.folio.invoices.utils.ResourcePathResolver.INVOICE_LINES) Prorate(org.folio.rest.jaxrs.model.Adjustment.Prorate) Money(org.javamoney.moneta.Money) PERCENTAGE(org.folio.rest.jaxrs.model.FundDistribution.DistributionType.PERCENTAGE) ConversionQuery(javax.money.convert.ConversionQuery) BudgetStatus(org.folio.rest.acq.model.finance.Budget.BudgetStatus) UUID(java.util.UUID) FundDistribution(org.folio.rest.jaxrs.model.FundDistribution) HasPropertyWithValue(org.hamcrest.beans.HasPropertyWithValue) CANNOT_PAY_INVOICE_WITHOUT_APPROVAL(org.folio.invoices.utils.ErrorCodes.CANNOT_PAY_INVOICE_WITHOUT_APPROVAL) Collectors(java.util.stream.Collectors) LOCK_AND_CALCULATED_TOTAL_MISMATCH(org.folio.invoices.utils.ErrorCodes.LOCK_AND_CALCULATED_TOTAL_MISMATCH) PREFIX_CONFIG_WITHOUT_VALUE_X_OKAPI_TENANT(org.folio.rest.impl.MockServer.PREFIX_CONFIG_WITHOUT_VALUE_X_OKAPI_TENANT) INVOICE_LINES_LIST_PATH(org.folio.rest.impl.InvoiceLinesApiTest.INVOICE_LINES_LIST_PATH) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) DEFAULT_SYSTEM_CURRENCY(org.folio.rest.impl.AbstractHelper.DEFAULT_SYSTEM_CURRENCY) HelperUtils.getFundDistributionAmount(org.folio.invoices.utils.HelperUtils.getFundDistributionAmount) List(java.util.List) FUNDS(org.folio.invoices.utils.ResourcePathResolver.FUNDS) MockServer.getRqRsEntries(org.folio.rest.impl.MockServer.getRqRsEntries) AcquisitionsUnitMembershipCollection(org.folio.rest.acq.model.units.AcquisitionsUnitMembershipCollection) FINANCE_PENDING_PAYMENTS(org.folio.invoices.utils.ResourcePathResolver.FINANCE_PENDING_PAYMENTS) ExchangeRateProvider(javax.money.convert.ExchangeRateProvider) Optional(java.util.Optional) ADJUSTMENT_FUND_DISTRIBUTIONS_NOT_PRESENT(org.folio.invoices.utils.ErrorCodes.ADJUSTMENT_FUND_DISTRIBUTIONS_NOT_PRESENT) MonetaryAmount(javax.money.MonetaryAmount) CurrencyConversion(javax.money.convert.CurrencyConversion) MockServer.getAcqUnitsSearches(org.folio.rest.impl.MockServer.getAcqUnitsSearches) HashMap(java.util.HashMap) VoucherCollection(org.folio.rest.jaxrs.model.VoucherCollection) Function(java.util.function.Function) PROHIBITED_FIELD_CHANGING(org.folio.invoices.utils.ErrorCodes.PROHIBITED_FIELD_CHANGING) Type(org.folio.rest.jaxrs.model.Adjustment.Type) VOUCHER_LINES(org.folio.invoices.utils.ResourcePathResolver.VOUCHER_LINES) RATE_KEY(org.folio.services.exchange.ExchangeRateProviderResolver.RATE_KEY) RequestContext(org.folio.rest.core.models.RequestContext) INVOICE(org.folio.invoices.utils.HelperUtils.INVOICE) MockServer.getAcqMembershipsSearches(org.folio.rest.impl.MockServer.getAcqMembershipsSearches) PENDING_PAYMENT_ERROR(org.folio.invoices.utils.ErrorCodes.PENDING_PAYMENT_ERROR) PO_LINE_NOT_FOUND(org.folio.invoices.utils.ErrorCodes.PO_LINE_NOT_FOUND) Matchers.empty(org.hamcrest.Matchers.empty) Matchers(org.hamcrest.Matchers) MockServer.getInvoiceCreations(org.folio.rest.impl.MockServer.getInvoiceCreations) ERROR_X_OKAPI_TENANT(org.folio.rest.impl.MockServer.ERROR_X_OKAPI_TENANT) Error(org.folio.rest.jaxrs.model.Error) UUID.randomUUID(java.util.UUID.randomUUID) Collectors.toList(java.util.stream.Collectors.toList) INVALID_INVOICE_TRANSITION_ON_PAID_STATUS(org.folio.invoices.utils.ErrorCodes.INVALID_INVOICE_TRANSITION_ON_PAID_STATUS) MockServer.getInvoiceUpdates(org.folio.rest.impl.MockServer.getInvoiceUpdates) VOUCHER_NUMBER_STORAGE(org.folio.invoices.utils.ResourcePathResolver.VOUCHER_NUMBER_STORAGE) Response(io.restassured.response.Response) HttpMethod(io.vertx.core.http.HttpMethod) FundCollection(org.folio.rest.acq.model.finance.FundCollection) EXTERNAL_ACCOUNT_NUMBER_IS_MISSING(org.folio.invoices.utils.ErrorCodes.EXTERNAL_ACCOUNT_NUMBER_IS_MISSING) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Invoice(org.folio.rest.jaxrs.model.Invoice) ExchangeRateProvider(javax.money.convert.ExchangeRateProvider) MonetaryAmount(javax.money.MonetaryAmount) InvoiceLine(org.folio.rest.jaxrs.model.InvoiceLine) Headers(io.restassured.http.Headers) JsonObject(io.vertx.core.json.JsonObject) CurrencyConversion(javax.money.convert.CurrencyConversion) Matchers.containsString(org.hamcrest.Matchers.containsString) ExchangeRateProviderResolver(org.folio.services.exchange.ExchangeRateProviderResolver) FundDistribution(org.folio.rest.jaxrs.model.FundDistribution) ConversionQuery(javax.money.convert.ConversionQuery) Transaction(org.folio.rest.acq.model.finance.Transaction) RequestContext(org.folio.rest.core.models.RequestContext) InvoiceTransactionSummary(org.folio.rest.acq.model.finance.InvoiceTransactionSummary) Test(org.junit.jupiter.api.Test)

Example 4 with ExchangeRateProviderResolver

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());
}
Also used : FiscalYear(org.folio.rest.acq.model.finance.FiscalYear) Invoice(org.folio.rest.jaxrs.model.Invoice) ExchangeRateProvider(javax.money.convert.ExchangeRateProvider) Adjustment(org.folio.rest.jaxrs.model.Adjustment) InvoiceLine(org.folio.rest.jaxrs.model.InvoiceLine) ArrayList(java.util.ArrayList) CurrencyConversion(javax.money.convert.CurrencyConversion) ManualCurrencyConversion(org.folio.services.exchange.ManualCurrencyConversion) ExchangeRateProviderResolver(org.folio.services.exchange.ExchangeRateProviderResolver) FundDistribution(org.folio.rest.jaxrs.model.FundDistribution) ConversionQuery(javax.money.convert.ConversionQuery) Transaction(org.folio.rest.acq.model.finance.Transaction) InvoiceWorkflowDataHolder(org.folio.models.InvoiceWorkflowDataHolder) ArrayList(java.util.ArrayList) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) List(java.util.List) RequestContext(org.folio.rest.core.models.RequestContext) InvoiceTransactionSummary(org.folio.rest.acq.model.finance.InvoiceTransactionSummary) Test(org.junit.jupiter.api.Test)

Aggregations

RequestContext (org.folio.rest.core.models.RequestContext)4 ExchangeRateProviderResolver (org.folio.services.exchange.ExchangeRateProviderResolver)4 Test (org.junit.jupiter.api.Test)4 JsonObject (io.vertx.core.json.JsonObject)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ConversionQuery (javax.money.convert.ConversionQuery)2 CurrencyConversion (javax.money.convert.CurrencyConversion)2 ExchangeRateProvider (javax.money.convert.ExchangeRateProvider)2 AddressConverter (org.folio.converters.AddressConverter)2 InvoiceTransactionSummary (org.folio.rest.acq.model.finance.InvoiceTransactionSummary)2 Transaction (org.folio.rest.acq.model.finance.Transaction)2 RestClient (org.folio.rest.core.RestClient)2 VoucherService (org.folio.rest.impl.VoucherService)2 Adjustment (org.folio.rest.jaxrs.model.Adjustment)2 BatchVoucher (org.folio.rest.jaxrs.model.BatchVoucher)2 BatchVoucherExport (org.folio.rest.jaxrs.model.BatchVoucherExport)2 FundDistribution (org.folio.rest.jaxrs.model.FundDistribution)2 Invoice (org.folio.rest.jaxrs.model.Invoice)2 InvoiceLine (org.folio.rest.jaxrs.model.InvoiceLine)2