Search in sources :

Example 11 with PieceUpdateHolder

use of org.folio.models.pieces.PieceUpdateHolder in project mod-orders by folio-org.

the class PieceUpdateFlowPoLineServiceTest method shouldUpdateLineQuantityIfElectronicPoLineIsNotPackageAndHoldingReferenceChanged.

@Test
void shouldUpdateLineQuantityIfElectronicPoLineIsNotPackageAndHoldingReferenceChanged() throws ExecutionException, InterruptedException {
    String orderId = UUID.randomUUID().toString();
    String oldHoldingId = UUID.randomUUID().toString();
    String newHoldingId = UUID.randomUUID().toString();
    String lineId = UUID.randomUUID().toString();
    String titleId = UUID.randomUUID().toString();
    String itemId = UUID.randomUUID().toString();
    String pieceId = UUID.randomUUID().toString();
    Piece pieceFromStorage = new Piece().withId(pieceId).withPoLineId(lineId).withItemId(itemId).withTitleId(titleId).withHoldingId(oldHoldingId).withFormat(Piece.Format.ELECTRONIC);
    Piece pieceToUpdate = new Piece().withId(pieceId).withPoLineId(lineId).withItemId(itemId).withTitleId(titleId).withHoldingId(newHoldingId).withFormat(Piece.Format.ELECTRONIC);
    Cost cost = new Cost().withQuantityElectronic(1).withListUnitPriceElectronic(1d).withExchangeRate(1d).withCurrency("USD").withPoLineEstimatedPrice(1d);
    Location loc = new Location().withHoldingId(oldHoldingId).withQuantityElectronic(1).withQuantity(1);
    PoLine poLineFromStorage = new PoLine().withIsPackage(false).withPurchaseOrderId(orderId).withId(lineId).withEresource(new Eresource().withCreateInventory(Eresource.CreateInventory.INSTANCE_HOLDING_ITEM)).withOrderFormat(PoLine.OrderFormat.ELECTRONIC_RESOURCE).withLocations(List.of(loc)).withCost(cost);
    PurchaseOrder purchaseOrderFromStorage = new PurchaseOrder().withId(orderId).withWorkflowStatus(PurchaseOrder.WorkflowStatus.OPEN);
    PieceUpdateHolder incomingUpdateHolder = new PieceUpdateHolder().withPieceToUpdate(pieceToUpdate).withPieceFromStorage(pieceFromStorage);
    incomingUpdateHolder.withOrderInformation(purchaseOrderFromStorage, poLineFromStorage);
    ArgumentCaptor<CompositePurchaseOrder> purchaseOrderToSaveCapture = ArgumentCaptor.forClass(CompositePurchaseOrder.class);
    ArgumentCaptor<CompositePoLine> poLineToSaveCapture = ArgumentCaptor.forClass(CompositePoLine.class);
    doReturn(completedFuture(null)).when(purchaseOrderLineService).saveOrderLine(poLineToSaveCapture.capture(), eq(requestContext));
    // When
    pieceUpdateFlowPoLineService.updatePoLine(incomingUpdateHolder, requestContext).get();
    // Then
    CompositePoLine poLineToUpdate = poLineToSaveCapture.getValue();
    assertNull(pieceToUpdate.getLocationId());
    assertEquals(newHoldingId, pieceToUpdate.getHoldingId());
    Location locationToSave = poLineToUpdate.getLocations().get(0);
    Cost costToSave = poLineToUpdate.getCost();
    assertNull(locationToSave.getLocationId());
    assertEquals(1, locationToSave.getQuantityElectronic());
    assertEquals(1, locationToSave.getQuantity());
    assertEquals(pieceToUpdate.getHoldingId(), locationToSave.getHoldingId());
    assertEquals(1, costToSave.getQuantityElectronic());
    assertNull(costToSave.getQuantityPhysical());
    assertNull(poLineFromStorage.getLocations().get(0).getLocationId());
    assertEquals(oldHoldingId, poLineFromStorage.getLocations().get(0).getHoldingId());
    verify(purchaseOrderLineService).saveOrderLine(incomingUpdateHolder.getPoLineToSave(), requestContext);
}
Also used : PieceUpdateHolder(org.folio.models.pieces.PieceUpdateHolder) Piece(org.folio.rest.jaxrs.model.Piece) PoLine(org.folio.rest.jaxrs.model.PoLine) CompositePoLine(org.folio.rest.jaxrs.model.CompositePoLine) PurchaseOrder(org.folio.rest.jaxrs.model.PurchaseOrder) CompositePurchaseOrder(org.folio.rest.jaxrs.model.CompositePurchaseOrder) CompositePoLine(org.folio.rest.jaxrs.model.CompositePoLine) CompositePurchaseOrder(org.folio.rest.jaxrs.model.CompositePurchaseOrder) Cost(org.folio.rest.jaxrs.model.Cost) Eresource(org.folio.rest.jaxrs.model.Eresource) Location(org.folio.rest.jaxrs.model.Location) Test(org.junit.jupiter.api.Test)

Example 12 with PieceUpdateHolder

use of org.folio.models.pieces.PieceUpdateHolder in project mod-orders by folio-org.

the class PieceUpdateFlowPoLineServiceTest method shouldUpdateLineQuantityWithAddNeLocationIfMixedPoLineIsNotPackageAndLocationReferenceChanged.

@Test
void shouldUpdateLineQuantityWithAddNeLocationIfMixedPoLineIsNotPackageAndLocationReferenceChanged() throws ExecutionException, InterruptedException {
    String orderId = UUID.randomUUID().toString();
    String oldHoldingId = UUID.randomUUID().toString();
    String locationToUpdate = UUID.randomUUID().toString();
    String lineId = UUID.randomUUID().toString();
    String titleId = UUID.randomUUID().toString();
    String itemId = UUID.randomUUID().toString();
    String pieceId = UUID.randomUUID().toString();
    Piece pieceFromStorage = new Piece().withId(pieceId).withPoLineId(lineId).withItemId(itemId).withTitleId(titleId).withHoldingId(oldHoldingId).withFormat(Piece.Format.ELECTRONIC);
    Piece pieceToUpdate = new Piece().withId(pieceId).withPoLineId(lineId).withItemId(itemId).withTitleId(titleId).withLocationId(locationToUpdate).withFormat(Piece.Format.ELECTRONIC);
    Cost cost = new Cost().withQuantityElectronic(1).withQuantityPhysical(1).withListUnitPrice(1d).withListUnitPriceElectronic(1d).withExchangeRate(1d).withCurrency("USD").withPoLineEstimatedPrice(1d);
    Location loc = new Location().withHoldingId(oldHoldingId).withQuantityPhysical(1).withQuantityElectronic(1).withQuantity(2);
    PoLine poLineFromStorage = new PoLine().withIsPackage(false).withPurchaseOrderId(orderId).withId(lineId).withOrderFormat(PoLine.OrderFormat.P_E_MIX).withEresource(new Eresource().withCreateInventory(INSTANCE)).withPhysical(new Physical().withCreateInventory(Physical.CreateInventory.INSTANCE_HOLDING_ITEM)).withLocations(List.of(loc)).withCost(cost);
    PurchaseOrder purchaseOrderFromStorage = new PurchaseOrder().withId(orderId).withWorkflowStatus(PurchaseOrder.WorkflowStatus.OPEN);
    PieceUpdateHolder incomingUpdateHolder = new PieceUpdateHolder().withPieceToUpdate(pieceToUpdate).withPieceFromStorage(pieceFromStorage);
    incomingUpdateHolder.withOrderInformation(purchaseOrderFromStorage, poLineFromStorage);
    ArgumentCaptor<CompositePoLine> poLineToSaveCapture = ArgumentCaptor.forClass(CompositePoLine.class);
    doReturn(completedFuture(null)).when(purchaseOrderLineService).saveOrderLine(poLineToSaveCapture.capture(), eq(requestContext));
    // When
    pieceUpdateFlowPoLineService.updatePoLine(incomingUpdateHolder, requestContext).get();
    // Then
    CompositePoLine poLineToUpdate = poLineToSaveCapture.getValue();
    assertEquals(locationToUpdate, pieceToUpdate.getLocationId());
    assertNull(pieceToUpdate.getHoldingId());
    List<Location> locations = poLineToUpdate.getLocations();
    assertEquals(2, locations.size());
    Location physicalLocationToSave = poLineToUpdate.getLocations().stream().filter(location -> location.getQuantityPhysical() != null).findAny().get();
    assertNull(physicalLocationToSave.getQuantityElectronic());
    assertNull(physicalLocationToSave.getLocationId());
    assertEquals(1, physicalLocationToSave.getQuantity());
    assertEquals(oldHoldingId, physicalLocationToSave.getHoldingId());
    Location elecLocationToSave = poLineToUpdate.getLocations().stream().filter(location -> location.getQuantityElectronic() != null).findAny().get();
    assertNull(elecLocationToSave.getQuantityPhysical());
    assertNull(elecLocationToSave.getHoldingId());
    assertEquals(1, elecLocationToSave.getQuantity());
    assertEquals(locationToUpdate, elecLocationToSave.getLocationId());
    Cost costToSave = poLineToUpdate.getCost();
    assertEquals(1, costToSave.getQuantityElectronic());
    assertEquals(1, costToSave.getQuantityPhysical());
    verify(purchaseOrderLineService).saveOrderLine(incomingUpdateHolder.getPoLineToSave(), requestContext);
}
Also used : Physical(org.folio.rest.jaxrs.model.Physical) PieceUpdateHolder(org.folio.models.pieces.PieceUpdateHolder) Piece(org.folio.rest.jaxrs.model.Piece) PoLine(org.folio.rest.jaxrs.model.PoLine) CompositePoLine(org.folio.rest.jaxrs.model.CompositePoLine) PurchaseOrder(org.folio.rest.jaxrs.model.PurchaseOrder) CompositePurchaseOrder(org.folio.rest.jaxrs.model.CompositePurchaseOrder) CompositePoLine(org.folio.rest.jaxrs.model.CompositePoLine) Cost(org.folio.rest.jaxrs.model.Cost) Eresource(org.folio.rest.jaxrs.model.Eresource) Location(org.folio.rest.jaxrs.model.Location) Test(org.junit.jupiter.api.Test)

Example 13 with PieceUpdateHolder

use of org.folio.models.pieces.PieceUpdateHolder in project mod-orders by folio-org.

the class PieceUpdateFlowInventoryManager method nonPackageUpdateTitleWithInstance.

private CompletableFuture<String> nonPackageUpdateTitleWithInstance(PieceUpdateHolder holder, RequestContext requestContext) {
    CompositePoLine poLineToSave = holder.getPoLineToSave();
    Piece pieceToUpdate = holder.getPieceToUpdate();
    if (poLineToSave.getInstanceId() == null && !PoLineCommonUtil.isInventoryUpdateNotRequired(poLineToSave)) {
        return titlesService.getTitleById(pieceToUpdate.getTitleId(), requestContext).thenCompose(title -> {
            if (title.getInstanceId() == null) {
                return createTitleInstance(title, requestContext);
            }
            return completedFuture(title.getInstanceId());
        }).thenApply(instanceId -> poLineToSave.withInstanceId(instanceId).getInstanceId());
    }
    return completedFuture(poLineToSave.getInstanceId());
}
Also used : Location(org.folio.rest.jaxrs.model.Location) ID(org.folio.service.inventory.InventoryManager.ID) Piece(org.folio.rest.jaxrs.model.Piece) DefaultPieceFlowsValidator(org.folio.service.pieces.flows.DefaultPieceFlowsValidator) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) CompletableFuture(java.util.concurrent.CompletableFuture) FolioVertxCompletableFuture(org.folio.completablefuture.FolioVertxCompletableFuture) ITEM_HOLDINGS_RECORD_ID(org.folio.service.inventory.InventoryManager.ITEM_HOLDINGS_RECORD_ID) ITEM_PURCHASE_ORDER_LINE_IDENTIFIER(org.folio.service.inventory.InventoryManager.ITEM_PURCHASE_ORDER_LINE_IDENTIFIER) Title(org.folio.rest.jaxrs.model.Title) PoLineCommonUtil(org.folio.orders.utils.PoLineCommonUtil) Logger(org.apache.logging.log4j.Logger) TitlesService(org.folio.service.titles.TitlesService) Pair(org.apache.commons.lang3.tuple.Pair) CompositePoLine(org.folio.rest.jaxrs.model.CompositePoLine) RequestContext(org.folio.rest.core.models.RequestContext) PieceUpdateInventoryService(org.folio.service.pieces.PieceUpdateInventoryService) Optional(java.util.Optional) InventoryManager(org.folio.service.inventory.InventoryManager) JsonObject(io.vertx.core.json.JsonObject) LogManager(org.apache.logging.log4j.LogManager) PieceUpdateHolder(org.folio.models.pieces.PieceUpdateHolder) Piece(org.folio.rest.jaxrs.model.Piece) CompositePoLine(org.folio.rest.jaxrs.model.CompositePoLine)

Example 14 with PieceUpdateHolder

use of org.folio.models.pieces.PieceUpdateHolder in project mod-orders by folio-org.

the class PieceUpdateFlowInventoryManager method handleItem.

private CompletableFuture<String> handleItem(PieceUpdateHolder holder, RequestContext requestContext) {
    CompositePoLine poLineToSave = holder.getPoLineToSave();
    Piece pieceToUpdate = holder.getPieceToUpdate();
    if (DefaultPieceFlowsValidator.isCreateItemForPiecePossible(pieceToUpdate, poLineToSave)) {
        return inventoryManager.getItemRecordById(pieceToUpdate.getItemId(), true, requestContext).thenCompose(jsonItem -> {
            if (holder.isCreateItem() && (jsonItem == null || jsonItem.isEmpty()) && pieceToUpdate.getHoldingId() != null) {
                return pieceUpdateInventoryService.manualPieceFlowCreateItemRecord(pieceToUpdate, poLineToSave, requestContext);
            } else if (jsonItem != null && !jsonItem.isEmpty()) {
                return updateItemWithFields(jsonItem, poLineToSave, pieceToUpdate, requestContext).thenCompose(aVoid -> inventoryManager.updateItem(jsonItem, requestContext).thenApply(item -> jsonItem.getString(ID)));
            } else {
                return completedFuture(null);
            }
        });
    }
    return completedFuture(null);
}
Also used : Location(org.folio.rest.jaxrs.model.Location) ID(org.folio.service.inventory.InventoryManager.ID) Piece(org.folio.rest.jaxrs.model.Piece) DefaultPieceFlowsValidator(org.folio.service.pieces.flows.DefaultPieceFlowsValidator) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) CompletableFuture(java.util.concurrent.CompletableFuture) FolioVertxCompletableFuture(org.folio.completablefuture.FolioVertxCompletableFuture) ITEM_HOLDINGS_RECORD_ID(org.folio.service.inventory.InventoryManager.ITEM_HOLDINGS_RECORD_ID) ITEM_PURCHASE_ORDER_LINE_IDENTIFIER(org.folio.service.inventory.InventoryManager.ITEM_PURCHASE_ORDER_LINE_IDENTIFIER) Title(org.folio.rest.jaxrs.model.Title) PoLineCommonUtil(org.folio.orders.utils.PoLineCommonUtil) Logger(org.apache.logging.log4j.Logger) TitlesService(org.folio.service.titles.TitlesService) Pair(org.apache.commons.lang3.tuple.Pair) CompositePoLine(org.folio.rest.jaxrs.model.CompositePoLine) RequestContext(org.folio.rest.core.models.RequestContext) PieceUpdateInventoryService(org.folio.service.pieces.PieceUpdateInventoryService) Optional(java.util.Optional) InventoryManager(org.folio.service.inventory.InventoryManager) JsonObject(io.vertx.core.json.JsonObject) LogManager(org.apache.logging.log4j.LogManager) PieceUpdateHolder(org.folio.models.pieces.PieceUpdateHolder) Piece(org.folio.rest.jaxrs.model.Piece) CompositePoLine(org.folio.rest.jaxrs.model.CompositePoLine)

Example 15 with PieceUpdateHolder

use of org.folio.models.pieces.PieceUpdateHolder in project mod-orders by folio-org.

the class PieceUpdateFlowManager method updatePiece.

// Flow to update piece
// 1. Before update, get piece by id from storage and store receiving status
// 2. Update piece with new content and complete future
// 3. Create a message and check if receivingStatus is not consistent with storage; if yes - send a message to event bus
public CompletableFuture<Void> updatePiece(Piece pieceToUpdate, boolean createItem, boolean deleteHolding, RequestContext requestContext) {
    CompletableFuture<Void> future = new FolioVertxCompletableFuture<>(requestContext.getContext());
    PieceUpdateHolder holder = new PieceUpdateHolder().withPieceToUpdate(pieceToUpdate).withCreateItem(createItem).withDeleteHolding(deleteHolding);
    pieceStorageService.getPieceById(pieceToUpdate.getId(), requestContext).thenAccept(holder::withPieceFromStorage).thenCompose(aHolder -> basePieceFlowHolderBuilder.updateHolderWithOrderInformation(holder, requestContext)).thenAccept(v -> defaultPieceFlowsValidator.isPieceRequestValid(pieceToUpdate, holder.getOriginPoLine(), createItem)).thenCompose(purchaseOrder -> protectionService.isOperationRestricted(holder.getOriginPurchaseOrder().getAcqUnitIds(), UPDATE, requestContext)).thenCompose(v -> pieceUpdateFlowInventoryManager.processInventory(holder, requestContext)).thenCompose(vVoid -> updatePoLine(holder, requestContext)).thenAccept(afterUpdate -> {
        JsonObject messageToEventBus = new JsonObject();
        messageToEventBus.put("poLineIdUpdate", holder.getPieceToUpdate().getPoLineId());
        Piece.ReceivingStatus receivingStatusStorage = holder.getPieceFromStorage().getReceivingStatus();
        Piece.ReceivingStatus receivingStatusUpdate = holder.getPieceToUpdate().getReceivingStatus();
        logger.debug("receivingStatusStorage -- {}", receivingStatusStorage);
        logger.debug("receivingStatusUpdate -- {}", receivingStatusUpdate);
        if (receivingStatusStorage.compareTo(receivingStatusUpdate) != 0) {
            pieceService.receiptConsistencyPiecePoLine(messageToEventBus, requestContext);
        }
    }).thenCompose(aVoid -> pieceStorageService.updatePiece(holder.getPieceToUpdate(), requestContext)).thenAccept(future::complete).exceptionally(t -> {
        logger.error("User to update piece with id={}", holder.getPieceToUpdate().getId(), t.getCause());
        future.completeExceptionally(t);
        return null;
    });
    return future;
}
Also used : UPDATE(org.folio.orders.utils.ProtectedOperationType.UPDATE) Piece(org.folio.rest.jaxrs.model.Piece) DefaultPieceFlowsValidator(org.folio.service.pieces.flows.DefaultPieceFlowsValidator) BasePieceFlowHolderBuilder(org.folio.service.pieces.flows.BasePieceFlowHolderBuilder) CompletableFuture(java.util.concurrent.CompletableFuture) FolioVertxCompletableFuture(org.folio.completablefuture.FolioVertxCompletableFuture) Logger(org.apache.logging.log4j.Logger) ProtectionService(org.folio.service.ProtectionService) RequestContext(org.folio.rest.core.models.RequestContext) PieceService(org.folio.service.pieces.PieceService) JsonObject(io.vertx.core.json.JsonObject) LogManager(org.apache.logging.log4j.LogManager) PieceStorageService(org.folio.service.pieces.PieceStorageService) PieceUpdateHolder(org.folio.models.pieces.PieceUpdateHolder) PieceUpdateHolder(org.folio.models.pieces.PieceUpdateHolder) JsonObject(io.vertx.core.json.JsonObject) FolioVertxCompletableFuture(org.folio.completablefuture.FolioVertxCompletableFuture)

Aggregations

PieceUpdateHolder (org.folio.models.pieces.PieceUpdateHolder)20 Piece (org.folio.rest.jaxrs.model.Piece)20 Location (org.folio.rest.jaxrs.model.Location)19 Cost (org.folio.rest.jaxrs.model.Cost)17 PoLine (org.folio.rest.jaxrs.model.PoLine)17 PurchaseOrder (org.folio.rest.jaxrs.model.PurchaseOrder)17 Test (org.junit.jupiter.api.Test)17 JsonObject (io.vertx.core.json.JsonObject)15 Physical (org.folio.rest.jaxrs.model.Physical)10 Eresource (org.folio.rest.jaxrs.model.Eresource)9 ArrayList (java.util.ArrayList)7 Title (org.folio.rest.jaxrs.model.Title)7 CompletableFuture (java.util.concurrent.CompletableFuture)6 RequestContext (org.folio.rest.core.models.RequestContext)6 CompositePoLine (org.folio.rest.jaxrs.model.CompositePoLine)6 DefaultPieceFlowsValidator (org.folio.service.pieces.flows.DefaultPieceFlowsValidator)6 CompletableFuture.completedFuture (java.util.concurrent.CompletableFuture.completedFuture)5 CompositePurchaseOrder (org.folio.rest.jaxrs.model.CompositePurchaseOrder)4 ProtectionService (org.folio.service.ProtectionService)4 PieceService (org.folio.service.pieces.PieceService)4