Skip to content

Commit

Permalink
Merge pull request #2026 from ojwanganto/refactor/enhance-visit-close…
Browse files Browse the repository at this point in the history
…-task

Close only outpatient visits which have been active for more than 8 h…
  • Loading branch information
makombe authored Oct 23, 2024
2 parents ffb715c + 15c845c commit a34881e
Showing 1 changed file with 46 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
package org.openmrs.module.kenyaemr.task;

import org.openmrs.Patient;
import org.openmrs.Visit;
import org.openmrs.api.VisitService;
import org.openmrs.api.context.Context;
Expand All @@ -18,53 +17,63 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;

/**
* A scheduled task that automatically closes all unvoided active visits
* The focus should only be on the OUTPATIENT visit type
* The focus should only be on the OUTPATIENT visit type with visit duration (checkin to now) > 8 hours
* Set date_stopped to 11 23:59:59 of the date_started
*/
public class AutoCloseActiveVisitsTask extends AbstractTask {

private static final Logger log = LoggerFactory.getLogger(AutoCloseActiveVisitsTask.class);

/**
* @see AbstractTask#execute()
*/
@Override
public void execute() {
if (!isExecuting) {
if (log.isDebugEnabled()) {
log.debug("Starting Auto Close Visits Task...");
}

String openVisitsQuery = "select visit_id from visit v\n" +
private static final Logger log = LoggerFactory.getLogger(AutoCloseActiveVisitsTask.class);

/**
* @see AbstractTask#execute()
*/
@Override
public void execute() {
if (!isExecuting) {
if (log.isDebugEnabled()) {
log.debug("Starting Auto Close Visits Task...");
}
System.out.println("Starting the close visits task");

String openVisitsQuery = "select visit_id, TIMESTAMPDIFF(HOUR, v.date_started, now()) as visit_duration from visit v\n" +
"inner join visit_type vt on vt.visit_type_id = v.visit_type_id and vt.uuid ='3371a4d4-f66f-4454-a86d-92c7b3da990c'\n" +
"where v.date_stopped is null";
"where v.date_stopped is null having visit_duration > 8;";

startExecuting();
VisitService visitService = Context.getVisitService();
List<List<Object>> visits = Context.getAdministrationService().executeSQL(openVisitsQuery, true);

startExecuting();
VisitService visitService = Context.getVisitService();
if (visits.size() > 0) {
try {
int counter = 0;
for (List<Object> ls : visits) {
if (ls.get(0) != null) {
Integer visitId = (Integer) ls.get(0);
Visit visit = visitService.getVisit(visitId);
visit.setStopDatetime(OpenmrsUtil.getLastMomentOfDay(visit.getStartDatetime()));
visitService.saveVisit(visit);
counter++;

List<List<Object>> visits = Context.getAdministrationService().executeSQL(openVisitsQuery, true);
if (visits.size() > 0) {
for (List<Object> ls : visits) {
try {
if (ls.get(0) != null) {
Integer visitId = (Integer) ls.get(0);
Visit visit = visitService.getVisit(visitId);
visit.setStopDatetime(OpenmrsUtil.getLastMomentOfDay(visit.getStartDatetime()));
visitService.saveVisit(visit);
}
if ((counter % 400) == 0) {
Context.flushSession();
Context.clearSession();
counter = 0;
System.out.println("AutoCloseVisitTask: Flushing session....");
}
}
}
} catch (Exception e) {
log.error("Error while auto closing visits:", e);
} finally {
stopExecuting();
System.out.println("Completed the close visits task");

} catch (Exception e) {
log.error("Error while auto closing visits:", e);
} finally {
stopExecuting();
}
}
}
}
}
}
}
}
}

0 comments on commit a34881e

Please sign in to comment.