Skip to content

Commit

Permalink
[MODORDERS-1174] Prevent incorrect order reopen
Browse files Browse the repository at this point in the history
  • Loading branch information
Saba-Zedginidze-EPAM committed Sep 19, 2024
1 parent f3e30da commit d16822e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
25 changes: 13 additions & 12 deletions src/main/java/org/folio/helper/PurchaseOrderLineHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -285,16 +285,16 @@ public Future<Void> 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<Void> updateEncumbranceStatus(CompositePoLine compOrderLine, PoLine poLineFromStorage, RequestContext requestContext) {
Expand Down Expand Up @@ -626,7 +626,8 @@ private Future<CompositePoLine> 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()) {
Expand Down
19 changes: 14 additions & 5 deletions src/main/java/org/folio/service/orders/utils/StatusUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Void> updateOrderStatusIfNeeded(CompositePoLine compOrderLine, PoLine poLineFromStorage, RequestContext requestContext) {
private static final Set<PaymentStatus> resolutionPaymentStatus = Set.of(PaymentStatus.CANCELLED, PaymentStatus.FULLY_PAID, PaymentStatus.PAYMENT_NOT_REQUIRED);
private static final Set<ReceiptStatus> resolutionReceiptStatus = Set.of(ReceiptStatus.CANCELLED, ReceiptStatus.FULLY_RECEIVED, ReceiptStatus.RECEIPT_NOT_REQUIRED);

public static Future<Void> 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);
}
Expand All @@ -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<PoLine> poLines) {
Expand Down

0 comments on commit d16822e

Please sign in to comment.