Search in sources :

Example 26 with HttpException

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());
}
Also used : FiscalYear(org.folio.rest.acq.model.finance.FiscalYear) Fund(org.folio.rest.acq.model.finance.Fund) Transaction(org.folio.rest.acq.model.finance.Transaction) InvoiceWorkflowDataHolder(org.folio.models.InvoiceWorkflowDataHolder) ArrayList(java.util.ArrayList) Error(org.folio.rest.jaxrs.model.Error) Budget(org.folio.rest.acq.model.finance.Budget) HttpException(org.folio.invoices.rest.exceptions.HttpException) Test(org.junit.jupiter.api.Test)

Example 27 with HttpException

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());
}
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 28 with HttpException

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());
}
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 29 with HttpException

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);
    });
}
Also used : Parameter(org.folio.rest.jaxrs.model.Parameter) Error(org.folio.rest.jaxrs.model.Error) HttpException(org.folio.invoices.rest.exceptions.HttpException)

Example 30 with HttpException

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())));
}
Also used : CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) HelperUtils.handlePutRequest(org.folio.invoices.utils.HelperUtils.handlePutRequest) OrderLineService(org.folio.services.order.OrderLineService) Autowired(org.springframework.beans.factory.annotation.Autowired) Context(io.vertx.core.Context) StringUtils(org.apache.commons.lang3.StringUtils) FAILED_TO_UPDATE_INVOICE_AND_OTHER_LINES(org.folio.invoices.utils.ErrorCodes.FAILED_TO_UPDATE_INVOICE_AND_OTHER_LINES) CompositePurchaseOrder(org.folio.rest.acq.model.orders.CompositePurchaseOrder) HttpException(org.folio.invoices.rest.exceptions.HttpException) InvoiceService(org.folio.services.invoice.InvoiceService) AdjustmentsService(org.folio.services.adjusment.AdjustmentsService) DELETE(org.folio.invoices.utils.ProtectedOperationType.DELETE) HelperUtils.isPostApproval(org.folio.invoices.utils.HelperUtils.isPostApproval) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) StringUtils.isEmpty(org.apache.commons.lang3.StringUtils.isEmpty) ORDER_INVOICE_RELATION_CREATE_FAILED(org.folio.invoices.utils.ErrorCodes.ORDER_INVOICE_RELATION_CREATE_FAILED) ResourcePathResolver.resourceByIdPath(org.folio.invoices.utils.ResourcePathResolver.resourceByIdPath) InvoiceLine(org.folio.rest.jaxrs.model.InvoiceLine) CompositePoLine(org.folio.rest.acq.model.orders.CompositePoLine) INVOICE_LINES(org.folio.invoices.utils.ResourcePathResolver.INVOICE_LINES) OrderInvoiceRelationship(org.folio.rest.acq.model.orders.OrderInvoiceRelationship) InvoiceRestrictionsUtil(org.folio.invoices.utils.InvoiceRestrictionsUtil) SequenceNumber(org.folio.rest.jaxrs.model.SequenceNumber) HelperUtils.getHttpClient(org.folio.invoices.utils.HelperUtils.getHttpClient) Objects(java.util.Objects) UPDATE(org.folio.invoices.utils.ProtectedOperationType.UPDATE) List(java.util.List) HttpClientInterface(org.folio.rest.tools.client.interfaces.HttpClientInterface) Adjustment(org.folio.rest.jaxrs.model.Adjustment) InvoiceLineValidator(org.folio.services.validator.InvoiceLineValidator) CANNOT_DELETE_INVOICE_LINE(org.folio.invoices.utils.ErrorCodes.CANNOT_DELETE_INVOICE_LINE) Parameter(org.folio.rest.jaxrs.model.Parameter) FAILED_TO_UPDATE_PONUMBERS(org.folio.invoices.utils.ErrorCodes.FAILED_TO_UPDATE_PONUMBERS) Invoice(org.folio.rest.jaxrs.model.Invoice) INVOICE_LINE_NUMBER(org.folio.invoices.utils.ResourcePathResolver.INVOICE_LINE_NUMBER) RestClient(org.folio.rest.core.RestClient) FolioVertxCompletableFuture(org.folio.completablefuture.FolioVertxCompletableFuture) CompletableFuture(java.util.concurrent.CompletableFuture) HelperUtils.combineCqlExpressions(org.folio.invoices.utils.HelperUtils.combineCqlExpressions) HelperUtils.getInvoices(org.folio.invoices.utils.HelperUtils.getInvoices) SpringContextUtil(org.folio.spring.SpringContextUtil) ArrayList(java.util.ArrayList) HelperUtils.handleGetRequest(org.folio.invoices.utils.HelperUtils.handleGetRequest) InvoiceLineCollection(org.folio.rest.jaxrs.model.InvoiceLineCollection) HelperUtils.calculateInvoiceLineTotals(org.folio.invoices.utils.HelperUtils.calculateInvoiceLineTotals) RequestContext(org.folio.rest.core.models.RequestContext) InvoiceLineService(org.folio.services.invoice.InvoiceLineService) PROHIBITED_INVOICE_LINE_CREATION(org.folio.invoices.utils.ErrorCodes.PROHIBITED_INVOICE_LINE_CREATION) OrderService(org.folio.services.order.OrderService) ResourcePathResolver.resourcesPath(org.folio.invoices.utils.ResourcePathResolver.resourcesPath) QUERY_BY_INVOICE_ID(org.folio.services.voucher.VoucherRetrieveService.QUERY_BY_INVOICE_ID) HelperUtils.getEndpointWithQuery(org.folio.invoices.utils.HelperUtils.getEndpointWithQuery) Vertx(io.vertx.core.Vertx) RequestEntry(org.folio.rest.core.models.RequestEntry) HelperUtils.handleDeleteRequest(org.folio.invoices.utils.HelperUtils.handleDeleteRequest) INVOICE_ID(org.folio.invoices.utils.HelperUtils.INVOICE_ID) HelperUtils.getInvoiceById(org.folio.invoices.utils.HelperUtils.getInvoiceById) Error(org.folio.rest.jaxrs.model.Error) Collectors.toList(java.util.stream.Collectors.toList) InvoiceHolder(org.folio.models.InvoiceHolder) QUERY_PARAM_START_WITH(org.folio.invoices.utils.HelperUtils.QUERY_PARAM_START_WITH) READ(org.folio.invoices.utils.ProtectedOperationType.READ) Collections(java.util.Collections) ProtectedOperationType(org.folio.invoices.utils.ProtectedOperationType) InvoiceRestrictionsUtil(org.folio.invoices.utils.InvoiceRestrictionsUtil) InvoiceHolder(org.folio.models.InvoiceHolder) Parameter(org.folio.rest.jaxrs.model.Parameter) Error(org.folio.rest.jaxrs.model.Error) HttpException(org.folio.invoices.rest.exceptions.HttpException)

Aggregations

HttpException (org.folio.invoices.rest.exceptions.HttpException)31 Error (org.folio.rest.jaxrs.model.Error)16 CompletableFuture (java.util.concurrent.CompletableFuture)14 Test (org.junit.jupiter.api.Test)13 Invoice (org.folio.rest.jaxrs.model.Invoice)12 InvoiceLine (org.folio.rest.jaxrs.model.InvoiceLine)12 Parameter (org.folio.rest.jaxrs.model.Parameter)11 ArrayList (java.util.ArrayList)10 List (java.util.List)9 RequestContext (org.folio.rest.core.models.RequestContext)9 Errors (org.folio.rest.jaxrs.model.Errors)9 LogManager (org.apache.logging.log4j.LogManager)8 Logger (org.apache.logging.log4j.Logger)8 InvoiceWorkflowDataHolder (org.folio.models.InvoiceWorkflowDataHolder)8 Transaction (org.folio.rest.acq.model.finance.Transaction)8 Collections (java.util.Collections)7 Collectors.toList (java.util.stream.Collectors.toList)7 Fund (org.folio.rest.acq.model.finance.Fund)7 JsonObject (io.vertx.core.json.JsonObject)6 Map (java.util.Map)6