Search in sources :

Example 6 with OrderService

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());
}
Also used : Order(org.openmrs.Order) ArrayList(java.util.ArrayList) Patient(org.openmrs.Patient) Encounter(org.openmrs.Encounter) OrderService(org.openmrs.api.OrderService) EncounterService(org.openmrs.api.EncounterService) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 7 with OrderService

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());
    }
}
Also used : Order(org.openmrs.Order) EncounterSearchCriteria(org.openmrs.parameter.EncounterSearchCriteria) User(org.openmrs.User) EncounterSearchCriteriaBuilder(org.openmrs.parameter.EncounterSearchCriteriaBuilder) Patient(org.openmrs.Patient) Encounter(org.openmrs.Encounter) OrderService(org.openmrs.api.OrderService) EncounterService(org.openmrs.api.EncounterService) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 8 with OrderService

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

Aggregations

OrderService (org.openmrs.api.OrderService)8 Encounter (org.openmrs.Encounter)5 Order (org.openmrs.Order)5 Test (org.junit.Test)4 Patient (org.openmrs.Patient)4 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)4 Date (java.util.Date)3 EncounterService (org.openmrs.api.EncounterService)3 CareSetting (org.openmrs.CareSetting)2 Concept (org.openmrs.Concept)2 Obs (org.openmrs.Obs)2 TestOrder (org.openmrs.TestOrder)2 APIException (org.openmrs.api.APIException)2 ConceptService (org.openmrs.api.ConceptService)2 ObsService (org.openmrs.api.ObsService)2 EncounterSearchCriteria (org.openmrs.parameter.EncounterSearchCriteria)2 EncounterSearchCriteriaBuilder (org.openmrs.parameter.EncounterSearchCriteriaBuilder)2 BindException (org.springframework.validation.BindException)2 Errors (org.springframework.validation.Errors)2 ArrayList (java.util.ArrayList)1