diff --git a/src/main/java/org/folio/helper/PurchaseOrderLineHelper.java b/src/main/java/org/folio/helper/PurchaseOrderLineHelper.java index 040508204..b95a01560 100644 --- a/src/main/java/org/folio/helper/PurchaseOrderLineHelper.java +++ b/src/main/java/org/folio/helper/PurchaseOrderLineHelper.java @@ -98,7 +98,7 @@ public class PurchaseOrderLineHelper { public static final String ERESOURCE = "eresource"; public static final String PHYSICAL = "physical"; private static final String OTHER = "other"; - private static final String QUERY_BY_PO_LINE_ID = "poLineId=="; + private static final String QUERY_BY_PO_LINE_ID = "poLineId==%s"; private final InventoryItemStatusSyncService itemStatusSyncService; private final InventoryInstanceManager inventoryInstanceManager; @@ -285,16 +285,16 @@ public Future updateOrderLine(CompositePoLine compOrderLine, RequestContex .compose(v -> purchaseOrderLineService.validateAndNormalizeISBNAndProductType(Collections.singletonList(compOrderLine), requestContext)) .compose(v -> validateAccessProviders(compOrderLine, requestContext)) .compose(v -> expenseClassValidationService.validateExpenseClassesForOpenedOrder(compOrder, Collections.singletonList(compOrderLine), requestContext)) - .compose(v -> processPoLineEncumbrances(compOrder, compOrderLine, poLineFromStorage, requestContext))) - .map(v -> compOrderLine.withPoLineNumber(poLineFromStorage.getPoLineNumber())) // PoLine number must not be modified during PoLine update, set original value - .map(v -> new PoLineInvoiceLineHolder(compOrderLine, poLineFromStorage)) - .compose(poLineInvoiceLineHolder -> polInvoiceLineRelationService.prepareRelatedInvoiceLines(poLineInvoiceLineHolder, requestContext) - .compose(v -> createShadowInstanceIfNeeded(compOrderLine, requestContext)) - .compose(v -> updateOrderLine(compOrderLine, JsonObject.mapFrom(poLineFromStorage), requestContext)) - .compose(v -> updateEncumbranceStatus(compOrderLine, poLineFromStorage, requestContext)) - .compose(v -> polInvoiceLineRelationService.updateInvoiceLineReference(poLineInvoiceLineHolder, requestContext)) - .compose(v -> updateInventoryItemStatus(compOrderLine, poLineFromStorage, requestContext)) - .compose(v -> updateOrderStatusIfNeeded(compOrderLine, poLineFromStorage, requestContext)))); + .compose(v -> processPoLineEncumbrances(compOrder, compOrderLine, poLineFromStorage, requestContext)) + .map(v -> compOrderLine.withPoLineNumber(poLineFromStorage.getPoLineNumber())) // PoLine number must not be modified during PoLine update, set original value + .map(v -> new PoLineInvoiceLineHolder(compOrderLine, poLineFromStorage)) + .compose(poLineInvoiceLineHolder -> polInvoiceLineRelationService.prepareRelatedInvoiceLines(poLineInvoiceLineHolder, requestContext) + .compose(v -> createShadowInstanceIfNeeded(compOrderLine, requestContext)) + .compose(v -> updateOrderLine(compOrderLine, JsonObject.mapFrom(poLineFromStorage), requestContext)) + .compose(v -> updateEncumbranceStatus(compOrderLine, poLineFromStorage, requestContext)) + .compose(v -> polInvoiceLineRelationService.updateInvoiceLineReference(poLineInvoiceLineHolder, requestContext)) + .compose(v -> updateInventoryItemStatus(compOrderLine, poLineFromStorage, requestContext)) + .compose(v -> updateOrderStatusIfNeeded(compOrder, compOrderLine, poLineFromStorage, requestContext))))); } private Future updateEncumbranceStatus(CompositePoLine compOrderLine, PoLine poLineFromStorage, RequestContext requestContext) { @@ -626,7 +626,8 @@ private Future getLineWithInstanceId(CompositePoLine line, Requ if (Boolean.TRUE.equals(line.getIsPackage())) { return Future.succeededFuture(line); } - return titlesService.getTitles(1, 0, QUERY_BY_PO_LINE_ID + line.getId(), requestContext) + var query = String.format(QUERY_BY_PO_LINE_ID, line.getId()); + return titlesService.getTitles(1, 0, query, requestContext) .map(titleCollection -> { var titles = titleCollection.getTitles(); if (!titles.isEmpty()) { diff --git a/src/main/java/org/folio/service/orders/utils/StatusUtils.java b/src/main/java/org/folio/service/orders/utils/StatusUtils.java index dbbec5e8f..d5ec49ead 100644 --- a/src/main/java/org/folio/service/orders/utils/StatusUtils.java +++ b/src/main/java/org/folio/service/orders/utils/StatusUtils.java @@ -8,18 +8,26 @@ import org.folio.orders.utils.HelperUtils; import org.folio.rest.core.models.RequestContext; import org.folio.rest.jaxrs.model.CompositePoLine; +import org.folio.rest.jaxrs.model.CompositePoLine.PaymentStatus; +import org.folio.rest.jaxrs.model.CompositePoLine.ReceiptStatus; +import org.folio.rest.jaxrs.model.CompositePurchaseOrder; import org.folio.rest.jaxrs.model.PoLine; import java.util.List; +import java.util.Set; import static org.folio.helper.BaseHelper.EVENT_PAYLOAD; import static org.folio.helper.BaseHelper.ORDER_ID; public class StatusUtils { - public static Future updateOrderStatusIfNeeded(CompositePoLine compOrderLine, PoLine poLineFromStorage, RequestContext requestContext) { + private static final Set resolutionPaymentStatus = Set.of(PaymentStatus.CANCELLED, PaymentStatus.FULLY_PAID, PaymentStatus.PAYMENT_NOT_REQUIRED); + private static final Set resolutionReceiptStatus = Set.of(ReceiptStatus.CANCELLED, ReceiptStatus.FULLY_RECEIVED, ReceiptStatus.RECEIPT_NOT_REQUIRED); + + public static Future updateOrderStatusIfNeeded(CompositePurchaseOrder compositePurchaseOrder, CompositePoLine compOrderLine, + PoLine poLineFromStorage, RequestContext requestContext) { // See MODORDERS-218 - if (isStatusChanged(compOrderLine, poLineFromStorage)) { + if (isStatusChanged(compOrderLine, poLineFromStorage) && shouldUpdateOrderStatus(compositePurchaseOrder, compOrderLine)) { var updateOrderMessage = JsonObject.of(EVENT_PAYLOAD, JsonArray.of(JsonObject.of(ORDER_ID, compOrderLine.getPurchaseOrderId()))); HelperUtils.sendEvent(MessageAddress.RECEIVE_ORDER_STATUS_UPDATE, updateOrderMessage, requestContext); } @@ -31,9 +39,10 @@ public static boolean isStatusChanged(CompositePoLine compOrderLine, PoLine line !StringUtils.equals(lineFromStorage.getPaymentStatus().value(), compOrderLine.getPaymentStatus().value()); } - public static boolean shouldUpdateOrderStatus(CompositePoLine compOrderLine, PoLine lineFromStorage) { - return !StringUtils.equals(lineFromStorage.getReceiptStatus().value(), compOrderLine.getReceiptStatus().value()) || - !StringUtils.equals(lineFromStorage.getPaymentStatus().value(), compOrderLine.getPaymentStatus().value()); + private static boolean shouldUpdateOrderStatus(CompositePurchaseOrder compositePurchaseOrder, CompositePoLine compOrderLine) { + return compositePurchaseOrder.getWorkflowStatus() != CompositePurchaseOrder.WorkflowStatus.CLOSED + && !resolutionPaymentStatus.contains(compOrderLine.getPaymentStatus()) + && !resolutionReceiptStatus.contains(compOrderLine.getReceiptStatus()); } public static boolean areAllPoLinesCanceled(List poLines) {