Search in sources :

Example 1 with InvoiceRestrictionsUtil

use of org.folio.invoices.utils.InvoiceRestrictionsUtil 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

Context (io.vertx.core.Context)1 Vertx (io.vertx.core.Vertx)1 JsonObject (io.vertx.core.json.JsonObject)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletableFuture.completedFuture (java.util.concurrent.CompletableFuture.completedFuture)1 Collectors.toList (java.util.stream.Collectors.toList)1 StringUtils (org.apache.commons.lang3.StringUtils)1 StringUtils.isEmpty (org.apache.commons.lang3.StringUtils.isEmpty)1 FolioVertxCompletableFuture (org.folio.completablefuture.FolioVertxCompletableFuture)1 HttpException (org.folio.invoices.rest.exceptions.HttpException)1 CANNOT_DELETE_INVOICE_LINE (org.folio.invoices.utils.ErrorCodes.CANNOT_DELETE_INVOICE_LINE)1 FAILED_TO_UPDATE_INVOICE_AND_OTHER_LINES (org.folio.invoices.utils.ErrorCodes.FAILED_TO_UPDATE_INVOICE_AND_OTHER_LINES)1 FAILED_TO_UPDATE_PONUMBERS (org.folio.invoices.utils.ErrorCodes.FAILED_TO_UPDATE_PONUMBERS)1 ORDER_INVOICE_RELATION_CREATE_FAILED (org.folio.invoices.utils.ErrorCodes.ORDER_INVOICE_RELATION_CREATE_FAILED)1 PROHIBITED_INVOICE_LINE_CREATION (org.folio.invoices.utils.ErrorCodes.PROHIBITED_INVOICE_LINE_CREATION)1