use of org.openmrs.api.OrderService in project openmrs-core by openmrs.
the class PatientDataUnvoidHandlerTest method handle_shouldNotUnvoidTheOrdersAndEncountersThatNeverGotVoidedWithThePatient.
/**
* @see PatientDataUnvoidHandler#handle(Patient,User,Date,String)
*/
@Test
public void handle_shouldNotUnvoidTheOrdersAndEncountersThatNeverGotVoidedWithThePatient() {
executeDataSet("org/openmrs/api/include/OrderServiceTest-otherEncounters.xml");
Patient patient = Context.getPatientService().getPatient(7);
EncounterService es = Context.getEncounterService();
OrderService os = Context.getOrderService();
Encounter testEncounter = es.getEncountersByPatient(patient).get(0);
// santy checks
assertFalse(testEncounter.getVoided());
assertNull(testEncounter.getDateVoided());
assertNull(testEncounter.getVoidedBy());
assertNull(testEncounter.getVoidReason());
// void one of the encounter orders be voided at a different time for testing purposes
assertFalse(testEncounter.getOrders().isEmpty());
Order testOrder = testEncounter.getOrders().iterator().next();
assertFalse(testOrder.getVoided());
Context.getOrderService().voidOrder(testOrder, "testing");
assertTrue(testOrder.getVoided());
TestUtil.waitForClockTick();
// void one of the unvoided encounters for testing purposes
es.voidEncounter(testEncounter, "random reason");
assertTrue(testEncounter.getVoided());
assertTrue(testOrder.getVoided());
List<Patient> patients = new ArrayList<>();
patients.add(patient);
// wait a bit so that the patient isn't voided on the same millisecond
TestUtil.waitForClockTick();
// now void the patient for testing purposes
patient = Context.getPatientService().voidPatient(patient, "Void Reason");
assertTrue(patient.getVoided());
new PatientDataUnvoidHandler().handle(patient, patient.getVoidedBy(), patient.getDateVoided(), null);
// the encounter that was initially voided separately should still be voided
testEncounter = es.getEncounter(testEncounter.getId());
assertTrue(testEncounter.getVoided());
assertNotNull(testEncounter.getDateVoided());
assertNotNull(testEncounter.getVoidedBy());
assertNotNull(testEncounter.getVoidReason());
// the order that was initially voided separately should still be voided
assertTrue(testOrder.getVoided());
assertNotNull(testOrder.getDateVoided());
assertNotNull(testOrder.getVoidedBy());
assertNotNull(testOrder.getVoidReason());
}
use of org.openmrs.api.OrderService in project openmrs-core by openmrs.
the class PatientDataUnvoidHandlerTest method handle_shouldUnvoidTheOrdersAndEncountersAssociatedWithThePatient.
/**
* @see PatientDataUnvoidHandler#handle(Patient,User,Date,String)
*/
@Test
public void handle_shouldUnvoidTheOrdersAndEncountersAssociatedWithThePatient() {
Patient patient = Context.getPatientService().getPatient(7);
patient = Context.getPatientService().voidPatient(patient, "Void Reason");
assertTrue(patient.getVoided());
EncounterService es = Context.getEncounterService();
EncounterSearchCriteria encounterSearchCriteria = new EncounterSearchCriteriaBuilder().setPatient(patient).setIncludeVoided(true).createEncounterSearchCriteria();
List<Encounter> encounters = es.getEncounters(encounterSearchCriteria);
assertTrue(CollectionUtils.isNotEmpty(encounters));
// all encounters void related fields should be null
for (Encounter encounter : encounters) {
assertTrue(encounter.getVoided());
assertNotNull(encounter.getDateVoided());
assertNotNull(encounter.getVoidedBy());
assertNotNull(encounter.getVoidReason());
}
OrderService os = Context.getOrderService();
List<Order> orders = os.getAllOrdersByPatient(patient);
assertFalse(orders.isEmpty());
// all order void related fields should be null
for (Order order : orders) {
assertTrue(order.getVoided());
assertNotNull(order.getDateVoided());
assertNotNull(order.getVoidedBy());
assertNotNull(order.getVoidReason());
}
User user = Context.getUserService().getUser(1);
new PatientDataUnvoidHandler().handle(patient, user, patient.getDateVoided(), null);
// check that the voided related fields were set null
for (Encounter encounter : encounters) {
assertFalse(encounter.getVoided());
assertNull(encounter.getDateVoided());
assertNull(encounter.getVoidedBy());
assertNull(encounter.getVoidReason());
}
for (Order order : orders) {
assertFalse(order.getVoided());
assertNull(order.getDateVoided());
assertNull(order.getVoidedBy());
assertNull(order.getVoidReason());
}
}
use of org.openmrs.api.OrderService in project openmrs-core by openmrs.
the class EncounterServiceImpl method unvoidEncounter.
/**
* @see org.openmrs.api.EncounterService#unvoidEncounter(org.openmrs.Encounter)
*/
@Override
public Encounter unvoidEncounter(Encounter encounter) throws APIException {
// if authenticated user is not supposed to edit encounter of certain type
if (!canEditEncounter(encounter, null)) {
throw new APIException("Encounter.error.privilege.required.unvoid", new Object[] { encounter.getEncounterType().getEditPrivilege() });
}
String voidReason = encounter.getVoidReason();
if (voidReason == null) {
voidReason = "";
}
ObsService os = Context.getObsService();
for (Obs o : encounter.getObsAtTopLevel(true)) {
if (voidReason.equals(o.getVoidReason())) {
os.unvoidObs(o);
}
}
OrderService orderService = Context.getOrderService();
for (Order o : encounter.getOrders()) {
if (voidReason.equals(o.getVoidReason())) {
orderService.unvoidOrder(o);
}
}
encounter.setVoided(false);
encounter.setVoidedBy(null);
encounter.setDateVoided(null);
encounter.setVoidReason(null);
Context.getEncounterService().saveEncounter(encounter);
return encounter;
}
Aggregations