-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MODORDERS-1174] Simplify status change logic
- Loading branch information
1 parent
15cbe2a
commit bfa70fb
Showing
7 changed files
with
104 additions
and
93 deletions.
There are no files selected for viewing
Submodule raml-util
updated
54 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 81 additions & 34 deletions
115
src/main/java/org/folio/service/orders/utils/StatusUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,109 @@ | ||
package org.folio.service.orders.utils; | ||
|
||
import io.vertx.core.Future; | ||
import io.vertx.core.json.JsonArray; | ||
import io.vertx.core.json.JsonObject; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.folio.orders.events.handlers.MessageAddress; | ||
import org.folio.orders.utils.HelperUtils; | ||
import org.folio.rest.core.models.RequestContext; | ||
import org.folio.rest.jaxrs.model.CloseReason; | ||
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 org.folio.rest.jaxrs.model.PoLine.PaymentStatus; | ||
import org.folio.rest.jaxrs.model.PoLine.ReceiptStatus; | ||
import org.folio.rest.jaxrs.model.PurchaseOrder; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import static org.folio.helper.BaseHelper.EVENT_PAYLOAD; | ||
import static org.folio.helper.BaseHelper.ORDER_ID; | ||
import static org.folio.orders.utils.HelperUtils.REASON_CANCELLED; | ||
import static org.folio.orders.utils.HelperUtils.REASON_COMPLETE; | ||
|
||
public class StatusUtils { | ||
|
||
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); | ||
private static final Set<String> resolutionPaymentStatus = Set.of(PaymentStatus.CANCELLED.value(), PaymentStatus.PAYMENT_NOT_REQUIRED.value(), PaymentStatus.FULLY_PAID.value()); | ||
private static final Set<String> resolutionReceiptStatus = Set.of(ReceiptStatus.CANCELLED.value(), ReceiptStatus.RECEIPT_NOT_REQUIRED.value(), ReceiptStatus.FULLY_RECEIVED.value()); | ||
|
||
public static Future<Void> updateOrderStatusIfNeeded(CompositePurchaseOrder compositePurchaseOrder, CompositePoLine compOrderLine, | ||
PoLine poLineFromStorage, RequestContext requestContext) { | ||
// See MODORDERS-218 | ||
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); | ||
} | ||
return Future.succeededFuture(); | ||
} | ||
|
||
// private static boolean isCompositeOrderClosed(CompositePurchaseOrder order) { | ||
// return order.getWorkflowStatus() == CompositePurchaseOrder.WorkflowStatus.CLOSED; | ||
// } | ||
|
||
public static boolean isStatusChanged(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 shouldUpdateOrderStatus(CompositePurchaseOrder compositePurchaseOrder, CompositePoLine compOrderLine, PoLine lineFromStorage) { | ||
// return !isCompositeOrderClosed(compositePurchaseOrder) && isStatusChanged(compOrderLine, lineFromStorage) | ||
// || isCompositeOrderClosed(compositePurchaseOrder) && isNonResolutionPoLine(compOrderLine); | ||
// } | ||
|
||
public static boolean areAllPoLinesCanceled(List<PoLine> poLines) { | ||
return poLines.stream().allMatch(StatusUtils::isPoLineStatusCanceled); | ||
return poLines.stream().allMatch(StatusUtils::isStatusCanceledPoLine); | ||
} | ||
|
||
public static boolean changeOrderStatus(PurchaseOrder purchaseOrder, List<PoLine> poLines) { | ||
if (toBeCancelled(purchaseOrder, poLines)) { | ||
purchaseOrder.setWorkflowStatus(PurchaseOrder.WorkflowStatus.CLOSED); | ||
purchaseOrder.setCloseReason(new CloseReason().withReason(REASON_CANCELLED)); | ||
return true; | ||
} | ||
if (toBeClosed(purchaseOrder, poLines)) { | ||
purchaseOrder.setWorkflowStatus(PurchaseOrder.WorkflowStatus.CLOSED); | ||
purchaseOrder.setCloseReason(new CloseReason().withReason(REASON_COMPLETE)); | ||
return true; | ||
} | ||
if (toBeReopened(purchaseOrder, poLines)) { | ||
purchaseOrder.setWorkflowStatus(PurchaseOrder.WorkflowStatus.OPEN); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private static boolean toBeClosed(PurchaseOrder purchaseOrder, List<PoLine> poLines) { | ||
return isOrderOpen(purchaseOrder) | ||
&& poLines.stream().allMatch(StatusUtils::isCompletedPoLine); | ||
} | ||
|
||
private static boolean toBeCancelled(PurchaseOrder purchaseOrder, List<PoLine> poLines) { | ||
return isOrderOpen(purchaseOrder) | ||
&& poLines.stream().allMatch(StatusUtils::isCancelledPoLine); | ||
} | ||
|
||
private static boolean isPoLineStatusCanceled(PoLine poLine) { | ||
return PoLine.PaymentStatus.CANCELLED.equals(poLine.getPaymentStatus()) || | ||
PoLine.ReceiptStatus.CANCELLED.equals(poLine.getReceiptStatus()); | ||
private static boolean toBeReopened(PurchaseOrder purchaseOrder, List<PoLine> poLines) { | ||
return isOrderClosed(purchaseOrder) | ||
&& poLines.stream().anyMatch(StatusUtils::isNonResolutionPoLine); | ||
} | ||
|
||
public static boolean isCompositePoLineStatusCanceled(CompositePoLine compOrderLine) { | ||
return CompositePoLine.ReceiptStatus.CANCELLED.equals(compOrderLine.getReceiptStatus()) || | ||
CompositePoLine.PaymentStatus.CANCELLED.equals(compOrderLine.getPaymentStatus()); | ||
private static boolean isCompletedPoLine(PoLine line) { | ||
return resolutionPaymentStatus.contains(line.getPaymentStatus().value()) | ||
&& resolutionReceiptStatus.contains(line.getReceiptStatus().value()); | ||
} | ||
|
||
private static boolean isNonResolutionPoLine(PoLine line) { | ||
return !resolutionPaymentStatus.contains(line.getPaymentStatus().value()) | ||
&& !resolutionReceiptStatus.contains(line.getReceiptStatus().value()); | ||
} | ||
|
||
private static boolean isCancelledPoLine(PoLine poLine) { | ||
return poLine.getPaymentStatus() == PoLine.PaymentStatus.CANCELLED | ||
&& poLine.getReceiptStatus() == PoLine.ReceiptStatus.CANCELLED; | ||
} | ||
|
||
private static boolean isStatusCanceledPoLine(PoLine poLine) { | ||
return poLine.getPaymentStatus() == PoLine.PaymentStatus.CANCELLED | ||
|| poLine.getReceiptStatus() == PoLine.ReceiptStatus.CANCELLED; | ||
} | ||
|
||
public static boolean isStatusCanceledCompositePoLine(CompositePoLine compOrderLine) { | ||
return compOrderLine.getReceiptStatus() == CompositePoLine.ReceiptStatus.CANCELLED | ||
|| compOrderLine.getPaymentStatus() == CompositePoLine.PaymentStatus.CANCELLED; | ||
} | ||
|
||
private static boolean isOrderOpen(PurchaseOrder order) { | ||
return order.getWorkflowStatus() == PurchaseOrder.WorkflowStatus.OPEN; | ||
} | ||
|
||
private static boolean isOrderClosed(PurchaseOrder order) { | ||
return order.getWorkflowStatus() == PurchaseOrder.WorkflowStatus.CLOSED; | ||
} | ||
|
||
private StatusUtils() {} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters