use of org.openmrs.api.AmbiguousOrderException in project openmrs-core by openmrs.
the class OrderServiceImpl method saveOrder.
private Order saveOrder(Order order, OrderContext orderContext, boolean isRetrospective) {
failOnExistingOrder(order);
ensureDateActivatedIsSet(order);
ensureConceptIsSet(order);
ensureDrugOrderAutoExpirationDateIsSet(order);
ensureOrderTypeIsSet(order, orderContext);
ensureCareSettingIsSet(order, orderContext);
failOnOrderTypeMismatch(order);
Order previousOrder = order.getPreviousOrder();
if (REVISE == order.getAction()) {
if (previousOrder == null) {
throw new MissingRequiredPropertyException("Order.previous.required", (Object[]) null);
}
stopOrder(previousOrder, aMomentBefore(order.getDateActivated()), isRetrospective);
} else if (DISCONTINUE == order.getAction()) {
discontinueExistingOrdersIfNecessary(order, isRetrospective);
}
if (previousOrder != null) {
// concept should be the same as on previous order, same applies to drug for drug orders
if (!order.hasSameOrderableAs(previousOrder)) {
throw new EditedOrderDoesNotMatchPreviousException("Order.orderable.doesnot.match");
} else if (!order.getOrderType().equals(previousOrder.getOrderType())) {
throw new EditedOrderDoesNotMatchPreviousException("Order.type.doesnot.match");
} else if (!order.getCareSetting().equals(previousOrder.getCareSetting())) {
throw new EditedOrderDoesNotMatchPreviousException("Order.care.setting.doesnot.match");
} else if (!getActualType(order).equals(getActualType(previousOrder))) {
throw new EditedOrderDoesNotMatchPreviousException("Order.class.doesnot.match");
}
}
if (DISCONTINUE != order.getAction()) {
Date asOfDate = new Date();
if (isRetrospective) {
asOfDate = order.getDateActivated();
}
List<Order> activeOrders = getActiveOrders(order.getPatient(), null, order.getCareSetting(), asOfDate);
List<String> parallelOrders = Collections.emptyList();
if (orderContext != null && orderContext.getAttribute(PARALLEL_ORDERS) != null) {
parallelOrders = Arrays.asList((String[]) orderContext.getAttribute(PARALLEL_ORDERS));
}
for (Order activeOrder : activeOrders) {
// Reject if there is an active drug order for the same orderable with overlapping schedule
if (!parallelOrders.contains(activeOrder.getUuid()) && areDrugOrdersOfSameOrderableAndOverlappingSchedule(order, activeOrder)) {
throw new AmbiguousOrderException("Order.cannot.have.more.than.one");
}
}
}
return saveOrderInternal(order, orderContext);
}
use of org.openmrs.api.AmbiguousOrderException in project openmrs-core by openmrs.
the class OrderServiceImpl method discontinueExistingOrdersIfNecessary.
/**
* If this is a discontinue order, ensure that the previous order is discontinued. If a
* previousOrder is present, then ensure this is discontinued. If no previousOrder is present,
* then try to find a previousOrder and discontinue it. If cannot find a previousOrder, throw
* exception
*
* @param order
* @param isRetrospective
*/
// Ignore and return if this is not an order to discontinue
private void discontinueExistingOrdersIfNecessary(Order order, Boolean isRetrospective) {
if (DISCONTINUE != order.getAction()) {
return;
}
// Mark previousOrder as discontinued if it is not already
Order previousOrder = order.getPreviousOrder();
if (previousOrder != null) {
stopOrder(previousOrder, aMomentBefore(order.getDateActivated()), isRetrospective);
return;
}
// Mark first order found corresponding to this DC order as discontinued.
Date asOfDate = null;
if (isRetrospective) {
asOfDate = order.getDateActivated();
}
List<? extends Order> orders = getActiveOrders(order.getPatient(), order.getOrderType(), order.getCareSetting(), asOfDate);
boolean isDrugOrderAndHasADrug = isDrugOrder(order) && (((DrugOrder) order).getDrug() != null || ((DrugOrder) order).isNonCodedDrug());
Order orderToBeDiscontinued = null;
for (Order activeOrder : orders) {
if (!getActualType(order).equals(getActualType(activeOrder))) {
continue;
}
// For drug orders, the drug must match if the order has a drug
if (isDrugOrderAndHasADrug) {
Order existing = order.hasSameOrderableAs(activeOrder) ? activeOrder : null;
if (existing != null) {
if (orderToBeDiscontinued == null) {
orderToBeDiscontinued = existing;
} else {
throw new AmbiguousOrderException("Order.discontinuing.ambiguous.orders");
}
}
} else if (activeOrder.getConcept().equals(order.getConcept())) {
if (orderToBeDiscontinued == null) {
orderToBeDiscontinued = activeOrder;
} else {
throw new AmbiguousOrderException("Order.discontinuing.ambiguous.orders");
}
}
}
if (orderToBeDiscontinued != null) {
order.setPreviousOrder(orderToBeDiscontinued);
stopOrder(orderToBeDiscontinued, aMomentBefore(order.getDateActivated()), isRetrospective);
}
}
Aggregations