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);
}
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);
}
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);
}
}
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);
}
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;
}
Aggregations