Search in sources :

Example 11 with Adjustment

use of org.folio.rest.jaxrs.model.Adjustment in project mod-invoice by folio-org.

the class InvoicesProratedAdjustmentsTest method testUpdateInvoiceWithOneLineAndOneAdjustmentWithoutAdjChange.

@ParameterizedTest
@CsvSource({ "BY_AMOUNT, AMOUNT", "BY_AMOUNT, PERCENTAGE", "BY_LINE, AMOUNT", "BY_LINE, PERCENTAGE", "BY_QUANTITY, AMOUNT", "BY_QUANTITY, PERCENTAGE" })
public void testUpdateInvoiceWithOneLineAndOneAdjustmentWithoutAdjChange(Adjustment.Prorate prorate, Adjustment.Type type) {
    logger.info("=== Updating invoice with one line and one prorated adjustment but not touching adjustment ===");
    // Prepare data "from storage"
    Invoice invoice = getMockAsJson(OPEN_INVOICE_SAMPLE_PATH).mapTo(Invoice.class).withId(randomUUID().toString());
    Adjustment adjustment = createAdjustment(prorate, type, 15d).withId(randomUUID().toString());
    invoice.setAdjustments(Collections.singletonList(adjustment));
    addMockEntry(INVOICES, invoice);
    InvoiceLine invoiceLine = getMockInvoiceLine(invoice.getId()).withSubTotal(25d).withQuantity(10).withAdjustments(Collections.singletonList(copyObject(adjustment).withAdjustmentId(adjustment.getId()).withId(null)));
    addMockEntry(INVOICE_LINES, invoiceLine);
    // Send update request
    verifyPut(String.format(INVOICE_ID_PATH, invoice.getId()), copyObject(invoice).withBillTo(randomUUID().toString()), "", 204);
    // Verification
    assertThat(getInvoiceUpdates(), hasSize(1));
    assertThat(getInvoiceLineUpdates(), empty());
    /*
     * Calculated 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) ? 15d : 3.75d;
    Invoice invoiceToStorage = getInvoiceUpdates().get(0).mapTo(Invoice.class);
    assertThat(invoiceToStorage.getAdjustments(), hasSize(1));
    assertThat(invoiceToStorage.getAdjustments(), equalTo(invoice.getAdjustments()));
    assertThat(invoiceToStorage.getAdjustmentsTotal(), is(expectedAdjTotal));
}
Also used : Invoice(org.folio.rest.jaxrs.model.Invoice) Adjustment(org.folio.rest.jaxrs.model.Adjustment) InvoiceLine(org.folio.rest.jaxrs.model.InvoiceLine) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 12 with Adjustment

use of org.folio.rest.jaxrs.model.Adjustment in project mod-invoice by folio-org.

the class InvoicesProratedAdjustmentsTest method testUpdateInvoiceWithTwoLinesAddingAmountAdjustmentByQuantity.

@Test
public void testUpdateInvoiceWithTwoLinesAddingAmountAdjustmentByQuantity() {
    logger.info("=== Updating invoice with two lines adding adjustment by quantity ===");
    // 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()).withQuantity(275).withInvoiceLineNumber("n-1");
    addMockEntry(INVOICE_LINES, invoiceLine1);
    InvoiceLine invoiceLine2 = getMockInvoiceLine(invoice.getId()).withQuantity(725).withInvoiceLineNumber("n-2");
    addMockEntry(INVOICE_LINES, invoiceLine2);
    // Prepare request body
    Invoice invoiceBody = copyObject(invoice);
    invoiceBody.getAdjustments().add(createAdjustment(Adjustment.Prorate.BY_QUANTITY, AMOUNT, 10d));
    // Send update request
    verifyPut(String.format(INVOICE_ID_PATH, invoice.getId()), invoiceBody, "", 204);
    // Verification
    assertThat(getInvoiceUpdates(), hasSize(1));
    assertThat(getInvoiceLineUpdates(), hasSize(2));
    Invoice invoiceToStorage = getInvoiceUpdates().get(0).mapTo(Invoice.class);
    assertThat(invoiceToStorage.getAdjustments(), hasSize(1));
    assertThat(invoiceToStorage.getAdjustmentsTotal(), is(10d));
    Adjustment invoiceAdjustment = invoiceToStorage.getAdjustments().get(0);
    assertThat(invoiceAdjustment.getId(), not(isEmptyOrNullString()));
    InvoiceLine lineToStorage1 = getLineToStorageById(invoiceLine1.getId());
    assertThat(lineToStorage1.getAdjustments(), hasSize(1));
    assertThat(lineToStorage1.getAdjustmentsTotal(), is(2.75d));
    Adjustment lineAdjustment1 = lineToStorage1.getAdjustments().get(0);
    verifyInvoiceLineAdjustmentCommon(invoiceAdjustment, lineAdjustment1);
    assertThat(lineAdjustment1.getValue(), is(2.75d));
    InvoiceLine lineToStorage2 = getLineToStorageById(invoiceLine2.getId());
    assertThat(lineToStorage2.getAdjustments(), hasSize(1));
    assertThat(lineToStorage2.getAdjustmentsTotal(), is(7.25d));
    Adjustment lineAdjustment2 = lineToStorage2.getAdjustments().get(0);
    verifyInvoiceLineAdjustmentCommon(invoiceAdjustment, lineAdjustment2);
    assertThat(lineAdjustment2.getValue(), is(7.25d));
}
Also used : Invoice(org.folio.rest.jaxrs.model.Invoice) Adjustment(org.folio.rest.jaxrs.model.Adjustment) InvoiceLine(org.folio.rest.jaxrs.model.InvoiceLine) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 13 with Adjustment

use of org.folio.rest.jaxrs.model.Adjustment in project mod-invoice by folio-org.

the class InvoicesProratedAdjustmentsTest method testUpdateInvoiceWithTwoGiftLinesAddingAdjustmentByAmount.

@Test
public void testUpdateInvoiceWithTwoGiftLinesAddingAdjustmentByAmount() {
    logger.info("=== Updating invoice with two lines (zero subTotal each) adding 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(0d).withInvoiceLineNumber("n-1");
    addMockEntry(INVOICE_LINES, invoiceLine1);
    InvoiceLine invoiceLine2 = getMockInvoiceLine(invoice.getId()).withSubTotal(0d).withInvoiceLineNumber("n-2");
    addMockEntry(INVOICE_LINES, invoiceLine2);
    // Prepare request body
    Invoice invoiceBody = copyObject(invoice);
    invoiceBody.getAdjustments().add(createAdjustment(BY_AMOUNT, AMOUNT, 15d));
    // Send update request
    verifyPut(String.format(INVOICE_ID_PATH, invoice.getId()), invoiceBody, "", 204);
    // Verification
    assertThat(getInvoiceUpdates(), hasSize(1));
    assertThat(getInvoiceLineUpdates(), hasSize(2));
    Invoice invoiceToStorage = getInvoiceUpdates().get(0).mapTo(Invoice.class);
    assertThat(invoiceToStorage.getAdjustments(), hasSize(1));
    Adjustment invoiceAdjustment = invoiceToStorage.getAdjustments().get(0);
    assertThat(invoiceAdjustment.getId(), not(isEmptyOrNullString()));
    // Depending on type either original prorated amount is split across lines adjustments or percent of subtotal is calculated
    double expectedValue = 7.5d;
    assertThat(invoiceToStorage.getAdjustmentsTotal(), is(15d));
    InvoiceLine lineToStorage1 = getLineToStorageById(invoiceLine1.getId());
    assertThat(lineToStorage1.getAdjustments(), hasSize(1));
    assertThat(lineToStorage1.getAdjustmentsTotal(), is(expectedValue));
    Adjustment lineAdjustment1 = lineToStorage1.getAdjustments().get(0);
    verifyInvoiceLineAdjustmentCommon(invoiceAdjustment, lineAdjustment1);
    assertThat(lineAdjustment1.getValue(), is(expectedValue));
    InvoiceLine lineToStorage2 = getLineToStorageById(invoiceLine2.getId());
    assertThat(lineToStorage2.getAdjustments(), hasSize(1));
    assertThat(lineToStorage2.getAdjustmentsTotal(), is(expectedValue));
    Adjustment lineAdjustment2 = lineToStorage2.getAdjustments().get(0);
    verifyInvoiceLineAdjustmentCommon(invoiceAdjustment, lineAdjustment2);
    assertThat(lineAdjustment2.getValue(), is(expectedValue));
}
Also used : Invoice(org.folio.rest.jaxrs.model.Invoice) Adjustment(org.folio.rest.jaxrs.model.Adjustment) InvoiceLine(org.folio.rest.jaxrs.model.InvoiceLine) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 14 with Adjustment

use of org.folio.rest.jaxrs.model.Adjustment in project mod-invoice by folio-org.

the class InvoicesProratedAdjustmentsTest method testUpdateInvoiceWithSevenLinesAddingAmountAdjustmentByLineWith6MissedPennies.

@Test
public void testUpdateInvoiceWithSevenLinesAddingAmountAdjustmentByLineWith6MissedPennies() {
    logger.info("=== Updating invoice with zero subTotal and seven lines (mixed subTotals) adding adjustment by line, missing six \"pennies\" ===");
    // 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);
    List<InvoiceLine> lines = new ArrayList<>();
    for (int i = 0; i < 7; i++) {
        InvoiceLine line = getMockInvoiceLine(invoice.getId()).withInvoiceLineNumber("n-" + (i + 1));
        lines.add(line);
        addMockEntry(INVOICE_LINES, line);
    }
    // Prepare request body
    Invoice invoiceBody = copyObject(invoice);
    invoiceBody.getAdjustments().add(createAdjustment(BY_LINE, AMOUNT, 6d));
    // Send update request
    verifyPut(String.format(INVOICE_ID_PATH, invoice.getId()), invoiceBody, "", 204);
    // Verification
    assertThat(getInvoiceUpdates(), hasSize(1));
    assertThat(getInvoiceLineUpdates(), hasSize(7));
    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(6d));
    Adjustment invoiceAdjustment = invoiceToStorage.getAdjustments().get(0);
    assertThat(invoiceAdjustment.getId(), not(isEmptyOrNullString()));
    // 6 / 7 = 0(.8571428571428571)
    verifyAdjustmentValue(lines.get(0).getId(), 0d);
    for (int i = 1; i < 7; i++) {
        // 6 / 7 = 0(.8571428571428571) + 1
        verifyAdjustmentValue(lines.get(i).getId(), 1d);
    }
}
Also used : Invoice(org.folio.rest.jaxrs.model.Invoice) Adjustment(org.folio.rest.jaxrs.model.Adjustment) InvoiceLine(org.folio.rest.jaxrs.model.InvoiceLine) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 15 with Adjustment

use of org.folio.rest.jaxrs.model.Adjustment in project mod-invoice by folio-org.

the class InvoicesProratedAdjustmentsTest method testUpdateInvoiceWithThreeLinesAddingAmountAdjustmentByAmountWithMissed2Pennies.

@Test
public void testUpdateInvoiceWithThreeLinesAddingAmountAdjustmentByAmountWithMissed2Pennies() {
    logger.info("=== Updating invoice with zero subTotal and three lines (mixed subTotals) adding adjustment by amount, missing one \"penny\" ===");
    // Prepare data "from storage"
    Invoice invoice = getMockAsJson(OPEN_INVOICE_SAMPLE_PATH).mapTo(Invoice.class).withId(randomUUID().toString());
    invoice.getAdjustments().clear();
    invoice.setCurrency("JOD");
    addMockEntry(INVOICES, invoice);
    InvoiceLine invoiceLine1 = getMockInvoiceLine(invoice.getId()).withSubTotal(25d).withInvoiceLineNumber("n-1");
    addMockEntry(INVOICE_LINES, invoiceLine1);
    InvoiceLine invoiceLine2 = getMockInvoiceLine(invoice.getId()).withSubTotal(25d).withInvoiceLineNumber("n-2");
    addMockEntry(INVOICE_LINES, invoiceLine2);
    InvoiceLine invoiceLine3 = getMockInvoiceLine(invoice.getId()).withSubTotal(0d).withInvoiceLineNumber("n-3");
    addMockEntry(INVOICE_LINES, invoiceLine3);
    // Prepare request body
    Invoice invoiceBody = copyObject(invoice);
    invoiceBody.getAdjustments().add(createAdjustment(BY_AMOUNT, AMOUNT, 5.001d));
    // 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(5.001d));
    Adjustment invoiceAdjustment = invoiceToStorage.getAdjustments().get(0);
    assertThat(invoiceAdjustment.getId(), not(isEmptyOrNullString()));
    // 5.001 * 25 / 50 = 2,500(5)
    verifyAdjustmentValue(invoiceLine1.getId(), 2.5d);
    // 5.001 * 25 / 50 = 2,500(5)
    verifyAdjustmentValue(invoiceLine2.getId(), 2.5d);
    // 5.001 * 0 / 50 = 0 + 0.001
    verifyAdjustmentValue(invoiceLine3.getId(), 0.001d);
}
Also used : Invoice(org.folio.rest.jaxrs.model.Invoice) Adjustment(org.folio.rest.jaxrs.model.Adjustment) InvoiceLine(org.folio.rest.jaxrs.model.InvoiceLine) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Adjustment (org.folio.rest.jaxrs.model.Adjustment)40 Invoice (org.folio.rest.jaxrs.model.Invoice)33 InvoiceLine (org.folio.rest.jaxrs.model.InvoiceLine)33 Test (org.junit.jupiter.api.Test)24 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)21 ArrayList (java.util.ArrayList)12 CsvSource (org.junit.jupiter.params.provider.CsvSource)10 FundDistribution (org.folio.rest.jaxrs.model.FundDistribution)7 JsonObject (io.vertx.core.json.JsonObject)6 Errors (org.folio.rest.jaxrs.model.Errors)6 List (java.util.List)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 Headers (io.restassured.http.Headers)4 Collections (java.util.Collections)4 Date (java.util.Date)4 MonetaryAmount (javax.money.MonetaryAmount)4 HttpException (org.folio.invoices.rest.exceptions.HttpException)4 RequestContext (org.folio.rest.core.models.RequestContext)4 Error (org.folio.rest.jaxrs.model.Error)4 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)4