use of org.folio.invoices.rest.exceptions.HttpException in project mod-invoice by folio-org.
the class FundAvailabilityHolderValidatorTest method shouldCountAdjustmentsFundDistributionsDuringBudgetRemainingAmountValidation.
@Test
void shouldCountAdjustmentsFundDistributionsDuringBudgetRemainingAmountValidation() {
String fiscalYearId = UUID.randomUUID().toString();
String fundId = UUID.randomUUID().toString();
String budgetId = UUID.randomUUID().toString();
String ledgerId = UUID.randomUUID().toString();
FiscalYear fiscalYear = new FiscalYear().withCurrency("USD").withId(fiscalYearId);
Fund fund = new Fund().withId(fundId).withName("TestFund").withLedgerId(ledgerId).withCode("FC").withFundStatus(Fund.FundStatus.ACTIVE);
Budget budget = new Budget().withId(budgetId).withFiscalYearId(fiscalYearId).withFundId(fundId).withAllocated(260d).withTotalFunding(260d).withAvailable(210d).withUnavailable(50d).withAwaitingPayment(50d).withAllowableExpenditure(100d);
List<InvoiceWorkflowDataHolder> holders = new ArrayList<>();
Transaction adjustmentPendingPayment = new Transaction().withAmount(20d).withCurrency("USD");
Transaction linePendingPayment = new Transaction().withAmount(200d).withCurrency("USD");
InvoiceWorkflowDataHolder holder1 = new InvoiceWorkflowDataHolder().withFund(fund).withBudget(budget).withRestrictExpenditures(true).withFiscalYear(fiscalYear).withNewTransaction(linePendingPayment);
InvoiceWorkflowDataHolder holder2 = new InvoiceWorkflowDataHolder().withFund(fund).withBudget(budget).withRestrictExpenditures(true).withFiscalYear(fiscalYear).withNewTransaction(adjustmentPendingPayment);
holders.add(holder1);
holders.add(holder2);
HttpException httpException = assertThrows(HttpException.class, () -> fundAvailabilityValidator.validate(holders));
assertEquals(422, httpException.getCode());
Error error = httpException.getErrors().getErrors().get(0);
assertEquals(FUND_CANNOT_BE_PAID.getCode(), error.getCode());
assertEquals(Collections.singletonList("FC").toString(), error.getParameters().get(0).getValue());
}
use of org.folio.invoices.rest.exceptions.HttpException 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.invoices.rest.exceptions.HttpException 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.invoices.rest.exceptions.HttpException in project mod-invoice by folio-org.
the class InvoiceLineHelper method getInvoicesIfExists.
private CompletableFuture<Invoice> getInvoicesIfExists(String lineId) {
String query = QUERY_PARAM_START_WITH + lineId;
return getInvoices(query, httpClient, ctx, okapiHeaders, logger, lang).thenCompose(invoiceCollection -> {
if (!invoiceCollection.getInvoices().isEmpty()) {
return CompletableFuture.completedFuture(invoiceCollection.getInvoices().get(0));
}
List<Parameter> parameters = Collections.singletonList(new Parameter().withKey("invoiceLineId").withValue(lineId));
Error error = CANNOT_DELETE_INVOICE_LINE.toError().withParameters(parameters);
throw new HttpException(404, error);
});
}
use of org.folio.invoices.rest.exceptions.HttpException in project mod-invoice by folio-org.
the class InvoiceLineHelper method deleteInvoiceLine.
/**
* Deletes Invoice Line and update Invoice if deletion is allowed 1. Get invoice via searching for invoices by invoiceLine.id
* field 2. Verify if user has permission to delete invoiceLine based on acquisitions units, if not then return 3. If user has
* permission to delete then delete invoiceLine 4. Update corresponding Invoice
*
* @param lineId invoiceLine id to be deleted
*/
public CompletableFuture<Void> deleteInvoiceLine(String lineId) {
InvoiceHolder invoiceHolder = new InvoiceHolder();
return getInvoicesIfExists(lineId).thenApply(invoiceHolder::setInvoice).thenCompose(invHolder -> protectionHelper.isOperationRestricted(invHolder.getInvoice().getAcqUnitIds(), DELETE).thenApply(vVoid -> invHolder.getInvoice())).thenCompose(InvoiceRestrictionsUtil::checkIfInvoiceDeletionPermitted).thenCompose(v -> invoiceLineService.getInvoiceLine(lineId, buildRequestContext()).thenApply(invoiceHolder::setInvoiceLine)).thenCompose(invoiceHold -> orderService.deleteOrderInvoiceRelationIfLastInvoice(lineId, buildRequestContext()).exceptionally(throwable -> {
logger.error("Can't delete Order Invoice relation for lineId: {}", lineId, throwable);
List<Parameter> parameters = Collections.singletonList(new Parameter().withKey("lineId").withValue(lineId));
Error error = CANNOT_DELETE_INVOICE_LINE.toError().withParameters(parameters);
throw new HttpException(404, error);
}).thenCompose(v -> handleDeleteRequest(resourceByIdPath(INVOICE_LINES, lineId, lang), httpClient, ctx, okapiHeaders, logger)).thenCompose(v -> updateInvoiceAndLines(invoiceHold.getInvoice(), buildRequestContext())).thenCompose(invoiceLine -> deleteInvoicePoNumbers(invoiceHold.getInvoice(), invoiceHolder.getInvoiceLine(), buildRequestContext())));
}
Aggregations