use of org.folio.rest.jaxrs.model.Invoice in project mod-invoice by folio-org.
the class InvoicesApiTest method testInvoiceTransitionApprovedWithZeroDollarAmount.
@Test
void testInvoiceTransitionApprovedWithZeroDollarAmount() {
logger.info("=== Test transition invoice to Approved with 0$ amount ===");
Invoice invoice = getMockAsJson(OPEN_INVOICE_SAMPLE_PATH).mapTo(Invoice.class);
invoice.setTotal(0.0);
String id = invoice.getId();
InvoiceLine invoiceLine = getMinimalContentInvoiceLineWithZeroAmount(id);
invoiceLine.setId(UUID.randomUUID().toString());
FundDistribution amountDistribution = new FundDistribution().withFundId("1d1574f1-9196-4a57-8d1f-3b2e4309eb81").withDistributionType(PERCENTAGE).withValue(100d);
invoiceLine.getFundDistributions().add(amountDistribution);
addMockEntry(INVOICE_LINES, JsonObject.mapFrom(invoiceLine));
invoice.setStatus(Invoice.Status.APPROVED);
String jsonBody = JsonObject.mapFrom(invoice).encode();
Headers headers = prepareHeaders(X_OKAPI_URL, X_OKAPI_TENANT, X_OKAPI_TOKEN, X_OKAPI_USER_ID);
verifyPut(String.format(INVOICE_ID_PATH, id), jsonBody, headers, "", 204);
// Verify that expected number of external calls made
assertThat(getInvoiceRetrievals(), hasSize(1));
assertThat(getInvoiceLineSearches(), hasSize(1));
Invoice updatedInvoice = serverRqRs.get(INVOICES, HttpMethod.PUT).get(0).mapTo(Invoice.class);
List<JsonObject> vouchersCreated = serverRqRs.get(VOUCHERS_STORAGE, HttpMethod.POST);
assertThat(vouchersCreated, notNullValue());
assertThat(vouchersCreated, hasSize(1));
Voucher voucherCreated = vouchersCreated.get(0).mapTo(Voucher.class);
assertThat(voucherCreated.getVoucherNumber(), equalTo(TEST_PREFIX + VOUCHER_NUMBER_VALUE));
assertThat(voucherCreated.getSystemCurrency(), equalTo(DEFAULT_SYSTEM_CURRENCY));
verifyTransitionToApproved(voucherCreated, Collections.singletonList(invoiceLine), updatedInvoice, 1);
checkVoucherAcqUnitIdsList(voucherCreated, invoice);
}
use of org.folio.rest.jaxrs.model.Invoice in project mod-invoice by folio-org.
the class InvoicesApiTest method testTransitionFromOpenToApprovedWithNotEnoughMoney.
@Test
void testTransitionFromOpenToApprovedWithNotEnoughMoney() {
Invoice reqData = getMockAsJson(OPEN_INVOICE_SAMPLE_PATH).mapTo(Invoice.class);
String id = reqData.getId();
InvoiceLine invoiceLine = getMinimalContentInvoiceLine(id);
invoiceLine.setSubTotal(60d);
invoiceLine.setId(UUID.randomUUID().toString());
FundDistribution amountDistribution = new FundDistribution().withFundId(FUND_ID_WITH_NOT_ENOUGH_AMOUNT_IN_BUDGET).withDistributionType(AMOUNT).withValue(60d);
invoiceLine.getFundDistributions().add(amountDistribution);
addMockEntry(INVOICE_LINES, JsonObject.mapFrom(invoiceLine));
reqData.setStatus(Invoice.Status.APPROVED);
String jsonBody = JsonObject.mapFrom(reqData).encode();
Headers headers = prepareHeaders(X_OKAPI_URL, X_OKAPI_TENANT, X_OKAPI_TOKEN, X_OKAPI_USER_ID);
Errors errors = verifyPut(String.format(INVOICE_ID_PATH, id), jsonBody, headers, APPLICATION_JSON, 422).as(Errors.class);
assertThat(errors.getErrors().get(0).getCode(), equalTo(FUND_CANNOT_BE_PAID.getCode()));
assertThat(errors.getErrors().get(0).getMessage(), equalTo(FUND_CANNOT_BE_PAID.getDescription()));
}
use of org.folio.rest.jaxrs.model.Invoice in project mod-invoice by folio-org.
the class InvoicesApiTest method testPostInvoicingInvoices.
@Test
void testPostInvoicingInvoices() throws Exception {
logger.info("=== Test create invoice without id and folioInvoiceNo ===");
String body = getMockData(APPROVED_INVOICE_SAMPLE_PATH);
final Invoice respData = verifyPostResponse(INVOICE_PATH, body, prepareHeaders(X_OKAPI_TENANT), APPLICATION_JSON, 201).as(Invoice.class);
String poId = respData.getId();
String folioInvoiceNo = respData.getFolioInvoiceNo();
assertThat(poId, notNullValue());
assertThat(folioInvoiceNo, notNullValue());
assertThat(getRqRsEntries(HttpMethod.GET, FOLIO_INVOICE_NUMBER), hasSize(1));
// Check that invoice in the response and the one in storage are the same
compareRecordWithSentToStorage(respData);
}
use of org.folio.rest.jaxrs.model.Invoice in project mod-invoice by folio-org.
the class InvoicesApiTest method testTransitionToApprovedWithoutFundDistrEncumbranceId.
@Test
void testTransitionToApprovedWithoutFundDistrEncumbranceId() {
logger.info("=== Test transition invoice to Approved without 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);
String id = reqData.getId();
invoiceLine.setId(UUID.randomUUID().toString());
invoiceLine.setInvoiceId(id);
invoiceLine.getFundDistributions().forEach(fundDistribution -> fundDistribution.setEncumbrance(null));
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> pendingPaymentsCreated = serverRqRs.get(FINANCE_PENDING_PAYMENTS, HttpMethod.POST);
assertThat(invoiceSummariesCreated, hasSize(1));
InvoiceTransactionSummary transactionSummary = invoiceSummariesCreated.get(0).mapTo(InvoiceTransactionSummary.class);
int numPendingPayments = invoiceLine.getFundDistributions().size();
assertThat(transactionSummary.getNumPendingPayments(), is(numPendingPayments));
assertThat(transactionSummary.getNumPaymentsCredits(), is(numPendingPayments));
assertThat(pendingPaymentsCreated, hasSize(numPendingPayments));
}
use of org.folio.rest.jaxrs.model.Invoice in project mod-invoice by folio-org.
the class InvoicesApiTest method testInvoiceTransitionApprovedWithOddNumberOfPennies.
@Test
void testInvoiceTransitionApprovedWithOddNumberOfPennies() {
logger.info("=== Test transition invoice to Approved with odd number of pennies ===");
InvoiceLine invoiceLine = getMockAsJson(INVOICE_LINES_LIST_PATH).mapTo(InvoiceLineCollection.class).getInvoiceLines().get(0);
Invoice invoice = getMockAsJson(OPEN_INVOICE_SAMPLE_PATH).mapTo(Invoice.class);
invoice.getAdjustments().clear();
invoice.getAdjustments().add(createAdjustment(Prorate.BY_LINE, Type.AMOUNT, 4d));
String id = invoice.getId();
invoiceLine.setId(UUID.randomUUID().toString());
invoiceLine.setInvoiceId(id);
var conversionFactor = 3d;
invoice.setExchangeRate(conversionFactor);
List<FundDistribution> fundDistrList = new ArrayList<>();
invoiceLine.setSubTotal(20.01d);
invoiceLine.getAdjustments().clear();
fundDistrList.add(new FundDistribution().withFundId("1d1574f1-9196-4a57-8d1f-3b2e4309eb81").withDistributionType(PERCENTAGE).withValue(50d));
fundDistrList.add(new FundDistribution().withFundId("55f48dc6-efa7-4cfe-bc7c-4786efe493e3").withDistributionType(PERCENTAGE).withValue(50d));
invoiceLine.setFundDistributions(fundDistrList);
addMockEntry(INVOICE_LINES, JsonObject.mapFrom(invoiceLine));
invoice.setStatus(Invoice.Status.APPROVED);
String jsonBody = JsonObject.mapFrom(invoice).encode();
Headers headers = prepareHeaders(X_OKAPI_URL, X_OKAPI_TENANT, X_OKAPI_TOKEN, X_OKAPI_USER_ID);
verifyPut(String.format(INVOICE_ID_PATH, id), jsonBody, headers, "", 204);
// Verify that expected number of external calls made
assertThat(getInvoiceRetrievals(), hasSize(1));
assertThat(getInvoiceLineSearches(), hasSize(1));
Invoice updatedInvoice = serverRqRs.get(INVOICES, HttpMethod.PUT).get(0).mapTo(Invoice.class);
List<JsonObject> vouchersCreated = serverRqRs.get(VOUCHERS_STORAGE, HttpMethod.POST);
assertThat(vouchersCreated, notNullValue());
assertThat(vouchersCreated, hasSize(1));
Voucher voucherCreated = vouchersCreated.get(0).mapTo(Voucher.class);
assertThat(voucherCreated.getVoucherNumber(), equalTo(TEST_PREFIX + VOUCHER_NUMBER_VALUE));
assertThat(voucherCreated.getSystemCurrency(), equalTo(DEFAULT_SYSTEM_CURRENCY));
verifyTransitionToApproved(voucherCreated, Collections.singletonList(invoiceLine), updatedInvoice, 2);
checkVoucherAcqUnitIdsList(voucherCreated, invoice);
List<Transaction> pendingPayments = serverRqRs.get(FINANCE_PENDING_PAYMENTS, HttpMethod.POST).stream().map(transaction -> transaction.mapTo(Transaction.class)).collect(toList());
Double transactionsAmount = pendingPayments.stream().map(tr -> Money.of(tr.getAmount(), tr.getCurrency())).reduce(Money::add).get().getNumber().doubleValue();
InvoiceLine invLineWithRecalculatedTotals = serverRqRs.get(INVOICE_LINES, HttpMethod.PUT).get(0).mapTo(InvoiceLine.class);
assertEquals(invLineWithRecalculatedTotals.getTotal(), transactionsAmount);
// check voucher totals
Voucher voucher = serverRqRs.get(VOUCHERS_STORAGE, HttpMethod.POST).stream().map(transaction -> transaction.mapTo(Voucher.class)).collect(toList()).get(0);
List<VoucherLine> voucherLines = serverRqRs.get(VOUCHER_LINES, HttpMethod.POST).stream().map(transaction -> transaction.mapTo(VoucherLine.class)).collect(toList());
Double voucherLinesTotal = voucherLines.stream().map(vLine -> BigDecimal.valueOf(vLine.getAmount())).reduce(BigDecimal::add).get().doubleValue();
var expectedVoucherAmount = Money.of(invLineWithRecalculatedTotals.getTotal(), invoice.getCurrency()).multiply(conversionFactor).getNumber().doubleValue();
assertEquals(expectedVoucherAmount, voucher.getAmount());
assertEquals(expectedVoucherAmount, voucherLinesTotal);
}
Aggregations