use of org.folio.models.PoLineUpdateHolder in project mod-orders by folio-org.
the class InventoryManager method handleItemRecords.
/**
* Handles Inventory items for passed list of locations. Items are either retrieved from Inventory or new ones are created
* if no corresponding item records exist yet.
* Returns list of {@link Piece} records with populated item id (and other info) corresponding to given PO line.
*
* @param compPOL PO line to retrieve/create Item Records for
* @param holder pair of new location provided from POl and location from storage
* @return future with list of piece objects
*/
public CompletableFuture<List<Piece>> handleItemRecords(CompositePoLine compPOL, PoLineUpdateHolder holder, RequestContext requestContext) {
List<Location> polLocations = compPOL.getLocations().stream().filter(location -> location.getLocationId().equals(holder.getNewLocationId())).collect(toList());
Map<Piece.Format, Integer> piecesWithItemsQuantities = HelperUtils.calculatePiecesWithItemIdQuantity(compPOL, polLocations);
int piecesWithItemsQty = IntStreamEx.of(piecesWithItemsQuantities.values()).sum();
String polId = compPOL.getId();
logger.debug("Handling {} items for PO Line with id={} and holdings with id={}", piecesWithItemsQty, polId, holder.getOldHoldingId());
if (piecesWithItemsQty == 0) {
return completedFuture(Collections.emptyList());
}
return pieceStorageService.getExpectedPiecesByLineId(compPOL.getId(), requestContext).thenApply(existingPieces -> {
List<Piece> needUpdatePieces = new ArrayList<>();
List<Piece> pieces = existingPieces.getPieces().stream().filter(piece -> piece.getLocationId().equals(holder.getOldLocationId())).map(piece -> piece.withLocationId(holder.getNewLocationId())).collect(toList());
if (!pieces.isEmpty()) {
needUpdatePieces.addAll(pieces);
}
return needUpdatePieces;
}).thenCompose(needUpdatePieces -> {
if (!needUpdatePieces.isEmpty()) {
return getItemRecordsByIds(needUpdatePieces.stream().map(Piece::getItemId).collect(toList()), requestContext).thenApply(items -> buildPieceItemPairList(needUpdatePieces, items));
}
return completedFuture(Collections.<PieceItemPair>emptyList());
}).thenCompose(pieceItemPairs -> {
List<CompletableFuture<String>> updatedItemIds = new ArrayList<>(pieceItemPairs.size());
pieceItemPairs.forEach(pair -> {
JsonObject item = pair.getItem();
if (isLocationContainsItemLocation(polLocations, item)) {
item.put(ITEM_HOLDINGS_RECORD_ID, holder.getNewHoldingId());
updatedItemIds.add(saveItem(item, requestContext));
}
});
// Wait for all items to be created and corresponding updatedItemIds are built
return collectResultsOnSuccess(updatedItemIds).thenApply(results -> {
validateItemsCreation(compPOL.getId(), updatedItemIds.size(), results.size());
return pieceItemPairs.stream().map(PieceItemPair::getPiece).collect(toList());
});
});
}
use of org.folio.models.PoLineUpdateHolder in project mod-orders by folio-org.
the class LocationUtil method convertToOldNewLocationIdPair.
public static List<PoLineUpdateHolder> convertToOldNewLocationIdPair(List<Location> newLocations, List<Location> oldLocations) {
Map<String, Integer> newLocationQty = LocationUtil.groupLocationQtyByLocationId(newLocations);
Map<String, Integer> storageLocationQty = LocationUtil.groupLocationQtyByLocationId(oldLocations);
int newLocationsQty = newLocations.stream().map(Location::getQuantity).mapToInt(Number::intValue).sum();
int storeLocationsQty = oldLocations.stream().map(Location::getQuantity).mapToInt(Number::intValue).sum();
List<PoLineUpdateHolder> poLineUpdateHolders = new ArrayList<>();
if (newLocationsQty == storeLocationsQty && newLocationQty.size() == storageLocationQty.size()) {
List<String> originNewLocationIds = newLocations.stream().map(Location::getLocationId).collect(toList());
originNewLocationIds.retainAll(storageLocationQty.keySet());
if (!originNewLocationIds.isEmpty()) {
newLocationQty.entrySet().removeIf(e -> originNewLocationIds.contains(e.getKey()));
storageLocationQty.entrySet().removeIf(e -> originNewLocationIds.contains(e.getKey()));
}
List<String> remNewLocationIds = new ArrayList<>(newLocationQty.keySet());
List<String> remOldLocationIds = new ArrayList<>(storageLocationQty.keySet());
IntStream.range(0, newLocationQty.size()).forEach(i -> {
String newLocationId = remNewLocationIds.get(i);
String oldLocationId = remOldLocationIds.get(i);
PoLineUpdateHolder holder = new PoLineUpdateHolder().withNewLocationId(newLocationId).withOldLocationId(oldLocationId);
poLineUpdateHolders.add(holder);
});
}
return poLineUpdateHolders;
}
use of org.folio.models.PoLineUpdateHolder in project mod-orders by folio-org.
the class InventoryManagerTest method testShouldHandleItemRecordsIfPhysycPresentInUpdatePoLineTime.
@Test
void testShouldHandleItemRecordsIfPhysycPresentInUpdatePoLineTime() throws IOException {
CompositePoLine reqData = getMockAsJson(COMP_PO_LINES_MOCK_DATA_PATH, "c0d08448-347b-418a-8c2f-5fb50248d67e").mapTo(CompositePoLine.class);
String poLineId = "c0d08448-347b-418a-8c2f-5fb50248d67d";
String itemId = "86481a22-633e-4b97-8061-0dc5fdaaeabb";
String materialType = "1a54b431-2e4f-452d-9cae-9cee66c9a892";
String locationId = "758258bc-ecc1-41b8-abca-f7b610822fff";
String oldLocationId = "fcd64ce1-6995-48f0-840e-89ffa2288371";
reqData.setId(poLineId);
reqData.setPurchaseOrderId("9d56b621-202d-414b-9e7f-5fefe4422ab3");
reqData.getPhysical().setMaterialType(materialType);
reqData.getPhysical().setMaterialSupplier(ACTIVE_ACCESS_PROVIDER_B);
reqData.getPhysical().setCreateInventory(Physical.CreateInventory.INSTANCE_HOLDING_ITEM);
reqData.getLocations().get(0).setQuantityPhysical(1);
reqData.getLocations().get(0).setQuantity(1);
reqData.getCost().setQuantityPhysical(1);
reqData.getLocations().get(0).setLocationId(locationId);
CompositePoLine storagePoLineCom = getMockAsJson(COMP_PO_LINES_MOCK_DATA_PATH, "c0d08448-347b-418a-8c2f-5fb50248d67e").mapTo(CompositePoLine.class);
storagePoLineCom.setAlerts(null);
storagePoLineCom.setReportingCodes(null);
storagePoLineCom.getLocations().get(0).setQuantityPhysical(1);
storagePoLineCom.getLocations().get(0).setQuantity(1);
storagePoLineCom.getCost().setQuantityPhysical(1);
JsonObject items = new JsonObject(getMockData(ITEMS_RECORDS_MOCK_DATA_PATH + "inventoryItemsCollection.json"));
List<JsonObject> needUpdateItems = items.getJsonArray(ITEMS).stream().map(o -> ((JsonObject) o)).filter(item -> item.getString(TestConstants.ID).equals(itemId)).map(item -> item.put(ITEM_PURCHASE_ORDER_LINE_IDENTIFIER, poLineId)).collect(toList());
;
String path = PIECE_RECORDS_MOCK_DATA_PATH + String.format("pieceRecords-%s.json", poLineId);
PieceCollection existedPieces = new JsonObject(getMockData(path)).mapTo(PieceCollection.class);
existedPieces.getPieces().get(0).setItemId(itemId);
existedPieces.getPieces().get(0).setFormat(Piece.Format.PHYSICAL);
existedPieces.getPieces().get(0).setPoLineId(poLineId);
// given
doReturn(completedFuture(existedPieces)).when(pieceStorageService).getExpectedPiecesByLineId(poLineId, requestContext);
doReturn(completedFuture(needUpdateItems)).when(inventoryManager).getItemRecordsByIds(Collections.singletonList(itemId), requestContext);
doReturn(completedFuture(null)).when(inventoryManager).updateItemRecords(any(), eq(requestContext));
doReturn(completedFuture(null)).when(restClient).put(any(RequestEntry.class), any(JsonObject.class), eq(requestContext));
// When
PoLineUpdateHolder poLineUpdateHolder = new PoLineUpdateHolder().withOldLocationId(oldLocationId).withNewLocationId(locationId);
List<Piece> pieces = inventoryManager.handleItemRecords(reqData, poLineUpdateHolder, requestContext).join();
assertEquals(1, pieces.size());
assertEquals(itemId, pieces.get(0).getItemId());
assertEquals(locationId, pieces.get(0).getLocationId());
}
use of org.folio.models.PoLineUpdateHolder in project mod-orders by folio-org.
the class InventoryManagerTest method testShouldNotHandleItemRecordsIfCheckinItemsIsTrueInUpdatePoLIneTime.
@Test
void testShouldNotHandleItemRecordsIfCheckinItemsIsTrueInUpdatePoLIneTime() {
CompositePoLine reqData = getMockAsJson(COMP_PO_LINES_MOCK_DATA_PATH, "c2755a78-2f8d-47d0-a218-059a9b7391b4").mapTo(CompositePoLine.class);
String poLineId = "c0d08448-347b-418a-8c2f-5fb50248d67e";
reqData.setId(poLineId);
reqData.setPurchaseOrderId("9d56b621-202d-414b-9e7f-5fefe4422ab3");
reqData.getEresource().setAccessProvider(ACTIVE_ACCESS_PROVIDER_B);
reqData.getEresource().setCreateInventory(INSTANCE_HOLDING);
reqData.getLocations().get(0).setLocationId("758258bc-ecc1-41b8-abca-f7b610822fff");
reqData.setCheckinItems(true);
CompositePoLine storagePoLineCom = getMockAsJson(COMP_PO_LINES_MOCK_DATA_PATH, "c0d08448-347b-418a-8c2f-5fb50248d67e").mapTo(CompositePoLine.class);
storagePoLineCom.setAlerts(null);
storagePoLineCom.setReportingCodes(null);
storagePoLineCom.getLocations().get(0).setQuantityPhysical(1);
storagePoLineCom.getLocations().get(0).setQuantity(1);
storagePoLineCom.getCost().setQuantityPhysical(1);
// When
PoLineUpdateHolder poLineUpdateHolder = new PoLineUpdateHolder().withNewLocationId(NEW_LOCATION_ID);
List<Piece> pieces = inventoryManager.handleItemRecords(reqData, poLineUpdateHolder, requestContext).join();
assertEquals(0, pieces.size());
}
Aggregations