Search in sources :

Example 31 with EncounterType

use of org.openmrs.EncounterType in project openmrs-module-pihcore by PIH.

the class BaseInpatientReportTest method setupDontOverrideBaseClassSetup.

@Before
public void setupDontOverrideBaseClassSetup() throws Exception {
    VisitType atFacility = visitService.getVisitTypeByUuid(VisitTypeBundle.VisitTypes.CLINIC_OR_HOSPITAL_VISIT);
    EncounterType checkIn = Metadata.lookup(EncounterTypes.CHECK_IN);
    EncounterType admission = Metadata.lookup(EncounterTypes.ADMISSION);
    EncounterType transfer = Metadata.lookup(EncounterTypes.TRANSFER);
    EncounterType exit = Metadata.lookup(EncounterTypes.EXIT_FROM_CARE);
    EncounterType consultation = Metadata.lookup(EncounterTypes.CONSULTATION);
    EncounterType postOpNote = Metadata.lookup(EncounterTypes.POST_OPERATIVE_NOTE);
    Location visitLocation = Metadata.lookup(MirebalaisLocations.MIREBALAIS_CDI_PARENT);
    Location outpatientClinic = Metadata.lookup(MirebalaisLocations.OUTPATIENT_CLINIC);
    Location womensInternalMedicine = Metadata.lookup(MirebalaisLocations.WOMENS_INTERNAL_MEDICINE);
    Location mensInternalMedicine = Metadata.lookup(MirebalaisLocations.MENS_INTERNAL_MEDICINE);
    Location surgicalWard = Metadata.lookup(MirebalaisLocations.SURGICAL_WARD);
    Location emergencyDepartment = Metadata.lookup(MirebalaisLocations.EMERGENCY);
    Concept dispositionConcept = conceptService.getConceptByUuid("c8b22b09-e2f2-4606-af7d-e52579996de3");
    // Already admitted at start of 3 Oct (Women's Internal Medicine)
    patient1 = data.randomPatient().save();
    Visit visit1 = data.visit().patient(patient1).visitType(atFacility).started("2013-10-02 09:15:00").stopped("2013-10-14 04:30:00").location(visitLocation).save();
    Encounter enc1a = data.encounter().visit(visit1).encounterType(checkIn).location(outpatientClinic).encounterDatetime("2013-10-02 09:15:00").save();
    Encounter enc1b = data.encounter().visit(visit1).encounterType(admission).location(womensInternalMedicine).encounterDatetime("2013-10-02 12:30:00").save();
    // Admitted and discharged the days before. Visit extends into 3 Oct, but they've already been discharged at that point. (I.e. not an inpatient on Oct 3.)
    patient2 = data.randomPatient().save();
    Visit visit2 = data.visit().patient(patient2).visitType(atFacility).started("2013-10-01 17:30:00").stopped("2013-10-03 12:45:00").location(visitLocation).save();
    data.encounter().visit(visit2).encounterType(checkIn).location(outpatientClinic).encounterDatetime("2013-10-01 17:30:00").save();
    data.encounter().visit(visit2).encounterType(admission).location(womensInternalMedicine).encounterDatetime("2013-10-01 18:30:00").save();
    data.encounter().visit(visit2).encounterType(exit).location(womensInternalMedicine).encounterDatetime("2013-10-02 23:45:00").save();
    // Admitted during the day of 3 Oct (Men's Internal Medicine)
    patient3 = data.randomPatient().save();
    Visit visit3a = data.visit().patient(patient3).visitType(atFacility).started("2013-10-03 12:34:00").stopped("2013-10-07 12:45:00").location(visitLocation).save();
    data.encounter().visit(visit3a).encounterType(checkIn).location(outpatientClinic).encounterDatetime("2013-10-03 12:34:00").save();
    data.encounter().visit(visit3a).encounterType(admission).location(mensInternalMedicine).encounterDatetime("2013-10-03 13:57:00").save();
    Encounter encounter3a = data.encounter().visit(visit3a).encounterType(exit).location(mensInternalMedicine).encounterDatetime("2013-10-07 12:45:00").save();
    data.obs().encounter(encounter3a).concept(dispositionConcept).value("Transfer out of hospital", "PIH").save();
    // This was a possible readmission, because the patient was admitted and left the previous day
    Visit visit3b = data.visit().patient(patient3).visitType(atFacility).started("2013-10-02 12:34:00").stopped("2013-10-02 16:45:00").location(visitLocation).save();
    data.encounter().visit(visit3b).encounterType(admission).location(mensInternalMedicine).encounterDatetime("2013-10-02 12:34:00").save();
    Encounter encounter3b = data.encounter().visit(visit3b).encounterType(exit).location(mensInternalMedicine).encounterDatetime("2013-10-02 16:45:00").save();
    data.obs().encounter(encounter3b).concept(dispositionConcept).value("Departed without medical discharge", "PIH").save();
    // Admitted earlier (Men's Internal Medicine), and discharged on 3 Oct
    patient4 = data.randomPatient().save();
    Visit visit4 = data.visit().patient(patient4).visitType(atFacility).started("2013-10-01 12:34:00").stopped("2013-10-03 12:45:00").location(visitLocation).save();
    data.encounter().visit(visit4).encounterType(checkIn).location(outpatientClinic).encounterDatetime("2013-10-01 12:34:00").save();
    data.encounter().visit(visit4).encounterType(admission).location(mensInternalMedicine).encounterDatetime("2013-10-01 13:57:00").save();
    data.encounter().visit(visit4).encounterType(exit).location(mensInternalMedicine).encounterDatetime("2013-10-03 12:45:00").save();
    // consult with discharge
    Encounter encounter4a = data.encounter().visit(visit4).encounterType(consultation).location(mensInternalMedicine).encounterDatetime("2013-10-03 12:15:00").save();
    data.obs().encounter(encounter4a).concept(dispositionConcept).value("DISCHARGED", "PIH").save();
    // Begins the day of 3 Oct at Women's Inpatient (was admitted earlier), and transferred to Surgical Ward during the day
    patient5 = data.randomPatient().save();
    Visit visit5 = data.visit().patient(patient5).visitType(atFacility).started("2013-10-01 12:34:00").stopped("2013-10-13 12:45:00").location(visitLocation).save();
    data.encounter().visit(visit5).encounterType(checkIn).location(outpatientClinic).encounterDatetime("2013-10-01 12:34:00").save();
    data.encounter().visit(visit5).encounterType(admission).location(womensInternalMedicine).encounterDatetime("2013-10-01 13:57:00").save();
    data.encounter().visit(visit5).encounterType(transfer).location(surgicalWard).encounterDatetime("2013-10-03 12:45:00").save();
    // Checks into ED during the day, transferred to surgery (with no admission), and has surgery
    patient6 = data.randomPatient().save();
    Visit visit6 = data.visit().patient(patient6).visitType(atFacility).started("2013-10-03 10:05:00").stopped("2013-10-03 18:32:21").location(visitLocation).save();
    data.encounter().visit(visit6).encounterType(checkIn).location(emergencyDepartment).encounterDatetime("2013-10-03 10:05:00").save();
    data.encounter().visit(visit6).encounterType(transfer).location(surgicalWard).encounterDatetime("2013-10-03 12:45:00").save();
    data.encounter().visit(visit6).encounterType(postOpNote).location(surgicalWard).encounterDatetime("2013-10-03 14:53:00").save();
}
Also used : Concept(org.openmrs.Concept) Visit(org.openmrs.Visit) VisitType(org.openmrs.VisitType) Encounter(org.openmrs.Encounter) EncounterType(org.openmrs.EncounterType) Location(org.openmrs.Location) Before(org.junit.Before)

Example 32 with EncounterType

use of org.openmrs.EncounterType in project openmrs-module-pihcore by PIH.

the class InpatientLocationCohortDefinitionEvaluator method evaluate.

@Override
public EvaluatedCohort evaluate(CohortDefinition cohortDefinition, EvaluationContext context) throws EvaluationException {
    InpatientLocationCohortDefinition cd = (InpatientLocationCohortDefinition) cohortDefinition;
    Date onDate = cd.getEffectiveDate();
    if (onDate == null) {
        onDate = new Date();
    }
    Location ward = cd.getWard();
    Location visitLocation = null;
    if (ward != null) {
        visitLocation = adtService.getLocationThatSupportsVisits(ward);
    }
    EncounterType admissionEncounterType = emrApiProperties.getAdmissionEncounterType();
    EncounterType dischargeEncounterType = emrApiProperties.getExitFromInpatientEncounterType();
    EncounterType transferEncounterType = emrApiProperties.getTransferWithinHospitalEncounterType();
    StringBuilder sb = new StringBuilder("select distinct v.patient_id " + "from visit v " + "inner join encounter admission " + "  on v.visit_id = admission.visit_id " + "  and admission.voided = false " + "  and admission.encounter_type = :admissionEncounterType " + "  and admission.encounter_datetime <= :onDate " + "inner join encounter mostRecentAdt " + "  on v.visit_id = mostRecentAdt.visit_id " + "  and mostRecentAdt.encounter_id = ( " + "    select encounter_id " + "    from encounter " + "    where visit_id = v.visit_id " + "    and voided = false " + "    and encounter_type in (:adtEncounterTypes) " + "    and encounter_datetime <= :onDate " + "    order by encounter_datetime desc, date_created desc limit 1" + "  ) ");
    sb.append("where v.voided = false");
    if (visitLocation != null) {
        sb.append("  and v.location_id = :visitLocation ");
    }
    sb.append("  and v.date_started <= :onDate ");
    sb.append("  and (v.date_stopped is null or v.date_stopped > :onDate) ");
    if (ward != null) {
        sb.append("  and mostRecentAdt.location_id = :ward ");
    }
    sb.append("  and mostRecentAdt.encounter_type in (:admitOrTransferEncounterTypes)");
    SQLQuery query = sessionFactory.getCurrentSession().createSQLQuery(sb.toString());
    query.setInteger("admissionEncounterType", admissionEncounterType.getId());
    query.setTimestamp("onDate", onDate);
    if (visitLocation != null) {
        query.setInteger("visitLocation", visitLocation.getId());
    }
    if (ward != null) {
        query.setInteger("ward", ward.getId());
    }
    query.setParameterList("adtEncounterTypes", new Integer[] { admissionEncounterType.getId(), dischargeEncounterType.getId(), transferEncounterType.getId() });
    query.setParameterList("admitOrTransferEncounterTypes", new Integer[] { admissionEncounterType.getId(), transferEncounterType.getId() });
    // This does not actually work: org.hibernate.hql.ast.QuerySyntaxException: with-clause referenced two different from-clause elements
    // Query hql = sessionFactory.getCurrentSession().createQuery("select distinct(v.patient.id) " +
    // "from Visit v " +
    // "join v.encounters as mostRecentAdt " +
    // "    with mostRecentAdt.voided = false " +
    // "    and mostRecentAdt.encounterType in (:adtEncounterTypes) " +
    // "    and mostRecentAdt.encounterDatetime = ( " +
    // "        select max(encounterDatetime)" +
    // "        from Encounter " +
    // "        where visit = v " +
    // "        and voided = false " +
    // "        and encounterType in (:adtEncounterTypes) " +
    // "        and encounterDatetime <= :onDate " +
    // "    ) " +
    // "where v.voided = false " +
    // "and v.location = :visitLocation " +
    // "and v.startDatetime <= :onDate " +
    // "and (v.stopDatetime is null or v.stopDatetime > :onDate) " +
    // "and exists ( " +
    // "    from Encounter admission " +
    // "    where admission.visit = v " +
    // "    and admission.voided = false " +
    // "    and admission.encounterType = :admissionEncounterType " +
    // "    and admission.encounterDatetime <= :onDate " +
    // ") " +
    // "and mostRecentAdt.location = :ward " +
    // "and mostRecentAdt.encounterType in (:admitOrTransferEncounterTypes) ");
    // 
    // hql.setParameter("onDate", onDate);
    // hql.setParameter("visitLocation", visitLocation);
    // hql.setParameter("ward", ward);
    // hql.setParameter("admissionEncounterType", admissionEncounterType);
    // hql.setParameterList("adtEncounterTypes", adtEncounterTypes);
    // hql.setParameterList("admitOrTransferEncounterTypes", admitOrTransferEncounterTypes);
    Cohort c = new Cohort();
    for (Integer i : (List<Integer>) query.list()) {
        c.addMember(i);
    }
    return new EvaluatedCohort(c, cohortDefinition, context);
}
Also used : InpatientLocationCohortDefinition(org.openmrs.module.pihcore.reporting.cohort.definition.InpatientLocationCohortDefinition) Cohort(org.openmrs.Cohort) EvaluatedCohort(org.openmrs.module.reporting.cohort.EvaluatedCohort) EvaluatedCohort(org.openmrs.module.reporting.cohort.EvaluatedCohort) List(java.util.List) EncounterType(org.openmrs.EncounterType) SQLQuery(org.hibernate.SQLQuery) Date(java.util.Date) Location(org.openmrs.Location)

Example 33 with EncounterType

use of org.openmrs.EncounterType in project openmrs-module-pihcore by PIH.

the class WaitingForConsultPageController method get.

public String get(PageModel model, @RequestParam(required = false, value = "filter") String filterString, @SpringBean("encounterService") EncounterService encounterService, @SpringBean("obsService") ObsService obsService, @SpringBean("conceptService") ConceptService conceptService, @SpringBean("personService") PersonService personService, @SpringBean("domainWrapperFactory") DomainWrapperFactory domainWrapperFactory) {
    // TODO restrict by location at some point if necessary
    Filter filter;
    if (filterString != null && filterString.toLowerCase().equals("in_consultation")) {
        filter = Filter.IN_CONSULTATION;
    } else {
        filter = Filter.WAITING_FOR_CONSULT;
    }
    List<EncounterType> primaryCareEncounterTypes = new ArrayList<EncounterType>();
    primaryCareEncounterTypes.add(encounterService.getEncounterTypeByUuid(EncounterTypes.PRIMARY_CARE_VISIT.uuid()));
    primaryCareEncounterTypes.add(encounterService.getEncounterTypeByUuid(EncounterTypes.PRIMARY_CARE_PEDS_INITIAL_CONSULT.uuid()));
    primaryCareEncounterTypes.add(encounterService.getEncounterTypeByUuid(EncounterTypes.PRIMARY_CARE_PEDS_FOLLOWUP_CONSULT.uuid()));
    primaryCareEncounterTypes.add(encounterService.getEncounterTypeByUuid(EncounterTypes.PRIMARY_CARE_ADULT_INITIAL_CONSULT.uuid()));
    primaryCareEncounterTypes.add(encounterService.getEncounterTypeByUuid(EncounterTypes.PRIMARY_CARE_ADULT_FOLLOWUP_CONSULT.uuid()));
    // first handle any patients with vitals taken today:
    // create a list of all patients that have a vitals encounter today, *ordered by time of first vitals encounter*
    LinkedHashSet<Patient> patientsWithVitalsToday = new LinkedHashSet<Patient>();
    for (Encounter encounter : encounterService.getEncounters(null, null, new DateMidnight().toDate(), null, null, Collections.singletonList(encounterService.getEncounterTypeByUuid(EncounterTypes.VITALS.uuid())), null, null, null, false)) {
        patientsWithVitalsToday.add(encounter.getPatient());
    }
    // fetch the set of all patients that have a primary care encounter today
    Set<Patient> patientsWithConsultToday = new HashSet<Patient>();
    for (Encounter encounter : encounterService.getEncounters(null, null, new DateMidnight().toDate(), null, null, primaryCareEncounterTypes, null, null, null, false)) {
        patientsWithConsultToday.add(encounter.getPatient());
    }
    // fetch the set of all patients who have a disposition today
    Set<Patient> patientsWithDispositionToday = new HashSet<Patient>();
    Concept dispoConceptSet = conceptService.getConceptByMapping(EmrApiConstants.CONCEPT_CODE_DISPOSITION_CONCEPT_SET, EmrApiConstants.EMR_CONCEPT_SOURCE_NAME);
    for (Obs obs : obsService.getObservations(null, null, Collections.singletonList(dispoConceptSet), null, null, null, null, null, null, new DateMidnight().toDate(), null, false)) {
        // assumption: only patients have obs, not plain persio
        patientsWithDispositionToday.add((Patient) obs.getPerson());
    }
    LinkedHashSet<Patient> patientListForToday = patientsWithVitalsToday;
    // assumption: you can't have a disposition without also having a consult
    if (filter.equals(Filter.WAITING_FOR_CONSULT)) {
        // the "waiting for consult" list is all patients with vitals today but no consult
        patientListForToday.removeAll(patientsWithConsultToday);
    } else {
        // the "in-consultation" list is all patients with vitals AND consult today but no dispostion (disposition is our trigger that a consult is finished)
        patientListForToday.retainAll(patientsWithConsultToday);
        patientListForToday.removeAll(patientsWithDispositionToday);
    }
    // now handle any patients with vitals taken on the last business day
    boolean isMonday = new DateMidnight().getDayOfWeek() == DateTimeConstants.MONDAY;
    // create a list of all patients that have a vitals encounter on last business day, *ordered by time of first vitals encounter*
    LinkedHashSet<Patient> patientsWithVitalsOnPreviousBusinessDay = new LinkedHashSet<Patient>();
    for (Encounter encounter : encounterService.getEncounters(null, null, isMonday ? new DateMidnight().minusDays(3).toDate() : new DateMidnight().minusDays(1).toDate(), isMonday ? new DateMidnight().minusDays(2).toDate() : new DateMidnight().toDate(), null, Collections.singletonList(encounterService.getEncounterTypeByUuid(EncounterTypes.VITALS.uuid())), null, null, null, false)) {
        patientsWithVitalsOnPreviousBusinessDay.add(encounter.getPatient());
    }
    Set<Patient> patientsWithConsultOnPreviousBusinessDayOrToday = new HashSet<Patient>();
    // fetch the set of all patients that have a primary care encounter last business day
    for (Encounter encounter : encounterService.getEncounters(null, null, isMonday ? new DateMidnight().minusDays(3).toDate() : new DateMidnight().minusDays(1).toDate(), isMonday ? new DateMidnight().minusDays(2).toDate() : new DateMidnight().toDate(), null, primaryCareEncounterTypes, null, null, null, false)) {
        patientsWithConsultOnPreviousBusinessDayOrToday.add(encounter.getPatient());
    }
    // add all patiens with primary care encounter today
    patientsWithConsultOnPreviousBusinessDayOrToday.addAll(patientsWithConsultToday);
    Set<Patient> patientsWithDispositionOnPreviousDaysOrToday = new HashSet<Patient>();
    // fetch the set of all patients who have a disposition last business day
    for (Obs obs : obsService.getObservations(null, null, Collections.singletonList(dispoConceptSet), null, null, null, null, null, null, isMonday ? new DateMidnight().minusDays(3).toDate() : new DateMidnight().minusDays(1).toDate(), isMonday ? new DateMidnight().minusDays(2).toDate() : new DateMidnight().toDate(), false)) {
        // assumption: only patients have obs, not plain persio
        patientsWithDispositionOnPreviousDaysOrToday.add((Patient) obs.getPerson());
    }
    // add all patiens with disposition today
    patientsWithConsultOnPreviousBusinessDayOrToday.addAll(patientsWithDispositionToday);
    LinkedHashSet<Patient> patientListForPreviousBusinessDay = patientsWithVitalsOnPreviousBusinessDay;
    // assumption: you can't have a disposition without also having a consult
    if (filter.equals(Filter.WAITING_FOR_CONSULT)) {
        // the "waiting for consult" list is all patients with vitals last business day but no consult then or today
        patientListForPreviousBusinessDay.removeAll(patientsWithConsultOnPreviousBusinessDayOrToday);
    } else {
        // the "in-consultation" list is all patients with vitals last business day and consult last business day or today, but no disposition
        patientListForPreviousBusinessDay.retainAll(patientsWithConsultOnPreviousBusinessDayOrToday);
        patientListForPreviousBusinessDay.removeAll(patientsWithDispositionOnPreviousDaysOrToday);
    }
    // now create our final list by combining the list for the previous business day with the list for the current day
    LinkedHashSet<Patient> patientList = patientListForPreviousBusinessDay;
    patientList.addAll(patientListForToday);
    // now wrap them all in a patient domain wrapper
    List<PatientDomainWrapper> patientsListWrapped = new ArrayList<PatientDomainWrapper>();
    for (Patient patient : patientList) {
        patientsListWrapped.add(domainWrapperFactory.newPatientDomainWrapper(patient));
    }
    model.addAttribute("patientList", patientsListWrapped);
    model.addAttribute("filter", filter.toString().toLowerCase());
    model.addAttribute("dossierIdentifierName", PihHaitiPatientIdentifierTypes.DOSSIER_NUMBER.name());
    model.addAttribute("mothersFirstName", personService.getPersonAttributeTypeByUuid(HaitiPersonAttributeTypes.MOTHERS_FIRST_NAME.uuid()));
    return null;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Concept(org.openmrs.Concept) Obs(org.openmrs.Obs) PatientDomainWrapper(org.openmrs.module.emrapi.patient.PatientDomainWrapper) ArrayList(java.util.ArrayList) Patient(org.openmrs.Patient) DateMidnight(org.joda.time.DateMidnight) Encounter(org.openmrs.Encounter) EncounterType(org.openmrs.EncounterType) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 34 with EncounterType

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

the class EncounterServiceImpl method filterEncountersByViewPermissions.

/**
 * @see org.openmrs.api.EncounterService#filterEncountersByViewPermissions(java.util.List,
 *      org.openmrs.User)
 */
@Override
@Transactional(readOnly = true)
public List<Encounter> filterEncountersByViewPermissions(List<Encounter> encounters, User user) {
    if (encounters != null) {
        // if user is not specified then use authenticated user from context by default
        if (user == null) {
            user = Context.getAuthenticatedUser();
        }
        for (Iterator<Encounter> iterator = encounters.iterator(); iterator.hasNext(); ) {
            Encounter encounter = iterator.next();
            // determine whether it's need to include this encounter into result or not
            // as it can be not accessed by current user due to permissions lack
            EncounterType et = encounter.getEncounterType();
            if (et != null && !userHasEncounterPrivilege(et.getViewPrivilege(), user)) {
                // exclude this encounter from result
                iterator.remove();
            }
        }
    }
    return encounters;
}
Also used : Encounter(org.openmrs.Encounter) EncounterType(org.openmrs.EncounterType) Transactional(org.springframework.transaction.annotation.Transactional)

Example 35 with EncounterType

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

the class EncounterServiceTest method saveEncounterType_shouldSaveEncounterType.

/**
 * @see EncounterService#saveEncounterType(EncounterType)
 */
@Test
public void saveEncounterType_shouldSaveEncounterType() {
    EncounterService encounterService = Context.getEncounterService();
    EncounterType encounterType = new EncounterType("testing", "desc");
    encounterService.saveEncounterType(encounterType);
    // make sure an encounter type id was created
    assertNotNull(encounterType.getEncounterTypeId());
    // make sure we can fetch this new encounter type
    // from the database
    EncounterType newEncounterType = encounterService.getEncounterType(encounterType.getEncounterTypeId());
    assertNotNull(newEncounterType);
}
Also used : EncounterType(org.openmrs.EncounterType) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) Test(org.junit.Test)

Aggregations

EncounterType (org.openmrs.EncounterType)60 Test (org.junit.Test)44 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)42 Encounter (org.openmrs.Encounter)19 Date (java.util.Date)17 Location (org.openmrs.Location)12 Patient (org.openmrs.Patient)10 User (org.openmrs.User)10 EncounterRole (org.openmrs.EncounterRole)9 Provider (org.openmrs.Provider)8 BindException (org.springframework.validation.BindException)7 Errors (org.springframework.validation.Errors)7 ArrayList (java.util.ArrayList)5 GlobalProperty (org.openmrs.GlobalProperty)5 List (java.util.List)4 Privilege (org.openmrs.Privilege)4 Concept (org.openmrs.Concept)3 Visit (org.openmrs.Visit)3 HashMap (java.util.HashMap)2 SQLQuery (org.hibernate.SQLQuery)2