Search in sources :

Example 1 with EditedOrderDoesNotMatchPreviousException

use of org.openmrs.api.EditedOrderDoesNotMatchPreviousException 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);
}
Also used : DrugOrder(org.openmrs.DrugOrder) TestOrder(org.openmrs.TestOrder) Order(org.openmrs.Order) EditedOrderDoesNotMatchPreviousException(org.openmrs.api.EditedOrderDoesNotMatchPreviousException) MissingRequiredPropertyException(org.openmrs.api.MissingRequiredPropertyException) Date(java.util.Date) AmbiguousOrderException(org.openmrs.api.AmbiguousOrderException)

Aggregations

Date (java.util.Date)1 DrugOrder (org.openmrs.DrugOrder)1 Order (org.openmrs.Order)1 TestOrder (org.openmrs.TestOrder)1 AmbiguousOrderException (org.openmrs.api.AmbiguousOrderException)1 EditedOrderDoesNotMatchPreviousException (org.openmrs.api.EditedOrderDoesNotMatchPreviousException)1 MissingRequiredPropertyException (org.openmrs.api.MissingRequiredPropertyException)1