use of org.folio.rest.jaxrs.model.Adjustment in project mod-invoice by folio-org.
the class InvoicesApiTest method testGetInvoiceWithoutLinesButProratedAdjustments.
@Test
void testGetInvoiceWithoutLinesButProratedAdjustments() throws IOException {
logger.info("=== Test Get Invoice without associated invoice lines ===");
// === Preparing invoice for test with random id to make sure no lines exists ===
String id = UUID.randomUUID().toString();
Invoice invoice = new JsonObject(getMockData(OPEN_INVOICE_SAMPLE_PATH)).mapTo(Invoice.class);
invoice.setId(id);
invoice.getAdjustments().forEach(adj -> adj.setProrate(Adjustment.Prorate.BY_LINE));
// Setting totals to verify that they are re-calculated
Adjustment adjustment1 = new Adjustment().withProrate(Prorate.NOT_PRORATED).withDescription("Description").withType(Type.AMOUNT).withValue(50d).withRelationToTotal(Adjustment.RelationToTotal.IN_ADDITION_TO);
FundDistribution fundDistribution1 = new FundDistribution().withDistributionType(AMOUNT).withValue(50d).withFundId(EXISTING_FUND_ID);
adjustment1.getFundDistributions().add(fundDistribution1);
invoice.getAdjustments().add(adjustment1);
addMockEntry(INVOICES, JsonObject.mapFrom(invoice));
// === Run test ===
final Invoice resp = verifySuccessGet(String.format(INVOICE_ID_PATH, id), Invoice.class);
logger.info(JsonObject.mapFrom(resp).encodePrettily());
assertThat(resp.getId(), equalTo(id));
/* The invoice has 2 not prorated adjustments one with fixed amount and another with percentage type */
assertThat(resp.getAdjustmentsTotal(), equalTo(50d));
assertThat(resp.getSubTotal(), equalTo(0d));
assertThat(resp.getTotal(), equalTo(50d));
// Verify that expected number of external calls made
assertThat(getInvoiceRetrievals(), hasSize(1));
assertThat(getInvoiceLineSearches(), hasSize(1));
verifyInvoiceUpdateCalls(0);
}
use of org.folio.rest.jaxrs.model.Adjustment in project mod-invoice by folio-org.
the class InvoicesProratedAdjustmentsTest method testUpdateInvoiceWithThreeLinesAddingAmountAdjustmentByAmount.
@Test
public void testUpdateInvoiceWithThreeLinesAddingAmountAdjustmentByAmount() {
logger.info("=== Updating invoice with zero subTotal and three lines (mixed subTotals) adding 5$ adjustment by amount ===");
// Prepare data "from storage"
Invoice invoice = getMockAsJson(OPEN_INVOICE_SAMPLE_PATH).mapTo(Invoice.class).withId(randomUUID().toString());
invoice.getAdjustments().clear();
addMockEntry(INVOICES, invoice);
InvoiceLine invoiceLine1 = getMockInvoiceLine(invoice.getId()).withSubTotal(5d).withInvoiceLineNumber("n-1").withAdjustments(Collections.singletonList(createAdjustment(NOT_PRORATED, AMOUNT, 10d)));
addMockEntry(INVOICE_LINES, invoiceLine1);
InvoiceLine invoiceLine2 = getMockInvoiceLine(invoice.getId()).withSubTotal(20d).withInvoiceLineNumber("n-2");
addMockEntry(INVOICE_LINES, invoiceLine2);
InvoiceLine invoiceLine3 = getMockInvoiceLine(invoice.getId()).withSubTotal(-25d).withInvoiceLineNumber("n-3");
addMockEntry(INVOICE_LINES, invoiceLine3);
// Prepare request body
Invoice invoiceBody = copyObject(invoice);
invoiceBody.getAdjustments().add(createAdjustment(BY_AMOUNT, AMOUNT, 5d));
// Send update request
verifyPut(String.format(INVOICE_ID_PATH, invoice.getId()), invoiceBody, "", 204);
// Verification
assertThat(getInvoiceUpdates(), hasSize(1));
assertThat(getInvoiceLineUpdates(), hasSize(3));
Invoice invoiceToStorage = getInvoiceUpdates().get(0).mapTo(Invoice.class);
assertThat(invoiceToStorage.getAdjustments(), hasSize(1));
// Prorated adj value + non prorated adj of first line
assertThat(invoiceToStorage.getAdjustmentsTotal(), is(15d));
Adjustment invoiceAdjustment = invoiceToStorage.getAdjustments().get(0);
assertThat(invoiceAdjustment.getId(), not(isEmptyOrNullString()));
InvoiceLine lineToStorage1 = getLineToStorageById(invoiceLine1.getId());
assertThat(lineToStorage1.getAdjustments(), hasSize(2));
assertThat(lineToStorage1.getAdjustmentsTotal(), is(10.5d));
// First adjustment is not prorated
assertThat(lineToStorage1.getAdjustments().get(0).getValue(), is(10d));
// Second adjustment is prorated
Adjustment line1Adjustment2 = lineToStorage1.getAdjustments().get(1);
verifyInvoiceLineAdjustmentCommon(invoiceAdjustment, line1Adjustment2);
assertThat(line1Adjustment2.getValue(), is(0.5d));
InvoiceLine lineToStorage2 = getLineToStorageById(invoiceLine2.getId());
assertThat(lineToStorage2.getAdjustments(), hasSize(1));
assertThat(lineToStorage2.getAdjustmentsTotal(), is(2d));
Adjustment line2Adjustment1 = lineToStorage2.getAdjustments().get(0);
verifyInvoiceLineAdjustmentCommon(invoiceAdjustment, line2Adjustment1);
assertThat(line2Adjustment1.getValue(), is(2d));
InvoiceLine lineToStorage3 = getLineToStorageById(invoiceLine3.getId());
assertThat(lineToStorage3.getAdjustments(), hasSize(1));
assertThat(lineToStorage3.getAdjustmentsTotal(), is(2.5d));
Adjustment line3Adjustment1 = lineToStorage3.getAdjustments().get(0);
verifyInvoiceLineAdjustmentCommon(invoiceAdjustment, line3Adjustment1);
assertThat(line3Adjustment1.getValue(), is(2.5d));
}
use of org.folio.rest.jaxrs.model.Adjustment in project mod-invoice by folio-org.
the class InvoicesProratedAdjustmentsTest method testUpdateInvoiceWithThreeLinesAddingAmountAdjustmentByQuantityWithMissed2JapaneseYen.
@Test
public void testUpdateInvoiceWithThreeLinesAddingAmountAdjustmentByQuantityWithMissed2JapaneseYen() {
logger.info("=== Updating invoice with zero subTotal and three lines adding adjustment by quantity, missing two Japanese yens ===");
// Prepare data "from storage"
Invoice invoice = getMockAsJson(OPEN_INVOICE_SAMPLE_PATH).mapTo(Invoice.class).withId(randomUUID().toString());
invoice.getAdjustments().clear();
// zero decimal places
invoice.setCurrency("JPY");
addMockEntry(INVOICES, invoice);
InvoiceLine invoiceLine1 = getMockInvoiceLine(invoice.getId()).withQuantity(4).withInvoiceLineNumber("n-1");
addMockEntry(INVOICE_LINES, invoiceLine1);
InvoiceLine invoiceLine2 = getMockInvoiceLine(invoice.getId()).withQuantity(6).withInvoiceLineNumber("n-2");
addMockEntry(INVOICE_LINES, invoiceLine2);
InvoiceLine invoiceLine3 = getMockInvoiceLine(invoice.getId()).withQuantity(1).withInvoiceLineNumber("n-3");
addMockEntry(INVOICE_LINES, invoiceLine3);
// Prepare request body
Invoice invoiceBody = copyObject(invoice);
invoiceBody.getAdjustments().add(createAdjustment(BY_QUANTITY, AMOUNT, 5d));
// Send update request
verifyPut(String.format(INVOICE_ID_PATH, invoice.getId()), invoiceBody, "", 204);
// Verification
assertThat(getInvoiceUpdates(), hasSize(1));
assertThat(getInvoiceLineUpdates(), hasSize(3));
Invoice invoiceToStorage = getInvoiceUpdates().get(0).mapTo(Invoice.class);
assertThat(invoiceToStorage.getAdjustments(), hasSize(1));
// Prorated adj value + non prorated adj of first line
assertThat(invoiceToStorage.getAdjustmentsTotal(), is(5d));
Adjustment invoiceAdjustment = invoiceToStorage.getAdjustments().get(0);
assertThat(invoiceAdjustment.getId(), not(isEmptyOrNullString()));
// 5 * 4 / 11 = 1(.818181818181818)
verifyAdjustmentValue(invoiceLine1.getId(), 2d);
// 5 * 6 / 1 = 2(.727272727272727) + 1
verifyAdjustmentValue(invoiceLine2.getId(), 3d);
// 5 * 1 / 11 = 0(.4545454545454545) + 1
verifyAdjustmentValue(invoiceLine3.getId(), 0d);
}
use of org.folio.rest.jaxrs.model.Adjustment in project mod-invoice by folio-org.
the class InvoicesProratedAdjustmentsTest method testUpdateInvoiceWithOneLineAddingAdjustment.
@ParameterizedTest
@CsvSource({ "BY_AMOUNT, AMOUNT", "BY_AMOUNT, PERCENTAGE", "BY_LINE, AMOUNT", "BY_LINE, PERCENTAGE", "BY_QUANTITY, AMOUNT", "BY_QUANTITY, PERCENTAGE" })
public void testUpdateInvoiceWithOneLineAddingAdjustment(Adjustment.Prorate prorate, Adjustment.Type type) {
logger.info("=== Updating invoice with one line adding one prorated adjustment ===");
// Prepare data "from storage"
Invoice invoice = getMockAsJson(OPEN_INVOICE_SAMPLE_PATH).mapTo(Invoice.class).withId(randomUUID().toString());
invoice.getAdjustments().clear();
addMockEntry(INVOICES, invoice);
InvoiceLine line = getMockInvoiceLine(invoice.getId()).withSubTotal(25d).withQuantity(10);
// Add non prorated adjustment
line.setAdjustments(Collections.singletonList(createAdjustment(NOT_PRORATED, AMOUNT, 10d)));
addMockEntry(INVOICE_LINES, line);
// Prepare request body
Invoice invoiceBody = copyObject(invoice);
invoiceBody.getAdjustments().add(createAdjustment(prorate, type, 15d));
// Send update request
verifyPut(String.format(INVOICE_ID_PATH, invoice.getId()), invoiceBody, "", 204);
// Verification
assertThat(getInvoiceUpdates(), hasSize(1));
assertThat(getInvoiceLineUpdates(), hasSize(1));
double expectedProratedAdjValue = (type == AMOUNT) ? 15d : 3.75;
/*
* Calculated prorated adjustment value depends of type:
* "Amount" - this is the same as adjustment value
* "Percentage" - this is the percentage of subTotal i.e. 15% of 25$ = 3.75$
*/
double expectedAdjTotal = (type == AMOUNT) ? 25d : 13.75d;
Invoice invoiceToStorage = getInvoiceUpdates().get(0).mapTo(Invoice.class);
assertThat(invoiceToStorage.getAdjustments(), hasSize(1));
assertThat(invoiceToStorage.getAdjustmentsTotal(), is(expectedAdjTotal));
Adjustment invoiceAdjustment = invoiceToStorage.getAdjustments().get(0);
assertThat(invoiceAdjustment.getId(), not(isEmptyOrNullString()));
InvoiceLine lineToStorage = getInvoiceLineUpdates().get(0).mapTo(InvoiceLine.class);
assertThat(lineToStorage.getAdjustments(), hasSize(2));
assertThat(lineToStorage.getAdjustmentsTotal(), is(expectedAdjTotal));
Adjustment lineAdjustment = lineToStorage.getAdjustments().stream().filter(adj -> adj.getProrate() == NOT_PRORATED && isNotEmpty(adj.getAdjustmentId())).findAny().orElse(null);
assertThat(lineAdjustment, notNullValue());
verifyInvoiceLineAdjustmentCommon(invoiceAdjustment, lineAdjustment);
assertThat(lineAdjustment.getValue(), is(expectedProratedAdjValue));
}
use of org.folio.rest.jaxrs.model.Adjustment in project mod-invoice by folio-org.
the class InvoicesProratedAdjustmentsTest method testUpdateInvoiceWithThreeLinesAddingPercentageAdjustmentByLines.
@Test
public void testUpdateInvoiceWithThreeLinesAddingPercentageAdjustmentByLines() {
logger.info("=== Updating invoice with three lines adding 5% adjustment by lines ===");
// Prepare data "from storage"
Invoice invoice = getMockAsJson(OPEN_INVOICE_SAMPLE_PATH).mapTo(Invoice.class).withId(randomUUID().toString());
invoice.getAdjustments().clear();
addMockEntry(INVOICES, invoice);
InvoiceLine invoiceLine1 = getMockInvoiceLine(invoice.getId()).withSubTotal(10d).withInvoiceLineNumber("n-1");
addMockEntry(INVOICE_LINES, invoiceLine1);
InvoiceLine invoiceLine2 = getMockInvoiceLine(invoice.getId()).withSubTotal(10d).withInvoiceLineNumber("n-2");
addMockEntry(INVOICE_LINES, invoiceLine2);
InvoiceLine invoiceLine3 = getMockInvoiceLine(invoice.getId()).withSubTotal(10d).withInvoiceLineNumber("n-3");
addMockEntry(INVOICE_LINES, invoiceLine3);
// Prepare request body
Invoice invoiceBody = copyObject(invoice);
invoiceBody.getAdjustments().add(createAdjustment(BY_LINE, PERCENTAGE, 5d));
// Send update request
verifyPut(String.format(INVOICE_ID_PATH, invoice.getId()), invoiceBody, "", 204);
// Verification
assertThat(getInvoiceUpdates(), hasSize(1));
assertThat(getInvoiceLineUpdates(), hasSize(3));
Invoice invoiceToStorage = getInvoiceUpdates().get(0).mapTo(Invoice.class);
assertThat(invoiceToStorage.getAdjustments(), hasSize(1));
assertThat(invoiceToStorage.getAdjustmentsTotal(), is(1.5d));
Adjustment invoiceAdjustment = invoiceToStorage.getAdjustments().get(0);
assertThat(invoiceAdjustment.getId(), not(isEmptyOrNullString()));
Stream.of(invoiceLine1.getId(), invoiceLine2.getId(), invoiceLine3.getId()).forEach(id -> {
InvoiceLine lineToStorage = getLineToStorageById(id);
assertThat(lineToStorage.getAdjustments(), hasSize(1));
assertThat(lineToStorage.getAdjustmentsTotal(), is(0.5d));
Adjustment lineAdjustment = lineToStorage.getAdjustments().get(0);
verifyInvoiceLineAdjustmentCommon(invoiceAdjustment, lineAdjustment);
assertThat(lineAdjustment.getValue(), is(0.5d));
});
}
Aggregations