Search in sources :

Example 66 with Order

use of org.openmrs.Order in project openmrs-core by openmrs.

the class OrderServiceImpl method discontinueOrder.

/**
 * @see org.openmrs.api.OrderService#discontinueOrder(org.openmrs.Order, org.openmrs.Concept,
 *      java.util.Date, org.openmrs.Provider, org.openmrs.Encounter)
 */
@Override
public Order discontinueOrder(Order orderToDiscontinue, Concept reasonCoded, Date discontinueDate, Provider orderer, Encounter encounter) {
    if (discontinueDate == null) {
        discontinueDate = aMomentBefore(new Date());
    }
    stopOrder(orderToDiscontinue, discontinueDate, false);
    Order newOrder = orderToDiscontinue.cloneForDiscontinuing();
    newOrder.setOrderReason(reasonCoded);
    newOrder.setOrderer(orderer);
    newOrder.setEncounter(encounter);
    newOrder.setDateActivated(discontinueDate);
    return saveOrderInternal(newOrder, null);
}
Also used : DrugOrder(org.openmrs.DrugOrder) TestOrder(org.openmrs.TestOrder) Order(org.openmrs.Order) Date(java.util.Date)

Example 67 with Order

use of org.openmrs.Order 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)

Example 68 with Order

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

Example 69 with Order

use of org.openmrs.Order in project openmrs-core by openmrs.

the class OrderServiceImpl method ensureOrderTypeIsSet.

private void ensureOrderTypeIsSet(Order order, OrderContext orderContext) {
    if (order.getOrderType() != null) {
        return;
    }
    OrderType orderType = null;
    if (orderContext != null) {
        orderType = orderContext.getOrderType();
    }
    if (orderType == null) {
        orderType = getOrderTypeByConcept(order.getConcept());
    }
    if (orderType == null && order instanceof DrugOrder) {
        orderType = Context.getOrderService().getOrderTypeByUuid(OrderType.DRUG_ORDER_TYPE_UUID);
    }
    if (orderType == null && order instanceof TestOrder) {
        orderType = Context.getOrderService().getOrderTypeByUuid(OrderType.TEST_ORDER_TYPE_UUID);
    }
    if (orderType == null) {
        throw new OrderEntryException("Order.type.cannot.determine");
    }
    Order previousOrder = order.getPreviousOrder();
    if (previousOrder != null && !orderType.equals(previousOrder.getOrderType())) {
        throw new OrderEntryException("Order.type.does.not.match");
    }
    order.setOrderType(orderType);
}
Also used : DrugOrder(org.openmrs.DrugOrder) DrugOrder(org.openmrs.DrugOrder) TestOrder(org.openmrs.TestOrder) Order(org.openmrs.Order) OrderEntryException(org.openmrs.api.OrderEntryException) OrderType(org.openmrs.OrderType) TestOrder(org.openmrs.TestOrder)

Example 70 with Order

use of org.openmrs.Order in project openmrs-core by openmrs.

the class EncounterServiceImpl method voidEncounter.

/**
 * @see org.openmrs.api.EncounterService#voidEncounter(org.openmrs.Encounter, java.lang.String)
 */
@Override
public Encounter voidEncounter(Encounter encounter, String reason) {
    // if authenticated user is not supposed to edit encounter of certain type
    if (!canEditEncounter(encounter, null)) {
        throw new APIException("Encounter.error.privilege.required.void", new Object[] { encounter.getEncounterType().getEditPrivilege() });
    }
    if (reason == null) {
        throw new IllegalArgumentException("The argument 'reason' is required and so cannot be null");
    }
    ObsService os = Context.getObsService();
    for (Obs o : encounter.getObsAtTopLevel(false)) {
        if (!o.getVoided()) {
            os.voidObs(o, reason);
        }
    }
    OrderService orderService = Context.getOrderService();
    for (Order o : encounter.getOrders()) {
        if (!o.getVoided()) {
            orderService.voidOrder(o, reason);
        }
    }
    encounter.setVoided(true);
    encounter.setVoidedBy(Context.getAuthenticatedUser());
    // unvoid handler to work
    if (encounter.getDateVoided() == null) {
        encounter.setDateVoided(new Date());
    }
    encounter.setVoidReason(reason);
    Context.getEncounterService().saveEncounter(encounter);
    return encounter;
}
Also used : Order(org.openmrs.Order) Obs(org.openmrs.Obs) APIException(org.openmrs.api.APIException) ObsService(org.openmrs.api.ObsService) OrderService(org.openmrs.api.OrderService) Date(java.util.Date)

Aggregations

Order (org.openmrs.Order)155 Test (org.junit.Test)133 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)115 TestOrder (org.openmrs.TestOrder)114 DrugOrder (org.openmrs.DrugOrder)106 OrderUtilTest (org.openmrs.order.OrderUtilTest)97 Matchers.containsInAnyOrder (org.hamcrest.Matchers.containsInAnyOrder)77 Date (java.util.Date)67 Encounter (org.openmrs.Encounter)41 Patient (org.openmrs.Patient)36 BindException (org.springframework.validation.BindException)20 Errors (org.springframework.validation.Errors)20 Concept (org.openmrs.Concept)15 CareSetting (org.openmrs.CareSetting)11 Obs (org.openmrs.Obs)11 SimpleDateFormat (java.text.SimpleDateFormat)10 OrderGroup (org.openmrs.OrderGroup)10 ArrayList (java.util.ArrayList)9 Calendar (java.util.Calendar)9 OrderBuilder (org.openmrs.api.builder.OrderBuilder)9