Search in sources :

Example 36 with Adjustment

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

the class BaseInvoiceService method calculateTotals.

@Override
public void calculateTotals(Invoice invoice, List<InvoiceLine> lines) {
    CurrencyUnit currency = Monetary.getCurrency(invoice.getCurrency());
    // 1. Sub-total
    MonetaryAmount subTotal = HelperUtils.summarizeSubTotals(lines, currency, false);
    // 2. Calculate Adjustments Total
    // If there are no invoice lines then adjustmentsTotal = sum of all invoice adjustments
    // If lines are present then adjustmentsTotal = notProratedInvoiceAdjustments + sum of invoiceLines adjustmentsTotal
    MonetaryAmount adjustmentsTotal;
    if (lines.isEmpty()) {
        List<Adjustment> proratedAdjustments = new ArrayList<>(adjustmentsService.getProratedAdjustments(invoice));
        proratedAdjustments.addAll(adjustmentsService.getNotProratedAdjustments(invoice));
        adjustmentsTotal = calculateAdjustmentsTotal(proratedAdjustments, subTotal);
    } else {
        adjustmentsTotal = calculateAdjustmentsTotal(adjustmentsService.getNotProratedAdjustments(invoice), subTotal).add(calculateInvoiceLinesAdjustmentsTotal(lines, currency));
    }
    // 3. Total
    invoice.setTotal(convertToDoubleWithRounding(subTotal.add(adjustmentsTotal)));
    invoice.setAdjustmentsTotal(convertToDoubleWithRounding(adjustmentsTotal));
    invoice.setSubTotal(convertToDoubleWithRounding(subTotal));
}
Also used : CurrencyUnit(javax.money.CurrencyUnit) MonetaryAmount(javax.money.MonetaryAmount) Adjustment(org.folio.rest.jaxrs.model.Adjustment) ArrayList(java.util.ArrayList)

Example 37 with Adjustment

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

the class InvoiceLineHolderValidatorTest method validateLineAdjustmentsOnCreate.

@Test
void validateLineAdjustmentsOnCreate() {
    Invoice invoice = new Invoice();
    List<Adjustment> invoiceAdjustments = new ArrayList<>();
    String deletedId = UUID.randomUUID().toString();
    invoiceAdjustments.add(new Adjustment().withProrate(Adjustment.Prorate.BY_AMOUNT).withId(deletedId));
    invoice.setAdjustments(invoiceAdjustments);
    InvoiceLine invoiceLine = new InvoiceLine();
    Adjustment adjustment = new Adjustment().withAdjustmentId(UUID.randomUUID().toString());
    List<Adjustment> invoiceLineAdjustments = new ArrayList<>();
    invoiceLineAdjustments.add(adjustment);
    invoiceLineAdjustments.add(adjustment);
    invoiceLine.setAdjustments(invoiceLineAdjustments);
    Errors expectedErrors = new Errors().withTotalRecords(2);
    expectedErrors.getErrors().add(ADJUSTMENT_IDS_NOT_UNIQUE.toError());
    expectedErrors.getErrors().add(CANNOT_ADD_ADJUSTMENTS.toError().withParameters(Collections.singletonList(new Parameter().withKey("adjustmentId").withValue(adjustment.getAdjustmentId()))));
    HttpException exception = assertThrows(HttpException.class, () -> invoiceLineValidator.validateLineAdjustmentsOnCreate(invoiceLine, invoice));
    assertEquals(422, exception.getCode());
    assertEquals(expectedErrors, exception.getErrors());
}
Also used : Errors(org.folio.rest.jaxrs.model.Errors) 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) Parameter(org.folio.rest.jaxrs.model.Parameter) HttpException(org.folio.invoices.rest.exceptions.HttpException) Test(org.junit.jupiter.api.Test)

Example 38 with Adjustment

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

the class InvoiceLineHolderValidatorTest method validateLineAdjustmentsOnUpdate.

@Test
void validateLineAdjustmentsOnUpdate() {
    Invoice invoice = new Invoice();
    List<Adjustment> invoiceAdjustments = new ArrayList<>();
    String deletedId = UUID.randomUUID().toString();
    invoiceAdjustments.add(new Adjustment().withProrate(Adjustment.Prorate.BY_AMOUNT).withId(deletedId));
    invoice.setAdjustments(invoiceAdjustments);
    InvoiceLine invoiceLine = new InvoiceLine();
    Adjustment adjustment = new Adjustment().withAdjustmentId(UUID.randomUUID().toString());
    List<Adjustment> invoiceLineAdjustments = new ArrayList<>();
    invoiceLineAdjustments.add(adjustment);
    invoiceLineAdjustments.add(adjustment);
    invoiceLine.setAdjustments(invoiceLineAdjustments);
    Errors expectedErrors = new Errors().withTotalRecords(3);
    expectedErrors.getErrors().add(ADJUSTMENT_IDS_NOT_UNIQUE.toError());
    expectedErrors.getErrors().add(CANNOT_DELETE_ADJUSTMENTS.toError().withParameters(Collections.singletonList(new Parameter().withKey("adjustmentId").withValue(deletedId))));
    expectedErrors.getErrors().add(CANNOT_ADD_ADJUSTMENTS.toError().withParameters(Collections.singletonList(new Parameter().withKey("adjustmentId").withValue(adjustment.getAdjustmentId()))));
    HttpException exception = assertThrows(HttpException.class, () -> invoiceLineValidator.validateLineAdjustmentsOnUpdate(invoiceLine, invoice));
    assertEquals(422, exception.getCode());
    assertEquals(expectedErrors, exception.getErrors());
}
Also used : Errors(org.folio.rest.jaxrs.model.Errors) 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) Parameter(org.folio.rest.jaxrs.model.Parameter) HttpException(org.folio.invoices.rest.exceptions.HttpException) Test(org.junit.jupiter.api.Test)

Example 39 with Adjustment

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

the class AdjustmentsService method convertToAmountAdjustment.

private Adjustment convertToAmountAdjustment(Adjustment adjustment, List<InvoiceLine> lines, CurrencyUnit currencyUnit) {
    MonetaryAmount subTotal = summarizeSubTotals(lines, currencyUnit, false);
    Adjustment amountAdjustment = JsonObject.mapFrom(adjustment).mapTo(adjustment.getClass());
    amountAdjustment.setValue(subTotal.with(MonetaryOperators.percent(adjustment.getValue())).with(Monetary.getDefaultRounding()).getNumber().doubleValue());
    amountAdjustment.setType(Adjustment.Type.AMOUNT);
    return amountAdjustment;
}
Also used : MonetaryAmount(javax.money.MonetaryAmount) Adjustment(org.folio.rest.jaxrs.model.Adjustment)

Example 40 with Adjustment

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

the class AdjustmentsService method applyProratedAdjustments.

public List<InvoiceLine> applyProratedAdjustments(List<InvoiceLine> lines, Invoice invoice) {
    CurrencyUnit currencyUnit = Monetary.getCurrency(invoice.getCurrency());
    sortByInvoiceLineNumber(lines);
    List<Adjustment> proratedAdjustments = getProratedAdjustments(invoice);
    List<InvoiceLine> updatedLines = new ArrayList<>();
    for (Adjustment adjustment : proratedAdjustments) {
        switch(adjustment.getProrate()) {
            case BY_LINE:
                updatedLines.addAll(applyProratedAdjustmentByLines(adjustment, lines, currencyUnit));
                break;
            case BY_AMOUNT:
                updatedLines.addAll(applyProratedAdjustmentByAmount(adjustment, lines, currencyUnit));
                break;
            case BY_QUANTITY:
                updatedLines.addAll(applyProratedAdjustmentByQuantity(adjustment, lines, currencyUnit));
                break;
            default:
                logger.warn("Unexpected {} adjustment's prorate type for invoice with id={}", adjustment.getProrate(), invoice.getId());
        }
    }
    // Return only unique invoice lines
    return updatedLines.stream().distinct().collect(toList());
}
Also used : CurrencyUnit(javax.money.CurrencyUnit) Adjustment(org.folio.rest.jaxrs.model.Adjustment) InvoiceLine(org.folio.rest.jaxrs.model.InvoiceLine) ArrayList(java.util.ArrayList)

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