Search in sources :

Example 1 with Adjustment

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

the class BatchedVoucherModelConverter method convertAdjustmentsLines.

private BatchedVoucherType.Adjustments convertAdjustmentsLines(BatchedVoucher batchedVoucher) {
    BatchedVoucherType.Adjustments adjustments = new BatchedVoucherType.Adjustments();
    List<AdjustmentLineType> adjustmentsList = new ArrayList<>();
    for (Adjustment adjustment : batchedVoucher.getAdjustments()) {
        AdjustmentLineType normalizedAdjustment = new AdjustmentLineType();
        normalizedAdjustment.setDescription(adjustment.getDescription());
        normalizedAdjustment.setRelationToTotal(adjustment.getRelationToTotal().value());
        normalizedAdjustment.setProrate(adjustment.getProrate().value());
        normalizedAdjustment.setType(adjustment.getType().value());
        normalizedAdjustment.setValue(adjustment.getValue());
        normalizedAdjustment.setTotalAmount(adjustment.getTotalAmount());
        adjustmentsList.add(normalizedAdjustment);
    }
    adjustments.withAdjustment(adjustmentsList);
    return adjustments;
}
Also used : Adjustment(org.folio.rest.jaxrs.model.Adjustment) ArrayList(java.util.ArrayList) BatchedVoucherType(org.folio.rest.jaxrs.model.jaxb.BatchedVoucherType) AdjustmentLineType(org.folio.rest.jaxrs.model.jaxb.AdjustmentLineType)

Example 2 with Adjustment

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

the class AdjustmentsService method applyAmountTypeProratedAdjustments.

private List<InvoiceLine> applyAmountTypeProratedAdjustments(Adjustment adjustment, List<InvoiceLine> lines, CurrencyUnit currencyUnit, BiFunction<MonetaryAmount, InvoiceLine, MonetaryAmount> prorateFunction) {
    List<InvoiceLine> updatedLines = new ArrayList<>();
    MonetaryAmount expectedAdjustmentTotal = Money.of(adjustment.getValue(), currencyUnit);
    Map<String, MonetaryAmount> lineIdAdjustmentValueMap = calculateAdjValueForEachLine(lines, prorateFunction, expectedAdjustmentTotal);
    MonetaryAmount remainder = expectedAdjustmentTotal.abs().subtract(getActualAdjustmentTotal(lineIdAdjustmentValueMap, currencyUnit).abs());
    int remainderSignum = remainder.signum();
    MonetaryAmount smallestUnit = getSmallestUnit(expectedAdjustmentTotal, remainderSignum);
    for (ListIterator<InvoiceLine> iterator = getIterator(lines, remainderSignum); isIteratorHasNext(iterator, remainderSignum); ) {
        final InvoiceLine line = iteratorNext(iterator, remainderSignum);
        MonetaryAmount amount = lineIdAdjustmentValueMap.get(line.getId());
        if (!remainder.isZero()) {
            amount = amount.add(smallestUnit);
            remainder = remainder.abs().subtract(smallestUnit.abs()).multiply(remainderSignum);
        }
        Adjustment proratedAdjustment = prepareAdjustmentForLine(adjustment);
        proratedAdjustment.setValue(amount.getNumber().doubleValue());
        if (addAdjustmentToLine(line, proratedAdjustment)) {
            updatedLines.add(line);
        }
    }
    return updatedLines;
}
Also used : MonetaryAmount(javax.money.MonetaryAmount) Adjustment(org.folio.rest.jaxrs.model.Adjustment) InvoiceLine(org.folio.rest.jaxrs.model.InvoiceLine) ArrayList(java.util.ArrayList)

Example 3 with Adjustment

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

the class InvoiceLinesProratedAdjustmentsTest method testCreateFirstLineForInvoiceWithOneAdj.

@ParameterizedTest
@CsvSource({ "BY_AMOUNT, AMOUNT", "BY_AMOUNT, PERCENTAGE", "BY_LINE, AMOUNT", "BY_LINE, PERCENTAGE", "BY_QUANTITY, AMOUNT", "BY_QUANTITY, PERCENTAGE" })
public void testCreateFirstLineForInvoiceWithOneAdj(Adjustment.Prorate prorate, Adjustment.Type type) {
    logger.info("=== Creating first line for invoice with one prorated adjustment ===");
    // Prepare data "from storage"
    Invoice invoice = getMockAsJson(OPEN_INVOICE_SAMPLE_PATH).mapTo(Invoice.class).withId(randomUUID().toString());
    Adjustment invoiceAdjustment = createAdjustment(prorate, type, 15d);
    invoice.withAdjustments(Collections.singletonList(invoiceAdjustment));
    addMockEntry(INVOICES, invoice);
    // Prepare request body
    InvoiceLine invoiceLineBody = getMockInvoiceLine(invoice.getId()).withSubTotal(25d).withQuantity(10);
    // Send update request
    InvoiceLine invoiceLine = verifySuccessPost(INVOICE_LINES_PATH, invoiceLineBody).as(InvoiceLine.class);
    // Verification
    assertThat(getInvoiceLineUpdates(), Matchers.hasSize(0));
    assertThat(getInvoiceUpdates(), Matchers.hasSize(1));
    compareRecordWithSentToStorage(invoiceLine);
    /*
     * 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 == Adjustment.Type.AMOUNT) ? 15d : 3.75d;
    double expectedAdjValue = expectedAdjTotal;
    assertThat(invoiceLine.getAdjustments(), hasSize(1));
    assertThat(invoiceLine.getAdjustmentsTotal(), is(expectedAdjTotal));
    Adjustment lineAdjustment = invoiceLine.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 4 with Adjustment

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

the class InvoiceLinesProratedAdjustmentsTest method testUpdateLineForInvoiceWithOneAdj.

@ParameterizedTest
@CsvSource({ "BY_AMOUNT, AMOUNT", "BY_AMOUNT, PERCENTAGE", "BY_LINE, AMOUNT", "BY_LINE, PERCENTAGE", "BY_QUANTITY, AMOUNT", "BY_QUANTITY, PERCENTAGE" })
public void testUpdateLineForInvoiceWithOneAdj(Adjustment.Prorate prorate, Adjustment.Type type) {
    logger.info("=== Updating line for invoice with only one line and one prorated adjustment ===");
    // Prepare data "from storage"
    Invoice invoice = getMockAsJson(OPEN_INVOICE_SAMPLE_PATH).mapTo(Invoice.class).withId(randomUUID().toString());
    Adjustment invoiceAdjustment = createAdjustment(prorate, type, 15d);
    invoice.withAdjustments(Collections.singletonList(invoiceAdjustment));
    addMockEntry(INVOICES, invoice);
    InvoiceLine lineData = getMockInvoiceLine(invoice.getId()).withSubTotal(15d).withQuantity(5);
    lineData.setAdjustments(Collections.singletonList(createAdjustment(Adjustment.Prorate.NOT_PRORATED, type, 15d).withAdjustmentId(invoiceAdjustment.getId())));
    addMockEntry(INVOICE_LINES, lineData);
    // Prepare request body
    InvoiceLine invoiceLineBody = copyObject(lineData).withSubTotal(25d).withQuantity(10);
    String lineId = invoiceLineBody.getId();
    // Send update request
    verifySuccessPut(String.format(INVOICE_LINE_ID_PATH, lineId), invoiceLineBody);
    // Verification
    assertThat(getInvoiceLineUpdates(), Matchers.hasSize(1));
    assertThat(getInvoiceUpdates(), Matchers.hasSize(1));
    InvoiceLine lineToStorage = getLineToStorageById(lineId);
    assertThat(lineToStorage.getAdjustments(), hasSize(1));
    /*
     * 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 == Adjustment.Type.AMOUNT) ? 15d : 3.75d;
    double expectedAdjValue = expectedAdjTotal;
    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 5 with Adjustment

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

the class InvoiceLinesProratedAdjustmentsTest method testDeleteLineForInvoiceWithOneAdj.

@ParameterizedTest
@CsvSource({ "BY_AMOUNT, AMOUNT", "BY_AMOUNT, PERCENTAGE", "BY_LINE, AMOUNT", "BY_LINE, PERCENTAGE", "BY_QUANTITY, AMOUNT", "BY_QUANTITY, PERCENTAGE" })
public void testDeleteLineForInvoiceWithOneAdj(Adjustment.Prorate prorate, Adjustment.Type type) {
    logger.info("=== Deleting line for invoice with 2 lines and one prorated adjustment ===");
    // Prepare data "from storage"
    Invoice invoice = getMockAsJson(OPEN_INVOICE_SAMPLE_PATH).mapTo(Invoice.class).withId(randomUUID().toString());
    Adjustment invoiceAdjustment = createAdjustment(prorate, type, 15d);
    invoice.withAdjustments(Collections.singletonList(invoiceAdjustment));
    addMockEntry(INVOICES, invoice);
    InvoiceLine line1 = getMockInvoiceLine(invoice.getId()).withSubTotal(25d).withQuantity(10);
    addMockEntry(INVOICE_LINES, line1);
    InvoiceLine line2 = getMockInvoiceLine(invoice.getId()).withSubTotal(15d).withQuantity(5);
    addMockEntry(INVOICE_LINES, line2);
    // Send update request
    verifyDeleteResponse(String.format(INVOICE_LINE_ID_PATH, line2.getId()), "", 204);
    // Verification
    assertThat(getInvoiceLineUpdates(), Matchers.hasSize(1));
    assertThat(getInvoiceUpdates(), Matchers.hasSize(1));
    InvoiceLine lineToStorage = getLineToStorageById(line1.getId());
    assertThat(lineToStorage.getAdjustments(), hasSize(1));
    /*
     * 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 == Adjustment.Type.AMOUNT) ? 15d : 3.75d;
    double expectedAdjValue = expectedAdjTotal;
    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)

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