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));
}
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());
}
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());
}
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;
}
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());
}
Aggregations