Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
imerabishvili committed Jan 15, 2024
1 parent 9c9716a commit b8bd184
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 69 deletions.
60 changes: 29 additions & 31 deletions src/main/java/org/folio/orders/utils/HelperUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public static Integer calculateTotalLocationQuantity(Location location) {
*/
public static int calculateTotalQuantity(CompositePoLine compPOL) {
Cost cost = compPOL.getCost();
int eQuantity = ObjectUtils.defaultIfNull(cost.getQuantityElectronic(), 0);
int eQuantity = ObjectUtils.defaultIfNull(cost.getQuantityElectronic(), 0);
int physicalQuantity = ObjectUtils.defaultIfNull(cost.getQuantityPhysical(), 0);
return eQuantity + physicalQuantity;
}
Expand All @@ -177,7 +177,7 @@ public static int calculateInventoryItemsQuantity(CompositePoLine compPOL) {
/**
* Calculates items quantity for specified locations.
*
* @param compPOL composite PO Line
* @param compPOL composite PO Line
* @param locations list of locations to calculate quantity for
* @return quantity of items expected in the inventory for PO Line
* @see #calculateInventoryItemsQuantity(CompositePoLine)
Expand Down Expand Up @@ -297,27 +297,25 @@ public static MonetaryAmount calculateEncumbranceEffectiveAmount(MonetaryAmount
}

public static int getPhysicalLocationsQuantity(List<Location> locations) {
if (CollectionUtils.isNotEmpty(locations)) {
return locations.stream()
.map(Location::getQuantityPhysical)
.filter(Objects::nonNull)
.mapToInt(Integer::intValue)
.sum();
} else {
if (CollectionUtils.isEmpty(locations)) {
return 0;
}
return locations.stream()
.map(Location::getQuantityPhysical)
.filter(Objects::nonNull)
.mapToInt(Integer::intValue)
.sum();
}

public static int getElectronicLocationsQuantity(List<Location> locations) {
if (CollectionUtils.isNotEmpty(locations)) {
return locations.stream()
.map(Location::getQuantityElectronic)
.filter(Objects::nonNull)
.mapToInt(Integer::intValue)
.sum();
} else {
if (CollectionUtils.isEmpty(locations)) {
return 0;
}
return locations.stream()
.map(Location::getQuantityElectronic)
.filter(Objects::nonNull)
.mapToInt(Integer::intValue)
.sum();
}

/**
Expand Down Expand Up @@ -375,23 +373,24 @@ public static CompositePurchaseOrder convertToCompositePurchaseOrder(JsonObject
}

public static boolean changeOrderStatus(PurchaseOrder purchaseOrder, List<PoLine> poLines) {
boolean isUpdateRequired = false;

if (toBeCancelled(purchaseOrder, poLines)) {
purchaseOrder.setWorkflowStatus(PurchaseOrder.WorkflowStatus.CLOSED);
purchaseOrder.setCloseReason(new CloseReason().withReason(REASON_CANCELLED));
return true;
}

if (toBeClosed(purchaseOrder, poLines)) {
isUpdateRequired = true;
purchaseOrder.setWorkflowStatus(PurchaseOrder.WorkflowStatus.CLOSED);
purchaseOrder.setCloseReason(new CloseReason().withReason(REASON_COMPLETE));
} else if (toBeReopened(purchaseOrder, poLines)) {
isUpdateRequired = true;
return true;
}

if (toBeReopened(purchaseOrder, poLines)) {
purchaseOrder.setWorkflowStatus(PurchaseOrder.WorkflowStatus.OPEN);
return true;
}
return isUpdateRequired;

return false;
}

public static String convertTagListToCqlQuery(Collection<String> values, String fieldName, boolean strictMatch) {
Expand Down Expand Up @@ -468,8 +467,7 @@ public static void verifyTitles(Map<String, List<Title>> lineIdTitles, Map<Strin

private static void verifyNonPackageLinesHaveSingleTitle(Map<String, List<Title>> titles,
Map<String, CompositePoLine> poLineById) {
if (titles.keySet().stream().anyMatch(lineId -> titles.get(lineId).size() > 1 &&
!poLineById.get(lineId).getIsPackage())) {
if (titles.keySet().stream().anyMatch(lineId -> titles.get(lineId).size() > 1 && !poLineById.get(lineId).getIsPackage())) {
throw new HttpException(400, MULTIPLE_NONPACKAGE_TITLES);
}
}
Expand Down Expand Up @@ -498,17 +496,17 @@ public static ConversionQuery getConversionQuery(Double exchangeRate, String fro
}

public static void makePoLinePending(CompositePoLine poLine) {
if (poLine.getPaymentStatus() == CompositePoLine.PaymentStatus.AWAITING_PAYMENT) {
poLine.setPaymentStatus(CompositePoLine.PaymentStatus.PENDING);
}
if (poLine.getReceiptStatus() == CompositePoLine.ReceiptStatus.AWAITING_RECEIPT) {
poLine.setReceiptStatus(CompositePoLine.ReceiptStatus.PENDING);
}
if (poLine.getPaymentStatus() == CompositePoLine.PaymentStatus.AWAITING_PAYMENT) {
poLine.setPaymentStatus(CompositePoLine.PaymentStatus.PENDING);
}
if (poLine.getReceiptStatus() == CompositePoLine.ReceiptStatus.AWAITING_RECEIPT) {
poLine.setReceiptStatus(CompositePoLine.ReceiptStatus.PENDING);
}
}

public static ConversionQuery buildConversionQuery(PoLine poLine, String systemCurrency) {
Cost cost = poLine.getCost();
if (cost.getExchangeRate() != null){
if (cost.getExchangeRate() != null) {
return ConversionQueryBuilder.of().setBaseCurrency(cost.getCurrency())
.setTermCurrency(systemCurrency)
.set(RATE_KEY, cost.getExchangeRate()).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ public Future<Void> processInventory(PieceUpdateHolder holder, RequestContext re
.compose(aVoid -> {
if (Boolean.TRUE.equals(holder.getOriginPoLine().getIsPackage())) {
return packagePoLineUpdateInventory(holder, requestContext);
}
else
{
} else {
return nonPackagePoLineUpdateInventory(holder, requestContext);
}
});
Expand Down Expand Up @@ -93,36 +91,37 @@ private Future<Location> handleHolding(PieceUpdateHolder holder, RequestContext
if (instanceId != null && DefaultPieceFlowsValidator.isCreateHoldingForPiecePossible(pieceToUpdate, poLineToSave)) {
Location location = new Location().withLocationId(pieceToUpdate.getLocationId());
return inventoryManager.getOrCreateHoldingsRecord(instanceId, location, requestContext)
.map(holdingId -> {
Optional.ofNullable(holdingId).ifPresent(holdingIdP -> {
pieceToUpdate.setLocationId(null);
pieceToUpdate.setHoldingId(holdingId);
location.setLocationId(null);
location.setHoldingId(holdingId);
});
return location;
});
.map(holdingId -> {
Optional.ofNullable(holdingId).ifPresent(holdingIdP -> {
pieceToUpdate.setLocationId(null);
pieceToUpdate.setHoldingId(holdingId);
location.setLocationId(null);
location.setHoldingId(holdingId);
});
return location;
});
}
return Future.succeededFuture(new Location().withLocationId(pieceToUpdate.getLocationId()));
}

private Future<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)
.compose(jsonItem -> {
if (holder.isCreateItem() && (jsonItem == null || jsonItem.isEmpty()) && pieceToUpdate.getHoldingId() != null) {
if (!DefaultPieceFlowsValidator.isCreateItemForPiecePossible(pieceToUpdate, poLineToSave)) {
return Future.succeededFuture();
}
return inventoryManager.getItemRecordById(pieceToUpdate.getItemId(), true, requestContext)
.compose(jsonItem -> {
boolean noJsonItem = jsonItem == null || jsonItem.isEmpty();
if (holder.isCreateItem() && noJsonItem && pieceToUpdate.getHoldingId() != null) {
return pieceUpdateInventoryService.manualPieceFlowCreateItemRecord(pieceToUpdate, poLineToSave, requestContext);
} else if (jsonItem != null && !jsonItem.isEmpty()) {
return updateItemWithFields(jsonItem, poLineToSave, pieceToUpdate).compose(
aVoid -> inventoryManager.updateItem(jsonItem, requestContext).map(item -> jsonItem.getString(ID)));
} else {
return Future.succeededFuture();
}
if (!noJsonItem) {
return updateItemWithFields(jsonItem, poLineToSave, pieceToUpdate)
.compose(ignored -> inventoryManager.updateItem(jsonItem, requestContext).map(item -> jsonItem.getString(ID)));
}
return Future.succeededFuture();
});
}
return Future.succeededFuture();
}

private Future<Void> updateItemWithFields(JsonObject item, CompositePoLine compPOL, Piece piece) {
Expand All @@ -137,28 +136,27 @@ private Future<Void> updateItemWithFields(JsonObject item, CompositePoLine compP
private Future<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)
.compose(title -> {
if (title.getInstanceId() == null) {
return createTitleInstance(title, requestContext);
}
return Future.succeededFuture(title.getInstanceId());
})
.map(instanceId -> poLineToSave.withInstanceId(instanceId).getInstanceId());
if (poLineToSave.getInstanceId() != null || PoLineCommonUtil.isInventoryUpdateNotRequired(poLineToSave)) {
return Future.succeededFuture(poLineToSave.getInstanceId());
}
return Future.succeededFuture(poLineToSave.getInstanceId());
return titlesService.getTitleById(pieceToUpdate.getTitleId(), requestContext)
.compose(title -> {
if (title.getInstanceId() == null) {
return createTitleInstance(title, requestContext);
}
return Future.succeededFuture(title.getInstanceId());
})
.map(instanceId -> poLineToSave.withInstanceId(instanceId).getInstanceId());
}

private Future<Title> packageUpdateTitleWithInstance(Title title, RequestContext requestContext) {
if (title.getInstanceId() != null) {
return Future.succeededFuture(title);
} else {
return inventoryManager.getOrCreateInstanceRecord(title, requestContext)
.map(title::withInstanceId)
.compose(titleWithInstanceId -> titlesService.saveTitle(titleWithInstanceId, requestContext))
.map(v -> title);
}
return inventoryManager.getOrCreateInstanceRecord(title, requestContext)
.map(title::withInstanceId)
.compose(titleWithInstanceId -> titlesService.saveTitle(titleWithInstanceId, requestContext))
.map(v -> title);
}

private Future<String> createTitleInstance(Title title, RequestContext requestContext) {
Expand Down

0 comments on commit b8bd184

Please sign in to comment.