Search in sources :

Example 26 with Adjustment

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

the class InvoicesProratedAdjustmentsTest method testUpdateInvoiceWithTwoLinesAddingAdjustmentByAmount.

@ParameterizedTest
@ValueSource(strings = { "AMOUNT", "PERCENTAGE" })
public void testUpdateInvoiceWithTwoLinesAddingAdjustmentByAmount(Adjustment.Type type) {
    logger.info("=== Updating invoice with two lines 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(10d).withInvoiceLineNumber("n-1");
    addMockEntry(INVOICE_LINES, invoiceLine1);
    InvoiceLine invoiceLine2 = getMockInvoiceLine(invoice.getId()).withSubTotal(20d).withInvoiceLineNumber("n-2");
    addMockEntry(INVOICE_LINES, invoiceLine2);
    // Prepare request body
    Invoice invoiceBody = copyObject(invoice);
    invoiceBody.getAdjustments().add(createAdjustment(BY_AMOUNT, type, 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 expectedAdjTotal1 = type == AMOUNT ? 5d : 1.5d;
    double expectedAdjValue1 = type == AMOUNT ? 5d : 1.5d;
    double expectedAdjTotal2 = type == AMOUNT ? 10d : 3d;
    double expectedAdjValue2 = type == AMOUNT ? 10d : 3d;
    assertThat(invoiceToStorage.getAdjustmentsTotal(), is(expectedAdjTotal1 + expectedAdjTotal2));
    InvoiceLine lineToStorage1 = getLineToStorageById(invoiceLine1.getId());
    assertThat(lineToStorage1.getAdjustments(), hasSize(1));
    assertThat(lineToStorage1.getAdjustmentsTotal(), is(expectedAdjTotal1));
    Adjustment lineAdjustment1 = lineToStorage1.getAdjustments().get(0);
    verifyInvoiceLineAdjustmentCommon(invoiceAdjustment, lineAdjustment1);
    assertThat(lineAdjustment1.getValue(), is(expectedAdjValue1));
    InvoiceLine lineToStorage2 = getLineToStorageById(invoiceLine2.getId());
    assertThat(lineToStorage2.getAdjustments(), hasSize(1));
    assertThat(lineToStorage2.getAdjustmentsTotal(), is(expectedAdjTotal2));
    Adjustment lineAdjustment2 = lineToStorage2.getAdjustments().get(0);
    verifyInvoiceLineAdjustmentCommon(invoiceAdjustment, lineAdjustment2);
    assertThat(lineAdjustment2.getValue(), is(expectedAdjValue2));
}
Also used : Invoice(org.folio.rest.jaxrs.model.Invoice) Adjustment(org.folio.rest.jaxrs.model.Adjustment) InvoiceLine(org.folio.rest.jaxrs.model.InvoiceLine) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 27 with Adjustment

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

the class InvoicesProratedAdjustmentsTest method testUpdateInvoiceWithOneLineAndOneAdjustmentWithAdjChange.

@ParameterizedTest
@CsvSource({ "BY_AMOUNT, AMOUNT", "BY_AMOUNT, PERCENTAGE", "BY_LINE, AMOUNT", "BY_LINE, PERCENTAGE", "BY_QUANTITY, AMOUNT", "BY_QUANTITY, PERCENTAGE" })
public void testUpdateInvoiceWithOneLineAndOneAdjustmentWithAdjChange(Adjustment.Prorate prorate, Adjustment.Type type) {
    logger.info("=== Updating invoice with one line and one prorated adjustment - updating 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
    Invoice body = copyObject(invoice);
    body.getAdjustments().get(0).setValue(25d);
    verifyPut(String.format(INVOICE_ID_PATH, invoice.getId()), body, "", 204);
    // Verification
    assertThat(getInvoiceUpdates(), hasSize(1));
    assertThat(getInvoiceLineUpdates(), hasSize(1));
    double expectedAdjValue = (type == AMOUNT) ? 25d : 6.25d;
    /*
     * Calculated adjustment value depends of type:
     * "Amount" - this is the same as adjustment value
     * "Percentage" - this is the percentage of subTotal i.e. 25% of 25$ = 6.25$
     */
    double expectedAdjTotal = (type == AMOUNT) ? expectedAdjValue : 6.25d;
    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(1));
    assertThat(lineToStorage.getAdjustmentsTotal(), is(expectedAdjTotal));
    Adjustment lineAdjustment = lineToStorage.getAdjustments().get(0);
    verifyInvoiceLineAdjustmentCommon(invoiceAdjustment, lineAdjustment);
    assertThat(lineAdjustment.getValue(), is(expectedAdjValue));
}
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 28 with Adjustment

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

the class InvoicesProratedAdjustmentsTest method verifyAdjustmentValue.

private void verifyAdjustmentValue(String id, double v) {
    InvoiceLine lineToStorage = getLineToStorageById(id);
    Adjustment adjustment = lineToStorage.getAdjustments().get(0);
    assertThat(adjustment.getValue(), is(v));
}
Also used : Adjustment(org.folio.rest.jaxrs.model.Adjustment) InvoiceLine(org.folio.rest.jaxrs.model.InvoiceLine)

Example 29 with Adjustment

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

the class InvoicesProratedAdjustmentsTest method testUpdateInvoiceWithoutLinesAddingAdjustment.

@ParameterizedTest
@CsvSource({ "BY_AMOUNT, AMOUNT", "BY_AMOUNT, PERCENTAGE", "BY_LINE, AMOUNT", "BY_LINE, PERCENTAGE", "BY_QUANTITY, AMOUNT", "BY_QUANTITY, PERCENTAGE" })
public void testUpdateInvoiceWithoutLinesAddingAdjustment(Adjustment.Prorate prorate, Adjustment.Type type) {
    logger.info("=== Updating invoice without lines ===");
    // Prepare data "from storage"
    Invoice invoice = getMockAsJson(OPEN_INVOICE_SAMPLE_PATH).mapTo(Invoice.class).withId(randomUUID().toString());
    invoice.getAdjustments().clear();
    addMockEntry(INVOICES, invoice);
    // 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(), empty());
    // Prorated adjustment is not applied if no lines available
    Invoice invoiceToStorage = getInvoiceUpdates().get(0).mapTo(Invoice.class);
    assertThat(invoiceToStorage.getAdjustments(), hasSize(1));
    assertThat(invoiceToStorage.getAdjustmentsTotal(), not(0));
    Adjustment adjustment = invoiceToStorage.getAdjustments().get(0);
    assertThat(adjustment.getId(), not(isEmptyOrNullString()));
}
Also used : Invoice(org.folio.rest.jaxrs.model.Invoice) Adjustment(org.folio.rest.jaxrs.model.Adjustment) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 30 with Adjustment

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

the class InvoiceLinesApiTest method testPutInvoicingInvoiceLinesWithProtectedFields.

@Test
public void testPutInvoicingInvoiceLinesWithProtectedFields() throws Exception {
    logger.info("=== Test update invoice line by id with protected fields (all fields set) ===");
    InvoiceLine invoiceLine = getMockAsJson(INVOICE_LINE_WITH_APPROVED_INVOICE_SAMPLE_PATH).mapTo(InvoiceLine.class);
    // Invoice line updated (invoice status = APPROVED) - protected field not modified
    verifyPut(invoiceLine.getId(), invoiceLine, "", HttpStatus.SC_NO_CONTENT);
    MatcherAssert.assertThat(getInvoiceUpdates(), hasSize(0));
    clearServiceInteractions();
    // Invoice line updated (invoice status = OPEN) - protected field not modified
    invoiceLine.setId(INVOICE_LINE_WITH_OPEN_EXISTED_INVOICE_ID);
    verifyPut(invoiceLine.getId(), invoiceLine, "", HttpStatus.SC_NO_CONTENT);
    MatcherAssert.assertThat(getInvoiceUpdates(), hasSize(0));
    clearServiceInteractions();
    // Invoice line updated (invoice status = APPROVED) - all protected fields modified
    InvoiceLine allProtectedFieldsModifiedInvoiceLine = getMockAsJson(INVOICE_LINE_WITH_APPROVED_INVOICE_SAMPLE_PATH).mapTo(InvoiceLine.class);
    invoiceLine.setInvoiceId(INVOICE_LINE_WITH_APPROVED_EXISTED_INVOICE_ID);
    Map<InvoiceLineProtectedFields, Object> allProtectedFieldsModification = new HashMap<>();
    // nested object verification
    // - field of nested object modified
    List<Adjustment> adjustments = invoiceLine.getAdjustments();
    adjustments.get(0).setValue(12345.54321);
    allProtectedFieldsModification.put(InvoiceLineProtectedFields.ADJUSTMENTS, adjustments);
    checkPreventInvoiceLineModificationRule(allProtectedFieldsModifiedInvoiceLine, allProtectedFieldsModification);
    // - total nested object replaced
    adjustments = new ArrayList<>();
    allProtectedFieldsModification.put(InvoiceLineProtectedFields.ADJUSTMENTS, adjustments);
    checkPreventInvoiceLineModificationRule(allProtectedFieldsModifiedInvoiceLine, allProtectedFieldsModification);
    // all other fields
    allProtectedFieldsModification.put(InvoiceLineProtectedFields.INVOICE_ID, UUID.randomUUID().toString());
    allProtectedFieldsModification.put(InvoiceLineProtectedFields.INVOICE_LINE_NUMBER, "123456789");
    allProtectedFieldsModification.put(InvoiceLineProtectedFields.PO_LINE_ID, UUID.randomUUID().toString());
    allProtectedFieldsModification.put(InvoiceLineProtectedFields.PRODUCT_ID, UUID.randomUUID().toString());
    allProtectedFieldsModification.put(InvoiceLineProtectedFields.PRODUCT_ID_TYPE, UUID.randomUUID().toString());
    allProtectedFieldsModification.put(InvoiceLineProtectedFields.QUANTITY, 10);
    allProtectedFieldsModification.put(InvoiceLineProtectedFields.SUBSCRIPTION_INFO, "Tested subscription info");
    allProtectedFieldsModification.put(InvoiceLineProtectedFields.SUBSCRIPTION_START, new Date(System.currentTimeMillis()));
    allProtectedFieldsModification.put(InvoiceLineProtectedFields.SUBSCRIPTION_END, new Date(System.currentTimeMillis()));
    checkPreventInvoiceLineModificationRule(allProtectedFieldsModifiedInvoiceLine, allProtectedFieldsModification);
}
Also used : Adjustment(org.folio.rest.jaxrs.model.Adjustment) InvoiceLine(org.folio.rest.jaxrs.model.InvoiceLine) HashMap(java.util.HashMap) JsonObject(io.vertx.core.json.JsonObject) Date(java.util.Date) InvoiceLineProtectedFields(org.folio.invoices.utils.InvoiceLineProtectedFields) Test(org.junit.jupiter.api.Test)

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