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